Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Apr 4, 2024
1 parent dfb98fa commit 8164d60
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 51 deletions.
4 changes: 2 additions & 2 deletions config.default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ max_order_breach = 3
stop_loss_threshold = 0.5
risk_factor = 0.618
tp_factor = 2.236
sl_factor = 2.236
sl_factor = 1.618
trl_factor = 0.618
depth = 80

Expand All @@ -41,7 +41,7 @@ reevaluate_timeout = 3600
[generator]
n_samples = 21
blacklist = [USDCUSDT]
timeframes = [1m]
timeframes = [5m]

[optimization]
max_generations = 5
Expand Down
109 changes: 70 additions & 39 deletions position/risk/volatility.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def __init__(self, config_service: AbstractConfig):
self.tp_model = SGDRegressor(max_iter=50, tol=1e-3)
self.sl_model = SGDRegressor(max_iter=50, tol=1e-3)

self.x_tp_buff = deque(maxlen=24)
self.y_tp_buff = deque(maxlen=24)
self.x_tp_buff = deque(maxlen=55)
self.y_tp_buff = deque(maxlen=55)

self.x_sl_buff = deque(maxlen=24)
self.y_sl_buff = deque(maxlen=24)
self.lookback = 14
self.x_sl_buff = deque(maxlen=55)
self.y_sl_buff = deque(maxlen=55)
self.lookback = 6

