<jupyter_start><jupyter_text>Python Algorithmic Trading Cookbook  Chapter 8: Algorithmic Trading: Code Strategies Step-by-step This Jupyter Notebook is created using Python version 3.8.2 ----  Requirements You can install the requirements for this Jupyter Notebook by executing the below cell<jupyter_code>!pip install pyalgotrading pyalgostrategypool TA-Lib<jupyter_output><empty_output><jupyter_text>If you get an error while installing TA-Lib, please refer to the book for installation instructions. ----  Master RecipeThe following code will help you set up the broker connection with Zerodha, which will be used by all the recipes in this chapter. Please make sure you have followed these steps before trying out any recipe.<jupyter_code>from pyalgotrading.strategy.strategy_base import StrategyBase
from pyalgotrading.constants import *<jupyter_output><empty_output><jupyter_text>----  Recipe 1: EMA-Regular-Order Strategy: Coding the '__init__', '\_\_initialize\_\_', 'name' and 'versions_supported' methods<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Recipe 2: EMA-Regular-Order Strategy: Coding the 'strategy_select_instruments_for_entry' method<jupyter_code>class StrategyEMARegularOrder(StrategyBase):
   
    # Previous methods not shown

    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<jupyter_output><empty_output><jupyter_text>Recipe 3: EMA-Regular-Order Strategy: Coding the 'strategy_enter_position' method<jupyter_code>class StrategyEMARegularOrder(StrategyBase):
   
    # Previous methods not shown

    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]<jupyter_output><empty_output><jupyter_text>Recipe 4: EMA-Regular-Order Strategy: Coding the 'strategy_select_instruments_for_exit' method<jupyter_code>class StrategyEMARegularOrder(StrategyBase):
   
    # Previous methods not shown

    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<jupyter_output><empty_output><jupyter_text>Recipe 5: EMA-Regular-Order Strategy: Coding the 'strategy_exit_position' method<jupyter_code>class StrategyEMARegularOrder(StrategyBase):
   
    # Previous methods not shown

    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<jupyter_output><empty_output><jupyter_text>Recipe 6:  EMA-Regular-Order Strategy: Create strategy on AlgoBulls Trading Platform<jupyter_code>import inspect
