modified strategy to pack a wrapper
This commit is contained in:
parent
2a55f67c6d
commit
e5726627ca
19
strategy.py
19
strategy.py
@ -4,7 +4,7 @@ import sympy.abc
|
|||||||
from bfxapi import Position
|
from bfxapi import Position
|
||||||
from sympy import Point, solve
|
from sympy import Point, solve
|
||||||
|
|
||||||
from bfxbot.models import Strategy, PositionState, SymbolStatus, Event, EventKind, EventMetadata
|
from bfxbot.models import Strategy, PositionState, SymbolStatus, Event, EventKind, EventMetadata, PositionWrapper
|
||||||
from bfxbot.utils import TAKER_FEE, net_pl_percentage
|
from bfxbot.utils import TAKER_FEE, net_pl_percentage
|
||||||
|
|
||||||
|
|
||||||
@ -42,12 +42,6 @@ class SquaredTrailingStop:
|
|||||||
return x - self.y(x)
|
return x - self.y(x)
|
||||||
|
|
||||||
|
|
||||||
class TrailingStopMetadata(EventMetadata):
|
|
||||||
def __init__(self, net_pl_percentage: float, **kwargs):
|
|
||||||
super(TrailingStopMetadata, self).__init__(**kwargs)
|
|
||||||
self.net_pl_percentage: float = net_pl_percentage
|
|
||||||
|
|
||||||
|
|
||||||
class TrailingStopStrategy(Strategy):
|
class TrailingStopStrategy(Strategy):
|
||||||
BREAK_EVEN_PERC = TAKER_FEE
|
BREAK_EVEN_PERC = TAKER_FEE
|
||||||
MIN_PROFIT_PERC = BREAK_EVEN_PERC + 0.3
|
MIN_PROFIT_PERC = BREAK_EVEN_PERC + 0.3
|
||||||
@ -62,7 +56,7 @@ class TrailingStopStrategy(Strategy):
|
|||||||
|
|
||||||
pl_perc = net_pl_percentage(position.profit_loss_percentage, TAKER_FEE)
|
pl_perc = net_pl_percentage(position.profit_loss_percentage, TAKER_FEE)
|
||||||
prev = ss.previous_pw(position.id)
|
prev = ss.previous_pw(position.id)
|
||||||
event_metadata = TrailingStopMetadata(position_id=position.id, net_pl_percentage=pl_perc)
|
event_metadata = EventMetadata(position_id=position.id)
|
||||||
|
|
||||||
if pl_perc > self.GOOD_PROFIT_PERC:
|
if pl_perc > self.GOOD_PROFIT_PERC:
|
||||||
state = PositionState.PROFIT
|
state = PositionState.PROFIT
|
||||||
@ -75,8 +69,11 @@ class TrailingStopStrategy(Strategy):
|
|||||||
else:
|
else:
|
||||||
state = PositionState.CRITICAL
|
state = PositionState.CRITICAL
|
||||||
|
|
||||||
if not prev or prev.state == state:
|
pw = PositionWrapper(position, state=state, net_profit_loss=position.profit_loss,
|
||||||
return state, events
|
net_profit_loss_percentage=pl_perc)
|
||||||
|
|
||||||
|
if not prev or prev.state() == state:
|
||||||
|
return pw, events
|
||||||
|
|
||||||
if state == PositionState.PROFIT:
|
if state == PositionState.PROFIT:
|
||||||
events.append(Event(EventKind.REACHED_GOOD_PROFIT, ss.current_tick, event_metadata))
|
events.append(Event(EventKind.REACHED_GOOD_PROFIT, ss.current_tick, event_metadata))
|
||||||
@ -90,4 +87,4 @@ class TrailingStopStrategy(Strategy):
|
|||||||
events.append(Event(EventKind.REACHED_MAX_LOSS, ss.current_tick, event_metadata))
|
events.append(Event(EventKind.REACHED_MAX_LOSS, ss.current_tick, event_metadata))
|
||||||
events.append(Event(EventKind.CLOSE_POSITION, ss.current_tick, event_metadata))
|
events.append(Event(EventKind.CLOSE_POSITION, ss.current_tick, event_metadata))
|
||||||
|
|
||||||
return state, events
|
return pw, events
|
||||||
|
Loading…
Reference in New Issue
Block a user