Backtesting error: StrategyEMARegularOrder

Hi,

I was doing backtesting for EMA regular Order strategy but encountered the below error. Can you please have a look at and suggest solution?

Code Snippet:

from pyalgotrading.strategy.strategy_base import StrategyBase
from pyalgotrading.constants import *

class StrategyEMARegularOrder(StrategyBase):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.timeperiod1 = self.strategy_parameters['timeperiod1']
        self.timeperiod2 = self.strategy_parameters['timeperiod2']

        self.main_order = None

    def initialize(self):
        self.main_order = {}

    @staticmethod
    def name():
        return 'EMA Regular Order Strategy'

    @staticmethod
    def versions_supported():
        return AlgoBullsEngineVersion.VERSION_3_2_0

    def get_crossover_value(self, instrument):
        hist_data = self.get_historical_data(instrument)
        ema_x = talib.EMA(hist_data['close'], timeperiod=self.timeperiod1)
        ema_y = talib.EMA(hist_data['close'], timeperiod=self.timeperiod2)
        crossover_value = self.utils.crossover(ema_x, ema_y)
        return crossover_value

    def strategy_select_instruments_for_entry(self, candle, instruments_bucket):

        selected_instruments_bucket = []
        sideband_info_bucket = []

        for instrument in instruments_bucket:
            crossover_value = self.get_crossover_value(instrument)
            if crossover_value == 1:
                selected_instruments_bucket.append(instrument)
                sideband_info_bucket.append({'action': 'BUY'})
            elif crossover_value == -1:
                if self.strategy_mode is StrategyMode.INTRADAY:
                    selected_instruments_bucket.append(instrument)
                    sideband_info_bucket.append({'action': 'SELL'})

        return selected_instruments_bucket, sideband_info_bucket

    def strategy_enter_position(self, candle, instrument, sideband_info):
        if sideband_info['action'] == 'BUY':
            qty = self.number_of_lots * instrument.lot_size
            self.main_order[instrument] = \
                self.broker.BuyOrderRegular(instrument=instrument,
                                            order_code=BrokerOrderCodeConstants.INTRADAY,
                                            order_variety=BrokerOrderVarietyConstants.MARKET,
                                            quantity=qty)
        elif sideband_info['action'] == 'SELL':
            qty = self.number_of_lots * instrument.lot_size
            self.main_order[instrument] = \
                self.broker.SellOrderRegular(instrument=instrument,
                                             order_code=BrokerOrderCodeConstants.INTRADAY,
                                             order_variety=BrokerOrderVarietyConstants.MARKET,
                                             quantity=qty)
        else:
            raise SystemExit(f'Got invalid sideband_info value: {sideband_info}')

        return self.main_order[instrument]

    def strategy_select_instruments_for_exit(self, candle, instruments_bucket):
        selected_instruments_bucket = []
        sideband_info_bucket = []

        for instrument in instruments_bucket:
            if self.main_order.get(instrument) is not None:
                crossover_value = self.get_crossover_value(instrument)
                if crossover_value in [1, -1]:
                    selected_instruments_bucket.append(instrument)
                    sideband_info_bucket.append({'action': 'EXIT'})
        return selected_instruments_bucket, sideband_info_bucket

    def strategy_exit_position(self, candle, instrument, sideband_info):
        if sideband_info['action'] == 'EXIT':
            self.main_order[instrument].exit_position()
            self.main_order[instrument] = None
            return True

        return False

Backtesting Error Log:

[2020-09-21 21:22:36] Performing sanity checks on cfg strategy_parameters, setting up broker connection and required data structures...
[2020-09-21 21:22:38] ABBroker connection has been setup successfully.
[2020-09-21 21:22:38] Sanity checks on cfg successful.
[2020-09-21 21:22:38] Setting up broker connection...
[2020-09-21 21:22:38] Broker connection has been setup successfully.
[2020-09-21 21:22:38] (NSE_EQ) Funds available in client's ABVirtualBroker account is : Rs. '1000000000.00'
[2020-09-21 21:22:38] 
########################################
 INITIALIZING ALGOBULLS CORE (v3.2.0)... 
