Skip to content

strategy

strategy_base

StrategyBase

Dummy placeholder class. Here to ensure all required methods are implemented and as per requirements.

Once uploaded, this strategy will be replaced with the real base class strategy

__init__(self, *args, **kwargs) special

Init method that is used while creating an object of this class

Parameters:

Name Type Description Default
*args

a variable-length argument list

()
**kwargs

a keyworded, variable-length argument list

{}
Source code in pyalgotrading/strategy/strategy_base.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(self, *args, **kwargs):
    """
    Init method that is used while creating an object of this class
    Args:
        *args: a variable-length argument list
        **kwargs: a keyworded, variable-length argument list
    """
    # Dummy attributes
    self.strategy_parameters = defaultdict(lambda: 'dummy')
    self.broker = broker.broker_connection_base.BrokerConnectionBase()
    self.utils = broker.utils

    # Dummy Order Attributes
    self.BuyOrderRegular = BuyOrderRegular
    self.SellOrderRegular = SellOrderRegular
    self.BuyOrderBracket = BuyOrderBracket
    self.SellOrderBracket = SellOrderBracket

    # Dummy strategy config attributes
    self.strategy_mode = StrategyMode.INTRADAY  # <Type: Enum of type StrategyMode; This attribute will hold one of the following values - StrategyMode.INTRADAY or StrategyMode.DELIVERY. This value is passed to pyalgotrading.algobulls.connection.backtest/papertest/realtrade methods.>
    self.number_of_lots = 1  # <Type: This attribute will hold one of the following values - StrategyMode.INTRADAY or StrategyMode.DELIVERY. This value is passed to pyalgotrading.algobulls.connection.backtest/papertest/realtrade methods.>

get_historical_data(self, instrument)

Fetch the historical data of an instrument This is a dummy function with a sole purpose of mimicing the signature of the actual function on the AlgoBulls platform, as described in the documentation.

Parameters:

Name Type Description Default
instrument

instrument key

required

Returns:

Type Description

None

Source code in pyalgotrading/strategy/strategy_base.py
52
53
54
55
56
57
58
59
60
61
62
63
def get_historical_data(self, instrument):
    """
    Fetch the historical data of an instrument
    This is a dummy function with a sole purpose of mimicing the signature of the actual function on the AlgoBulls platform, as described in the documentation.
    Args:
        instrument: instrument key

    Returns:
        None
    """
    # This is a dummy function with a sole purpose of mimicing the signature of the actual function on the AlgoBulls platform, as described in the documentation.
    pass

initialize(self)

Initialization task for this strategy. Calling this method should initialise/reset the strategy's internal state variables to original state.

Every Strategy should initialize its internal state variables in this task to reset values.

Indented to be called by TLS as part of pre market activity & for backtesting mode, at the start of every new backtesting day.

Source code in pyalgotrading/strategy/strategy_base.py
74
75
76
77
78
79
80
81
82
83
@abstractmethod
def initialize(self):
    """
    Initialization task for this strategy. Calling this method should initialise/reset the strategy's internal state variables to original state.

    Every Strategy should initialize its internal state variables in this task to reset values.

    Indented to be called by TLS as part of pre market activity & for backtesting mode, at the start of every new backtesting day.
    """
    raise NotImplementedError

name() staticmethod

Name of the strategy Returns: None

Source code in pyalgotrading/strategy/strategy_base.py
42
43
44
45
46
47
48
49
50
@staticmethod
@abstractmethod
def name():
    """
    Name of the strategy
    Returns: None

    """
    raise NotImplementedError

strategy_enter_position(self, candle, instrument, sideband_info)

Enter position for each instrument

Parameters:

Name Type Description Default
candle

the current candle

required
instrument

the instrument key

required
sideband_info

the additional information regarding the instrument action

required

Returns:

Type Description

the order, if position is entered

Source code in pyalgotrading/strategy/strategy_base.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
@abstractmethod
def strategy_enter_position(self, candle, instrument, sideband_info):
    """
    Enter position for each instrument
    Args:
        candle: the current candle
        instrument: the instrument key
        sideband_info: the additional information regarding the instrument action

    Returns:
        the order, if position is entered
    """
    raise NotImplementedError

strategy_exit_position(self, candle, instrument, sideband_info)

Exit position for each instrument

Parameters:

Name Type Description Default
candle

the current candle

required
instrument

the instrument key

required
sideband_info

the additional information regarding the instrument action

required

Returns:

Type Description

True if order is exited else False

Source code in pyalgotrading/strategy/strategy_base.py
124
125
126
127
128
129
130
131
132
133
134
135
136
@abstractmethod
def strategy_exit_position(self, candle, instrument, sideband_info):
    """
    Exit position for each instrument
    Args:
        candle: the current candle
        instrument: the instrument key
        sideband_info: the additional information regarding the instrument action

    Returns:
        True if order is exited else False
    """
    raise NotImplementedError

strategy_select_instruments_for_entry(self, candle, instruments_bucket)

Select instruments for entry here

Parameters:

Name Type Description Default
candle

the current candle

required
instruments_bucket

the list of instruments to be selected from

required

Returns:

Type Description

selected instruments and sideband (additional) information for each

Source code in pyalgotrading/strategy/strategy_base.py
85
86
87
88
89
90
91
92
93
94
95
def strategy_select_instruments_for_entry(self, candle, instruments_bucket):
    """
    Select instruments for entry here
    Args:
        candle: the current candle
        instruments_bucket: the list of instruments to be selected from

    Returns:
        selected instruments and sideband (additional) information for each
    """
    raise NotImplementedError

strategy_select_instruments_for_exit(self, candle, instruments_bucket)

Select instruments for exit here

Parameters:

Name Type Description Default
candle

the current candle

required
instruments_bucket

the list of instruments to be selected from

required

Returns:

Type Description

selected instruments and sideband (additional) information for each

Source code in pyalgotrading/strategy/strategy_base.py
111
112
113
114
115
116
117
118
119
120
121
122
@abstractmethod
def strategy_select_instruments_for_exit(self, candle, instruments_bucket):
    """
    Select instruments for exit here
    Args:
        candle: the current candle
        instruments_bucket: the list of instruments to be selected from

    Returns:
        selected instruments and sideband (additional) information for each
    """
    raise NotImplementedError

versions_supported() staticmethod

Version of the AlgoBulls platform that this Strategy will run on/tested on :return: Should return a single version or list of versions of ABC on which the current strategy has been tested to run successfully

Source code in pyalgotrading/strategy/strategy_base.py
65
66
67
68
69
70
71
72
@staticmethod
@abstractmethod
def versions_supported():
    """
    Version of the AlgoBulls platform that this Strategy will run on/tested on
    :return: Should return a single version or list of versions of ABC on which the current strategy has been tested to run successfully
    """
    raise NotImplementedError

validate_strategy

A module in construction. Create class which validates the strategy - 1. Ensures all methods are implemented and all looks good 2. Basic sanity checks

Add additional information - 1. Add a versioning information in the class automatically


Last update:

Comments