Overall Statistics Total Trades291Average Win2.48%Average Loss-1.68%Compounding Annual Return-62.821%Drawdown42.600%Expectancy-0.090Net Profit-22.283%Sharpe Ratio-0.889Loss Rate63%Win Rate37%Profit-Loss Ratio1.47Alpha-0.483Beta3.184Annual Standard Deviation0.648Annual Variance0.42Information Ratio-0.865Tracking Error0.633Treynor Ratio-0.181Total Fees\$0.00
```import numpy as np
import decimal
import datetime, time
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
### <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):

def Initialize(self):
self.SetStartDate(2018,8,20)  #Set Start Date
self.SetEndDate(2018,11,20)    #Set End Date
self.SetCash(2000)           #Set Strategy Cash
self.SetBenchmark("EURUSD")

for symbol in self.symbols:

self.WilliamsPercentR = self.WILR(symbol, 20, Resolution.Hour) #7200
#fast_period = 300
slow_period = 40 #was 5/27 resolution.HF
#self.fast = self.EMA(symbol, fast_period)
self.slow = self.EMA(symbol, slow_period)

overlayPlot = Chart("Overlay Plot")

self.SetWarmup(slow_period)
self.first = True
def OnData(self, data):
for symbol in self.symbols:
return
# get the indicator value
if self.first and not self.IsWarmingUp:
self.first = False
current = data[symbol].Value

willsignal = self.WilliamsPercentR.Current.Value

if self.Portfolio["EURUSD"].Invested == False:
if willsignal > -10 and self.Securities["EURUSD"].Price > self.slow.Current.Value: #go short
self.Debug("Going SHORT, EUR invested is " + str(self.Portfolio["EURUSD"].Invested))
#self.Debug("portfolio invested is " + str(self.Portfolio.Cash))
takeProfit = decimal.Decimal(0.0200)
stoploss = decimal.Decimal(0.0037)
allocation = decimal.Decimal(5)
invested = self.Portfolio.TotalPortfolioValue * allocation
#Buy stop limit , stop price should be lower than limit price

limitPrice = self.Securities["EURUSD"].Price - takeProfit
stopLimit = self.Securities["EURUSD"].Price + stoploss

self.MarketOrder("EURUSD", -invested)
self.LimitOrder("EURUSD", invested, limitPrice)
self.StopMarketOrder("EURUSD", invested, stopLimit) #verified it works because my losses didn't exceed 2.5% a day

sell_signal_triggered = True
if self.Portfolio["GBPNZD"].Invested == False:
if willsignal > -10 and self.Securities["GBPNZD"].Price > self.slow.Current.Value: #go short
self.Debug("Going SHORT, gbp invested is " + str(self.Portfolio["GBPNZD"].Invested))

takeProfit = decimal.Decimal(0.0200)
stoploss = decimal.Decimal(0.0037)
allocation = decimal.Decimal(5)
invested = self.Portfolio.TotalPortfolioValue * allocation
#Buy stop limit , stop price should be lower than limit price

limitPrice = self.Securities["GBPNZD"].Price - takeProfit
stopLimit = self.Securities["GBPNZD"].Price + stoploss

self.MarketOrder("GBPNZD", -invested)
self.LimitOrder("GBPNZD", invested, limitPrice)
self.StopMarketOrder("GBPNZD", invested, stopLimit) #verified it works because my losses didn't exceed 2.5% a day

sell_signal_triggered = True
if self.Portfolio["EURUSD"].Invested == False:
if willsignal < -90 and self.Securities[symbol].Price < self.slow.Current.Value: #go long

self.Debug("Going Long EUR invested is " + str(self.Portfolio["EURUSD"].Invested))

takeProfit = decimal.Decimal(0.0200)
stoploss = decimal.Decimal(0.0037)
allocation = decimal.Decimal(5)
invested = self.Portfolio.TotalPortfolioValue * allocation
#Buy stop limit , stop price should be lower than limit price

limitPrice = self.Securities["EURUSD"].Price + takeProfit
stopLimit = self.Securities["EURUSD"].Price - stoploss

self.MarketOrder("EURUSD", invested)
self.LimitOrder("EURUSD", -invested, limitPrice)
self.StopMarketOrder("EURUSD", -invested, stopLimit)  #verified it works because my losses didn't exceed 2.5% a day

#self.Debug(" portfolio invested NOW is " + str(self.Portfolio.Invested))
if self.Portfolio["GBPNZD"].Invested == False:
if willsignal < -90 and self.Securities["GBPNZD"].Price < self.slow.Current.Value: #go long

self.Debug("Going Long GBP invested is " + str(self.Portfolio["GBPNZD"].Invested))

takeProfit = decimal.Decimal(0.0200)
stoploss = decimal.Decimal(0.0037)
allocation = decimal.Decimal(5)
invested = self.Portfolio.TotalPortfolioValue * allocation
#Buy stop limit , stop price should be lower than limit price

limitPrice = self.Securities["GBPNZD"].Price + takeProfit
stopLimit = self.Securities["GBPNZD"].Price - stoploss

self.MarketOrder("GBPNZD", invested)
self.LimitOrder("GBPNZD", -invested, limitPrice)
self.StopMarketOrder("GBPNZD", -invested, stopLimit)  #verified it works because my losses didn't exceed 2.5% a day

#self.Debug(" portfolio invested NOW is " + str(self.Portfolio.Invested))
if self.Portfolio["GBPNZD"].Invested == True:
if self.Portfolio["GBPNZD"].IsLong:
if willsignal > -50 and (self.Securities["GBPNZD"].Price <= self.slow.Current.Value):
self.Liquidate("GBPNZD")
self.Debug("GBPNZD liquidated is " + str(self.Portfolio["GBPNZD"].Invested))
#self.Debug("post sale cash is " + str(self.Portfolio.Cash))
if self.Portfolio["GBPNZD"].IsShort:
if willsignal < -50 and (self.Securities["GBPNZD"].Price >= self.slow.Current.Value):
self.Liquidate("GBPNZD")

if self.Portfolio["EURUSD"].Invested == True:
if self.Portfolio["EURUSD"].IsLong:
if willsignal > -50 and (self.Securities["EURUSD"].Price <= self.slow.Current.Value):
self.Liquidate("EURUSD")
self.Debug("EUR liquidated is " + str(self.Portfolio["EURUSD"].Invested))
#self.Debug("post sale cash is " + str(self.Portfolio.Cash))
if self.Portfolio["EURUSD"].IsShort:
if willsignal < -50 and (self.Securities["EURUSD"].Price >= self.slow.Current.Value):
self.Liquidate("EURUSD")

elif sell_signal_triggered:
self.Plot("Overlay Plot", "Sell", current)
self.Plot("Overlay Plot", "EURUSD", current)
self.Plot("Overlay Plot", "EMA_slow", self.slow.Current.Value)
self.Plot("Overlay Plot", "WilliamsPercentR", self.WilliamsPercentR.Current.Value)