Strategies
The bot provides an event driven structure to build custom strategies. In order to create a strategy, all you have to do
is create a class that extends from the class Strategy
provided by the bot, and than you can start implementing the
things you want.
A very simple strategy for example would look like this:
from hawkbot.strategies.strategy import Strategy
class AbstractBaseStrategy(Strategy):
def __init__(self,
symbol_config,
position_side_config,
plugin_loader,
order_executor,
time_provider,
entry_manager,
candidate_state,
mode_processor):
super().__init__(symbol_config=symbol_config,
position_side_config=position_side_config,
plugin_loader=plugin_loader,
order_executor=order_executor,
time_provider=time_provider,
entry_manager=entry_manager,
candidate_state=candidate_state,
mode_processor=mode_processor)
Using only that basic init function to allow Hawkbot to provide certain key items that the strategy can use if needed, the strategy can then be run by defining it in the config.
The next step to creating a strategy is to make it react to events. Events are generated by Hawkbot for various situations, and a strategy can execute logic as desired for events as it desired.
It is possible to build strategies acting on various events:
on_dca_order_filled
on_entry_order_filled
on_initial_entry_order_filled
on_mode_changed
on_no_open_position
on_order_cancelled
on_orderbook_updated
on_periodic_check
on_position_change
on_position_closed
on_position_on_startup
on_pulse
on_shutdown
on_stoploss_filled
on_strategy_activated
on_tick
on_tp_order_filled
on_tp_refill_order_filled
on_unknown_filled
on_wallet_changed
on_wiggle_decrease_filled
on_wiggle_increase_filled
As an example, the following function will create an initial entry order:
from hawkbot.core.model import PositionSide, Position, SymbolInformation
def on_no_open_position(self,
symbol: str,
position: Position,
symbol_information: SymbolInformation,
wallet_balance: float,
current_price: float):
self.order_executor.create_order(
LimitOrder(order_type_identifier=OrderTypeIdentifier.INITIAL_ENTRY,
symbol=symbol,
quantity=1,
side=Side.BUY,
position_side=PositionSide.LONG,
price=current_price))
logger.info(f'{symbol} {position_side.name}: Finished placing orders')