Overall Statistics Total Trades181Average Win0.85%Average Loss-1.83%Compounding Annual Return-90.992%Drawdown21.600%Expectancy-0.105Net Profit-17.950%Sharpe Ratio-4.601Loss Rate39%Win Rate61%Profit-Loss Ratio0.46Alpha-2.538Beta50.234Annual Standard Deviation0.384Annual Variance0.148Information Ratio-4.641Tracking Error0.384Treynor Ratio-0.035Total 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")

def OnData(self, data):

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)

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")
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", float(self.MovingAverageConvergenceDivergence.Current.Value))

self.Plot("Overlay Plot", "CCI", self.CommodityChannelIndex.Current.Value)

self.Plot("Overlay Plot", "MACD_Signal", float(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)'''```