tbapi.api.bases.strategy

 1from __future__ import annotations
 2from typing import TYPE_CHECKING
 3import clr
 4from tbapi.common.decorators import tb_class, tb_interface
 5from tbapi.common.converters import to_python_datetime, to_net_datetime
 6from datetime import datetime
 7from Tickblaze.Scripts.Api.Bases import Strategy as _Strategy
 8from typing import Any, overload
 9from abc import ABC, abstractmethod
10from tbapi.api.bases.trading_script import TradingScript
11from tbapi.api.interfaces.ichart_object import IChartObject
12from tbapi.api.interfaces.orders.istrategy_order_manager import IStrategyOrderManager
13from tbapi.api.enums.ztype import ZType
14from tbapi.api.enums.run_type import RunType
15from Tickblaze.Scripts.Api.Enums import ZType as _ZType
16from Tickblaze.Scripts.Api.Enums import RunType as _RunType
17if TYPE_CHECKING:
18    from tbapi.api.interfaces.ichart import IChart
19    from tbapi.api.interfaces.ichart_scale import IChartScale
20    from tbapi.api.interfaces.idrawing_context import IDrawingContext
21
22@tb_interface(_Strategy)
23class Strategy(TradingScript, IChartObject, IStrategyOrderManager):
24    """Represents a base class for strategy scripts allowing order management."""
25
26    @staticmethod
27    def new(*args, **kwargs):
28        """Generic factory method for Strategy. Use overloads for IDE type hints."""
29        return Strategy(*args, **kwargs)
30
31    @property
32    def chart(self) -> IChart:
33        from tbapi.api.interfaces.ichart import IChart
34        val = self._value.Chart
35        return IChart(_existing=val)
36    @chart.setter
37    def chart(self, val: IChart):
38        tmp = self._value
39        tmp.Chart = val._value
40        self._value = tmp
41    @property
42    def chart_scale(self) -> IChartScale:
43        from tbapi.api.interfaces.ichart_scale import IChartScale
44        val = self._value.ChartScale
45        return IChartScale(_existing=val)
46    @chart_scale.setter
47    def chart_scale(self, val: IChartScale):
48        tmp = self._value
49        tmp.ChartScale = val._value
50        self._value = tmp
51    @property
52    def show_on_chart(self) -> bool:
53        val = self._value.ShowOnChart
54        return val
55    @show_on_chart.setter
56    def show_on_chart(self, val: bool):
57        tmp = self._value
58        tmp.ShowOnChart = val
59        self._value = tmp
60    @property
61    def ztype(self) -> ZType:
62        val = int(self._value.ZType)
63        return ZType(val)
64    @ztype.setter
65    def ztype(self, val: ZType):
66        tmp = self._value
67        tmp.ZType = _ZType(val.value if hasattr(val, "value") else int(val))
68        self._value = tmp
69    @property
70    def run_type(self) -> RunType:
71        val = int(self._value.RunType)
72        return RunType(val)
73
74    def on_render(self, context: IDrawingContext) -> None:
75        result = self._value.OnRender(context._value)
76        return result
77  
78    def flatten(self, comment: str = "") -> None:
79        result = self._value.Flatten(comment)
80        return result
81  
82    def cancel_pending_orders(self, include_attached_orders: bool, comment: str = "") -> None:
83        result = self._value.CancelPendingOrders(include_attached_orders, comment)
84        return result
85  
86
87    @clr.clrmethod(None, [None])
88    def on_tick(self) -> None:
89        """Method to handle tick updates for the strategy."""
90        ...
91
92    @clr.clrmethod(None, [None])
93    def on_shutdown(self) -> None:
94        """Method called when the strategy is shutting down."""
95        ...
 30@tb_interface(_Strategy)
 31class Strategy(TradingScript, IChartObject, IStrategyOrderManager):
 32    """Represents a base class for strategy scripts allowing order management."""
 33
 34    @staticmethod
 35    def new(*args, **kwargs):
 36        """Generic factory method for Strategy. Use overloads for IDE type hints."""
 37        return Strategy(*args, **kwargs)
 38
 39    @property
 40    def chart(self) -> IChart:
 41        from tbapi.api.interfaces.ichart import IChart
 42        val = self._value.Chart
 43        return IChart(_existing=val)
 44    @chart.setter
 45    def chart(self, val: IChart):
 46        tmp = self._value
 47        tmp.Chart = val._value
 48        self._value = tmp
 49    @property
 50    def chart_scale(self) -> IChartScale:
 51        from tbapi.api.interfaces.ichart_scale import IChartScale
 52        val = self._value.ChartScale
 53        return IChartScale(_existing=val)
 54    @chart_scale.setter
 55    def chart_scale(self, val: IChartScale):
 56        tmp = self._value
 57        tmp.ChartScale = val._value
 58        self._value = tmp
 59    @property
 60    def show_on_chart(self) -> bool:
 61        val = self._value.ShowOnChart
 62        return val
 63    @show_on_chart.setter
 64    def show_on_chart(self, val: bool):
 65        tmp = self._value
 66        tmp.ShowOnChart = val
 67        self._value = tmp
 68    @property
 69    def ztype(self) -> ZType:
 70        val = int(self._value.ZType)
 71        return ZType(val)
 72    @ztype.setter
 73    def ztype(self, val: ZType):
 74        tmp = self._value
 75        tmp.ZType = _ZType(val.value if hasattr(val, "value") else int(val))
 76        self._value = tmp
 77    @property
 78    def run_type(self) -> RunType:
 79        val = int(self._value.RunType)
 80        return RunType(val)
 81
 82    def on_render(self, context: IDrawingContext) -> None:
 83        result = self._value.OnRender(context._value)
 84        return result
 85  
 86    def flatten(self, comment: str = "") -> None:
 87        result = self._value.Flatten(comment)
 88        return result
 89  
 90    def cancel_pending_orders(self, include_attached_orders: bool, comment: str = "") -> None:
 91        result = self._value.CancelPendingOrders(include_attached_orders, comment)
 92        return result
 93  
 94
 95    @clr.clrmethod(None, [None])
 96    def on_tick(self) -> None:
 97        """Method to handle tick updates for the strategy."""
 98        ...
 99
