Overall Statistics Total Trades784Average Win0.20%Average Loss-0.12%Compounding Annual Return2.830%Drawdown2.500%Expectancy0.062Net Profit2.830%Sharpe Ratio0.869Loss Rate60%Win Rate40%Profit-Loss Ratio1.69Alpha0.021Beta0.103Annual Standard Deviation0.026Annual Variance0.001Information Ratio0.256Tracking Error0.026Treynor Ratio0.222Total Fees\$0.00
```import pandas
import numpy as np
### <summary>
### Simple RSI Strategy intended to provide a minimal algorithm example using
### one indicator
### </summary>
class RSIAlgorithm(QCAlgorithm):

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.'''

# Set our main strategy parameters
self.SetStartDate(2013,1, 1)   # Set Start Date
self.SetEndDate(2014,1,1)      # Set End Date
self.SetCash(10000)            # Set Strategy Cash

RSI_Period    = 14                # RSI Look back period
self.RSI_OB   = 60                # RSI Overbought level
self.RSI_OS   = 40                # RSI Oversold level
self.Allocate = 2              # Percentage of captital to allocate
self.tolerance = 0.00015
self.first_stg_up = 0
self.first_stg_down = 0
self.trend = 0
self.trend_n = 0

self.symbol = "EURUSD"

## Rename variables for percentages to make more readable and less confusing
self.tpPercent = 20/10000
self.slPercent = 10/10000

## Reassign these to be variables for Order Tickets
self.tp = None
self.sl = None
self.holdings = 0
self.quant = 10000

# Find more symbols here: http://quantconnect.com/data

self.RSI_Ind = self.RSI("EURUSD", RSI_Period)
self.bb_ind = self.BB("EURUSD", 20, 1, MovingAverageType.Simple);
self.slow = self.SMA("EURUSD", 20, Resolution.Hour)
self.fast = self.SMA("EURUSD", 7, Resolution.Hour)

# Ensure that the Indicator has enough data before trading,.
self.SetWarmUp(20)

def OnData(self, data):

trend_sma = np.where(self.fast.Current.Value > self.slow.Current.Value,1,0)
# Check if we are in the market
fxOpen = data[self.symbol].Open
fxClose = data[self.symbol].Close
price = data[self.symbol].Price
if trend_sma == 1 and fxClose > fxOpen and fxClose >= self.bb_ind.UpperBand.Current.Value:
self.trend = 1

if trend_sma == 0 and fxClose < fxOpen and fxClose <= self.bb_ind.LowerBand.Current.Value :
self.trend = -1

self.first_stg_up = 0
self.first_stg_down = 0
self.trend = 0
self.trend_n = 0

if not self.Portfolio.Invested and not self.IsWarmingUp:
# If not, we check the RSI Indicator
if self.trend == 1 :
#
## you need to assign to self.order or else variable will remain local
self.order = self.MarketOrder(self.symbol, self.quant)

## you need to define fxClose or else the orders won't be properly placed
fxClose = data[self.symbol].Close

## you need to replace sl with self.sl, as you want these to be global variables
## otherwise, they will not be accessible outside of OnData
self.sl = self.StopMarketOrder(self.symbol, -self.quant, fxClose - self.slPercent)
self.tp = self.LimitOrder(self.symbol, -self.quant, fxClose + self.tpPercent)

if self.trend == -1 :
self.order = self.MarketOrder(self.symbol, -self.quant)

## you need to define fxClose or else the orders won't be properly placed
fxClose = data[self.symbol].Close

## you need to replace sl with self.sl, as you want these to be global variables
## otherwise, they will not be accessible outside of OnData
self.sl = self.StopMarketOrder(self.symbol, self.quant, fxClose + self.slPercent)
self.tp = self.LimitOrder(self.symbol, self.quant, fxClose - self.tpPercent)

def OnOrderEvent(self, orderEvent):
self.Log(orderEvent)
## This will check for the boolean value of whether or not the order has been filled
if not (orderEvent.Status == OrderStatus.Filled):
return

## python doesn't support null. Instead, check for None
if (self.tp is None) or (self.sl is None):
return

filledOrderId = orderEvent.OrderId

if self.tp.OrderId == filledOrderId:
self.Liquidate()

if self.sl.OrderId == filledOrderId:
self.Liquidate()```