Overall Statistics Total Trades1Average Win44.8%Average Loss0%Compounding Annual Return22.137%Drawdown9.300%Expectancy0Net Profit44.799%Sharpe Ratio1.854Loss Rate0%Win Rate100%Profit-Loss Ratio0Alpha0.221Beta-0.079Annual Standard Deviation0.111Annual Variance0.012Information Ratio0.019Tracking Error0.161Treynor 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:
*
*   3. If you're sensitive to the precise SMA values you push wait until the indicator is Ready:
*/

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.
get { return samples >= period; }
}

// Online implementation of simple moving average
{
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);
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.
{

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:
*
*   3. If you're sensitive to the precise SMA values you push wait until the indicator is Ready.
*/

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.
get { return samples >= period; }
}

// Online implementation of simple moving average
{
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.
*/

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.
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;
}
}
}
}

}
}```