Import pandas and handle NaN values in Supertrend

Added pandas import and implemented fillna to handle NaN values in the Supertrend strategy.
This commit is contained in:
Kagari
2026-01-12 23:07:28 +08:00
committed by GitHub
parent c07aa68697
commit fc4894e5a5
+7 -3
View File
@@ -18,6 +18,7 @@ from freqtrade.strategy import IStrategy, IntParameter
from pandas import DataFrame from pandas import DataFrame
import talib.abstract as ta import talib.abstract as ta
import numpy as np import numpy as np
import pandas as pd
class Supertrend(IStrategy): class Supertrend(IStrategy):
# Buy params, Sell params, ROI, Stoploss and Trailing Stop are values generated by 'freqtrade hyperopt --strategy Supertrend --hyperopt-loss ShortTradeDurHyperOptLoss --timerange=20210101- --timeframe=1h --spaces all' # Buy params, Sell params, ROI, Stoploss and Trailing Stop are values generated by 'freqtrade hyperopt --strategy Supertrend --hyperopt-loss ShortTradeDurHyperOptLoss --timerange=20210101- --timeframe=1h --spaces all'
@@ -171,8 +172,6 @@ class Supertrend(IStrategy):
# 3. final upper / lower bands # 3. final upper / lower bands
final_ub = np.zeros(length) final_ub = np.zeros(length)
final_lb = np.zeros(length) final_lb = np.zeros(length)
final_ub[:period] = basic_ub[:period]
final_lb[:period] = basic_lb[:period]
for i in range(period, length): for i in range(period, length):
final_ub[i] = basic_ub[i] if basic_ub[i] < final_ub[i-1] or close[i-1] > final_ub[i-1] else final_ub[i-1] final_ub[i] = basic_ub[i] if basic_ub[i] < final_ub[i-1] or close[i-1] > final_ub[i-1] else final_ub[i-1]
@@ -189,4 +188,9 @@ class Supertrend(IStrategy):
# 5. STX direction # 5. STX direction
stx = np.where(st > 0, np.where(close < st, 'down', 'up'), None) stx = np.where(st > 0, np.where(close < st, 'down', 'up'), None)
return pd.DataFrame({'ST': st, 'STX': stx}, index=df.index) # 6. fillna
result = pd.DataFrame({'ST': st, 'STX': stx}, index=df.index)
result.fillna(0, inplace=True)
return result