########################################
[2020-09-21 21:22:38] Welcome ALGOBULLS VIRTUAL USER!
[2020-09-21 21:22:38] Reading strategy...
[2020-09-21 21:22:40] Entering Backtesting mode. Henceforth, all timestamps will be Backtesting timestamps...
[BT] [2020-08-03 09:30:00] [INFO] [tls] STARTING ALGOBULLS CORE...
[BT] [2020-08-03 09:30:00] [INFO] [tls] 
            
  #####  #######    #    ######  ####### ### #     #  #####     
 #     #    #      # #   #     #    #     #  ##    # #     #    
 #          #     #   #  #     #    #     #  # #   # #          
  #####     #    #     # ######     #     #  #  #  # #  ####    
       #    #    ####### #   #      #     #  #   # # #     #    
 #     #    #    #     # #    #     #     #  #    ## #     #    
  #####     #    #     # #     #    #    ### #     #  #####     

    #    #        #####  ####### ######  #     # #       #        #####    
   # #   #       #     # #     # #     # #     # #       #       #     #   
  #   #  #       #       #     # #     # #     # #       #       #         
 #     # #       #  #### #     # ######  #     # #       #        #####    
 ####### #       #     # #     # #     # #     # #       #             #   
 #     # #       #     # #     # #     # #     # #       #       #     #   
 #     # #######  #####  ####### ######   #####  ####### #######  #####    

  #####  ####### ######  #######
 #     # #     # #     # #
 #       #     # #     # #
 #       #     # ######  #####
 #       #     # #   #   #
 #     # #     # #    #  #
  #####  ####### #     # #######
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------Master Cfg Parameters:--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] User: ALGOBULLS VIRTUAL USER
[BT] [2020-08-03 09:30:00] [INFO] [tls] Broker: ABVIRTUALBROKER
[BT] [2020-08-03 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_POSITIONS: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_HOLDINGS_TPLUS1: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_HOLDINGS_TPLUS2: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] RESUME_POSITIONS_ON_START: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] RESUME_HOLDINGS_TPLUS1_ON_START: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] RESUME_HOLDINGS_TPLUS2_ON_START: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] EXIT_INTRADAY_ORDERS_ON_STOP: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] EXIT_TPLUS1_DELIVERY_ORDERS_ON_STOP: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] EXIT_TPLUS2_DELIVERY_ORDERS_ON_STOP: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: System Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Trading type: BACKTESTING
[BT] [2020-08-03 09:30:00] [INFO] [tls] User trading start time: 2020-08-03 09:30:00
[BT] [2020-08-03 09:30:00] [INFO] [tls] User trading end time: 2020-09-21 15:15:00
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Funds Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Credit Exposure: 1
[BT] [2020-08-03 09:30:00] [INFO] [tls] Fund Allocator: FundAllocatorLots
[BT] [2020-08-03 09:30:00] [INFO] [tls] Fund Allocator Parameters: {'NUMBER_OF_LOTS': 1}
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Candle Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Candle interval (sec): 900
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Strategy Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Strategy: EMA Regular Order Strategy
[BT] [2020-08-03 09:30:00] [INFO] [tls] Parameters: {'timeperiod1': 4.0, 'timeperiod2': 9.0}
[BT] [2020-08-03 09:30:00] [INFO] [tls] Strategy Mode: INTRADAY
[BT] [2020-08-03 09:30:00] [INFO] [tls] Instruments Bucket: instruments_bucket | [NSE_EQ:SBIN]
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Risk Management Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Target Trigger: 0.1000
[BT] [2020-08-03 09:30:00] [INFO] [tls] Stoploss Trigger: 0.1000
[BT] [2020-08-03 09:30:00] [INFO] [tls] Trailing Stoploss Trigger: 0.1000
[BT] [2020-08-03 09:30:00] [INFO] [tls] Desired profit: 1000000.0
[BT] [2020-08-03 09:30:00] [INFO] [tls] Risk Appetite: 10000.0
[BT] [2020-08-03 09:30:00] [INFO] [tls] PNL scan frequency (in sec): 30
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Other Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Allow multiple orders for same script: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] Allow new order when order for script already running: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] Instrument max orders count: 100
[BT] [2020-08-03 09:30:00] [INFO] [tls] Remove Instrument if Order Rejected: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] ------------------------------------------------------------
[BT] [2020-08-03 09:30:00] [INFO] [utils] Instrument (NSE_EQ:SBIN) subscribed to historical data successfully
[BT] [2020-08-03 09:30:00] [INFO] [utils] 1 Instrument(s) (re)subscribed to historical data successfully
[BT] [2020-08-03 09:30:00] [INFO] [utils] Prefetching historical data for 1 instruments...
[BT] [2020-08-03 09:30:00] [INFO] [tls] [User: ALGOBULLS VIRTUAL USER] Trading session completed
[BT] [2020-08-03 09:30:00] [INFO] [oms] 

