Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *

import numpy as np
from datetime import timedelta, datetime

class RegressionChannelAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetCash(10000000)
        self.SetStartDate(2015,1,6)
        #self.SetEndDate(2019,11,18)
        self.symbols = ["EURUSD", "GBPUSD", "USDCHF", "EURGBP"] 
        slowperiod = 14
        
        self.SetWarmUp(slowperiod)
        for symbol in self.symbols:
            equity = self.AddForex(symbol, Resolution.Daily, Market.Oanda)
            self.Consolidate(symbol, timedelta(days=7), self.twoforty)
        self._rc0 = self.BB("EURUSD", 25, 1.15, Resolution.Daily)#as bb period goes down , std should go up 0.1
        self.ema0 = self.SMA("EURUSD", 37, Resolution.Daily)
        '''
        symbols = "EURUSD"
        history = self.History([symbols], slowperiod + 1)
        for tuple in history.loc[str(symbols)].itertuples():
            bar = QuoteBar(tuple.Index-timedelta(days=1),
               symbols,
               Bar(tuple.bidclose, tuple.bidhigh, tuple.bidlow,  tuple.bidopen),
               0,
               Bar(tuple.askclose, tuple.askhigh, tuple.asklow, tuple.askopen),
               0,
               timedelta(days=1))
            self._rc0.Update(bar)
            self.ema0.Update(bar)
        ''' 
        stockPlot1 = Chart("Trade Plot")
        stockPlot1.AddSeries(Series("Long", SeriesType.Scatter, 0))
        stockPlot1.AddSeries(Series("Short", SeriesType.Scatter, 0))
        stockPlot1.AddSeries(Series("UpperChannel", SeriesType.Line, 0))
        stockPlot1.AddSeries(Series("LowerChannel", SeriesType.Line, 0))
        stockPlot1.AddSeries(Series("ema", SeriesType.Line, 0))
        stockPlot1.AddSeries(Series("Price", SeriesType.Line, 0))
        self.AddChart(stockPlot1)
        
    def OnData(self, data):
        self.Log("what the heck is going on?")
        if (not data.ContainsKey(self.symbols[0])): return
        if (not data.ContainsKey(self.symbols[1])): return 
        self.Log("is it here???")
        if (not data.ContainsKey(self.symbols[2])): return
        if (not data.ContainsKey(self.symbols[3])): return
        self.Log("lets warm up...")
        if self.IsWarmingUp: return
        self.Log("done warming up")
        if (not self._rc0.IsReady) or (not self.ema02.IsReady): return
        self.Log("done warming up indicators.")
    def twoforty(self, consolidated):
        
        value0 = self.Securities[self.symbols[0]].Price
        
        if self.ema0.Current.Value > value0: #then the trend is up
            self.Log("trend is up let's buy")
            if not self.Portfolio[self.symbols[0]].Invested and value0 < self._rc0.LowerBand.Current.Value:
                self.SetHoldings(self.symbols[0], 4)
                #self.SetHoldings(self.symbols[1], 4)
                #self.SetHoldings(self.symbols[2], 4)
                #self.SetHoldings(self.symbols[3], 4)
                self.Plot("Trade Plot", "Long", value0)
            elif self.Portfolio[self.symbols[0]].Invested and value0 > self._rc0.UpperBand.Current.Value:
                self.SetHoldings(self.symbols[0], 0, True, "LIQUIDATED")
                #self.Plot("Trade Plot", "SellLong", value0)   
        if self.ema0.Current.Value < value0: #then the trend is down - short
            self.Log("trend is down let's buy")
            if not self.Portfolio[self.symbols[0]].Invested and value0 > self._rc0.UpperBand.Current.Value:
                self.SetHoldings(self.symbols[0], -4)
                self.Plot("Trade Plot", "Short", value0) 
            elif self.Portfolio[self.symbols[0]].Invested and value0 < self._rc0.LowerBand.Current.Value:
                self.SetHoldings(self.symbols[0], 0, True, "LIQUIDATED")
                #self.SetHoldings(self.symbols[1], 4)
                #self.SetHoldings(self.symbols[2], 4)
                #self.SetHoldings(self.symbols[3], 4)
                #self.Plot("Trade Plot", "SellShort", value0)
        self.Plot("Trade Plot", "UpperChannel", self._rc0.UpperBand.Current.Value)
        self.Plot("Trade Plot", "LowerChannel", self._rc0.LowerBand.Current.Value)
        self.Plot("Trade Plot", "ema", self.ema0.Current.Value)
        self.Plot("Trade Plot", "Price", value0)
        
    def OnOrderEvent(self, orderEvent):
        self.Log("{} {}".format(self.Time, orderEvent.ToString()))
        #self.Notify.Sms("17574091415", "{} {}".format(self.Time, orderEvent.ToString()))
    
    '''
    def OnEndOfDay(self):
        #self.Log("unrealized profit" + str(self.Portfolio.TotalUnrealizedProfit))
        #self.Log("margin used" + str(self.Portfolio.TotalMarginUsed))
    '''