from pyalgotrading.algobulls import AlgoBullsConnection
from pyalgostrategypool.strategy_ema_regular_order import StrategyEMARegularOrder
algobulls_connection = AlgoBullsConnection()
algobulls_connection.get_authorization_url()
algobulls_connection.set_access_token('<KEY>')
print(inspect.getsource(StrategyEMARegularOrder))
algobulls_connection.create_strategy(StrategyEMARegularOrder)<jupyter_output>Validating Strategy...<jupyter_text>There's more...<jupyter_code>algobulls_connection.create_strategy(StrategyEMARegularOrder, overwrite=True)<jupyter_output>Validating Strategy...<jupyter_text>Recipe 7: MACD-Bracket-Order Strategy: Coding the '__init__', '\_\_initialize\_\_', 'name' and 'versions_supported' methods<jupyter_code>class StrategyMACDBracketOrder(StrategyBase):
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fastMA_period = self.strategy_parameters['fastma_period']
        self.slowMA_period = self.strategy_parameters['slowma_period']
        self.signal_period = self.strategy_parameters['signal_period']
        self.stoploss = self.strategy_parameters['stoploss_trigger']
        self.target = self.strategy_parameters['target_trigger']
        self.trailing_stoploss = self.strategy_parameters['trailing_stoploss_trigger']

        self.main_order = None

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

    @staticmethod
    def name():
        return 'MACD Bracket Order Strategy'

    @staticmethod
    def versions_supported():
        return VERSION_3_2_0<jupyter_output><empty_output><jupyter_text>Recipe 8: MACD-Bracket-Order Strategy: Coding the 'strategy_select_instruments_for_entry' method<jupyter_code>class StrategyMACDBracketOrder(StrategyBase):
    
    # Note: Some methods are not shown here    
    
    def get_crossover_value(self, instrument):
        hist_data = self.get_historical_data(instrument)
        macdline, macdsignal, _ = talib.MACD(hist_data['close'], 
                                             fastperiod=self.fastMA_period, 
                                             slowperiod=self.slowMA_period, 
                                             signalperiod=self.signal_period)
        crossover_value = self.utils.crossover(macdline, macdsignal)
        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<jupyter_output><empty_output><jupyter_text>Recipe 9: MACD-Bracket-Order Strategy: Coding the 'strategy_enter_position' method<jupyter_code>class StrategyMACDBracketOrder(StrategyBase):
    
    # Note: Some methods are not shown here 
    
    def strategy_enter_position(self, candle, instrument, sideband_info):
        if sideband_info['action'] == 'BUY':
            qty = self.number_of_lots * instrument.lot_size
            ltp = self.broker.get_ltp(instrument)
            self.main_order[instrument] = \
                self.broker.BuyOrderBracket(instrument=instrument,
                                            order_code=BrokerOrderCodeConstants.INTRADAY,
                                            order_variety=BrokerOrderVarietyConstants.LIMIT,
                                            quantity=qty,
                                            price=ltp,
                                            stoploss_trigger=ltp - (ltp * self.stoploss),
                                            target_trigger=ltp + (ltp * self.target),
                                            trailing_stoploss_trigger=ltp * self.trailing_stoploss)

        elif sideband_info['action'] == 'SELL':
            qty = self.number_of_lots * instrument.lot_size
            ltp = self.broker.get_ltp(instrument)
            self.main_order[instrument] = \
                self.broker.SellOrderBracket(instrument=instrument,
                                             order_code=BrokerOrderCodeConstants.INTRADAY,
                                             order_variety=BrokerOrderVarietyConstants.LIMIT,
                                             quantity=qty,
                                             price=ltp,
                                             stoploss_trigger=ltp + (ltp * self.stoploss),
                                             target_trigger=ltp - (ltp * self.target),
                                             trailing_stoploss_trigger=ltp * self.trailing_stoploss)
        else:
            raise SystemExit(f'Got invalid sideband_info value: {sideband_info}')

        return self.main_order[instrument]<jupyter_output><empty_output><jupyter_text>Recipe 10: MACD-Bracket-Order Strategy: Coding the 'strategy_select_instruments_for_exit' method<jupyter_code>class StrategyMACDBracketOrder(StrategyBase):
    
    # Note: Some methods are not shown here   
    
    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<jupyter_output><empty_output><jupyter_text>Recipe 11: MACD-Bracket-Order Strategy: Coding the 'strategy_exit_position' method<jupyter_code>class StrategyMACDBracketOrder(StrategyBase):
    
    # Note: Some methods are not shown here   
    
    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<jupyter_output><empty_output><jupyter_text>Recipe 12: MACD-Bracket-Order Strategy: Create strategy on AlgoBulls Trading Platform<jupyter_code>import inspect
from pyalgostrategypool.strategy_macd_bracket_order import StrategyMACDBracketOrder
from pyalgotrading.algobulls import AlgoBullsConnection
algobulls_connection = AlgoBullsConnection()
algobulls_connection.get_authorization_url()
algobulls_connection.set_access_token('<KEY>')
print(inspect.getsource(StrategyMACDBracketOrder))
algobulls_connection.create_strategy(StrategyMACDBracketOrder)<jupyter_output>Validating Strategy...<jupyter_text>There's more...<jupyter_code>algobulls_connection.create_strategy(StrategyMACDBracketOrder, overwrite=True)<jupyter_output>Validating Strategy...
{'fraction_non_alphanumeric': 0.07718327183271832, 'fraction_numerical': 0.0029981549815498157, 'mean_word_length': 2.8856033452807646, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '430359', 'license': 'MIT', 'n_tokens_mistral': 3778, 'n_tokens_neox': 3577, 'n_words': 626, 'path': 'Chapter08/CHAPTER 8.ipynb', 'repo_name': 'rahulmr/Python-Algorithmic-Trading-Cookbook', 'size': '13078'}