My first attempt to make something for QC that I needed myself :) Here you'll find simple implementation for: 1) ATR. 2) ATR Channels. 3) Standard deviation. 4) Bolinger bands. SMA is required for StDev and Bolinger. SMA description is corrected a bit from cod library version's.
Jared Broad
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Tadas Talaikis
Tadas Talaikis
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 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(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)
{
decimal smaVal = sma.AddSample(quote);
samples++;
sum += Math.Abs(smaVal - quote)*Math.Abs(smaVal - 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 : ConcurrentQueue {
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;
}
}
}
}
}
}
Jared Broad
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Tadas Talaikis
decimal smaVal = sma.AddSample(quote);
Well, will see what I'll use as I have so much work I can't give enoug time to consider some exact features, heh :) I'm on the side "try everything and see what works", haha, just came here to test one simple idea I discovered when trying to create regime switcher. My workflow seems will be, 1) discovery on desktop framework, 2) going here.Jared Broad
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Tadas Talaikis
MaverickChoo
Tadas Talaikis
Michael H
Tadas Talaikis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!