class MyAlgo(QCAlgorithm):
def Initialize(self):
# Initalise data, resolution, cash, start-date, end-date, pair to trade and make that 100% of portfolio::
self.SetStartDate(2000,1,1)
self.SetEndDate(2016,12,31)
self.SetCash(5000)
self.AddForex("EURUSD", Resolution.Minute)
self.SetHoldings("EURUSD", 1.00)
# Broker to get data from and trade with:
self.SetBrokerageModel(BrokerageName.OandaBrokerage, AccountType.Margin)
# Consolidator to create 15 minute bars:
consolidator = QuoteBarConsolidator(15) # Consolidate the Minute bar data
consolidator.DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator("EURUSD", consolidator)
# Moving Average:
self.sma = SimpleMovingAverage(20) # Create the MA
self.RegisterIndicator("EURUSD", self.sma, consolidator) # Put it on EURUSD and our consolidator so it all matches up
self.SetWarmUp(20) # Warm up of 20 bars to account for our SMA and get data
# coming in straight away
# Get history for all instruments and those timeframes:
self.History(5, Resolution.Daily) # get history of all subscribed instruments in,
# 5 bars back from current bar and for our daily bars
self.History(5, Resolution.Hour) # same as above for our hourly bars
self.History(5, Resolution.Minute) # same as above but for our minute chart
# Rolling Window
self.quoteBarWindow = RollingWindow[QuoteBar](2) # Where the confusion comes in
# QUETION: Is the above what allows me to query the current bar close '[0]', then [1],[2],[3] going back further? Also, how do i determine how many bars back i can query in my algorhythm? What is the 2 at the end for?
def OnData(self, data):
self.quoteBarWindow.Add(data["EURUSD"])
stopprice = self.quoteBarWindow[0].Close * 0.07
# Place an order if conditions are met:
if not self.Portfolio.Invested:
if self.quoteBarWindow[0].Close > self.sma.Current.Value:
open_an_order = self.MarketOrder("EURUSD", 1000)
# Stop out the order if conditions are met:
if self.quoteBarWindow[0].Close < self.sma.Current.Value:
close_an_order = self.StopMarketOrder("EURUSD", -1000, stopprice)
# Consolidator Class:
def OnDataConsolidated(self, sender, bar):
self.Debug(str(self.Time) + " > New Bar!")
def SmaUpdated(self, sender, updated):
self.smaWin.Add(updated)
Just had a few questions:
1. Could you let me know if my notes # are correct? Just want to get the logic of putting it all together correct as I have a number of strategies (the rules for them) that I want to just implement here.
2. self.quoteBarWindow[0].Close <<< Is this the correct syntax to get the last bar that closed? If so, how do i get the bar before and bar before that? I got an error when i tried before saying only accepts 0 to 0. so [1] wouldnt work.
For example, I would like to say if the last bar closed goes above the SMA line, open a trade. If the last bar to close closed below the SMA, close the trade.
3. If I wanted to do a fixed pip based stop and target, what is the syntax for this?
For exmaple, if last closed bar is anove SMA, enter a trade with a target of 40 pips and stop of 20 pips?
Sorry for all the questions, but I think once i get these fundamental bits down, I can build on that and go from there. I really appreciate your help! :)