def simple_buy_sell(mkt: MarketData, sig: Signal) -> Trades: direction_action_dict = { TernaryDirection.UP: Action.TakeLong, TernaryDirection.NEUTRAL: Action.Donothing, TernaryDirection.DOWN: Action.TakeShort, } return direction_based_entry(mkt, sig, direction_action_dict)
def only_take_short(mkt: MarketData, sig: Signal) -> Trades: direction_action_dict = { TernaryDirection.UP: Action.Donothing, TernaryDirection.NEUTRAL: Action.Donothing, TernaryDirection.DOWN: Action.TakeShort, } return direction_based_entry(mkt, sig, direction_action_dict)
def enter_exit_loss_gain( mkt: MarketData, sig: Signal, gain_threshold: float, loss_threshold: float, max_holding: pd.Timedelta, ) -> Trades: """Take positions and close them when a certain threshold of gain is attained OR loss in attained. Otherwise close it after max_holding Args: mkt: Market data sig: Signal data direction_action_dict: Dictionary from signals to actions max_holding_time: maximum holding time gain_threshold: positive float when to take gain, loss_threshold: positive float when to stop loss, max_holding: max holding time, Result: Trades """ direction_action_dict = { TernaryDirection.UP: Action.TakeLong, TernaryDirection.NEUTRAL: Action.Donothing, TernaryDirection.DOWN: Action.TakeShort, } entries = direction_based_entry(mkt, sig, direction_action_dict) trades = exit_at_loss_and_gain(mkt, sig, entries, max_holding, loss_threshold, gain_threshold) return trades
def entry_and_exit_at_other_signals(mkt: MarketData, sig: Signal, max_holding_time: pd.Timedelta) -> Trades: """ Take both positions and close them within maximum holding time. If other signals appear, also close positions. """ direction_action_dict = { TernaryDirection.UP: Action.TakeLong, TernaryDirection.NEUTRAL: Action.Donothing, TernaryDirection.DOWN: Action.TakeShort, } entries = direction_based_entry(mkt, sig, direction_action_dict) opposite_signals_dict = { TernaryDirection.UP: [ TernaryDirection.DOWN.value, TernaryDirection.NEUTRAL.value, ], TernaryDirection.NEUTRAL: [], TernaryDirection.DOWN: [ TernaryDirection.UP.value, TernaryDirection.NEUTRAL.value, ], } trades = exit_at_opposite_signals(mkt, sig, entries, max_holding_time, opposite_signals_dict) return trades
def entries(signal, market): direction_action_dict = { TernaryDirection.UP: Action.TakeLong, TernaryDirection.NEUTRAL: Action.Donothing, TernaryDirection.DOWN: Action.TakeShort, } entries = direction_based_entry(market, signal, direction_action_dict) return entries
def entry_and_exit_by_expectation(mkt: MarketData, sig: Signal, max_holding_time: pd.Timedelta) -> Trades: """ Take both positions and close them within maximum holding time. If opposite signals appear, also close positions. """ direction_action_dict = { TernaryDirection.UP: Action.TakeLong, TernaryDirection.NEUTRAL: Action.Donothing, TernaryDirection.DOWN: Action.TakeShort, } entries = direction_based_entry(mkt, sig, direction_action_dict) trades = exit_by_expectation(mkt, sig, entries, max_holding_time) return trades
def _entry_and_exit_at_max_holding_time( mkt: MarketData, sig: Signal, direction_action_dict: dict, max_holding_time: pd.Timedelta, ) -> Trades: """Take positions and close them within maximum holding time. Args: mkt: Market data sig: Signal data direction_action_dict: Dictionary from signals to actions max_holding_time: maximum holding time exit_condition: The entry is closed most closest time which condition is `True`. Result: Trades """ entries = direction_based_entry(mkt, sig, direction_action_dict) trades = exit_at_max_holding_time(mkt, sig, entries, max_holding_time) return trades