Overall Statistics
import numpy as np
from decimal import *
from datetime import timedelta, datetime

class RegressionChannelAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetCash(1000000)
        self.SetStartDate(2015,3,6)
        self.symbols = ["EURGBP", "GBPUSD", "USDCHF", "CADJPY", "EURUSD"] 
        slowperiod = 4000
        self.SetWarmUp(slowperiod)
        for symbol in self.symbols:
            equity = self.AddForex(symbol, Resolution.Hour, Market.Oanda)
            self.Consolidate(symbol, timedelta(hours=5), self.twoforty)
            self.Consolidate(symbol, timedelta(hours=23), self.days)
        #240MIN SETTINGS
        self.lsma4 = self.LSMA("EURUSD", 850, Resolution.Hour)  #lsma 1700, atr kama 1day, 1700, 1700, 300LinearWeightedMovingAverage
        self.ladx4 = self.ADXR("EURUSD", 850, Resolution.Hour) #ADXR 
        self.sadx4 = self.ADXR("EURUSD", 150, Resolution.Hour) 
        #1day settings
        self.sma4 = self.LSMA("EURUSD", 1707, Resolution.Hour)  #lsma 1700, atr kama 1day, 1700, 1700, 300
        self.latr4 = self.ADXR("EURUSD", 1707, Resolution.Hour) #ADXR 
        self.satr4 = self.ADXR("EURUSD", 307, Resolution.Hour) 
    
    def OnData(self, data):
        if (not data.ContainsKey(self.symbols[0])): return #(not data.ContainsKey(self.symbols[1])) or
        if (not data.ContainsKey(self.symbols[1])) or (not data.ContainsKey(self.symbols[2])): return
        if (not data.ContainsKey(self.symbols[3])) or (not data.ContainsKey(self.symbols[4])): return
    def twoforty(self, consolidated):
        
        if self.IsWarmingUp: return
        #if (not self.satr4.IsReady): return
        #if (not self.latr4.IsReady): return
        value1 = self.Securities[self.symbols[1]].Price
        value4 = self.Securities[self.symbols[4]].Price
        #holdings =  self.Portfolio["EURUSD"].TotalHoldingsValue
        #amount240 = decimal.Decimal(0.013)
        quantity = self.CalculateOrderQuantity("EURUSD",1) 
        holdings = False
        if holdings == False:
            if self.sadx4.Current.Value > self.ladx4.Current.Value and value4 < self.lsma4.Current.Value:
                self.MarketOrder(self.symbols[4], -quantity)
                self.MarketOrder(self.symbols[0], -quantity)
                self.MarketOrder(self.symbols[1], -quantity)
                self.MarketOrder(self.symbols[2], -quantity)
                holdings == True
                self.Debug("just went short, holdings is: " + str(holdings))
            elif self.sadx4.Current.Value > self.ladx4.Current.Value and value4 > self.lsma4.Current.Value:
                self.SetHoldings(self.symbols[4], quantity)
                self.SetHoldings(self.symbols[0], quantity)
                self.SetHoldings(self.symbols[1], quantity)
                self.SetHoldings(self.symbols[2], quantity)
                holdings == True
                self.Debug("just went long, holdings is: " + str(holdings))
        if holdings == True:
            if self.Portfolio[self.symbols[4]].IsShort:
                if value4 > self.lsma4.Current.Value and self.sadx4.Current.Value < self.ladx4.Current.Value:
                    self.Liquidate(self.symbols[4])
                    self.Liquidate(self.symbols[0])
                    self.Liquidate(self.symbols[1])
                    self.Liquidate(self.symbols[2])
                    holdings == False
            elif self.Portfolio[self.symbols[4]].IsLong:
                if value4 < self.lsma4.Current.Value and self.sadx4.Current.Value < self.ladx4.Current.Value:
                    self.Liquidate(self.symbols[4])
                    self.Liquidate(self.symbols[0])
                    self.Liquidate(self.symbols[1])
                    self.Liquidate(self.symbols[2])
                    holdings == False
    def days(self, consolidated):
        '''
        value1 = self.Securities[self.symbols[1]].Price
        value4 = self.Securities[self.symbols[4]].Price
        if not self.Securities["EURUSD"].Invested:
            if self.satr4.Current.Value > self.latr4.Current.Value and value4 < self.sma4.Current.Value:
                self.SetHoldings(self.symbols[4], -1)
                self.SetHoldings(self.symbols[0], -1)
                self.SetHoldings(self.symbols[1], -1)
                self.SetHoldings(self.symbols[2], -1)
            elif self.satr4.Current.Value > self.latr4.Current.Value and value4 > self.sma4.Current.Value:
                self.SetHoldings(self.symbols[4], 1)
                self.SetHoldings(self.symbols[0], 1)
                self.SetHoldings(self.symbols[1], 1)
                self.SetHoldings(self.symbols[2], 1)
        if self.Securities["EURUSD"].Invested:
            if self.Portfolio[self.symbols[4]].IsShort:
                if value4 > self.sma4.Current.Value and self.satr4.Current.Value < self.latr4.Current.Value:
                    self.Liquidate(self.symbols[4])
                    self.Liquidate(self.symbols[0])
                    self.Liquidate(self.symbols[1])
                    self.Liquidate(self.symbols[2])
            elif self.Portfolio[self.symbols[4]].IsLong:
                if value4 < self.sma4.Current.Value and self.satr4.Current.Value < self.latr4.Current.Value:
                    self.Liquidate(self.symbols[4])
                    self.Liquidate(self.symbols[0])
                    self.Liquidate(self.symbols[1])
                    self.Liquidate(self.symbols[2])
        '''
        pass