PENDING ORDERS:


COMPLETED ORDERS:


POSITIONS:


HOLDINGS:
[BT] [2020-08-03 09:30:00] [CRITICAL] [tls] Dumping Debug Stack to Python friendly Traceback Format for Python Build Customers: Uncaught exception | Exception Class: <class 'TypeError'> | Exception Details: 'IndexError' object is not subscriptable
[BT] [2020-08-03 09:30:00] [CRITICAL] [tls] An error has occurred due to which the strategy cannot proceed anymore. Please double check the tweak parameters. If everything seems fine, please contact support@algobulls.com for support.
[2020-09-21 21:23:37] Performing sanity checks on cfg strategy_parameters, setting up broker connection and required data structures...
[2020-09-21 21:23:38] ABBroker connection has been setup successfully.
[2020-09-21 21:23:38] Sanity checks on cfg successful.
[2020-09-21 21:23:38] Setting up broker connection...
[2020-09-21 21:23:39] Broker connection has been setup successfully.
[2020-09-21 21:23:39] (NSE_EQ) Funds available in client's ABVirtualBroker account is : Rs. '1000000000.00'
[2020-09-21 21:23:39] 
########################################
 INITIALIZING ALGOBULLS CORE (v3.2.0)... 
########################################
[2020-09-21 21:23:39] Welcome ALGOBULLS VIRTUAL USER!
[2020-09-21 21:23:39] Reading strategy...
[2020-09-21 21:23:40] Entering Backtesting mode. Henceforth, all timestamps will be Backtesting timestamps...
[BT] [2020-08-03 09:30:00] [INFO] [tls] STARTING ALGOBULLS CORE...
[BT] [2020-08-03 09:30:00] [INFO] [tls] 
            
  #####  #######    #    ######  ####### ### #     #  #####     
 #     #    #      # #   #     #    #     #  ##    # #     #    
 #          #     #   #  #     #    #     #  # #   # #          
  #####     #    #     # ######     #     #  #  #  # #  ####    
       #    #    ####### #   #      #     #  #   # # #     #    
 #     #    #    #     # #    #     #     #  #    ## #     #    
  #####     #    #     # #     #    #    ### #     #  #####     

    #    #        #####  ####### ######  #     # #       #        #####    
   # #   #       #     # #     # #     # #     # #       #       #     #   
  #   #  #       #       #     # #     # #     # #       #       #         
 #     # #       #  #### #     # ######  #     # #       #        #####    
 ####### #       #     # #     # #     # #     # #       #             #   
 #     # #       #     # #     # #     # #     # #       #       #     #   
 #     # #######  #####  ####### ######   #####  ####### #######  #####    

  #####  ####### ######  #######
 #     # #     # #     # #
 #       #     # #     # #
 #       #     # ######  #####
 #       #     # #   #   #
 #     # #     # #    #  #
  #####  ####### #     # #######
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------Master Cfg Parameters:--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] User: ALGOBULLS VIRTUAL USER
[BT] [2020-08-03 09:30:00] [INFO] [tls] Broker: ABVIRTUALBROKER
[BT] [2020-08-03 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_POSITIONS: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_HOLDINGS_TPLUS1: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_HOLDINGS_TPLUS2: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] RESUME_POSITIONS_ON_START: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] RESUME_HOLDINGS_TPLUS1_ON_START: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] RESUME_HOLDINGS_TPLUS2_ON_START: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] EXIT_INTRADAY_ORDERS_ON_STOP: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] EXIT_TPLUS1_DELIVERY_ORDERS_ON_STOP: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] EXIT_TPLUS2_DELIVERY_ORDERS_ON_STOP: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: System Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Trading type: BACKTESTING
[BT] [2020-08-03 09:30:00] [INFO] [tls] User trading start time: 2020-08-03 09:30:00
[BT] [2020-08-03 09:30:00] [INFO] [tls] User trading end time: 2020-09-21 15:15:00
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Funds Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Credit Exposure: 1
[BT] [2020-08-03 09:30:00] [INFO] [tls] Fund Allocator: FundAllocatorLots
[BT] [2020-08-03 09:30:00] [INFO] [tls] Fund Allocator Parameters: {'NUMBER_OF_LOTS': 1}
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Candle Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Candle interval (sec): 900
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Strategy Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Strategy: EMA Regular Order Strategy
[BT] [2020-08-03 09:30:00] [INFO] [tls] Parameters: {'timeperiod1': 4.0, 'timeperiod2': 9.0}
[BT] [2020-08-03 09:30:00] [INFO] [tls] Strategy Mode: INTRADAY
[BT] [2020-08-03 09:30:00] [INFO] [tls] Instruments Bucket: instruments_bucket | [NSE_EQ:SBIN]
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Risk Management Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Target Trigger: 0.1000
[BT] [2020-08-03 09:30:00] [INFO] [tls] Stoploss Trigger: 0.1000
[BT] [2020-08-03 09:30:00] [INFO] [tls] Trailing Stoploss Trigger: 0.1000
[BT] [2020-08-03 09:30:00] [INFO] [tls] Desired profit: 1000000.0
[BT] [2020-08-03 09:30:00] [INFO] [tls] Risk Appetite: 10000.0
[BT] [2020-08-03 09:30:00] [INFO] [tls] PNL scan frequency (in sec): 30
[BT] [2020-08-03 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Other Parameters--------------------
[BT] [2020-08-03 09:30:00] [INFO] [tls] Allow multiple orders for same script: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] Allow new order when order for script already running: True
[BT] [2020-08-03 09:30:00] [INFO] [tls] Instrument max orders count: 100
[BT] [2020-08-03 09:30:00] [INFO] [tls] Remove Instrument if Order Rejected: False
[BT] [2020-08-03 09:30:00] [INFO] [tls] ------------------------------------------------------------
[BT] [2020-08-03 09:30:00] [INFO] [utils] Instrument (NSE_EQ:SBIN) subscribed to historical data successfully
[BT] [2020-08-03 09:30:00] [INFO] [utils] 1 Instrument(s) (re)subscribed to historical data successfully
[BT] [2020-08-03 09:30:00] [INFO] [utils] Prefetching historical data for 1 instruments...
[BT] [2020-08-03 09:30:00] [INFO] [tls] [User: ALGOBULLS VIRTUAL USER] Trading session completed
[BT] [2020-08-03 09:30:00] [INFO] [oms] 

