Overall Statistics
from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Consolidators import *
from datetime import datetime
import decimal as d
import numpy as np

class CVTap(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2017,11,1) #Set Start Date
        self.SetEndDate(2017,11,14) #Set End Date
        self.SetCash(10000) #Set Strategy Cash
        
        self.fxPair = "EURUSD"
        
        # Add EURUSD
        self.AddForex(self.fxPair, Resolution.Minute)

        # Create consolidator for 30 minutes
        consThirtyMin = QuoteBarConsolidator(30)
        consThirtyMin.DataConsolidated += self.ThirtyMinHandler
        self.SubscriptionManager.AddConsolidator(self.fxPair, consThirtyMin)
        
        # Create consolidator for 60 minutes
        cons60Min = QuoteBarConsolidator(60)
        cons60Min.DataConsolidated += self.SixtyMinHandler
        self.SubscriptionManager.AddConsolidator(self.fxPair, cons60Min)

        self.bb30 = BollingerBands("30minBB", 20,2,MovingAverageType.Simple)
        
        self.bb60 = BollingerBands("60minBB", 20,2,MovingAverageType.Simple)
        
        self.current_price_30 = 0
        self.current_price_60 = 0
        
        # Inititalize rolling window which stores to store upper bollinger band indicator values
        self.bbUpperRollingWindow = RollingWindow[float](3)
        
        self.bbUpperRollingWindow_60 = RollingWindow[float](3)
    
    def ThirtyMinHandler(self, sender, bar):
        # Update bb30 indicator
        self.bb30.Update(bar.EndTime, bar.Close)
        
        # Add to rolling window
        self.bbUpperRollingWindow.Add(self.bb30.Current.Value)
        
        # Update 30min current price
        self.current_price_30 = bar.Close
        
    def SixtyMinHandler(self, sender, bar):
        # Update bb60 indicator
        self.bb60.Update(bar.EndTime, bar.Close)
        
        # Add to rolling window
        self.bbUpperRollingWindow_60.Add(self.bb60.Current.Value)
        
        # Update 60min current price
        self.current_price_60 = bar.Close

    def OnData(self, data):
        # Return if rolling window is not ready
        if not (self.bbUpperRollingWindow.IsReady and self.bbUpperRollingWindow_60.IsReady):
            return
    
        self.Log("upper rolling window : " + str(self.bbUpperRollingWindow[0]))
        
        bb30cv = self.bbUpperRollingWindow[0]
        bb60cv = self.bbUpperRollingWindow_60[0]
        
        cv_30 = self.current_price_30
        cv_60 = self.current_price_60
        
        holdings = self.Portfolio[self.fxPair].Quantity
        
        stop_price_ = self.Securities[self.fxPair].Price * 0.97
        stop_price = self.Securities[self.fxPair].Price * 1.05
        
        if not self.Portfolio[self.fxPair].Invested:
            
            if cv_30 > bb30cv:
                return
   
    def OnOrderEvent(self, orderEvent):
        
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
       
        if order.Status == OrderStatus.Filled:
            if order.Type == OrderType.Limit or order.Type == OrderType.StopMarket:
                self.Transactions.CancelOpenOrders(order.Symbol)
               
        if order.Status == OrderStatus.Canceled:
            self.Log(str(orderEvent))