| Overall Statistics |
|
Total Trades 193 Average Win 6.22% Average Loss -6.40% Compounding Annual Return 25.686% Drawdown 69.700% Expectancy 0.233 Net Profit 225.225% Sharpe Ratio 0.736 Probabilistic Sharpe Ratio 17.674% Loss Rate 38% Win Rate 62% Profit-Loss Ratio 0.97 Alpha 0.33 Beta -0.056 Annual Standard Deviation 0.439 Annual Variance 0.193 Information Ratio 0.409 Tracking Error 0.474 Treynor Ratio -5.791 Total Fees $0.00 |
class MovingAverageCrossAlgorithm(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.'''
self.SetStartDate(2015, 10, 20) #Set Start Date
self.SetCash(1000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.symbol = 'EURUSD'
self.positionSize = 10000.0
self.AddForex(self.symbol, Resolution.Hour)
resolution = Resolution.Hour
# create a 15 day exponential moving average
self.fast = self.EMA(self.symbol, 15, resolution)
# create a 30 day exponential moving average
self.slow = self.EMA(self.symbol, 30, resolution)
self.firstRun = True
stockPlot = Chart('Trade Plot')
# Import the necessary module before using Custom color
from System.Drawing import Color
stockPlot.AddSeries(Series('Price', SeriesType.Candle, '$', Color.Green))
stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, '$', Color.Red, ScatterMarkerSymbol.Triangle))
stockPlot.AddSeries(Series('Sell', SeriesType.Scatter, '$', Color.Blue, ScatterMarkerSymbol.TriangleDown))
stockPlot.AddSeries(Series('fastEma', SeriesType.Line, '$', Color.Orange))
stockPlot.AddSeries(Series('slowEma', SeriesType.Line, '$', Color.Black))
self.AddChart(stockPlot)
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
if not self.slow.IsReady:
return
# define a small tolerance on our checks to avoid bouncing
tolerance = 0.00015
holdings = self.Portfolio[self.symbol].Quantity
close = self.Securities[self.symbol].Close
self.emaSlowValue = self.slow.Current.Value
self.emaFastValue = self.fast.Current.Value
if self.firstRun == True:
self.isLong = self.emaFastValue < self.emaSlowValue
self.firstRun = False
self.Plot('Trade Plot', 'Price', data[self.symbol].Price)
self.Plot('Trade Plot', 'fastEma', self.emaFastValue)
self.Plot('Trade Plot', 'slowEma', self.emaSlowValue)
self.Log("Portfolio Invested: " + str(self.Portfolio.Invested))
if not self.Portfolio.Invested:
self.Log("self.fast.Current.Value > self.slow.Current.Value: " + str(self.fast.Current.Value > self.slow.Current.Value) + " IsLong: " + str(self.isLong))
if (self.fast.Current.Value > self.slow.Current.Value) and self.isLong:
self.Log("BUY >> {0}".format(self.Securities[self.symbol].Price))
self.MarketOrder(self.symbol, self.positionSize)
self.LimitOrder(self.symbol, -self.positionSize, 1.01*close)
self.StopMarketOrder(self.symbol, -self.positionSize, 0.990*close)
self.Plot('Trade Plot', 'Buy', data[self.symbol].Close)
if (self.fast.Current.Value < self.slow.Current.Value) and not self.isLong:
self.Log("SELL >> {0}".format(self.Securities[self.symbol].Price))
self.Log("Take profit ORDER PRICE >> {0}".format(0.990*close))
self.Log("Stop Loss ORDER PRICE >> {0}".format(1.01*close))
self.MarketOrder(self.symbol, -self.positionSize)
self.LimitOrder(self.symbol, self.positionSize, 0.990*close)
self.StopLimitOrder(self.symbol, self.positionSize, 1.01*close,1.011*close)
self.Plot('Trade Plot', 'Sell', data[self.symbol].Close)
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if order.Type == OrderType.Market:
self.Log("Market Order {0}: {1}: {2}: {3}".format(self.Time, order.Type, orderEvent, order.Price))
return
### Cancel remaining order if limit order or stop loss order is executed
if order.Status == OrderStatus.Filled:
self.Log("Order filled {0}: {1}: {2}".format(self.Time, order.Type, orderEvent))
if order.Type == OrderType.Limit or OrderType.StopLimit:
self.isLong = self.emaFastValue < self.emaSlowValue
self.Log(">>> IS LONG VAR: " + str(self.isLong))
self.Transactions.CancelOpenOrders(order.Symbol)
if order.Status == OrderStatus.Canceled:
self.Log("Canceled order: " +str(orderEvent))