PENDING ORDERS:


COMPLETED ORDERS:


POSITIONS:


HOLDINGS:
[BT] [2020-08-03 09:30:00] [CRITICAL] [tls] Dumping Debug Stack to Python friendly Traceback Format for Python Build Customers: Uncaught exception | Exception Class: <class 'TypeError'> | Exception Details: 'IndexError' object is not subscriptable
[BT] [2020-08-03 09:30:00] [CRITICAL] [tls] An error has occurred due to which the strategy cannot proceed anymore. Please double check the tweak parameters. If everything seems fine, please contact support@algobulls.com for support.

Hi Ganesh,

The team is looking into this.

I will get back to you soon.

Thanks,

` aki

Hi Ganesh,

Kindly try running this strategy using a shorter BT range.

So instead of:

Try running from (example):
14-SEP to 18-SEP

Do let me know if it solved the problem. If it does, then I will provide the explanation of why this particular solution worked.

If this doesn’t work, kindly paste the new logs again.

Thanks,
` aki

By reducing timeframe, I got strategy breached code of conduct message in log.

[BT] [2020-09-14 09:45:00] [CRITICAL] [tls] Strategy breached code of conduct. Please adjust your strategy to follow guidelines and run again. Halting strategy...

Complete Log:

[2020-09-22 15:04:56] Performing sanity checks on cfg strategy_parameters, setting up broker connection and required data structures...
[2020-09-22 15:04:57] ABBroker connection has been setup successfully.
[2020-09-22 15:04:57] Sanity checks on cfg successful.
[2020-09-22 15:04:57] Setting up broker connection...
[2020-09-22 15:04:58] Broker connection has been setup successfully.
[2020-09-22 15:04:58] (NSE_EQ) Funds available in client's ABVirtualBroker account is : Rs. '1000000000.00'
[2020-09-22 15:04:58] 
########################################
 INITIALIZING ALGOBULLS CORE (v3.2.0)... 
