Back

ATR/ ATR Channels/ StDev/ Bolinger Indicators

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.

Update Backtest








Awesome @Tadas! Did you test the standard deviation with a benchmark/reference library? I'd be interested in making sure your online method lines up. It seems like a nice efficient, windowed method. The Math.Numerics library has standard deviation calculators but they are not online/windowed.
0

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.


Not yet tested thoroughly, but should work OK :)
0

I'm back and found an error on StDev, haha :) Here's fixed:


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

}
}
3

Nice work Tadas. What was the fix? I can't tell from the two files. I gave up trying to do it online and used the RunningStatistics class for my tests; like this -

using MathNet.Numerics.Statistics;
var stats = new RunningStatistics( queue.ToList() )
stats.StandardDeviation


Its pretty fast -- a single pass calculation for each new point. But not as fast as online :)

We're going to rework how the indicators work so the user doesn't need to add data. It will automatically subscribe for data updates when created. I'll loop you in once we start the work :)
0

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.


this line added:

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.
0

Interesting, what desktop framework do you use? Hopefully the open source engine can replace your desktop framework then you'll have a streamlined process.
0

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.


MT4 actually, but it is just for easier charting and displaying of math formulas on the same chart, nothing more.
0

Win rate of 100% and yet the drawdown is 9.3%. What is the possibility/probability that the win rate will remain 100% while the drawdown reaches 20%, for example?
0

This backtests does not show any useful strategy, it's just buy and hold on currently existing one stock market. Possibility of keeping that over long run is 0 ;)
0

I think he was showcasing the pieces that compute/produce pretty graphs :)
0

Update Backtest





0

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.


Loading...

This discussion is closed