4.5 KiB
Strategy
The base class for creating strategies.
Table of Contents
- Strategy
- _init_
- sleep
- delay
- live_sleep
- backtest_sleep
- run_strategy
- live_strategy
- backtest_strategy
- trade
- test
Strategy
class Strategy(ABC)
The base class for creating strategies.
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str |
A name for the strategy. | None |
symbol |
Symbol |
The Financial Instrument as a Symbol Object | None |
sessions |
Sessions |
Trading sessions. | None |
mt5 |
MetaTrader | MetaBackTester |
MetaTrader instance. | None |
config |
Config |
Config instance. | None |
parameters |
dict |
A dictionary of parameters for the strategy. | None |
backtest_controller |
BackTesterController |
A controller for the backtester. | |
current_session |
Session |
The current trading session | |
running |
bool |
A flag to indicate if the strategy is running. | True |
_init_
def __init__(*, symbol: Symbol, params: dict = None, sessions: Sessions, name: str = "")
Initiate the parameters dict and add name and symbol fields. Use class name as strategy name if name is not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol |
Symbol |
The Financial instrument | |
params |
Dict |
Trading strategy parameters | None |
sessions |
Sessions |
Trading sessions | None |
name |
str |
The name of the strategy | "" |
sleep
async def sleep(*, secs: float)
Sleep for the needed amount of seconds in between requests to the terminal.
computes the accurate amount of time needed to sleep ensuring that the next request is made at the start of
a new bar and making cooperative multitasking possible.
This method calls the live_sleep method during live trading or backtest_sleep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secs |
float |
The time in seconds. Usually the timeframe you are trading on. | None |
delay
async def delay(*, secs: float)
Sleep for the needed amount of seconds specified in the parameter.
live_sleep
async def live_sleep(*, secs: float)
Sleep method for live trading
backtest_sleep
async def backtest_sleep(*, secs: float)
Sleep method for backtesting
trade
@abstractmethod
async def trade()
Place trades using this method. Implement this method in your own strategy as you wish.
test
@abstractmethod
async def test()
Use for backtesting. If not implemented use the trade method.
run_strategy
async def run_strategy()
Run the strategy by calling the trade or test method repeatedly in a while loop.
This method actually calls the live_strategy or backtest_strategy depending on the mode.
live_strategy
async def live_strategy()
Runs the strategy in live mode.
backtest_strategy
async def live_strategy()
Runs the strategy in backtest mode.