Overall Statistics Total Trades231Average Win2.50%Average Loss-3.37%Compounding Annual Return19.846%Drawdown29.200%Expectancy0.119Net Profit43.702%Sharpe Ratio0.603Probabilistic Sharpe Ratio25.025%Loss Rate36%Win Rate64%Profit-Loss Ratio0.74Alpha0.159Beta0.026Annual Standard Deviation0.267Annual Variance0.071Information Ratio0.265Tracking Error0.29Treynor Ratio6.147Total Fees\$0.00
import numpy as np
### <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):
self.SetStartDate(2017,7, 31)  #Set Start Date
self.SetEndDate(2019,7,31)    #Set End Date
self.SetCash(5000)           #Set Strategy Cash

#This algorithm trades EURGBP on the Hour Resolution
self.SetBrokerageModel(BrokerageName.OandaBrokerage)

#We add our RSI 14 period indicator
self.rsi = self.RSI("EURGBP", 14)

#pointer to keep track of last bar's RSI Value
self.lastrsi = None

def OnData(self, data):
#Make sure our indicator is ready before we can use it
return

#Make sure there is a past RSI value to compare to
if self.lastrsi is None:
self.lastrsi = self.rsi.Current.Value
return

#If we cross oversold threshold from below
if self.lastrsi < 30 and self.rsi.Current.Value > 30:

#if we are not currently in a trade
if not self.Portfolio["EURGBP"].Invested:
#we place a long market order
self.SetHoldings("EURGBP", 5)

#If RSI is oversold while we are short
elif self.rsi.Current.Value < 30 and self.Portfolio["EURGBP"].IsShort:
# if we are already in a short trade we liquidate
self.Liquidate()

#if RSI signals overbought
if self.lastrsi > 70 and self.rsi.Current.Value < 70:

if not self.Portfolio["EURGBP"].Invested:
#enter short position
self.SetHoldings("EURGBP", -5)

#if RSI is overbought while we are long
elif self.rsi.Current.Value > 70 and self.Portfolio["EURGBP"].IsLong:
#if we already in a long trade we liquidate
self.Liquidate()

#store current RSI value to use later
self.lastrsi = self.rsi.Current.Value