首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >我的python交易机器人计算MACD错误吗?

我的python交易机器人计算MACD错误吗?
EN

Stack Overflow用户
提问于 2020-12-12 13:49:36
回答 1查看 325关注 0票数 0

我的python交易机器人计算出MACD在开始时非常高,然后开始逐渐下降,无论什么股票或股票实际上是如何移动的。

Here is the matplotlib chart of the MACD and Signal Line (All stocks/crytocurrency's plots look very similar)

我尝试调整span值,但无济于事。这发生在我使用的任何股票/加密货币上。此代码基于计算机科学的MACD交易教程,但适用于实时交易,而不是为过去的数据和日志等内容生成图表。(另外,我还是个编程新手,所以请试着用简单的术语解释一下是怎么回事)这是代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    def algorithm(self, df):
    '''
    Generates MACD

    '''

    # Set the date(first column) to be the index of the dataframe
    df = df.set_index(df[self.time_stamp_index])
    # print(df.head())

    # Visually show the stock price
    if self.do_plotting == True:
        # print('Plotting Data')
        plt1 = px.line(df, x=self.time_stamp_index,
                       y=self.close_price_index)

    # Calculate the MACD and signal line indicators

    # Calculate the Short term EWM(for 10 minutes)
    ShortEMA = df[self.close_price_index].ewm(
        span=self.short_span, adjust=False).mean()

    # Calculate the long term exponential moving avg (1 hour)
    LongEMA = df[self.close_price_index].ewm(
        span=self.long_span, adjust=False).mean()

    # Calculate the MACD line
    MACD = ShortEMA - LongEMA

    # Calculate signal line
    signal = MACD.ewm(span=self.signal_span, adjust=False).mean()

    # Add signal line and MACD line to dataframe
    df['MACD'] = MACD
    df['Signal Line'] = signal

    # Plot the chart with the lines
    if self.do_plotting == True:
        plt2 = px.line(df, x=self.time_stamp_index, y=[
            'MACD', 'Signal Line', self.close_price_index],
            title=f'{self.symbol} STOCK DATA')
        plt2.show()

    # DEBUG
    if df['MACD'][len(df) - 1] > df['Signal Line'][len(df) - 1]:
        print('[DEBUG]: BUY SIGNAL')

    elif df['MACD'][len(df) - 1] < df['Signal Line'][len(df) - 1]:
        print('[DEBUG]: SELL SIGNAL')

    # send vals to self. so that it can be accessed from the log function.
    self.macd = df['MACD'][len(df) - 1]
    self.signal_line = df['Signal Line'][len(df) - 1]

    Buy = []
    Sell = []

    current_hold_flag = self.symbol in current_stocks

    for i in range(len(df)):
        if df['MACD'][i] > df['Signal Line'][i]:
            Sell.append(None)
            if current_hold_flag == False:
                Buy.append(df[self.close_price_index][i])
                self.flag = 1
            else:
                Buy.append(None)
        elif df['MACD'][i] < df['Signal Line'][i]:
            Buy.append(None)
            if current_hold_flag == True:
                Sell.append(df[self.close_price_index][i])
                self.flag = 0
            else:
                Sell.append(None)
        else:
            Buy.append(None)
            Sell.append(None)

    buy_sell = (Buy, Sell)

    df['Buy_Signal_Price'] = buy_sell[0]
    df['Sell Signal_Price'] = buy_sell[1] 

    if Buy[-1] != None:                             # If last trade was a buy
        self.buy_or_sell = 'buy'
        self.buy(Buy[-1])
    elif Sell[-1] != None:                          # If last trade was a sell
        self.buy_or_sell = 'sell'
        self.sell(Sell[-1])
    elif Buy[-1] == None and Sell[-1] == None:      # If no trade happened
        self.buy_or_sell = 'no trade' 
        print(f'{self.log_time}Did not trade')
        if len(current_stocks) > 0:
            print(
                f'{self.log_time}Currently holding: {",".join(current_stocks)}')
        else:

            print(f'{self.log_time}Not holding any stocks currently')
    else:
        print('ERROR')
        r = input()
        exit()
EN

回答 1

Stack Overflow用户

发布于 2021-11-23 18:49:42

如果您试图从交易密码赚钱,您可以有兴趣使用新的货币上市信息ang=nd添加新的对在最大的交易所,如果使用得当,您可以赚取约20%,每天。要获取有关加密交易所的新列表的信息,您可以使用此API:

https://rapidapi.com/Diver44/api/new-cryptocurrencies-listings/

它包括一个新上市的端点,来自最大交易所的新对,以及一个非常有用的端点,其中包含有关交易所的信息,您可以在该交易所购买特定硬币和该硬币的价格。这是有点付费的,但这是值得的!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65265653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文