from datetime import datetime, timedelta
import decimal as d
class macross(QCAlgorithm):
def Initialize(self):
self.SetCash(10000)
self.SetStartDate(2016, 3, 1)
self.SetEndDate(2017, 5, 30)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.SetWarmUp(222)
self.Min = Resolution.Minute
self.Hour = Resolution.Hour
self.Day = Resolution.Daily
self.Indicators = {}
self.Consolidators = {}
self.pairs = ['USDCAD']
for symbol in self.pairs:
self.AddForex(symbol, self.Min, Market.Oanda)
self.Consolidators[symbol] = {}
self.Consolidators[symbol]['CON_1'] = QuoteBarConsolidator(timedelta(minutes=15))
self.Consolidators[symbol]['CON_1'].DataConsolidated += self.on_custom
self.Indicators[symbol] = {}
self.Indicators[symbol]['EMA'] = {}
self.Indicators[symbol]['EMA']['CUSTOM_1'] = ExponentialMovingAverage(symbol, 13)
self.RegisterIndicator(symbol, self.Indicators[symbol]['EMA']['CUSTOM_1'], self.Consolidators[symbol]['CON_1'])
self.SubscriptionManager.AddConsolidator(symbol, self.Consolidators[symbol]['CON_1'])
def OnData(self, data):
if self.IsWarmingUp:
return
for symbol in self.pairs:
if not self.Indicators[symbol]['EMA']['CUSTOM_1'].IsReady: return
EMA_1 = self.Indicators[symbol]['EMA']['CUSTOM_1'].Current.Value
Close = data[symbol].Close
Open = data[symbol].Open
High = data[symbol].High
Low = data[symbol].Low
Use = self.Portfolio.MarginRemaining * 4
Invested = self.Securities[symbol].Invested
if not Invested:
if Low < EMA_1:
self.Sell(symbol, Use)
self.Debug("sell " + str(symbol) + " on: {0}".format(self.Time))
elif Invested:
if Low > EMA_1:
self.Liquidate()
self.Debug("LIQUIDATION")
def on_custom(self, sender, bar):
if self.IsWarmingUp:
return
symbol = str(bar.get_Symbol())
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
self.Log("{0}: {1}: {2}".format(self.Time, order.Type, orderEvent))
self.ordersymbol = orderEvent.Symbol
self.orderquantity = orderEvent.FillQuantity
self.orderprice = orderEvent.FillPrice
if order.Status == OrderStatus.Filled:
if order.Type == OrderType.StopMarket:
self.Transactions.CancelOpenOrders(order.Symbol)
if order.Status == OrderStatus.Canceled:
self.Log(str(orderEvent))