def next(
self,
Expand All @@ -38,7 +38,9 @@ def next(
return stop_loss_price, take_profit_price

atr = self._atr(ohlcvs, self.lookback)
price = self._hlc2(ohlcvs)
price = self._hlc(ohlcvs)
mean = np.mean(price)
std = np.std(price)

curr_price, curr_atr = price[-1], atr[-1]

Expand All @@ -47,78 +49,103 @@ def next(
self.update_tp_model(features, np.array([take_profit_price]))
self.update_sl_model(features, np.array([stop_loss_price]))

tatr = curr_atr * self.config["trl_factor"]
sl_threshold = curr_atr * self.config["sl_factor"]
risk_value = curr_atr * self.config["risk_factor"]
sl_threshold = curr_atr * self.config["sl_factor"]
tp_threshold = curr_atr * self.config["tp_factor"]

tsl = abs(entry_price - take_profit_price) * self.config["trl_factor"]
curr_dist = abs(entry_price - curr_price)

high = min(ohlcvs[-self.lookback :], key=lambda x: abs(x.high - tsl)).high
low = min(ohlcvs[-self.lookback :], key=lambda x: abs(x.low - tsl)).low

tsl = abs(entry_price - curr_price) * tatr
next_stop_loss, next_take_profit = stop_loss_price, take_profit_price

next_stop_loss = stop_loss_price
next_take_profit = take_profit_price
upper_bound, lower_bound = mean + 2 * std, mean - 2 * std

predict_tp = self.predict_take_profit(
take_profit_price - tsl
if side == PositionSide.LONG
else take_profit_price + tsl
upper_bound, lower_bound, take_profit_price, side, tp_threshold
)

predict_sl = self.predict_stop_loss(
stop_loss_price + tsl
if side == PositionSide.LONG
else stop_loss_price - tsl
upper_bound, lower_bound, stop_loss_price, side, sl_threshold
)

high = min(ohlcvs[-self.lookback :], key=lambda x: abs(x.high - tsl)).high
low = min(ohlcvs[-self.lookback :], key=lambda x: abs(x.low - tsl)).low

print(
f"PREDICT: ENTRY: {entry_price}, TP: {predict_tp}, SL: {predict_sl}, SIDE: {side}, PRICE: {curr_price}, H:{high}, L:{low}"
f"PREDICT: ENTRY: {entry_price}, TP: {predict_tp}, SL: {predict_sl}, SIDE: {side}, PRICE: {curr_price}, TSL: {tsl}"
)

if side == PositionSide.LONG:
if predict_tp > curr_price:
next_take_profit = max(entry_price + risk_value, predict_tp)

if predict_sl < curr_price:
next_stop_loss = max(stop_loss_price, low - sl_threshold, predict_sl)
next_stop_loss = max(
stop_loss_price,
low - tp_threshold,
predict_sl,
)

elif side == PositionSide.SHORT:
if predict_tp < curr_price:
next_take_profit = min(entry_price - risk_value, predict_tp)

if predict_sl > curr_price:
next_stop_loss = min(stop_loss_price, high + sl_threshold, predict_sl)
next_stop_loss = min(
stop_loss_price,
high + sl_threshold,
predict_sl,
)

return next_stop_loss, next_take_profit

def predict_take_profit(self, take_profit_price) -> float:
def predict_take_profit(
self,
upper_bound: float,
lower_bound: float,
take_profit_price: float,
side: PositionSide,
buff: float,
) -> float:
if len(self.x_tp_buff) < self.lookback:
return take_profit_price

latest_X = self.x_tp_buff[-1]
prediction = self.tp_model.predict(latest_X)

if (
prediction[0] > 2.236 * take_profit_price
or prediction[0] < 0.618 * take_profit_price
):
tpp = abs(prediction[0])

if tpp > upper_bound or tpp < lower_bound:
return take_profit_price

return prediction[0]
if side == PositionSide.LONG:
return tpp + buff
else:
return tpp - buff

def predict_stop_loss(self, stop_loss_price) -> float:
def predict_stop_loss(
self,
upper_bound: float,
lower_bound: float,
stop_loss_price: float,
side: PositionSide,
buff: float,
) -> float:
if len(self.x_sl_buff) < self.lookback:
return stop_loss_price

latest_X = self.x_sl_buff[-1]
prediction = self.sl_model.predict(latest_X)

if (
prediction[0] > 2.236 * stop_loss_price
or prediction[0] < 0.618 * stop_loss_price
):
slp = abs(prediction[0])

if slp > upper_bound or slp < lower_bound:
return stop_loss_price

return prediction[0]
if side == PositionSide.LONG:
return slp - buff
else:
return slp + buff

@staticmethod
def _atr(ohlcvs: List[OHLCV], period: int) -> List[float]:
Expand Down Expand Up @@ -157,8 +184,10 @@ def _pp(ohlcvs: List[OHLCV]):
previous_low = ohlcvs[-2].low

pivot_point = (previous_high + previous_low + previous_close) / 3

support1 = (2 * pivot_point) - previous_high
resistance1 = (2 * pivot_point) - previous_low

support2 = pivot_point - (previous_high - previous_low)
resistance2 = pivot_point + (previous_high - previous_low)

Expand All @@ -167,21 +196,23 @@ def _pp(ohlcvs: List[OHLCV]):
def _features(self, ohlcvs: List[Tuple[OHLCV]], atr: List[float]):
support1, resistance1, support2, resistance2 = self._pp(ohlcvs)
closes = [ohlcv.close for ohlcv in ohlcvs]
mean_vol = np.mean([ohlcv.volume for ohlcv in ohlcvs])

mean = np.mean(closes)
std = np.std(closes)

features = np.array(
[
self._hlc2(ohlcvs)[-1],
self._hlc(ohlcvs)[-1],
ohlcvs[-1].volume / np.mean([ohlcv.volume for ohlcv in ohlcvs]),
atr[-1],
mean + 2 * std,
self._hlc2(ohlcvs)[-1],
3 * atr[-1],
mean - 2 * std,
mean + 2 * std,
support1,
resistance1,
support2,
resistance2,
ohlcvs[-1].volume / mean_vol,
]
)

Expand Down
6 changes: 3 additions & 3 deletions strategy/generator/confirm/dso.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
class DsoConfirm(Confirm):
type: Confirm = ConfirmType.Dso
smooth_type: Parameter = StaticParameter(Smooth.EMA)
smooth_period: Parameter = StaticParameter(13.0)
k_period: Parameter = StaticParameter(8.0)
d_period: Parameter = StaticParameter(9.0)
smooth_period: Parameter = StaticParameter(10.0)
k_period: Parameter = StaticParameter(5.0)
d_period: Parameter = StaticParameter(7.0)
4 changes: 2 additions & 2 deletions strategy/generator/signal/dmi_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
class DmiCrossSignal(Signal):
type: SignalType = SignalType.DmiCross
smooth_type: Parameter = StaticParameter(Smooth.SMMA)
adx_period: Parameter = StaticParameter(4.0)
di_period: Parameter = StaticParameter(4.0)
adx_period: Parameter = StaticParameter(8.0)
di_period: Parameter = StaticParameter(8.0)
2 changes: 1 addition & 1 deletion strategy/generator/signal/vi_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
class ViCrossSignal(Signal):
type: SignalType = SignalType.ViCross
atr_period: Parameter = StaticParameter(1.0)
period: Parameter = StaticParameter(2.0)
period: Parameter = StaticParameter(8.0)
8 changes: 4 additions & 4 deletions ta_lib/strategies/base/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ impl Strategy for BaseStrategy {
let theo_price = self.suggested_entry();

match self.trade_signals() {
(true, false, false, false) => TradeAction::GoLong(theo_price),
(false, true, false, false) => TradeAction::GoShort(theo_price),
(false, false, true, false) => TradeAction::ExitLong(data.close),
(false, false, false, true) => TradeAction::ExitShort(data.close),
(true, _, false, false) => TradeAction::GoLong(theo_price),
(_, true, false, false) => TradeAction::GoShort(theo_price),
(false, false, true, _) => TradeAction::ExitLong(data.close),
(false, false, _, true) => TradeAction::ExitShort(data.close),
_ => TradeAction::DoNothing,
}
}
Expand Down

0 comments on commit 8164d60

Please sign in to comment.