Synchronise

This commit is contained in:
Jérôme Delacotte
2025-03-09 16:53:02 +01:00
parent 9449d25384
commit 8a1c69e53a
4 changed files with 486 additions and 211 deletions

View File

@@ -24,6 +24,7 @@ import ta
import talib.abstract as talib
import freqtrade.vendor.qtpylib.indicators as qtpylib
import requests
from datetime import timezone, timedelta
logger = logging.getLogger(__name__)
@@ -233,7 +234,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# allow_to_buy = rate <= dataframe['lbp_3']
self.trades = list()
dispo = round(self.wallets.get_available_stake_amount())
logger.info(f"{pair} allow_to_buy {allow_to_buy} limit={limit} Buy {entry_tag} {current_time} dispo={dispo}")
logger.info(f"BUY {pair} allow_to_buy {allow_to_buy} limit={limit} Buy {entry_tag} {current_time} dispo={dispo}")
return allow_to_buy
@@ -250,14 +251,14 @@ class Zeus_8_3_2_B_4_2(IStrategy):
if allow_to_sell:
self.trades = list()
logger.info('Sell trade ' + exit_reason + ' ' + str(current_time) + ' ' + pair + " dispo=" + str(
logger.info('Sell ' + exit_reason + ' ' + str(current_time) + ' ' + pair + " dispo=" + str(
round(self.wallets.get_available_stake_amount())) # "+ str(amount) + ' ' + str(rate)
+ " open_rate=" + str(trade.open_rate) + " rate=" + str(rate) + " profit=" + str(
trade.calc_profit(rate, amount))
+ " " + string)
# del self.max_profit_pairs[pair]
else:
logger.info('Cancel Sell trade ' + exit_reason + ' ' + str(current_time) + ' ' + pair)
logger.info('Cancel Sell ' + exit_reason + ' ' + str(current_time) + ' ' + pair)
return (allow_to_sell) | (exit_reason == 'force_exit')
def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float,
@@ -272,36 +273,35 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# Use default stake amount.
return adjusted_stake_amount
def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
current_rate: float, current_profit: float, **kwargs) -> float:
# # hard stoploss profit
# HSL = self.pHSL.value
# PF_1 = self.pPF_1.value
# SL_1 = self.pSL_1.value
# PF_2 = self.pPF_2.value
# SL_2 = self.pSL_2.value
#
# # For profits between PF_1 and PF_2 the stoploss (sl_profit) used is linearly interpolated
# # between the values of SL_1 and SL_2. For all profits above PL_2 the sl_profit value
# # rises linearly with current profit, for profits below PF_1 the hard stoploss profit is used.
#
# if current_profit > PF_2:
# sl_profit = SL_2 + (current_profit - PF_2)
# elif current_profit > PF_1:
# sl_profit = SL_1 + ((current_profit - PF_1) * (SL_2 - SL_1) / (PF_2 - PF_1))
# else:
# sl_profit = HSL
#print(f"entry_tag={trade.entry_tag} max={trade.max_rate} min={trade.min_rate} ")
if current_profit > 0.0125:
sl_profit = 0.75 * current_profit # 75% du profit en cours
else:
sl_profit = self.pHSL.value # Hard stop-loss
stoploss = stoploss_from_open(sl_profit, current_profit)
logger.info(f"stoploss={stoploss}")
return stoploss
#
# def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
# current_rate: float, current_profit: float, **kwargs) -> float:
#
# # # hard stoploss profit
# # HSL = self.pHSL.value
# # PF_1 = self.pPF_1.value
# # SL_1 = self.pSL_1.value
# # PF_2 = self.pPF_2.value
# # SL_2 = self.pSL_2.value
# #
# # # For profits between PF_1 and PF_2 the stoploss (sl_profit) used is linearly interpolated
# # # between the values of SL_1 and SL_2. For all profits above PL_2 the sl_profit value
# # # rises linearly with current profit, for profits below PF_1 the hard stoploss profit is used.
# #
# # if current_profit > PF_2:
# # sl_profit = SL_2 + (current_profit - PF_2)
# # elif current_profit > PF_1:
# # sl_profit = SL_1 + ((current_profit - PF_1) * (SL_2 - SL_1) / (PF_2 - PF_1))
# # else:
# # sl_profit = HSL
#
# #print(f"entry_tag={trade.entry_tag} max={trade.max_rate} min={trade.min_rate} ")
# if current_profit > 0.0125:
# sl_profit = 0.75 * current_profit # 75% du profit en cours
# else:
# sl_profit = self.pHSL.value # Hard stop-loss
# stoploss = stoploss_from_open(sl_profit, current_profit)
# return stoploss
#
# dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
@@ -347,33 +347,33 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# print('current_profit=' + str(current_profit) + ' stop from open=' + str(slfo))
# return slfo
# def custom_exit(self, pair: str, trade: Trade, current_time, current_rate, current_profit, **kwargs):
#
# dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
# last_candle = dataframe.iloc[-1].squeeze()
#
# # self.analyze_conditions(pair, dataframe)
#
# print("---------------" + pair + "----------------")
# expected_profit = self.expectedProfit(pair, last_candle)
#
# dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
# last_candle = dataframe.iloc[-1]
#
# # Calcul du prix cible basé sur l'ATR
# atr_take_profit = trade.open_rate + (last_candle['atr'] * 2) # Prendre profit à 2x l'ATR
#
# logger.info(f"{pair} Custom exit atr_take_profit={atr_take_profit:.4f}")
# # if current_rate >= atr_take_profit:
# # return 'sell_atr_take_profit'
#
# if (last_candle['percent3'] < -0.002) & (last_candle['percent12'] < 0) & (
# current_profit > last_candle['min_max200'] / 2):
# self.trades = list()
# return 'min_max200'
# if (last_candle['percent12'] <= -0.01) & (current_profit >= expected_profit):
# self.trades = list()
# return 'profit'
def custom_exit(self, pair: str, trade: Trade, current_time, current_rate, current_profit, **kwargs):
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
last_candle = dataframe.iloc[-1].squeeze()
# self.analyze_conditions(pair, dataframe)
# print("---------------" + pair + "----------------")
expected_profit = self.expectedProfit(pair, last_candle)
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
last_candle = dataframe.iloc[-1]
# Calcul du prix cible basé sur l'ATR
atr_take_profit = trade.open_rate + (last_candle['atr'] * 2) # Prendre profit à 2x l'ATR
# logger.info(f"{pair} Custom exit atr_take_profit={atr_take_profit:.4f}")
# if current_rate >= atr_take_profit:
# return 'sell_atr_take_profit'
if (last_candle['percent3'] < -0.002) & (last_candle['percent12'] < 0) & (
current_profit > last_candle['min_max200'] / 2):
self.trades = list()
return 'min_max200'
if (last_candle['percent12'] <= -0.01) & (current_profit >= expected_profit):
self.trades = list()
return 'profit'
def informative_pairs(self):
# get access to all pairs available in whitelist.
@@ -548,6 +548,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
dataframe['limit'] = dataframe['last_price'] * (1 - self.baisse[count] / 100)
dataframe['amount'] = amount
print(f"amount= {amount}")
# trades = Trade.get_trades([Trade.is_open is False]).all()
trades = Trade.get_trades_proxy(is_open=False, pair=metadata['pair'])
if trades:
@@ -627,19 +628,19 @@ class Zeus_8_3_2_B_4_2(IStrategy):
self.trades = Trade.get_open_trades()
return self.trades
def getTrade(self, pair):
trades = self.getOpenTrades()
trade_for_pair = None
for trade in trades:
if trade.pair == pair:
trade_for_pair = trade
break
return trade_for_pair
# def getTrade(self, pair):
# trades = self.getOpenTrades()
# trade_for_pair = None
# for trade in trades:
# if trade.pair == pair:
# trade_for_pair = trade
# break
# return trade_for_pair
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
pair = metadata['pair']
self.getOpenTrades()
# self.getOpenTrades()
expected_profit = self.expectedProfit(pair, dataframe.iloc[-1])
# self.getBinanceOrderBook(pair, dataframe)
last_candle = dataframe.iloc[-1].squeeze()
@@ -648,7 +649,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# self.updateLastValue(dataframe, 'expected_profit', expected_profit)
print("---------------" + pair + "----------------")
print('adjust stake amount ' + str(self.adjust_stake_amount(pair, dataframe.iloc[-1])))
print('adjust exit price ' + str(self.adjust_exit_price(dataframe.iloc[-1])))
# print('adjust exit price ' + str(self.adjust_exit_price(dataframe.iloc[-1])))
print('calcul expected_profit ' + str(expected_profit))
buy_level = dataframe['buy_level'] # self.get_buy_level(pair, dataframe)
@@ -705,7 +706,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
)
& (dataframe['percent'] >= -0.0005)
& (dataframe['min12'].shift(2) == dataframe['min12'])
), ['buy', 'enter_tag']] = (1, 'buy_min_max200_2')
), ['enter_long', 'enter_tag']] = (1, 'buy_min_max200_2')
dataframe.loc[
(
((dataframe['count_buys'] > 0) & (dataframe['close'] <= dataframe['limit']))
@@ -715,7 +716,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
(dataframe['min12'].shift(2) == dataframe['min12']) |
(dataframe['min200'].shift(60) >= dataframe['min200'] * 1.03)
)
), ['buy', 'enter_tag']] = (1, 'buy_count_buy')
), ['enter_long', 'enter_tag']] = (1, 'buy_count_buy')
dataframe.loc[
(
@@ -737,7 +738,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
| (dataframe['percent12'] < -0.022)
| (dataframe['percent24'] < -0.022)
)
), ['buy', 'enter_tag']] = (1, 'buy_0_percent12')
), ['enter_long', 'enter_tag']] = (1, 'buy_0_percent12')
dataframe.loc[
(
# (dataframe['percent12'] < -0.015)
@@ -745,7 +746,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
& (dataframe['open'] < dataframe['average_line_50'])
& (dataframe['close'] < dataframe['min12'] * 1.002)
& (dataframe['min12'].shift(2) == dataframe['min12'])
), ['buy', 'enter_tag']] = (1, 'buy_percent12')
), ['enter_long', 'enter_tag']] = (1, 'buy_percent12')
dataframe.loc[
(
@@ -755,7 +756,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
& (dataframe['min50'].shift(3) == dataframe['min50'])
& (dataframe['close'] <= dataframe['min50'] * 1.002)
& (dataframe['open'] < dataframe['average_line_288'])
), ['buy', 'enter_tag']] = (1, 'buy_percent_max_144')
), ['enter_long', 'enter_tag']] = (1, 'buy_percent_max_144')
dataframe.loc[
(
(dataframe['close'] <= dataframe['min200'] * 1.002)
@@ -763,7 +764,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
& (dataframe['pct_change'] < 0)
& (dataframe['haopen'] < buy_level)
& (dataframe['open'] < dataframe['average_line_288'])
), ['buy', 'enter_tag']] = (1, 'buy_min_max_200')
), ['enter_long', 'enter_tag']] = (1, 'buy_min_max_200')
dataframe.loc[
(
(dataframe['percent_max_144'] <= -0.02)
@@ -773,21 +774,21 @@ class Zeus_8_3_2_B_4_2(IStrategy):
& (dataframe['close'] <= dataframe['average_line_288_099'])
& (dataframe['min50'].shift(3) == dataframe['min50'])
& (dataframe['close'] <= dataframe['min50'] * 1.002)
), ['buy', 'enter_tag']] = (1, 'buy_close_02')
dataframe.loc[
(
(dataframe['close'] <= dataframe['lowest_4_average'] * 1.002)
& (dataframe['haopen'] >= dataframe['lbp_3'])
& (dataframe['haclose'] <= dataframe['lbp_3'])
& (dataframe['haopen'] < buy_level)
), ['buy', 'enter_tag']] = (1, 'buy_lbp_3')
), ['enter_long', 'enter_tag']] = (1, 'buy_close_02')
# dataframe.loc[
# (
# (dataframe['close'] <= dataframe['lowest_4_average'] * 1.002)
# & (dataframe['haopen'] >= dataframe['lbp_3'])
# & (dataframe['haclose'] <= dataframe['lbp_3'])
# & (dataframe['haopen'] < buy_level)
# ), ['enter_long', 'enter_tag']] = (1, 'buy_lbp_3')
dataframe.loc[
(
(dataframe['close'] <= dataframe['lowest_4_average'] * 1.002)
& (dataframe['haopen'] >= dataframe['average_line_288_098'])
& (dataframe['haclose'] <= dataframe['average_line_288_098'])
& (dataframe['haopen'] < buy_level)
), ['buy', 'enter_tag']] = (1, 'buy_average_line_288_098')
), ['enter_long', 'enter_tag']] = (1, 'buy_average_line_288_098')
dataframe.loc[
(
(dataframe['close'].shift(2) <= dataframe['min200'])
@@ -797,9 +798,9 @@ class Zeus_8_3_2_B_4_2(IStrategy):
& (dataframe['count_buys'] == 0 |
((dataframe['count_buys'] > 0) & (dataframe['close'] <= dataframe['limit']))
)
), ['buy', 'enter_tag']] = (1, 'buy_min200')
), ['enter_long', 'enter_tag']] = (1, 'buy_min200')
dataframe['test'] = np.where(dataframe['buy'] == 1, dataframe['close'] * 1.01, np.nan)
dataframe['test'] = np.where(dataframe['enter_long'] == 1, dataframe['close'] * 1.01, np.nan)
return dataframe
@@ -810,7 +811,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# for trade in self.trades:
# if trade.pair != pair:
# continue
# filled_buys = trade.select_filled_orders('buy')
# filled_buys = trade.select_filled_orders('enter_long')
# print('populate_buy_trend filled_buys : ' + str(len(filled_buys)))
# # Affichez les valeurs
# print(pair, limit)
@@ -848,10 +849,10 @@ class Zeus_8_3_2_B_4_2(IStrategy):
if count_of_buys >= max_buys:
return None
if 'buy' in last_candle:
condition = (last_candle['buy'] == 1)
else:
condition = False
# if 'buy' in last_candle:
# condition = (last_candle['buy'] == 1)
# else:
# condition = False
# self.protection_nb_buy_lost.value
# limits = ['lbp_3', 'lbp_6', 'lbp_9', 'lbp_12', 'lbp_20']
# limit = last_candle[limits[count_of_buys]]
@@ -859,17 +860,20 @@ class Zeus_8_3_2_B_4_2(IStrategy):
stake_amount = min(200, self.adjust_stake_amount(pair, last_candle) * self.fibo[count_of_buys])
# print("Adjust " + trade.pair + " time=" + str(current_time) + ' rate=' + str(current_rate) + " buys=" + str(count_of_buys) + " limit=" + str(limit) + " stake=" + str(stake_amount))
logger.info(
f"Adjust price={trade.pair} buy={condition} rate={current_rate:.4f} buys={count_of_buys} limit={limit:.4f} stake={stake_amount:.4f}")
# logger.info(
# f"Adjust price={trade.pair} buy={condition} rate={current_rate:.4f} buys={count_of_buys} limit={limit:.4f} stake={stake_amount:.4f}")
current_time_utc = current_time.astimezone(timezone.utc)
open_date = trade.open_date.astimezone(timezone.utc)
days_since_open = (current_time_utc - open_date).days
if (0 < count_of_buys <= max_buys) & (current_rate <= limit) & (condition):
if (days_since_open > count_of_buys) & (0 < count_of_buys <= max_buys) & (current_rate <= limit) & (last_candle['enter_long'] == 1):
try:
# This then calculates current safety order size
# stake_amount = stake_amount * pow(1.5, count_of_buys)
# print("Effective Adjust " + trade.pair + " time=" + str(current_time) + ' rate=' + str(current_rate) + " buys=" + str(count_of_buys) + " limit=" + str(limit) + " stake=" + str(stake_amount))
logger.info(
f"Effective Adjust price={trade.pair} rate={current_rate:.4f} buys={count_of_buys} limit={limit:.4f} stake={stake_amount:.4f}")
f"Adjust {current_time} price={trade.pair} rate={current_rate:.4f} buys={count_of_buys} limit={limit:.4f} stake={stake_amount:.4f}")
return stake_amount
except Exception as exception:
@@ -907,24 +911,24 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# adjusted_stake_amount_2 = max(base_stake_amount / 2.5, min(75, base_stake_amount * percent))
print(
f"Stack amount ajusté price={current_price} max_min={max_min_4:.4f} min_14={min_14_days_4:.4f} max_14={max_14_days_4:.4f} factor={factor_4:.4f} percent={percent_4:.4f} amount={adjusted_stake_amount:.4f}")
# print(
# f"Stack amount ajusté price={current_price} max_min={max_min_4:.4f} min_14={min_14_days_4:.4f} max_14={max_14_days_4:.4f} factor={factor_4:.4f} percent={percent_4:.4f} amount={adjusted_stake_amount:.4f}")
# print(f"Stack amount ajusté price={current_price} max_min={max_min:.4f} min_14={min_14_days:.4f} max_14={max_14_days:.4f} factor={factor:.4f} percent={percent:.4f} amount={adjusted_stake_amount_2:.4f}")
return adjusted_stake_amount
def adjust_exit_price(self, dataframe: DataFrame):
# Calculer le max des 14 derniers jours
min_14_days = dataframe['lowest_1d']
max_14_days = dataframe['highest_1d']
entry_price = dataframe['fbp']
current_price = dataframe['close']
percent = 0.5 * (max_14_days - min_14_days) / min_14_days
exit_price = (1 + percent) * entry_price
print(f"Exit price ajusté price={current_price:.4f} max_14={max_14_days:.4f} exit_price={exit_price:.4f}")
return exit_price
# def adjust_exit_price(self, dataframe: DataFrame):
# # Calculer le max des 14 derniers jours
# min_14_days = dataframe['lowest_1d']
# max_14_days = dataframe['highest_1d']
# entry_price = dataframe['fbp']
# current_price = dataframe['close']
# percent = 0.5 * (max_14_days - min_14_days) / min_14_days
# exit_price = (1 + percent) * entry_price
#
# print(f"Exit price ajusté price={current_price:.4f} max_14={max_14_days:.4f} exit_price={exit_price:.4f}")
#
# return exit_price
def adjust_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
current_rate: float, current_profit: float, **kwargs) -> float:
@@ -954,8 +958,8 @@ class Zeus_8_3_2_B_4_2(IStrategy):
min_max = dataframe['pct_min_max_1d'] # (max_14_days - min_14_days) / min_14_days
expected_profit = min(0.1, max(0.01, dataframe['min_max200'] * 0.5))
print(
f"Expected profit price={current_price:.4f} min_max={min_max:.4f} min_14={min_14_days:.4f} max_14={max_14_days:.4f} percent={percent:.4f} expected_profit={expected_profit:.4f}")
# print(
# f"Expected profit price={current_price:.4f} min_max={min_max:.4f} min_14={min_14_days:.4f} max_14={max_14_days:.4f} percent={percent:.4f} expected_profit={expected_profit:.4f}")
# self.analyze_conditions(pair, dataframe)
return expected_profit