Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
7.374%
Drawdown
4.400%
Expectancy
0
Net Profit
6.749%
Sharpe Ratio
0.918
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.181
Beta
-7.585
Annual Standard Deviation
0.064
Annual Variance
0.004
Information Ratio
0.669
Tracking Error
0.065
Treynor Ratio
-0.008
Total Fees
$0.00
from QuantConnect.Indicators import *
import decimal as d



class BollingerBreakoutAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2017, 6, 1)  #Set Start Date
        self.SetEndDate(2018, 5, 1)    #Set End Date
        self.SetCash(10000)            #Set Strategy Cash
        self.SetBrokerageModel(BrokerageName.OandaBrokerage)
        self.eurusd = self.AddForex("EURUSD", Resolution.Minute).Symbol
        
        # create a bollinger band
        self.Bolband = self.BB("EURUSD", 21, 2, MovingAverageType.Simple, Resolution.Minute)
        # create a bollinger band of 4 std deviations for stop
        self.Bolband_stop = self.BB("EURUSD", 21, 6, MovingAverageType.Simple, Resolution.Minute)

        # set warmup period
        self.SetWarmUp(21)
        

    
    def OnData(self, data):
        
        
        holdings = self.Portfolio["EURUSD"].Quantity
        price = self.Securities["EURUSD"].Close
        price_now = self.Securities["EURUSD"].Price
        
        # If price closes above upper bollinger band then sell and close when price hits lower bollinger band or price hits the Upper band of the stop
        if self.Time.hour >=10 or self.Time.hour <= 8: #only trading between 10pm and 8am UTC
           if holdings <= 0:
              if price >= self.Bolband.UpperBand.Current.Value:
                 self.SetHoldings("EURUSD", -1.0)
                 if price_now <= self.Bolband.LowerBand.Current.Value:
                   self.Liquidate("EURUSD")
                 elif price_now >= self.Bolband_stop.UpperBand.Current.Value:
                    self.Liquidate("EURUSD")
                 elif self.Time.hour >= 9:
                    self.Liquidate("EURUSD")
        
        
        # Buy if price closes below lower bollinger band then close when price hits lower bollinger band or price hits the stop
        if self.Time.hour >=10 or self.Time.hour <= 8: #only trading between 10pm and 8am UTC
            if holdings <= 0:
                if price <= self.Bolband.LowerBand.Current.Value:
                 self.SetHoldings("EURUSD", 1.0)
                if price_now >= self.Bolband.UpperBand.Current.Value:
                   self.Liquidate("EURUSD")
                elif price_now <= self.Bolband_stop.LowerBand.Current.Value:
                    self.Liquidate("EURUSD")
                elif self.Time.hour >= 9:
                    self.Liquidate("EURUSD")