########################################
[2020-09-22 15:04:58] Welcome ALGOBULLS VIRTUAL USER!
[2020-09-22 15:04:58] Reading strategy...
[2020-09-22 15:04:59] Entering Backtesting mode. Henceforth, all timestamps will be Backtesting timestamps...
[BT] [2020-09-14 09:30:00] [INFO] [tls] STARTING ALGOBULLS CORE...
[BT] [2020-09-14 09:30:00] [INFO] [tls] 
            
  #####  #######    #    ######  ####### ### #     #  #####     
 #     #    #      # #   #     #    #     #  ##    # #     #    
 #          #     #   #  #     #    #     #  # #   # #          
  #####     #    #     # ######     #     #  #  #  # #  ####    
       #    #    ####### #   #      #     #  #   # # #     #    
 #     #    #    #     # #    #     #     #  #    ## #     #    
  #####     #    #     # #     #    #    ### #     #  #####     

    #    #        #####  ####### ######  #     # #       #        #####    
   # #   #       #     # #     # #     # #     # #       #       #     #   
  #   #  #       #       #     # #     # #     # #       #       #         
 #     # #       #  #### #     # ######  #     # #       #        #####    
 ####### #       #     # #     # #     # #     # #       #             #   
 #     # #       #     # #     # #     # #     # #       #       #     #   
 #     # #######  #####  ####### ######   #####  ####### #######  #####    

  #####  ####### ######  #######
 #     # #     # #     # #
 #       #     # #     # #
 #       #     # ######  #####
 #       #     # #   #   #
 #     # #     # #    #  #
  #####  ####### #     # #######
