Back

Custom Indicator Problem - Bandpass Filter

Hello, I have tried to implement the Bandpass Filter using c#. It is very simple in thoeory. The code on Tradingview is also just 1 line basically.

Problem: The code is compiling fine, but the values go to +/- infinity after a couple of datapoints...

The values should normaly stay between +- 0.1 at max

I simply cant find the reason why this happens.

Please help! Thanks

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.


/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using QuantConnect.Data;
using QuantConnect.Data.Market;
using System;



namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Algorithm demonstrating FOREX asset types and requesting history on them in bulk. As FOREX uses
/// QuoteBars you should request slices or QuoteBars


public class MainAlgo : QCAlgorithm
{
readonly static int slen = 60;
readonly static int flen = 40;
readonly decimal a1 = 5m / (decimal)flen;
readonly decimal a2 = 5m / (decimal)slen;

private RollingWindow<decimal> close;
private RollingWindow<decimal> _PB;

int count = 0;


public override void Initialize()
{

SetStartDate(2020, 5, 8);
SetEndDate(2020, 5, 15);
SetCash(100000);

AddForex("EURUSD", Resolution.Hour, Market.Oanda);

close = new RollingWindow<decimal>(3);
_PB = new RollingWindow<decimal>(3);
_PB.Add(0);
_PB.Add(0);
_PB.Add(0);

var _chart = new Chart("PB Chart");

AddChart(_chart);

}

public override void OnData(Slice data)
{


close.Add(data["EURUSD"].Close);


if (!close.IsReady){
return;
}


_PB.Add((a1 - a2) * close[0] + (a2*(1 - a1) - a1 * (1 - a2))* close[1] + ((1 - a1) + (1 - a2))*_PB[1] - (1 - a1)* (1 - a2)*_PB[2]);

Plot("PB Chart", "PB", _PB[0]);

if (!Portfolio.Invested)
{
Console.WriteLine("TEST" + data["EURUSD"].Close);
MarketOrder("EURUSD", 1000);
}

count++;
}
}
}

 

0

Hi PuchaFX,

 

I added some Debug statements to your algorithm to see which parts of your equation

(a1 - a2) * close[0] + (a2*(1 - a1) - a1 * (1 - a2))* close[1] + ((1 - a1) + (1 - a2))*_PB[1] - (1 - a1)* (1 - a2)*_PB[2]

diverge.

It seems that this section diverges.

((1 - a1) + (1 - a2))*_PB[1] - (1 - a1)* (1 - a2)*_PB[2]

It is possible that the recursive formula was translated incorrectly. Do you mind posting the original formula?

Best
Rahul

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.


Thanks foryour answer. The orignial code and idea is from trading view. There are also a couple examples in .mql5 format ffor metatrader online. 

 

Its written in Pinescript from tradingview.

//@version=2
//plot by marco
//The Super Passband Filter by John Ehlers
//Buy on the filter crossing above its -RMS line
//Short on the filter crossing below its RMS line
//Exit long when the filter either crosses below its RMS or crosses below -RMS (which signifies a false entry signal)
//Cover short when the filter either crosses above its -RMS or crosses above RMS (which signifies a false entry signal)

study("super bandpass filter",overlay=false)
src=input(close,title="source")
bbs=input(true,"show background colors ?" )
flen=input(40)
slen=input(60)
a1= 5/flen
a2= 5/slen
PB = (a1 - a2) * close + (a2*(1 - a1) - a1 * (1 - a2))* close[1] + ((1 - a1) + (1 - a2))*nz(PB[1])- (1 - a1)* (1 - a2)*nz(PB[2])
RMSa= sum( PB*PB, 50)
RMS= sqrt(RMSa/50)
rms=-RMS
con=crossover(PB,rms) and bbs ?lime: crossunder(PB,RMS) and bbs?red:na
bgcolor(con,transp=80)
plot(PB,title="Pass Band",color=red,linewidth=2)
m=plot(RMS,title="RMS+",color=blue,linewidth=1)
s=plot(rms,title="RMS-",color=lime,linewidth=1)
fill(m,s,color=silver,transp=90)
//sbfa=nz(sbfa[1])*pow((slen-flen)+flen,2)
//sbfma=nz(sbfma[1])+sbfa*(src-nz(sbfma[1]))
//plot(sbfma,color=red,linewidth=2,title=" super bandpass filter MA")

0

Looks like you are missing the nz variable. What does nz mean?  

Original formula:

(1 - a1)* (1 - a2)*nz(PB[2])

You have :

decimal term4 = (1 - a1)* (1 - a2)*_PB[2];THere is no nz here.
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