Overall Statistics
Total Trades
246
Average Win
0.00%
Average Loss
0.00%
Compounding Annual Return
-0.390%
Drawdown
0.100%
Expectancy
-0.164
Net Profit
-0.028%
Sharpe Ratio
-3.162
Loss Rate
70%
Win Rate
30%
Profit-Loss Ratio
1.78
Alpha
-0.001
Beta
-0.162
Annual Standard Deviation
0.001
Annual Variance
0
Information Ratio
-17.477
Tracking Error
0.001
Treynor Ratio
0.019
Total Fees
$0.00
from System import *
from QuantConnect import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Data.Market import *
from QuantConnect.Orders import OrderStatus
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Indicators import *
import numpy as np
from datetime import timedelta, datetime
import decimal as d

class MultipleSymbolConsolidationAlgorithm(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2018, 3, 1)
        self.SetEndDate(2018, 3, 26)
        self.SetCash(50000)
        self.SetWarmup(10)
        self.SetBrokerageModel(BrokerageName.OandaBrokerage)
        
        BarPeriod = TimeSpan.FromMinutes(5)
        SimpleMovingAveragePeriodfast = 8
        SimpleMovingAveragePeriodslow = 13
        ADXPeriod = 9
        RollingWindowSize = 5
        self.Data = {}
        ForexSymbols =["EURAUD"]#, "USDJPY", "EURGBP", "EURCHF", "USDCAD", "USDCHF", "AUDUSD","NZDUSD", "EURUSD"]

   
        for symbol in ForexSymbols:
            
            forex = self.AddForex(symbol)
            self.Data[symbol] = SymbolData(forex.Symbol, BarPeriod, RollingWindowSize)

        for symbol, symbolData in self.Data.items():
            
            symbolData.SMAfast = SimpleMovingAverage(self.CreateIndicatorName(symbol, "SMA" + str(SimpleMovingAveragePeriodfast), Resolution.Minute), SimpleMovingAveragePeriodfast)
            symbolData.SMAslow = SimpleMovingAverage(self.CreateIndicatorName(symbol, "SMA" + str(SimpleMovingAveragePeriodslow), Resolution.Minute), SimpleMovingAveragePeriodslow)
            symbolData.ADX = AverageDirectionalIndex(self.CreateIndicatorName(symbol, "ADX" + str(ADXPeriod), Resolution.Minute), ADXPeriod)
            consolidator = QuoteBarConsolidator(BarPeriod)
            consolidator.DataConsolidated += self.OnDataConsolidated
            self.SubscriptionManager.AddConsolidator(symbolData.Symbol, consolidator)
   
   
    def OnDataConsolidated(self, sender, bar):
        
        self.Data[bar.Symbol.Value].SMAfast.Update(bar.Time, bar.Close)
        self.Data[bar.Symbol.Value].SMAslow.Update(bar.Time, bar.Close)
        self.Data[bar.Symbol.Value].ADX.Update(bar)
        self.Data[bar.Symbol.Value].Bars.Add(bar)
   
   
    def OnData(self, data):     
        
        for symbol in self.Data.keys():
            symbolData = self.Data[symbol]
  
            NowPrice = self.Securities[symbol].Price
  
            if symbolData.IsReady() and symbolData.WasJustUpdated(self.Time):
                symbolData.smaWin.Add(symbolData.SMAfast.Current.Value)
                                    
                if symbolData.smaWin.Count == 5:
                    window_list = [i for i in symbolData.smaWin]
                   
                    fast = symbolData.SMAfast.Current.Value
                    slow = symbolData.SMAslow.Current.Value 
                    
                    ADX = symbolData.ADX.Current.Value
                     
                    if not self.Portfolio[symbol].Invested:
                       
                        
                        if fast > slow and ADX >= 30:
                            self.MarketOrder(symbol, 1000)
                            self.Log("Current Price {0}".format(str(NowPrice)))
                            self.Log("ADX {0}".format(str(ADX)))
                        
                    if self.Portfolio[symbol].Quantity >0:
                            
                        if slow > fast:
                            self.Liquidate(symbol)
 
                            

class SymbolData(object):
    
    def __init__(self, symbol, barPeriod, windowSize):
      
        self.Symbol = symbol
        self.BarPeriod = barPeriod
        self.Bars = RollingWindow[IBaseDataBar](windowSize)
        self.SMAfast = None
        self.SMAslow = None
        self.ADX = None
        self.smaWin = RollingWindow[float](5)
  
    def IsReady(self):
        return self.Bars.IsReady and self.SMAfast.IsReady and self.SMAslow.IsReady

    def WasJustUpdated(self, current):
        return self.Bars.Count > 0 and self.Bars[0].Time == current - self.BarPeriod