我试图通过使用stockstats macdh值来获取购买信号。我可以获取macdh值,当我打印它们时,我可以看到值。但是当我使用if的状态表示购买信号时,我的购买信号列表为空。 macdh值类型可能有问题。如何填写购买信号清单?
import pandas as pd
import requests
import json
from stockstats import StockDataFrame as Sdf
class TradingModel:
def __init__(self, symbol):
self.symbol = symbol
self.df = self.getData
@property
def getData(self):
base = 'https://api.binance.com'
endpoint = '/api/v3/klines'
params = '?&symbol='+self.symbol+'&interval=4h'
url = base + endpoint + params
Data = requests.get(url)
dictionary = Data.json()
df = pd.DataFrame.from_dict(dictionary)
df = df.drop(range(6, 12), axis=1)
# rename columns and stockstasts
col_names = ['time', 'open', 'high', 'low', 'close', 'volume']
df.columns = col_names
stock = Sdf.retype(df)
for col in col_names:
df[col]=df[col].astype(float)
df['macdh']=stock['macdh']
return (df)
def strategy(self):
df = self.df
buy_signals=[]
for i in range(1, len(df['close'])):
if df['macdh'].iloc[-1]>0 and df['macdh'].iloc[-2]<0:
buy_signals.append([df['time'][i], df['low'][i]])
print(buy_signals)
def Main():
symbol = "BTCUSDT"
model = TradingModel(symbol)
model.strategy()
if __name__ == '__main__':
Main()
在运行代码时,我观察到df['macdh'].iloc[-1]
和df['macdh'].iloc[-2]
的值在所有情况下都为负,因此if df['macdh'].iloc[-1]>0 and df['macdh'].iloc[-2]<0
的条件从不满足,但是用于追加列表的代码是正确的,并且在条件得到满足。
希望有帮助!