Overall Statistics
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d

class QuantumResistanceRadiator(QCAlgorithm):
    tickbar = None

    def Initialize(self):
        self.SetStartDate(2019, 6, 1)  # Set Start Date
        self.SetEndDate(2019, 6, 30)
        self.SetCash(100000)  # Set Strategy Cash
        # self.AddEquity("SPY", Resolution.Minute)
        self.AddForex("USDCAD", Resolution.Hour, Market.FXCM )
        self.SetBrokerageModel(BrokerageName.FxcmBrokerage , AccountType.Margin)
        self.Consolidate("USDCAD", Resolution.Tick, TickType.Trade, self.OnDataConsolidated)
        self.emafast = self.SMA("USDCAD", 1, Resolution.Hour, Field.Close)
        self.emaslow = self.SMA("USDCAD", 4, Resolution.Hour, Field.Close)
        
        
    def OnData(self, data):
        #if self.tickbar is not None:
            #self.Debug(str(self.tickbar.Bid.Close) + "Time:" + str(self.Time.hour))
            
        if not self.emafast.IsReady or not self.emaslow.IsReady:
            return
       
        holdings = self.Portfolio["USDCAD"].Quantity
        tolerance = 0.00015
       
        if not self.Portfolio.Invested:
            # if the fast is greater than the slow, we'll go long
            if self.tickbar.Ask.Close < self.emaslow.Current.Value * d.Decimal(1 + tolerance) and self.tickbar is not None:
                self.Log("BUY  >> {0}".format(self.Securities["USDCAD"].Price) + ", Ask Price is" + str(self.tickbar.Ask.Close))
                self.SetHoldings("USDCAD", -1.0)
        #else:
        #    if self.emafast.Current.Value > self.emaslow.Current.Value * d.Decimal(1 + tolerance):
        #        self.Log("CLOSE SHORT  >> {0}".format(self.Securities["USDCAD"].Price))
        #        self.Liquidate("USDCAD")
           
               
 
       
        # if the fast is less than the slow we'll liquidate our long
        if self.tickbar.Bid.Close > self.emaslow.Current.Value:
            self.Log("SELL >> {0}".format(self.Securities["USDCAD"].Price) +", Profit from trade:"+ 
                str(self.Portfolio["USDCAD"].LastTradeProfit) +"TotalProfit:"+str(self.Portfolio.TotalProfit)+" Sold For: "+str(self.tickbar.Ask.Close))
            self.Liquidate("USDCAD")
            #self.SetHoldings("USDCAD", -1.0)
            #self.StrategyReturns.append(self.Portfolio["USDCAD"].LastTradeProfit)
            
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''
        
    def OnOrderEvent(self, event):
        return
    # if not self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)
        
    def OnSecuritiesChanged(self, securities):
        if not self.Portfolio.Invested:
            self.SetHoldings("USDCAD", 1)
            
        
        
    def OnDataConsolidated(self, bar):
        #2. Save one bar as openingBar
        self.tickbar = bar
        
        return