100    @clr.clrmethod(None, [None])
101    def on_shutdown(self) -> None:
102        """Method called when the strategy is shutting down."""
103        ...

Represents a base class for strategy scripts allowing order management.

Strategy(*args, **kwargs)
199        def __init__(self, *args, **kwargs):
200            pass
@staticmethod
def new(*args, **kwargs):
34    @staticmethod
35    def new(*args, **kwargs):
36        """Generic factory method for Strategy. Use overloads for IDE type hints."""
37        return Strategy(*args, **kwargs)

Generic factory method for Strategy. Use overloads for IDE type hints.

39    @property
40    def chart(self) -> IChart:
41        from tbapi.api.interfaces.ichart import IChart
42        val = self._value.Chart
43        return IChart(_existing=val)

The chart to which this object belongs.

49    @property
50    def chart_scale(self) -> IChartScale:
51        from tbapi.api.interfaces.ichart_scale import IChartScale
52        val = self._value.ChartScale
53        return IChartScale(_existing=val)

The scale used by this object on the chart.

show_on_chart: bool
59    @property
60    def show_on_chart(self) -> bool:
61        val = self._value.ShowOnChart
62        return val

Indicates whether the object is visible on the chart.

ztype: tbapi.api.enums.ztype.ZType
68    @property
69    def ztype(self) -> ZType:
70        val = int(self._value.ZType)
71        return ZType(val)
run_type: tbapi.api.enums.run_type.RunType
77    @property
78    def run_type(self) -> RunType:
79        val = int(self._value.RunType)
80        return RunType(val)
def on_render( self, context: tbapi.api.interfaces.idrawing_context.IDrawingContext) -> None:
82    def on_render(self, context: IDrawingContext) -> None:
83        result = self._value.OnRender(context._value)
84        return result

Draws the chart object using the specified drawing context. The context used for rendering.

def flatten(self, comment: str = '') -> None:
86    def flatten(self, comment: str = "") -> None:
87        result = self._value.Flatten(comment)
88        return result

Closes positions and cancel pending orders. Optional comment for the orders and cancellations. Will also automatically flatten incoming fills appearing after the call is made. This behavior is automatically disabled when a new entry is made.

def cancel_pending_orders(self, include_attached_orders: bool, comment: str = '') -> None:
90    def cancel_pending_orders(self, include_attached_orders: bool, comment: str = "") -> None:
91        result = self._value.CancelPendingOrders(include_attached_orders, comment)
92        return result

Cancels all pending orders True if attached orders should be included Comment to place on order

def on_tick(unknown):

Method decorator for exposing python methods to .NET. The argument and return types must be specified as arguments to clrmethod.

e.g.::

class X(object):
    @clrmethod(int, [str])
    def test(self, x):
        return len(x)

Methods decorated this way can be called from .NET, e.g.::

dynamic x = getX(); // get an instance of X declared in Python
int z = x.test("hello"); // calls into python and returns len("hello")
def on_shutdown(unknown):

Method decorator for exposing python methods to .NET. The argument and return types must be specified as arguments to clrmethod.

e.g.::

class X(object):
    @clrmethod(int, [str])
    def test(self, x):
        return len(x)

Methods decorated this way can be called from .NET, e.g.::

dynamic x = getX(); // get an instance of X declared in Python
int z = x.test("hello"); // calls into python and returns len("hello")