Strategy Examples:

Java
Java
Python
Python
PHP
PHP
C#
C#
C++
C++
TypeScript
TypeScript
▸ Strategy Quick Review

Strategy Pattern in Python

For this demo of the strategy pattern, we'll take the role of an up-and-coming trading firm looking to implement some algorithmic trading. To us, it's very important that we tune our trading strategy to the current market conditions in order to make the biggest profit in bull markets, while losing as little money as possible during bear markets.


   # Types of trading strategies:
   class RiskyTradingStrategy(object):
       def MakeTrades(self):
           print("Making risky trades!")

   class ModerateTradingStrategy(object):
       def MakeTrades(self):
           print("Making moderate trades.")

   class ConservativeTradingStrategy(object):
       def MakeTrades(self):
           print("Making safe trades.")

   # Not Supported below Python 3.4!
   from enum import Enum
   class TradeConditions(Enum):
       BearMarket = 0,
       BullMarket = 1,
       RecoveringMarket = 2

   # The trading firm which changes strategies based on market conditions:
   class TradingFirm(object):
       def __init__(self):
           self.riskyStrategy = RiskyTradingStrategy()
           self.moderateStrategy = ModerateTradingStrategy()
           self.safeStrategy = ConservativeTradingStrategy()

           self.currentStrategy = self.moderateStrategy

       def MarketUpdte(self, tradeConditions):
           # Select the best strategy for the market conditions:
           if tradeConditions == TradeConditions.BearMarket:
               self.currentStrategy = self.safeStrategy
           elif tradeConditions == TradeConditions.BullMarket:
               self.currentStrategy = self.riskyStrategy
           elif tradeConditions == TradeConditions.RecoveringMarket:
               self.currentStrategy = self.moderateStrategy

           # Make trades with that strategy:
           self.currentStrategy.MakeTrades()

Find any bugs in the code? let us know!