MaxBarsBack in Signal problem  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
Sylpha
Posts: 29
Joined: 14 Apr 2015
Has thanked: 1 time

MaxBarsBack in Signal problem

Postby Sylpha » 23 Jun 2015

A bit driving me nuts...

I tried to code some very simple signal using my own indicator as below:

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy {
public class Jeffrey_SimpleHMADailyLE : SignalObject {
public Jeffrey_SimpleHMADailyLE(object _ctx):base(_ctx){}

private int _Period;

[Input]
public int Period
{
get { return this._Period; }
set {

if (value<1) {
this._Period = 1;
} else {
this._Period = value;
}
}
}

private IOrderMarket buy_order;
private IOrderMarket sell_order;

private PowerLanguage.Indicator.Jeffrey_ATR_v2 atr;

private PowerLanguage.Indicator.Jeffrey_HMA hma;

protected override void Create() {
// create variable objects, function objects, order objects etc.

// create variable objects, function objects, order objects etc.
buy_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, EOrderAction.Buy));
sell_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, EOrderAction.Sell));

/*
atr = (PowerLanguage.Indicator.Jeffrey_ATR_v2)AddIndicator("Jeffrey_ATR_v2");
atr.Mode=1;
atr.Period=Period;
*/


hma = (PowerLanguage.Indicator.Jeffrey_HMA)AddIndicator("Jeffrey_HMA");
hma.Period=Period;


}
protected override void StartCalc() {
// assign inputs

#if DEBUG
Output.Clear();
Output.WriteLine("{0} StartCalc()", this.GetType().ToString());
Output.WriteLine("[{0}] CurrentBar={1} CurrentBarAbsolute+1={2} MaxBarsBack={3} Period={4} Close[0]={5}",
Bars.Time[0].ToString("yyyy/MM/dd HH:mm:ss"),
Bars.CurrentBar,
Bars.CurrentBarAbsolute()+1,
ExecInfo.MaxBarsBack,
Period,
Bars.Close[0]);
#endif



#if DEBUG
Output.WriteLine("StartCalc()");
Output.WriteLine("");
#endif
}
protected override void CalcBar(){
// strategy logic


#if DEBUG
Output.WriteLine("CalcBar()");
Output.WriteLine("[{0}] CurrentBar={1} CurrentBarAbsolute+1={2}",
Bars.Time[0].ToString("yyyy/MM/dd HH:mm:ss"),
Bars.CurrentBar,
Bars.CurrentBarAbsolute()+1);
#endif

bool entryCondition1 = hma.Plots[0].Values[0] > hma.Plots[0].Values[1];
bool exitCondition1 = hma.Plots[0].Values[0] < hma.Plots[0].Values[1];


if (entryCondition1) {
buy_order.Send();
}

if (exitCondition1) {
sell_order.Send();
}

#if DEBUG
Output.WriteLine("CalcBar()");
Output.WriteLine("");
#endif

}
}
}
I have set the period to 10 and maxbarsSetting in the properties to 500. However I still get the error saying I am referencing the bars outside the range.

It is totally fine if I use my own HMA indicator as study since I will set the period to 2 as minimum programmatically. Not sure why it become so trouble when it is referenced inside a strategy.
Attachments
Screen Shot 2015-06-24 at 11.39.06 am.jpg
(129.61 KiB) Downloaded 854 times
Last edited by Sylpha on 23 Jun 2015, edited 1 time in total.

Sylpha
Posts: 29
Joined: 14 Apr 2015
Has thanked: 1 time

Re: MaxBarsBack in Signal problem

Postby Sylpha » 23 Jun 2015

Error message. Why does it complaint (1) bar only?
Screen Shot 2015-06-24 at 11.39.22 am.jpg
(25.24 KiB) Downloaded 845 times

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: MaxBarsBack in Signal problem

Postby Henry MultiСharts » 24 Jun 2015

Hello Sylpha,

This study should not generate this error. Most likely the reason is in the studies you are accessing via AddIndicator.

Sylpha
Posts: 29
Joined: 14 Apr 2015
Has thanked: 1 time

Re: MaxBarsBack in Signal problem

Postby Sylpha » 25 Jun 2015

Hi Henry,

I have attached the workspace, indicator and the signal. Please have a look and advise since I don't even try to read the values so I am not sure why the AddIndicator call will throw errors....
Attachments
export.zip
(32.33 KiB) Downloaded 257 times

Sylpha
Posts: 29
Joined: 14 Apr 2015
Has thanked: 1 time

Re: MaxBarsBack in Signal problem

Postby Sylpha » 25 Jun 2015

I think I nail down the problem....

If I comment out the Execlnfo.MaxBarsBack = Period-1 then it is fine....but why?

The strategy's maxbarsback itself is 500, which is so much longer than the period pass into the indicator already.

Code: Select all

protected override void StartCalc() {
// assign inputs


//ExecInfo.MaxBarsBack=Period-1;
.........
}

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: MaxBarsBack in Signal problem  [SOLVED]

Postby Henry MultiСharts » 29 Jun 2015

Hello Sylpha,

MaxBarsBack for signal cannot be changed from the code. You are trying to do that implicitly by changing the value from the code of the indicator that is being called from the signal. That is why when you have removed the MBB adjustment from the indicator code you got it working.


Return to “MultiCharts .NET”