Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
0.528%
Drawdown
0.100%
Expectancy
0
Net Profit
0.063%
Sharpe Ratio
2.79
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.011
Beta
-0.454
Annual Standard Deviation
0.001
Annual Variance
0
Information Ratio
-7.238
Tracking Error
0.002
Treynor Ratio
-0.009
Total Fees
$0.00
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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


class MultipleSymbolConsolidationAlgorithm(QCAlgorithm):
    

    def Initialize(self):
        
        
        BarPeriod = TimeSpan.FromHours(1)
        SimpleMovingAveragePeriod = 5
        ADXPeriod = 10
        RollingWindowSize = 5
        self.Data = {}
 
        ForexSymbols =["EURAUD"] #, "USDJPY"]#, "EURGBP", "EURCHF", "USDCAD", "USDCHF", "AUDUSD","NZDUSD"]
        
        self.SetStartDate(2018, 2, 1)
        self.SetEndDate(2018, 3, 17)
        self.SetCash(50000)
   
        

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




        for symbol, symbolData in self.Data.items():
            
        
            symbolData.SMA = SimpleMovingAverage(self.CreateIndicatorName(symbol, "SMA" + str(SimpleMovingAveragePeriod), Resolution.Hour), SimpleMovingAveragePeriod)
            symbolData.ADX = AverageDirectionalIndex(self.CreateIndicatorName(symbol, "ADX" + str(ADXPeriod), Resolution.Hour), ADXPeriod)
            consolidator = QuoteBarConsolidator(BarPeriod)
            consolidator.DataConsolidated += self.OnDataConsolidated
            self.SubscriptionManager.AddConsolidator(symbolData.Symbol, consolidator)

   
   
    def OnDataConsolidated(self, sender, bar):
        
        self.Data[bar.Symbol.Value].SMA.Update(bar.Time, bar.Close)
 #       self.Data[bar.Symbol.Value].ADX.Update(bar.Time, bar.Close)
        self.Data[bar.Symbol.Value].Bars.Add(bar)

   
   
   
   
   
    def OnData(self, data):     
        for symbol in self.Data.keys():
            symbolData = self.Data[symbol]
  

            if symbolData.IsReady() and symbolData.WasJustUpdated(self.Time):

                symbolData.smaWin.Add(symbolData.SMA.Current.Value)
  #              symbolData.Add(symbolData.ADX.Current.Value)
            
                                    
                if symbolData.smaWin.Count == 5:
              
                    window_list = [i for i in symbolData.smaWin]
                    self.Log("sma Window of {0} is {1}".format(str(symbol), str(window_list)))
    #                self.Log("ADX of {0} is {1}".format(str(symbol), str(self.ADX.Current.Value)))
                    
               
                if not self.Portfolio[symbol].Invested:
                    self.MarketOrder(symbol, 1000)
                    

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

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