Overall Statistics
Total Trades
64
Average Win
0.93%
Average Loss
-0.54%
Compounding Annual Return
2.912%
Drawdown
4.400%
Expectancy
0.700
Net Profit
12.646%
Sharpe Ratio
0.535
Loss Rate
38%
Win Rate
62%
Profit-Loss Ratio
1.72
Alpha
0.117
Beta
-5.724
Annual Standard Deviation
0.046
Annual Variance
0.002
Information Ratio
0.178
Tracking Error
0.046
Treynor Ratio
-0.004
Total Fees
$21.60
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar


class RollingWindowAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2013, 6, 1)  #Set Start Date
        self.SetEndDate(2017, 7, 1)    #Set End Date
        self.SetCash(10000)             #Set Strategy Cash
        self.SetBrokerageModel(BrokerageName.FxcmBrokerage)
        self.AddForex("EURUSD", Resolution.Daily)
        
        self.window = RollingWindow[QuoteBar](2)
        
        # create a bollinger band
        self.Bolband = self.BB("EURUSD", 20, 0.75, MovingAverageType.Simple, Resolution.Daily)
        self.Bolband.Updated += self.BolbandUpdated
        self.BolbandWin = RollingWindow[IndicatorDataPoint](5)
        
        # set warmup period
        self.SetWarmUp(20)
        
        
    def BolbandUpdated(self, sender, updated):
        '''Adds updated values to rolling window'''
        self.BolbandWin.Add(updated)
        

    def OnData(self, data):
        
        self.window.Add(data["EURUSD"])
        if not (self.window.IsReady and self.BolbandWin.IsReady): return
        
        holdings = self.Portfolio["EURUSD"].Quantity
        price = self.window[0].Close
        previousPrice = self.window[1].Close
        
        if holdings <= 0:
             
             if previousPrice <= self.Bolband.LowerBand.Current.Value and price > self.Bolband.LowerBand.Current.Value:
                self.SetHoldings("EURUSD", 1.0)
               
        
        if holdings > 0:
            
            if previousPrice >= self.Bolband.UpperBand.Current.Value and price < self.Bolband.UpperBand.Current.Value:
                self.Liquidate()
                
            if previousPrice <= self.Bolband.LowerBand.Current.Value and price > self.Bolband.LowerBand.Current.Value:
                self.SetHoldings("EURUSD", 1.0)