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.OnDataConsolidated
cons60Min = QuoteBarConsolidator(60)
cons60Min.DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator(self.fxPair, consThirtyMin)
self.SubscriptionManager.AddConsolidator(self.fxPair, cons60Min)
self.bb30 = BollingerBands("30minBB", 20,2,MovingAverageType.Simple)
self.bb60 = BollingerBands("60minBB", 20,2,MovingAverageType.Simple)
# Inititalize rolling window which stores to store upper bollinger band indicator values
self.bbUpperRollingWindow = RollingWindow[float](3)
self.bbUpperRollingWindow_60 = RollingWindow[float](3)
def OnDataConsolidated(self, sender, bar):
# Update bb30 indicator
self.bb30.Update(bar.EndTime, bar.Close)
self.bb60.Update(bar.EndTime, bar.Close)
# Add to rolling window
self.bbUpperRollingWindow.Add(self.bb30.Current.Value)
self.bbUpperRollingWindow_60.Add(self.bb60.Current.Value)
def OnData(self, data):
# Return if rolling window is not ready
if not self.bbUpperRollingWindow.IsReady and self.bbUpperRollingWindow_60:
return
self.Log("upper rolling window : " + str(self.bbUpperRollingWindow[0]))
self.bb30cv = self.bbUpperrollingWindow[0]
self.bb60cv = self.bbUpperRollingWindow_60[0]
cv_30 = consThirtyMin[0].Current.Value
cv_60 = cons60Min[0].Current.Value
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 > self.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))