股票的移动平均线交叉

时间:2021-04-23 08:08:55

标签: 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.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”

        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' ] )
        )

我的最终目标是拥有一个快速移动平均线,假设 21 天移动平均线向上穿越 50 天移动平均线将表示买入,反之亦然,21 天移动平均线向下穿越 50 天移动平均线将表示卖出< /p>

编辑::

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

0 个答案:

没有答案