Overall Statistics |
Total Trades 1 Average Win 44.8% Average Loss 0% Compounding Annual Return 22.137% Drawdown 9.300% Expectancy 0 Net Profit 44.799% Sharpe Ratio 1.854 Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha 0.221 Beta -0.079 Annual Standard Deviation 0.111 Annual Variance 0.012 Information Ratio 0.019 Tracking Error 0.161 Treynor Ratio -2.605 |
using System; using System.Collections; using System.Collections.Generic; using System.Collections.Concurrent; using System.Text; using System.Linq; using QuantConnect.Models; namespace QuantConnect { /* * ATR Indicator (v. 1.0). Implementatation by Tadas Talaikis. * * Initial code by QC. Online calculator for faster backtesting. * * To use this indicator: * * 1. Create an instance of it in your algorithm: * AverageTrueRange atr = new AverageTrueRange(10, 1); // 1 - period, 2 - coefficient * * 2. Push in data with AddSample: * decimal atrValue = atr.AddSample(data["SPY"].Close); * * 3. If you're sensitive to the precise SMA values you push wait until the indicator is Ready: * if(!atr.Ready) return; */ public class AverageTrueRange { //Class Variables: private int period, samples, dev; private decimal atr, sum, divisor; private FixedSizedQueue<decimal> sampleQueue; // Initialise the Simple Moving Average public AverageTrueRange(int period, int dev) { this.period = period; this.dev = dev; this.samples = 0; this.sampleQueue = new FixedSizedQueue<decimal>(period); } //Public Result Access: Current value of the SMA. public decimal ATR { get{ return atr; } } //Public Result Access: Track the number of samples: public int Samples { get { return samples; } } //Public Result Access: We've got sufficient data samples to know its the SMA. public bool Ready { get { return samples >= period; } } // Online implementation of simple moving average public decimal AddSample(TradeBar quote) { samples++; if(quote.Low != 0) { sum += Math.Abs(quote.High - quote.Low); } else { sum += Math.Abs(quote.High - ((quote.Close+quote.Open)/2)); } //Add this sample to the SMA, subtract the previous sampleQueue.Enqueue(Math.Abs(quote.High - quote.Low)); if (sampleQueue.Size == period) { //"last" is a dequeued item: minus it from the SMA. sum -= sampleQueue.LastDequeued; } //When less than period samples, only divide by the number of samples. if (samples < period) { divisor = samples; } else { divisor = period; } atr = sum / divisor; if(dev != 0) { return dev*atr; } else { return atr; } } //Fixed length queue that dumps things off when no more space in queue. private class FixedSizedQueue<T> : ConcurrentQueue<T> { public int Size { get; private set; } public T LastDequeued { get; private set; } public FixedSizedQueue(int size) { Size = size; } public new void Enqueue(T obj) { base.Enqueue(obj); lock (this) { if (base.Count > Size) { T outObj; base.TryDequeue(out outObj); LastDequeued = outObj; } } } } } }
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; namespace QuantConnect { // Name your algorithm class anything, as long as it inherits QCAlgorithm public class XT3 : QCAlgorithm { SimpleMovingAverage sma = new SimpleMovingAverage(10); AverageTrueRange atr = new AverageTrueRange(10,2); StDev std = new StDev(10, 2); // Inputs: 1 - period, 2 - deviation //--------------------------------------------------------------------- INITIALIZE public override void Initialize() { SetStartDate(2013, 1, 1); SetEndDate(DateTime.Now.Date.AddDays(-1)); SetCash(25000); AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute, true, 1, false); } //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol. public void OnData(TradeBars data) { decimal smaVal = sma.AddSample(data["SPY"].Close); decimal atrVal = atr.AddSample(data["SPY"]); decimal stdVal = std.AddSample(data["SPY"].Close, sma); if(!atr.Ready || !sma.Ready) return; decimal upband = smaVal + stdVal; decimal downband = smaVal - stdVal; Plot("Indicators", "SMA", smaVal); Plot("Indicators", "UpBand", upband); Plot("Indicators", "DnBand", downband); Plot("Indicators", "Price", data["SPY"].Close); //Plot("Indicators", "SMA", smaVal); if (!Portfolio.HoldStock) { Order("SPY", (int)Math.Floor(Portfolio.Cash / data["SPY"].Close) ); //Debug("Debug Purchased SPY"); } } } }
using System; using System.Collections; using System.Collections.Generic; using System.Collections.Concurrent; using System.Text; using System.Linq; using QuantConnect.Models; namespace QuantConnect { /* * SMA Indicator: Online calculator for faster backtesting. * * To use this indicator: * * 1. Create an instance of it in your algorithm: * SimpleMovingAverage sma = new SimpleMovingAverage(10); // period * * 2. Push in data with AddSample: * decimal smaValue = sma.AddSample(data["SPY"].Close); * * 3. If you're sensitive to the precise SMA values you push wait until the indicator is Ready. * if(!sma.Ready) return; */ public class SimpleMovingAverage { //Class Variables: private int period, samples; private decimal sma, sum, divisor; private FixedSizedQueue<decimal> sampleQueue; // Initialise the Simple Moving Average public SimpleMovingAverage(int period) { this.period = period; this.samples = 0; this.sampleQueue = new FixedSizedQueue<decimal>(period); } //Public Result Access: Current value of the SMA. public decimal SMA { get{ return sma;} } //Public Result Access: Track the number of samples: public int Samples { get { return samples; } } //Public Result Access: We've got sufficient data samples to know its the SMA. public bool Ready { get { return samples >= period; } } // Online implementation of simple moving average public decimal AddSample(decimal quote) { samples++; sum += quote; //Add this sample to the SMA, subtract the previous sampleQueue.Enqueue(quote); if (sampleQueue.Size == period) { //"last" is a dequeued item: minus it from the SMA. sum -= sampleQueue.LastDequeued; } //When less than period samples, only divide by the number of samples. if (samples < period) { divisor = samples; } else { divisor = period; } sma = sum / divisor; return sma; } //Fixed length queue that dumps things off when no more space in queue. private class FixedSizedQueue<T> : ConcurrentQueue<T> { public int Size { get; private set; } public T LastDequeued { get; private set; } public FixedSizedQueue(int size) { Size = size; } public new void Enqueue(T obj) { base.Enqueue(obj); lock (this) { if (base.Count > Size) { T outObj; base.TryDequeue(out outObj); LastDequeued = outObj; } } } } } }
using System; using System.Collections; using System.Collections.Generic; using System.Collections.Concurrent; using System.Text; using System.Linq; using QuantConnect.Models; namespace QuantConnect { /* * STANDARD DEVIATION Indicator (v. 1.0). Implementation by Tadas Talaikis. * * Initialcode by QC. Online calculator for faster backtesting. * * To use this indicator: * * 1. Import Simple Moving Average clas from code library. * * 2. Create an instance of it in your algorithm: * StDev std = new StDev(10, 2); // Inputs: 1 - period, 2 - deviation * * 3. Push in data with AddSample: * decimal stdValue = std.AddSample(data["SPY"].Close, sma); // Inputs: 1 - input data, 2 - sma instance * * 4. If you're sensitive to the precise SMA values you push wait until the indicator is Ready. * if(!std.Ready) return; */ public class StDev { //Class Variables: private int period, samples, dev; private decimal sum, divisor; private double std; private FixedSizedQueue<decimal> sampleQueue; // Initialise the Simple Moving Average public StDev(int period, int dev) { this.period = period; this.dev = dev; this.samples = 0; this.sampleQueue = new FixedSizedQueue<decimal>(period); } //Public Result Access: Current value of the SMA. public double STDEV { get{ return std; } } //Public Result Access: Track the number of samples: public int Samples { get { return samples; } } //Public Result Access: We've got sufficient data samples to know its the SMA. public bool Ready { get { return samples >= period; } } // Online implementation of simple moving average public decimal AddSample(decimal quote, SimpleMovingAverage sma) { samples++; sum += Math.Abs(sma.SMA - quote)*Math.Abs(sma.SMA - quote); //Add this sample to the SMA, subtract the previous sampleQueue.Enqueue((Math.Abs(sma.SMA - quote)*Math.Abs(sma.SMA - quote))); if (sampleQueue.Size == period) { //"last" is a dequeued item: minus it from the SMA. sum -= sampleQueue.LastDequeued; } //When less than period samples, only divide by the number of samples. if (samples < period) { divisor = samples; } else { divisor = period; } std = (double)(sum/divisor); if(dev != 0) { return (decimal)(dev * Math.Sqrt(std)); } else { return (decimal)Math.Sqrt(std); } } //Fixed length queue that dumps things off when no more space in queue. private class FixedSizedQueue<T> : ConcurrentQueue<T> { public int Size { get; private set; } public T LastDequeued { get; private set; } public FixedSizedQueue(int size) { Size = size; } public new void Enqueue(T obj) { base.Enqueue(obj); lock (this) { if (base.Count > Size) { T outObj; base.TryDequeue(out outObj); LastDequeued = outObj; } } } } } }