tailwind #9
32
strategy.py
32
strategy.py
@ -1,20 +1,13 @@
|
||||
from typing import List
|
||||
|
||||
import sympy.abc
|
||||
from bfxapi import Position
|
||||
from sympy import Point, solve
|
||||
|
||||
from bfxbot.models import Strategy, PositionState, SymbolStatus, Event, EventKind
|
||||
from bfxbot.models import Strategy, PositionState, SymbolStatus, Event, EventKind, EventMetadata
|
||||
from bfxbot.utils import TAKER_FEE, net_pl_percentage
|
||||
|
||||
|
||||
class Position(object):
|
||||
def __init__(self):
|
||||
self.id = None
|
||||
self.profit_loss_percentage = None
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class SquaredTrailingStop:
|
||||
def __init__(self, p_min: Point, p_max: Point):
|
||||
a = sympy.abc.a
|
||||
@ -49,6 +42,12 @@ class SquaredTrailingStop:
|
||||
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):
|
||||
BREAK_EVEN_PERC = TAKER_FEE
|
||||
MIN_PROFIT_PERC = BREAK_EVEN_PERC + 0.3
|
||||
@ -62,7 +61,8 @@ class TrailingStopStrategy(Strategy):
|
||||
events = []
|
||||
|
||||
pl_perc = net_pl_percentage(position.profit_loss_percentage, TAKER_FEE)
|
||||
prev = ss.previous_position_w(position.id)
|
||||
prev = ss.previous_pw(position.id)
|
||||
event_metadata = TrailingStopMetadata(position_id=position.id, net_pl_percentage=pl_perc)
|
||||
|
||||
if pl_perc > self.GOOD_PROFIT_PERC:
|
||||
state = PositionState.PROFIT
|
||||
@ -79,15 +79,15 @@ class TrailingStopStrategy(Strategy):
|
||||
return state, events
|
||||
|
||||
if state == PositionState.PROFIT:
|
||||
events.append(Event(EventKind.REACHED_GOOD_PROFIT, ss.current_tick))
|
||||
events.append(Event(EventKind.REACHED_GOOD_PROFIT, ss.current_tick, event_metadata))
|
||||
elif state == PositionState.MINIMUM_PROFIT:
|
||||
events.append(Event(EventKind.REACHED_MIN_PROFIT, ss.current_tick))
|
||||
events.append(Event(EventKind.REACHED_MIN_PROFIT, ss.current_tick, event_metadata))
|
||||
elif state == PositionState.BREAK_EVEN:
|
||||
events.append(Event(EventKind.REACHED_BREAK_EVEN, ss.current_tick))
|
||||
events.append(Event(EventKind.REACHED_BREAK_EVEN, ss.current_tick, event_metadata))
|
||||
elif state == PositionState.LOSS:
|
||||
events.append(Event(EventKind.REACHED_LOSS, ss.current_tick))
|
||||
events.append(Event(EventKind.REACHED_LOSS, ss.current_tick, event_metadata))
|
||||
else:
|
||||
events.append(Event(EventKind.REACHED_MAX_LOSS, ss.current_tick))
|
||||
events.append(Event(EventKind.CLOSE_POSITION, ss.current_tick))
|
||||
events.append(Event(EventKind.REACHED_MAX_LOSS, ss.current_tick, event_metadata))
|
||||
events.append(Event(EventKind.CLOSE_POSITION, ss.current_tick, event_metadata))
|
||||
|
||||
return state, events
|
||||
|
Loading…
Reference in New Issue
Block a user