Overall Statistics
from datetime import datetime
import decimal as d
from QuantConnect.Indicators import *
from NodaTime import DateTimeZone

class ExchangeRateGrinder(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2018, 6, 17)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.tickers = ["DKKEUR"]
        self.SetWarmUp(20)
        self.market = Market.Oanda
        self.res = Resolution.Hour
        self.bb = {}
        self.states = {} 
        self.risk = 0.2
        self.SetTimeZone(DateTimeZone.Utc)

        for ticker in self.tickers: 
            self.AddForex(ticker,self.res, self.market)
            self.bb[ticker] = self.BB(ticker, 20, 1.5, MovingAverageType.Simple, self.res)
            self.states[ticker] = 0
            
    def OnData(self,data):
        self.cash = self.Portfolio.Cash
        self.investment = self.cash * self.risk
        self.Debug(investment)

        if self.Time.hour >= 22 or self.Time.hour <= 8: #only trading between 10pm and 8am UTC
            for ticker in self.tickers:
                self.holdings = self.Securities[ticker].Quantity
                price = self.Securities[symbol].Price 
                lower = self.bb[ticker].LowerBand.Current.Value
                upper = self.bb[ticker].UpperBand.Current.Value
                middle = self.bb[ticker].MiddleBand.Current.Value
                self.Debug(price)
                
                # if already long - liquidate if >= mean 
                if (self.states[ticker] == 1) and (price >= middle):
                    self.Liquidate(ticker)
                    self.states[ticker] = 0
    
                # if already short - liquidate if <=mean
                if (self.states[ticker] == -1) and (price <= middle):
                    self.Liquidate(ticker)
                    self.states[ticker] = 0
                    
                # if close is below lower band go long 
                if price <= lower:
                    # and not already holding or short 
                    if self.states[ticker] <= 0:
                        self.Liquidate(ticker)
                        self.SetHoldings(ticker, 1)
                        self.states[ticker] = 1 
                
                # if close is abover upper band go short 
                if price >= upper:
                    # and not already holding or long 
                    if self.states[ticker] >= 0:
                        self.Liquidate(ticker)
                        self.SetHoldings(ticker, -1)
                        self.states[ticker] = -1