Overall Statistics
#

import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *

class MovingAverageCrossAlgorithm(QCAlgorithm):

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2019, 7, 28)    #Set Start Date
        self.SetEndDate(2019, 8, 8)      #Set End Date
        self.SetCash(5000)             #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.AddForex("GBPAUD", Resolution.Minute, Market.Oanda)
        self.eurusd = self.AddForex("GBPAUD", Resolution.Minute).Symbol
        self.numberOfBars = 5


    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''

        holdings = self.Portfolio["GBPAUD"].Quantity
        self.price = data["GBPAUD"].Price
        
        ##############################################
        self.df = self.History(self.eurusd, self.numberOfBars)
        
        if not self.df.empty:
            self.eurusd_quotebars = self.df.loc["GBPAUD"]
        
        self.high = self.eurusd_quotebars["high"]
        self.low = self.eurusd_quotebars["low"]
        self.highestHigh = max(self.high)
        self.lowestLow = min(self.low)

        ###############################################
        if holdings == 0:
            if self.price >= self.highestHigh:
                self.Buy("GBPAUD", 5000)
                
            if self.price <= self.lowestLow:
                self.Sell("GBPAUD", 5000)

        else:
            if self.price >= self.highestHigh and holdings < 0:
                self.Buy("GBPAUD", 10000)
                
            if self.price <= self.lowestLow and holdings > 0:
                self.Sell("GBPAUD", 10000)