[BT] [2020-09-14 09:30:00] [INFO] [tls] --------------------Master Cfg Parameters:--------------------
[BT] [2020-09-14 09:30:00] [INFO] [tls] User: ALGOBULLS VIRTUAL USER
[BT] [2020-09-14 09:30:00] [INFO] [tls] Broker: ABVIRTUALBROKER
[BT] [2020-09-14 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_POSITIONS: False
[BT] [2020-09-14 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_HOLDINGS_TPLUS1: False
[BT] [2020-09-14 09:30:00] [INFO] [tls] AUTO_ADD_INSTRUMENTS_BUCKET_FROM_HOLDINGS_TPLUS2: False
[BT] [2020-09-14 09:30:00] [INFO] [tls] RESUME_POSITIONS_ON_START: True
[BT] [2020-09-14 09:30:00] [INFO] [tls] RESUME_HOLDINGS_TPLUS1_ON_START: True
[BT] [2020-09-14 09:30:00] [INFO] [tls] RESUME_HOLDINGS_TPLUS2_ON_START: True
[BT] [2020-09-14 09:30:00] [INFO] [tls] EXIT_INTRADAY_ORDERS_ON_STOP: True
[BT] [2020-09-14 09:30:00] [INFO] [tls] EXIT_TPLUS1_DELIVERY_ORDERS_ON_STOP: False
[BT] [2020-09-14 09:30:00] [INFO] [tls] EXIT_TPLUS2_DELIVERY_ORDERS_ON_STOP: False
[BT] [2020-09-14 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: System Parameters--------------------
[BT] [2020-09-14 09:30:00] [INFO] [tls] Trading type: BACKTESTING
[BT] [2020-09-14 09:30:00] [INFO] [tls] User trading start time: 2020-09-14 09:30:00
[BT] [2020-09-14 09:30:00] [INFO] [tls] User trading end time: 2020-09-18 15:15:00
[BT] [2020-09-14 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Funds Parameters--------------------
[BT] [2020-09-14 09:30:00] [INFO] [tls] Credit Exposure: 1
[BT] [2020-09-14 09:30:00] [INFO] [tls] Fund Allocator: FundAllocatorLots
[BT] [2020-09-14 09:30:00] [INFO] [tls] Fund Allocator Parameters: {'NUMBER_OF_LOTS': 1}
[BT] [2020-09-14 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Candle Parameters--------------------
[BT] [2020-09-14 09:30:00] [INFO] [tls] Candle interval (sec): 900
[BT] [2020-09-14 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Strategy Parameters--------------------
[BT] [2020-09-14 09:30:00] [INFO] [tls] Strategy: EMA Regular Order Strategy
[BT] [2020-09-14 09:30:00] [INFO] [tls] Parameters: {'timeperiod1': 4.0, 'timeperiod2': 9.0}
[BT] [2020-09-14 09:30:00] [INFO] [tls] Strategy Mode: INTRADAY
[BT] [2020-09-14 09:30:00] [INFO] [tls] Instruments Bucket: instruments_bucket | [NSE_EQ:SBIN]
[BT] [2020-09-14 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Risk Management Parameters--------------------
[BT] [2020-09-14 09:30:00] [INFO] [tls] Target Trigger: 0.1000
[BT] [2020-09-14 09:30:00] [INFO] [tls] Stoploss Trigger: 0.1000
[BT] [2020-09-14 09:30:00] [INFO] [tls] Trailing Stoploss Trigger: 0.1000
[BT] [2020-09-14 09:30:00] [INFO] [tls] Desired profit: 1000000.0
[BT] [2020-09-14 09:30:00] [INFO] [tls] Risk Appetite: 10000.0
[BT] [2020-09-14 09:30:00] [INFO] [tls] PNL scan frequency (in sec): 30
[BT] [2020-09-14 09:30:00] [INFO] [tls] --------------------User Cfg Parameters: Other Parameters--------------------
[BT] [2020-09-14 09:30:00] [INFO] [tls] Allow multiple orders for same script: True
[BT] [2020-09-14 09:30:00] [INFO] [tls] Allow new order when order for script already running: True
[BT] [2020-09-14 09:30:00] [INFO] [tls] Instrument max orders count: 100
[BT] [2020-09-14 09:30:00] [INFO] [tls] Remove Instrument if Order Rejected: False
[BT] [2020-09-14 09:30:00] [INFO] [tls] ------------------------------------------------------------
[BT] [2020-09-14 09:30:00] [INFO] [utils] Instrument (NSE_EQ:SBIN) subscribed to historical data successfully
[BT] [2020-09-14 09:30:00] [INFO] [utils] 1 Instrument(s) (re)subscribed to historical data successfully
[BT] [2020-09-14 09:30:00] [INFO] [utils] Prefetching historical data for 1 instruments...
[BT] [2020-09-14 09:30:00] [INFO] [strategy] 
####################
Strategy Parameters:
StrategyParameters([('timeperiod1', 4), ('timeperiod2', 9)])
####################
[BT] [2020-09-14 09:30:00] [INFO] [tls] Opening current intraday positions, if any...
[BT] [2020-09-14 09:30:00] [INFO] [tls] Not processing holdings as STRATEGY_MODE is INTRADAY
[BT] [2020-09-14 09:30:00] [INFO] [utils] 1 Instrument(s) (re)subscribed to historical data successfully
[BT] [2020-09-14 09:30:00] [INFO] [clock] Waiting for 900.00 seconds until end of current candle (2020-09-14 09:45:00)...
[BT] [2020-09-14 09:45:00] [CRITICAL] [tls] Strategy breached code of conduct. Please adjust your strategy to follow guidelines and run again. Halting strategy...
[BT] [2020-09-14 09:45:00] [INFO] [tls] Received event EXCEPTION. Stopping AlgoBulls Trading Core Engine...
[BT] [2020-09-14 09:45:00] [INFO] [tls] Stopping without exiting any positions...
[BT] [2020-09-14 09:45:00] [INFO] [tls] [User: ALGOBULLS VIRTUAL USER] Trading session completed
[BT] [2020-09-14 09:45:00] [INFO] [oms] 

PENDING ORDERS:


COMPLETED ORDERS:


POSITIONS:


HOLDINGS:

Hi Ganesh,

Kindly post your code here again.

I saw the issue in the logs but the team needs more information.

Thanks,
` aki

Code snippet:

from pyalgotrading.strategy.strategy_base import StrategyBase
from pyalgotrading.constants import *

class StrategyEMARegularOrder(StrategyBase):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.timeperiod1 = self.strategy_parameters['timeperiod1']
        self.timeperiod2 = self.strategy_parameters['timeperiod2']

        self.main_order = None

    def initialize(self):
        self.main_order = {}

    @staticmethod
    def name():
        return 'EMA Regular Order Strategy'

    @staticmethod
    def versions_supported():
        return AlgoBullsEngineVersion.VERSION_3_2_0

    def get_crossover_value(self, instrument):
        hist_data = self.get_historical_data(instrument)
        ema_x = talib.EMA(hist_data['close'], timeperiod=self.timeperiod1)
        ema_y = talib.EMA(hist_data['close'], timeperiod=self.timeperiod2)
        crossover_value = self.utils.crossover(ema_x, ema_y)
        return crossover_value

    def strategy_select_instruments_for_entry(self, candle, instruments_bucket):

        selected_instruments_bucket = []
        sideband_info_bucket = []

        for instrument in instruments_bucket:
            crossover_value = self.get_crossover_value(instrument)
            if crossover_value == 1:
                selected_instruments_bucket.append(instrument)
                sideband_info_bucket.append({'action': 'BUY'})
            elif crossover_value == -1:
                if self.strategy_mode is StrategyMode.INTRADAY:
                    selected_instruments_bucket.append(instrument)
                    sideband_info_bucket.append({'action': 'SELL'})

        return selected_instruments_bucket, sideband_info_bucket

    def strategy_enter_position(self, candle, instrument, sideband_info):
        if sideband_info['action'] == 'BUY':
            qty = self.number_of_lots * instrument.lot_size
            self.main_order[instrument] = \
                self.broker.BuyOrderRegular(instrument=instrument,
                                            order_code=BrokerOrderCodeConstants.INTRADAY,
                                            order_variety=BrokerOrderVarietyConstants.MARKET,
                                            quantity=qty)
        elif sideband_info['action'] == 'SELL':
            qty = self.number_of_lots * instrument.lot_size
            self.main_order[instrument] = \
                self.broker.SellOrderRegular(instrument=instrument,
                                             order_code=BrokerOrderCodeConstants.INTRADAY,
                                             order_variety=BrokerOrderVarietyConstants.MARKET,
                                             quantity=qty)
        else:
            raise SystemExit(f'Got invalid sideband_info value: {sideband_info}')

        return self.main_order[instrument]

    def strategy_select_instruments_for_exit(self, candle, instruments_bucket):
        selected_instruments_bucket = []
        sideband_info_bucket = []

        for instrument in instruments_bucket:
            if self.main_order.get(instrument) is not None:
                crossover_value = self.get_crossover_value(instrument)
                if crossover_value in [1, -1]:
                    selected_instruments_bucket.append(instrument)
                    sideband_info_bucket.append({'action': 'EXIT'})
        return selected_instruments_bucket, sideband_info_bucket

    def strategy_exit_position(self, candle, instrument, sideband_info):
        if sideband_info['action'] == 'EXIT':
            self.main_order[instrument].exit_position()
            self.main_order[instrument] = None
            return True

        return False

Hi Ganesh,

I am covering some points here:

  1. Issues related to “Strategy breached code of conduct”:
    This is related to the platform, which ensures that the strategy code adheres to certain guidelines. We have made some updates at our end. So, kindly run the strategy code again to see if you can proceed forward.

  2. Issues related to “Pythonic errors”:
    This is related to some errors encountered in your strategy code. We have updated the logs feature to give you a more detailed report of the particular error you are encountering. This should help you to google the particular Python code error and proceed forward.

To summarize, run your strategy code again to see whether the security feature is letting you proceed forward. And then kindly see if the detailed (logs) report is helping you to isolate specific code errors better now.

Thanks,
` aki

Yes, it’s now working fine.

What is the minimum timeframe (start and end date) needs to be passed for backtesting of any strategy?

Hi Ganesh,

For testing your strategy, you can experiment with the start and end dates to suit your strategy.

For example:

If you are testing your strategy on 01-OCT-2020, then the dates can be from:

15-SEP-2020
to
30-SEP-2020

If you are testing your strategy on 16-OCT-2020, then the dates can be from

15-SEP-2020
to
15-OCT-2020

However, you can try for 1-2 days also.
As an example, the start and end dates could be:

29-SEP-2020
to
30-SEP-2020

OR

30-SEP-2020
to
30-SEP-2020

Cheers!
` aki