Overall Statistics Total Trades8Average Win3.93%Average Loss0%Compounding Annual Return22.468%Drawdown2.900%Expectancy0Net Profit16.563%Sharpe Ratio2.153Loss Rate0%Win Rate100%Profit-Loss Ratio0Alpha0.124Beta2.613Annual Standard Deviation0.077Annual Variance0.006Information Ratio1.944Tracking Error0.077Treynor Ratio0.064Total Fees\$0.00
```import decimal as d
from datetime import timedelta

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(2017, 1, 1)    #Set Start Date
self.SetEndDate(2017, 10, 1)      #Set End Date
self.SetCash(100000)             #Set Strategy Cash

# create a 13 day exponential moving average
self.fast = self.EMA("XAUUSD", 5, Resolution.Daily)

# create a 48 day exponential moving average
self.slow = self.EMA("XAUUSD", 20, Resolution.Daily)

self.previous = None

self.SetWarmUp(timedelta(days=20))

def OnData(self, data):

# wait for our slow ema to fully initialize
if self.IsWarmingUp: return

# only once per day
if self.previous is not None and self.previous.date() == self.Time.date():
return

# define a small tolerance on our checks to avoid bouncing
tolerance = 0.00015
holdings = self.Portfolio["XAUUSD"].Quantity

# we only want to go long if we're currently short or flat
if holdings <= 0:
# if the fast is greater than the slow, we'll go long
if self.fast.Current.Value > self.slow.Current.Value * d.Decimal(1 + tolerance):
self.SetHoldings("XAUUSD", 1.0)

# we only want to liquidate if we're currently long
# if the fast is less than the slow we'll liquidate our long
if holdings > 0 and self.fast.Current.Value < self.slow.Current.Value:
self.Liquidate("XAUUSD")

self.previous = self.Time```