Overall Statistics |
Total Trades 8 Average Win 3.93% Average Loss 0% Compounding Annual Return 22.468% Drawdown 2.900% Expectancy 0 Net Profit 16.563% Sharpe Ratio 2.153 Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha 0.124 Beta 2.613 Annual Standard Deviation 0.077 Annual Variance 0.006 Information Ratio 1.944 Tracking Error 0.077 Treynor Ratio 0.064 Total 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 self.AddCfd("XAUUSD", Resolution.Daily, Market.Oanda) # 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