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
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
import numpy as np
import decimal
from datetime import datetime
### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2018,1,1)  #Set Start Date
        self.SetEndDate(2018,1,30)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        
        self.forex = self.AddForex("EURUSD", Resolution.Minute, Market.Oanda)
        #self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.At(10, 0), Action(self.rebalance))
        #define minute cci for scalping
        self.CommodityChannelIndex = self.CCI("EURUSD", 13, MovingAverageType.Exponential, Resolution.Minute)
        
        # define our daily macd(12,26) with a 9 day signal
        self.MovingAverageConvergenceDivergence = self.MACD("EURUSD", 13, 26, 9, MovingAverageType.Exponential, Resolution.Minute)
        
        '''fast_period = 30
        slow_period = 150
        self.fast = self.EMA("EURUSD", fast_period)
        self.slow = self.EMA("EURUSD", slow_period)
        #self.Debug("numpy test >>> print numpy.pi: " + str(np.pi)) #testing that Numpy module works'''
        
        overlayPlot = Chart("Overlay Plot")
        overlayPlot.AddSeries(Series("EURUSD", SeriesType.Line, 0))
        overlayPlot.AddSeries(Series("Buy", SeriesType.Scatter, 0))
        overlayPlot.AddSeries(Series("Sell", SeriesType.Scatter, 0))
        overlayPlot.AddSeries(Series("MACD", SeriesType.Line, 1))
        overlayPlot.AddSeries(Series("MACD_Signal", SeriesType.Line, 1))
        overlayPlot.AddSeries(Series("CCI", SeriesType.Line, 2))
        self.AddChart(overlayPlot)

    def OnData(self, data):
        
        if not self.CommodityChannelIndex.IsReady or not self.MovingAverageConvergenceDivergence.IsReady: 
            return
        
        #define a small tolerance on our checks to avoid bouncing?
        tolerance = 0.00021
        signalDeltaPercent = self.MovingAverageConvergenceDivergence.Signal.Current.Value 
        cci = self.CommodityChannelIndex.Current.Value
        #self.PlotIndicator("MACD", self.MovingAverageConvergenceDivergence, self.MovingAverageConvergenceDivergence.Signal)
        #self.PlotIndicator("CCI", self.CommodityChannelIndex)
        
        current = data["EURUSD"].Close
        buy_signal_triggered, sell_signal_triggered = False, False
    #def rebalance(self):    
        if not self.Portfolio.Invested:
            
            if cci >= 100 and signalDeltaPercent > tolerance and self.Portfolio.Invested <= 0:# and self.fast < self.slow: #downtrend
                #set stop price and limit price at +-2%
                twoPercent = decimal.Decimal(1.01)
                takeProfit = decimal.Decimal(0.98)
                #self.SetHoldings("EURUSD", -5)
                
                #SL and TP orders to protect investment
                stopPrice = self.Securities["EURUSD"].Price * twoPercent
                limitPrice = self.Securities["EURUSD"].Price * takeProfit
                #self.Debug("signalDeltaPercent is " + str(signalDeltaPercent))
                #sell shares if SL/TP is reached before cci and macd diverge
                self.StopLimitOrder("EURUSD", 500000, stopPrice, stopPrice)
                self.LimitOrder("EURUSD", -500000, limitPrice)
                sell_signal_triggered = True
            if cci <= -100 and signalDeltaPercent < -tolerance and self.Portfolio.Invested <= 0:
                #if self.fast > self.slow: #uptrend
                    #set stop price and limit price at +-2%
                    twoPercent = decimal.Decimal(0.99)
                    takeProfit = decimal.Decimal(1.02)
                    #self.SetHoldings("EURUSD", 5)
                    
                    #SL and TP orders to protect investment
                    stopPrice = self.Securities["EURUSD"].Price * twoPercent
                    limitPrice = self.Securities["EURUSD"].Price * takeProfit
                    #self.Debug("signalDeltaPercent is " + str(signalDeltaPercent))
                    #sell shares if SL/TP is reached before cci and macd diverge
                    self.StopLimitOrder("EURUSD", -500000, stopPrice, stopPrice)
                    self.LimitOrder("EURUSD", 500000, limitPrice)
                    buy_signal_triggered = True
              
        if self.Portfolio.Invested:       
            if self.Portfolio["EURUSD"].IsLong:
                if cci >= 50 and signalDeltaPercent > tolerance:
                    
                    self.Liquidate("EURUSD")
                    
            if self.Portfolio["EURUSD"].IsShort:
                if cci <= -50 and signalDeltaPercent < -tolerance:
                    
                    self.Liquidate("EURUSD")
        if buy_signal_triggered:
                self.Plot("Overlay Plot", "Buy", current)
        elif sell_signal_triggered:
            self.Plot("Overlay Plot", "Sell", current)
        self.Plot("Overlay Plot", "EURUSD", current)
        
        self.Plot("Overlay Plot", "MACD", self.MovingAverageConvergenceDivergence.Current.Value)
        
        self.Plot("Overlay Plot", "CCI", self.CommodityChannelIndex)
        
        self.Plot("Overlay Plot", "MACD_Signal", self.MovingAverageConvergenceDivergence.Signal.Current.Value)   
        
    '''def OnEndofDay(self):
            self.Plot("EURUSD", current)
        self.Plot("EMA", self.fast, self.slow)
        self.Plot("CCI", self.CommodityChannelIndex)
        self.Plot("MACD", self.MovingAverageConvergenceDivergence, self.MovingAverageConvergenceDivergence.Signal)'''