| Overall Statistics |
|
Total Trades 2 Average Win 0% Average Loss -4.12% Compounding Annual Return -1.394% Drawdown 5.800% Expectancy -1 Net Profit -4.120% Sharpe Ratio -0.782 Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.027 Beta 0.964 Annual Standard Deviation 0.014 Annual Variance 0 Information Ratio -1.906 Tracking Error 0.014 Treynor Ratio -0.012 Total Fees $0.00 |
import numpy as np
from System.Drawing import Color
class RSIAlgorithm(QCAlgorithm):
def Initialize(self):
# Set our main strategy parameters
self.SetStartDate(2015,1, 1) # Set Start Date
self.SetEndDate(2018,1,1) # Set End Date
self.SetCash(10000) # Set Strategy Cash
RSI_Period = 14 # RSI Look back period
self.RSI_OB = 80 # RSI Overbought level
self.RSI_OS = 20 # RSI Oversold level
self.RSI_MID = 50 # RSI Mid level
self.Allocate = 50 # Percentage of captital to allocate
self.pip = 0.0001
# Find more symbols here: http://quantconnect.com/data
self.AddForex("EURUSD", Resolution.Daily,Leverage=1)
self.RSI_Ind = self.RSI("EURUSD", RSI_Period)
# Ensure that the Indicator has enough data before trading,.
self.SetWarmUp(RSI_Period)
# Construct "Trade Plot"
IndicatorPlot = Chart("Trade Plot")
IndicatorPlot.AddSeries(Series("RSI", SeriesType.Line,"", Color.Black))
IndicatorPlot.AddSeries(Series("Over Bought", SeriesType.Line,"", Color.Black))
IndicatorPlot.AddSeries(Series("Mid", SeriesType.Line, "", Color.Black))
IndicatorPlot.AddSeries(Series("Over Sold", SeriesType.Line, "", Color.Black))
IndicatorPlot.AddSeries(Series("Buy", SeriesType.Scatter,"", Color.Green))
IndicatorPlot.AddSeries(Series("Sell", SeriesType.Scatter, "", Color.Red))
self.AddChart(IndicatorPlot)
def OnData(self, data):
if not self.RSI_Ind.IsReady: return
self.Plot("Trade Plot", "RSI", self.RSI_Ind.Current.Value)
self.Plot("Trade Plot", "Over Bought", self.RSI_OB)
self.Plot("Trade Plot", "Over Sold", self.RSI_OS)
self.Plot("Trade Plot", "Mid", self.RSI_MID)
rate = data["EURUSD"].Close
# Open position
if not self.Portfolio.Invested:
# Target quantity is 50% of portfolio value
quantity = self.CalculateOrderQuantity("EURUSD",self.Allocate/100)
# Long EURUSD
if self.RSI_Ind.Current.Value < self.RSI_OS:
# Buy Stop Market Order with a Stop-Loss
self.StopMarketOrder("EURUSD", quantity, rate - self.pip*100)
self.Plot("Trade Plot", "Buy", self.RSI_Ind.Current.Value)
# Short EURUSD
elif self.RSI_Ind.Current.Value > self.RSI_OB:
# Sell Stop Market Order with a Stop-Loss
self.StopMarketOrder("EURUSD", -quantity, rate + self.pip*100)
self.Plot("Trade Plot", "Sell", self.RSI_Ind.Current.Value)
# Close position
if self.Portfolio.Invested:
if self.Portfolio["EURUSD"].Quantity > 0:
if self.RSI_Ind.Current.Value > self.RSI_MID:
self.Liquidate("EURUSD")
self.Plot("Trade Plot", "Sell", self.RSI_Ind.Current.Value)
else:
if self.RSI_Ind.Current.Value < self.RSI_MID:
self.Liquidate("EURUSD")
self.Plot("Trade Plot", "Buy", self.RSI_Ind.Current.Value)