Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
0.067%
Drawdown
0.000%
Expectancy
0
Net Profit
0.005%
Sharpe Ratio
1.722
Probabilistic Sharpe Ratio
58.316%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0.001
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-4.306
Tracking Error
0.049
Treynor Ratio
0.377
Total Fees
$2.00
from System import *
from QuantConnect import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Data.Market import *
from QuantConnect.Orders import *
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Indicators import *
import decimal as d
import numpy as np
from datetime import timedelta, datetime, date
import datetime
from System.Drawing import Color
import pytz
# NEW
import time
import uuid


class MultipleSymbolConsolidationAlgorithm(QCAlgorithm):
    
    # Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
    def Initialize(self):
        #######################################
        #       B A S I C   C O N F I G       #
        #######################################
        self.SetStartDate(2017, 7, 1)                      # Set Starttime
        self.SetEndDate(2017, 7, 30)                        # Set Endtime
        BarPeriod = TimeSpan.FromMinutes(1)                 # Set Custom Bar Period
        RollingWindowSize = 15                               # Number of consolidated bars in window
        AssetList = ["OILU"
         ]



        startingCash = 3000.0;                             # Set Starting Cash in EUR



        self.Data = {}

        
        #######################################
        #  I N I T I A L I Z E   E Q U I T Y  # 
        #######################################
        for symbol in AssetList:
            _equity = self.AddEquity(symbol, Resolution.Minute)
            self.Data[symbol] = SymbolData(_equity.Symbol, BarPeriod, RollingWindowSize)
            
        # loop through all our symbols and request data subscriptions and initialize indicator
        for symbol, symbolData in self.Data.items():


            # C R E A T E   C O N S O L I D A T O R
            #https://www.quantconnect.com/forum/discussion/3683/multiple-symbol-rolling-window/p1
            # define a consolidator to consolidate data for this symbol on the requested period
            oneMinuteConsolidator = TradeBarConsolidator(BarPeriod)
            # write up our consolidator to update the indicator
            oneMinuteConsolidator.DataConsolidated += self.OnDataConsolidated_1Minute
            # we need to add this consolidator so it gets auto update
            self.SubscriptionManager.AddConsolidator(symbolData.Symbol, oneMinuteConsolidator)
            
    
    def OnDataConsolidated_1Minute(self, sender, bar):
        self.Data[bar.Symbol.Value].Bars.Add(bar)
        
                         
 
    

            
 
    def OnData(self,data):
 
        # LOOP THROUGH ASSETS
        for symbol in self.Data.keys():
           
            symbolData = self.Data[symbol]
            if self.Portfolio[symbol].Quantity < 1:
            
                symbolData._marketOrderTicket = self.MarketOrder(symbol, 1, False,"Marketorder" )
                
                symbolData._limitTicket = self.LimitOrder(symbol, 1, symbolData.Bars[0].Close, "LimitOrder" );
                


    def OnOrderEvent(self, orderEvent):  
        #self.Log("onOrderEvent##############")
        ## This will check for the boolean value of whether or not the order has been filled
        self.Log("OnOrderEvent::  Orderevent: " + str(orderEvent))
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        #self.Log("OnOrderEvent > ordertickets: " + str(order))
        self.Log("OnOrderEvent::  ordertickets.TAG: " + str(order.Tag))
        
        
        for symbol in self.Data.keys():
            symbolData = self.Data[symbol]


            if symbolData._limitTicket is not None: 
                if symbolData._limitTicket.OrderId == orderEvent.OrderId and orderEvent.Status == OrderStatus.Filled: 
                    self.Log("Order event for _limitTicket arrived ")

            if symbolData._marketOrderTicket is not None:
                if symbolData._marketOrderTicket.OrderId == orderEvent.OrderId:
                    self.Log("Order event for _marketOrderTicket arrived")

   
                        
class SymbolData(object):
    
    def __init__(self, symbol, barPeriod, windowSize):
        self.Symbol = symbol
        # The period used when population the Bars rolling window
        self.BarPeriod = barPeriod
        # A rolling window of data, data needs to be pumped into Bars by using Bars.Update( tradeBar ) and can be accessed like:
        # mySymbolData.Bars[0] - most first recent piece of data
        # mySymbolData.Bars[5] - the sixth most recent piece of data (zero based indexing)
        self.Bars = RollingWindow[IBaseDataBar](windowSize)         # 1 minute Bar
        self.BarsDaily = RollingWindow[IBaseDataBar](windowSize)
        
        self._limitTicket = None;
        self._marketOrderTicket = None;  
        self.symbolData = None