如何将数据添加到列表中

问题描述 投票:0回答:1

我试图通过使用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()





python list dataframe append
1个回答
0
投票

在运行代码时,我观察到df['macdh'].iloc[-1]df['macdh'].iloc[-2]的值在所有情况下都为负,因此if df['macdh'].iloc[-1]>0 and df['macdh'].iloc[-2]<0的条件从不满足,但是用于追加列表的代码是正确的,并且在条件得到满足。

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.