class HeikinCashi(QCAlgorithm):
def Initialize(self):
# Set our main strategy parameters
self.SetStartDate(2019,1,7)
self.SetEndDate(2019,10,16)
self.SetCash(200)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.SetTimeZone( "Europe/Berlin" );
# Direction current position
self.CP_D = "None"
self.Currencies = ["AUDUSD"]
self.HK = dict() # Create dict to hold all our HK Indicators
self.MO = dict() # Create dict to hold all our MO Indicators
for Currency in self.Currencies:
# Find more symbols here: http://quantconnect.com/data
self.AddForex(Currency, Resolution.Daily, Market.Oanda)
self.HK[Currency] = self.HeikinAshi(Currency, Resolution.Daily)
self.MO[Currency] = self.MOM(Currency, 10, Resolution.Daily)
def OnData(self, data):
for Currency in self.Currencies:
# Aliases
# -----------------------------------------------------------------
# Heikin
HK_O = self.HK[Currency].Open.Current.Value
HK_C = self.HK[Currency].Close.Current.Value
HK_P = self.HK[Currency].Current.Price
# OHLC
O = data[Currency].Open
C = data[Currency].Close
P = data[Currency].Price
# Momentum
MO_D = self.MO[Currency].Current.Value
# -----------------------------------------------------------------
# Work out Heikin Sentiment
# ---------------------------------------
if HK_O < HK_C:
HK_S = "Bull"
elif HK_O > HK_C:
HK_S = "Bear"
else:
HK_S = "None"
# Logging
# -----------------------------------------------------------------
self.Log("{0} OC >> O: {1}, C:{2} | Price: {3}".format(Currency, O,C,P))
self.Log("{0} Heikin >> O: {1}, C:{2} | Price: {3} | Sentiment: {4}".format(Currency, HK_O,HK_C,HK_P,HK_S))
self.Log("{0} Momentum >> {1}".format(Currency, MO_D))
# Entry / Exit Criteria
# -----------------------------------------------------------------
# Check if we are in the market
if not self.Portfolio.Invested:
# If not, we check HK sentiment is bullish
if HK_S == "Bull" and MO_D > 0:
self.MarketOrder(Currency, 1000, False, "Long")
self.CP_D = "Bull"
elif HK_S == "Bear" and MO_D < 0:
self.MarketOrder(Currency, -1000, False, "Short")
self.CP_D = "Bear"
else:
if self.CP_D != "None" and self.CP_D != HK_S:
self.Liquidate(Currency)
self.CP_D = "None"
# Override the base class event handler for order events
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
self.Debug("{0}: {1}: {2}".format(self.Time, order.Type, orderEvent))