Python 股票的移动平均线交叉

Python 股票的移动平均线交叉,python,numpy,math,stock,ta-lib,Python,Numpy,Math,Stock,Ta Lib,我用python编写了这段代码,我正试图修改它,但一直收到一条错误消息 File "C:\ProgramData\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\ProgramData\Anaconda3\lib\threading.py", line 1254, in run self.function(*self.arg

我用python编写了这段代码,我正试图修改它,但一直收到一条错误消息

  File "C:\ProgramData\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 1254, in run
    self.function(*self.args, **self.kwargs)
  File "C:\Users\1071396\Downloads\rh-trading-bot_MA_Cross-master\bot.py", line 381, in run
    getattr( self.signal, 'sell_' + str(  config[ 'trade_strategies' ][ 'sell' ] ) )( a_asset, self.data ) or
  File "C:\Users\1071396\Downloads\rh-trading-bot_MA_Cross-master\signals.py", line 78, in sell_sma_crossover_rsi
    data.iloc[ -4 ][ asset.ticker + '_SMA_F' ] > data.iloc[ -4 ][ asset.ticker + '_SMA_S' ][ 'sell' ]  and
IndexError: invalid index to scalar variable.
下面是脚本“signals.py”的样子

从配置导入配置
从数学导入isnan
#信号按字母顺序定义
等级信号:
def购买sma交叉rsi(自身、股票代码、数据):
#带RSI滤波器的移动平均交叉
#学分:https://trader.autochartist.com/moving-average-crossover-with-rsi-filter/
#当快速SMA从下方穿过慢速SMA时买入,并在3个连续读数中保持在上方,RSI>买入阈值(建议50)
报税表(
#确保数据有效
不是isnan(data.iloc[-1][ticker+''u SMA_F'])和
不是isnan(data.iloc[-2][ticker+''u SMA_F'])和
不是isnan(data.iloc[-3][ticker+''u SMA_F'])和
不是isnan(data.iloc[-4][ticker+''u SMA_F'])和
不是isnan(data.iloc[-1][ticker+''u SMA'u S'])和
不是isnan(data.iloc[-2][ticker+''u SMA'u S'])和
不是isnan(data.iloc[-3][ticker+''u SMA'u S'])和
不是isnan(data.iloc[-4][ticker+''u SMA'u S'])和
#不是isnan(data.iloc[-1][ticker+'_RSI'])和
#快速SMA穿过慢速SMA并保持在上方
data.iloc[-1][ticker+''\u SMA\u F']>=data.iloc[-1][ticker+'\u SMA\u S']和
data.iloc[-2][ticker+'\u SMA\u F']>=data.iloc[-2][ticker+'\u SMA\u S']和
data.iloc[-3][ticker+'.\u SMA\u F']>=data.iloc[-3][ticker+'.\u SMA\u S']和
data.iloc[-4][ticker+'_SMA_F']=data.iloc[-2][ticker+''u SMA_F']-data.iloc[-2][ticker+''u SMA_S']['buy']
#RSI高于阈值
#data.iloc[-1][ticker+'\u RSI']>config['RSI\u threshold']['buy']
#)
def购买sma rsi阈值(自身、股票代码、数据):
#简单快速SMA和RSI
#当价格低于快速SMA和RSI低于阈值时买入
返回(
不是isnan(data.iloc[-1][ticker+''u SMA_F'])和
不是isnan(data.iloc[-1][ticker+'_RSI'])和
#当前价格是否低于配置文件中定义的Fast SMA百分比?

data.iloc[-1][ticker]嘿,你能在github上分享你的python笔记本吗?我需要你的代码。

忘记发布调用这些函数的脚本了。
        from config import config
        from math import isnan

# Signals are defined in alphabetical order
class signals:
    def buy_sma_crossover_rsi( self, ticker, data ):
        # Moving Average Crossover with RSI Filter
        # Credits: https://trader.autochartist.com/moving-average-crossover-with-rsi-filter/
        # Buy when Fast-SMA crosses Slow-SMA from below, and stays above for 3 consecutive readings, and RSI > buy threshold (50 suggested)

        return(        
            # Make sure the data is valid
            not isnan( data.iloc[ -1 ][ ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -2 ][ ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -3 ][ ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -4 ][ ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -1 ][ ticker + '_SMA_S' ] ) and
            not isnan( data.iloc[ -2 ][ ticker + '_SMA_S' ] ) and
            not isnan( data.iloc[ -3 ][ ticker + '_SMA_S' ] ) and
            not isnan( data.iloc[ -4 ][ ticker + '_SMA_S' ] ) and
            #not isnan( data.iloc[ -1 ][ ticker + '_RSI' ] ) and

            # Fast-SMA crossed Slow-SMA and stays above
            data.iloc[ -1 ][ ticker + '_SMA_F' ] >= data.iloc[ -1 ][ ticker + '_SMA_S' ]  and
            data.iloc[ -2 ][ ticker + '_SMA_F' ] >= data.iloc[ -2 ][ ticker + '_SMA_S' ]  and
            data.iloc[ -3 ][ ticker + '_SMA_F' ] >= data.iloc[ -3 ][ ticker + '_SMA_S' ]  and
            data.iloc[ -4 ][ ticker + '_SMA_F' ] < data.iloc[ -4 ][ ticker + '_SMA_S' ][ 'buy' ] #  and
            )
            # ... and they diverge
            #data.iloc[ -1 ][ ticker + '_SMA_F' ] - data.iloc[ -1 ][ ticker + '_SMA_S' ] >= data.iloc[ -2 ][ ticker + '_SMA_F' ] - data.iloc[ -2 ][ ticker + '_SMA_S' ][ 'buy' ] # and
            
            # RSI above threshold
            #data.iloc[ -1 ][ ticker + '_RSI' ] > config[ 'rsi_threshold' ][ 'buy' ]
        #)

    def buy_sma_rsi_threshold( self, ticker, data ):
        # Simple Fast-SMA and RSI 
        # Buy when price is below Fast-SMA and RSI is below threshold
        
        return (
            not isnan( data.iloc[ -1 ][ ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -1 ][ ticker + '_RSI' ] ) and

            # Is the current price below the Fast-SMA by the percentage defined in the config file?
            data.iloc[ -1 ][ ticker ] <= data.iloc[ -1 ][ ticker + '_SMA_F' ] - ( data.iloc[ -1 ][ ticker + '_SMA_F' ] * config[ 'buy_below_moving_average' ] ) and

            # RSI below the threshold
            data.iloc[ -1 ][ ticker + '_RSI' ] <= config[ 'rsi_threshold' ][ 'buy' ]
        )

    def sell_above_buy( self, asset, data ):
        # Simple percentage
        
        return (
            data.iloc[ -1 ][ asset.ticker ] > asset.price + ( asset.price * config[ 'profit_percentage' ] )
        )

    def sell_sma_crossover_rsi( self, asset, data ):
        # Moving Average Crossover with RSI Filter
        # Credits: https://trader.autochartist.com/moving-average-crossover-with-rsi-filter/

        return(        
            # Make sure the data is valid
            not isnan( data.iloc[ -1 ][ asset.ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -2 ][ asset.ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -3 ][ asset.ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -3 ][ asset.ticker + '_SMA_F' ] ) and
            not isnan( data.iloc[ -1 ][ asset.ticker + '_SMA_S' ] ) and
            not isnan( data.iloc[ -2 ][ asset.ticker + '_SMA_S' ] ) and
            not isnan( data.iloc[ -3 ][ asset.ticker + '_SMA_S' ] ) and
            not isnan( data.iloc[ -3 ][ asset.ticker + '_SMA_S' ] ) and
            #not isnan( data.iloc[ -1 ][ asset.ticker + '_RSI' ] ) and

            # Fast-SMA crossed Slow-SMA and stays above
            data.iloc[ -1 ][ asset.ticker + '_SMA_F' ] <= data.iloc[ -1 ][ asset.ticker + '_SMA_S' ]  and
            data.iloc[ -2 ][ asset.ticker + '_SMA_F' ] <= data.iloc[ -2 ][ asset.ticker + '_SMA_S' ]  and
            data.iloc[ -3 ][ asset.ticker + '_SMA_F' ] <= data.iloc[ -3 ][ asset.ticker + '_SMA_S' ]  and
            data.iloc[ -4 ][ asset.ticker + '_SMA_F' ] > data.iloc[ -4 ][ asset.ticker + '_SMA_S' ][ 'sell' ]  and
            
            # ... and they diverge
            #data.iloc[ -1 ][ ticker + '_SMA_S' ] - data.iloc[ -1 ][ ticker + '_SMA_F' ] >= data.iloc[ -2 ][ ticker + '_SMA_S' ] - data.iloc[ -2 ][ ticker + '_SMA_F' ] and
            
            # RSI below threshold
            #data.iloc[ -1 ][ ticker + '_RSI' ] <= config[ 'rsi_threshold' ][ 'sell' ] and

            # Price is greater than purchase price by at least profit percentage
            data.iloc[ -1 ][ asset.ticker ] >= asset.price + (  asset.price * config[ 'profit_percentage' ] )
        )
def buy( self, ticker ):
        if ( self.available_cash < config[ 'buy_amount_per_trade' ] or self.is_trading_locked ):
            return False
        
        # Values need to be specified to no more precision than listed in min_price_increments.
        # Truncate to 7 decimal places to avoid floating point problems way out at the precision limit
        price = round( floor( self.data.iloc[ -1 ][ ticker ] / self.min_price_increments[ ticker ] ) * self.min_price_increments[ ticker ], 7 )
        
        # How much to buy depends on the configuration
        quantity = ( self.available_cash if ( config[ 'buy_amount_per_trade' ] == 0 ) else config[ 'buy_amount_per_trade' ] ) / price
        quantity = round( floor( quantity / self.min_share_increments[ ticker ] ) * self.min_share_increments[ ticker ], 7 )

        print( 'Buying ' + str( ticker ) + ' ' + str( quantity ) + ' at $' + str( price ) )

        if ( config[ 'trades_enabled' ] and not config[ 'debug_enabled' ] ):
            try:
                buy_info = rh.order_buy_crypto_limit( str( ticker ), quantity, price )

                # Add this new asset to our orders
                self.orders[ buy_info[ 'id' ] ] = asset( ticker, quantity, price, buy_info[ 'id' ] )
            except:
                print( 'Got exception trying to buy, aborting.' )
                return False

        return True

    def sell( self, asset ):
        # Do we have enough of this asset to sell?
        if ( asset.quantity <= 0.0 or self.is_trading_locked ):
            return False
           
        # Values needs to be specified to no more precision than listed in min_price_increments. 
        # Truncate to 7 decimal places to avoid floating point problems way out at the precision limit
        price = round( floor( self.data.iloc[ -1 ][ asset.ticker ] / self.min_price_increments[ asset.ticker ] ) * self.min_price_increments[ asset.ticker ], 7 )
        profit = round( ( asset.quantity * price ) - ( asset.quantity * asset.price ), 3 )

        print( 'Selling ' + str( asset.ticker ) + ' ' + str( asset.quantity ) + ' for $' + str( price ) + ' (profit: $' + str( profit ) + ')' )

        if ( config[ 'trades_enabled' ] and not config[ 'debug_enabled' ] ):
            try:
                sell_info = rh.order_sell_crypto_limit( str( asset.ticker ), asset.quantity, price )

                # Mark this asset as sold, the garbage collector (see 'run' method) will remove it from our orders at the next iteration
                self.orders[ asset.order_id ].quantity = 0
            except:
                print( 'Got exception trying to sell, aborting.' )
                return False

        return True