2020-09-04 17:44:35 +00:00
|
|
|
import pandas as pd
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2020-09-04 17:44:35 +00:00
|
|
|
from freqtrade.exchange import timeframe_to_minutes
|
|
|
|
|
|
|
|
|
2020-09-04 18:02:31 +00:00
|
|
|
def merge_informative_pair(dataframe: pd.DataFrame, informative: pd.DataFrame,
|
2021-07-17 16:19:49 +00:00
|
|
|
timeframe: str, timeframe_inf: str, ffill: bool = True,
|
|
|
|
append_timeframe: bool = True,
|
|
|
|
date_column: str = 'date') -> pd.DataFrame:
|
2020-09-04 17:44:35 +00:00
|
|
|
"""
|
|
|
|
Correctly merge informative samples to the original dataframe, avoiding lookahead bias.
|
|
|
|
|
|
|
|
Since dates are candle open dates, merging a 15m candle that starts at 15:00, and a
|
|
|
|
1h candle that starts at 15:00 will result in all candles to know the close at 16:00
|
|
|
|
which they should not know.
|
|
|
|
|
|
|
|
Moves the date of the informative pair by 1 time interval forward.
|
|
|
|
This way, the 14:00 1h candle is merged to 15:00 15m candle, since the 14:00 1h candle is the
|
|
|
|
last candle that's closed at 15:00, 15:15, 15:30 or 15:45.
|
|
|
|
|
2020-09-04 18:02:31 +00:00
|
|
|
Assuming inf_tf = '1d' - then the resulting columns will be:
|
|
|
|
date_1d, open_1d, high_1d, low_1d, close_1d, rsi_1d
|
|
|
|
|
2020-09-04 17:44:35 +00:00
|
|
|
:param dataframe: Original dataframe
|
|
|
|
:param informative: Informative pair, most likely loaded via dp.get_pair_dataframe
|
2020-09-04 18:09:02 +00:00
|
|
|
:param timeframe: Timeframe of the original pair sample.
|
2020-09-04 17:44:35 +00:00
|
|
|
:param timeframe_inf: Timeframe of the informative pair sample.
|
|
|
|
:param ffill: Forwardfill missing values - optional but usually required
|
2021-07-17 16:19:49 +00:00
|
|
|
:param append_timeframe: Rename columns by appending timeframe.
|
|
|
|
:param date_column: A custom date column name.
|
2021-01-04 12:47:16 +00:00
|
|
|
:return: Merged dataframe
|
|
|
|
:raise: ValueError if the secondary timeframe is shorter than the dataframe timeframe
|
2020-09-04 17:44:35 +00:00
|
|
|
"""
|
|
|
|
|
2020-09-04 18:09:02 +00:00
|
|
|
minutes_inf = timeframe_to_minutes(timeframe_inf)
|
2020-09-07 07:06:43 +00:00
|
|
|
minutes = timeframe_to_minutes(timeframe)
|
2021-01-04 12:47:16 +00:00
|
|
|
if minutes == minutes_inf:
|
2020-09-04 18:09:02 +00:00
|
|
|
# No need to forwardshift if the timeframes are identical
|
2021-07-17 16:19:49 +00:00
|
|
|
informative['date_merge'] = informative[date_column]
|
2021-01-04 12:47:16 +00:00
|
|
|
elif minutes < minutes_inf:
|
|
|
|
# Subtract "small" timeframe so merging is not delayed by 1 small candle
|
|
|
|
# Detailed explanation in https://github.com/freqtrade/freqtrade/issues/4073
|
|
|
|
informative['date_merge'] = (
|
2021-07-17 16:19:49 +00:00
|
|
|
informative[date_column] + pd.to_timedelta(minutes_inf, 'm') -
|
|
|
|
pd.to_timedelta(minutes, 'm')
|
2021-08-06 22:19:36 +00:00
|
|
|
)
|
2020-09-04 18:09:02 +00:00
|
|
|
else:
|
2021-01-04 12:47:16 +00:00
|
|
|
raise ValueError("Tried to merge a faster timeframe to a slower timeframe."
|
|
|
|
"This would create new rows, and can throw off your regular indicators.")
|
2020-09-04 17:44:35 +00:00
|
|
|
|
2020-09-04 18:10:43 +00:00
|
|
|
# Rename columns to be unique
|
2021-07-17 16:19:49 +00:00
|
|
|
date_merge = 'date_merge'
|
|
|
|
if append_timeframe:
|
|
|
|
date_merge = f'date_merge_{timeframe_inf}'
|
|
|
|
informative.columns = [f"{col}_{timeframe_inf}" for col in informative.columns]
|
2020-09-04 17:44:35 +00:00
|
|
|
|
|
|
|
# Combine the 2 dataframes
|
|
|
|
# all indicators on the informative sample MUST be calculated before this point
|
|
|
|
dataframe = pd.merge(dataframe, informative, left_on='date',
|
2021-07-17 16:19:49 +00:00
|
|
|
right_on=date_merge, how='left')
|
|
|
|
dataframe = dataframe.drop(date_merge, axis=1)
|
2020-09-04 17:44:35 +00:00
|
|
|
|
|
|
|
if ffill:
|
|
|
|
dataframe = dataframe.ffill()
|
|
|
|
|
|
|
|
return dataframe
|
2021-03-17 08:58:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def stoploss_from_open(open_relative_stop: float, current_profit: float) -> float:
|
|
|
|
"""
|
|
|
|
|
|
|
|
Given the current profit, and a desired stop loss value relative to the open price,
|
|
|
|
return a stop loss value that is relative to the current price, and which can be
|
|
|
|
returned from `custom_stoploss`.
|
|
|
|
|
2021-03-17 13:44:10 +00:00
|
|
|
The requested stop can be positive for a stop above the open price, or negative for
|
|
|
|
a stop below the open price. The return value is always >= 0.
|
|
|
|
|
|
|
|
Returns 0 if the resulting stop price would be above the current price.
|
|
|
|
|
|
|
|
:param open_relative_stop: Desired stop loss percentage relative to open price
|
2021-03-17 08:58:23 +00:00
|
|
|
:param current_profit: The current profit percentage
|
2021-03-17 13:44:10 +00:00
|
|
|
:return: Positive stop loss value relative to current price
|
2021-03-17 08:58:23 +00:00
|
|
|
"""
|
|
|
|
|
2021-03-17 13:44:10 +00:00
|
|
|
# formula is undefined for current_profit -1, return maximum value
|
2021-03-17 08:58:23 +00:00
|
|
|
if current_profit == -1:
|
|
|
|
return 1
|
|
|
|
|
2021-03-17 13:44:10 +00:00
|
|
|
stoploss = 1-((1+open_relative_stop)/(1+current_profit))
|
|
|
|
|
|
|
|
# negative stoploss values indicate the requested stop price is higher than the current price
|
|
|
|
return max(stoploss, 0.0)
|
2021-07-17 15:09:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def stoploss_from_absolute(stop_rate: float, current_rate: float) -> float:
|
|
|
|
"""
|
|
|
|
Given current price and desired stop price, return a stop loss value that is relative to current
|
|
|
|
price.
|
|
|
|
:param stop_rate: Stop loss price.
|
|
|
|
:param current_rate: Current asset price.
|
|
|
|
:return: Positive stop loss value relative to current price
|
|
|
|
"""
|
|
|
|
return 1 - (stop_rate / current_rate)
|