Getting nuts with the "Tried to reference back more bars"

Questions about MultiCharts and user contributed studies.
nlaporte
Posts: 21
Joined: 16 Feb 2011
Location: Switzerland
Has thanked: 1 time
Been thanked: 1 time

Getting nuts with the "Tried to reference back more bars"

Postby nlaporte » 29 May 2011

Hello,

I am stuck again with this error message "Tried to reference back more bars (..) than allowed by the current MaxBarsBack setting".
I tried it all but I cant figure it out.
Is it my way to setup the code?
The barssinceentry could be the problem but I don't understand why?

Thanks for the help and any suggestion more than welcomed,
Best,

N.


inputs:
Length(14),
OverSold(-45),
OverBought(45);

variables:
var_CCI(0),
current_CCI_High(0),
cum_CCI(0),

open_bars_count(0),

var1(0),
var2(0);

var_CCI = CCI(Length);

// ====================================================================================================================
{ Opening/Closing Long Position }

{ Open Long: CCI crosses above the Overbought level }
condition1 = (currentBar > 1) and (MarketPosition = 0) and (var_CCI crosses above OverBought);
if condition1 then
var1 = 1
else
var1 = 0;

condition2 = (CurrentBar > 1) and (MarketPosition = 1) and (cum_CCI >= 5);
if condition2 then
var2 = 1
else
var2 =0;

{ Enter a long trade }
if (var1 = 1) then begin
Buy ("LE") 100 shares next bar at market;
current_CCI_High = var_CCI;
end;

{ Close Long: 5 down bars since entr}
if (MarketPosition = 1) then begin
cum_CCI = 0;
for open_bars_count = 0 to barssinceentry begin
if (var_CCI[open_bars_count] > current_CCI_High) then begin
cum_CCI = cum_CCI + 1;
current_CCI_High = var_CCI[open_bars_count];
end;
end;
end;

{ Close a long trade }
if (var2 = 1) and (MarketPosition = 1) then begin
Sell ("LX") 100 shares next bar at market;
end;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Getting nuts with the "Tried to reference back more bars

Postby TJ » 29 May 2011

ps. please use the code tags when posting codes.

nlaporte
Posts: 21
Joined: 16 Feb 2011
Location: Switzerland
Has thanked: 1 time
Been thanked: 1 time

Re: Getting nuts with the "Tried to reference back more bars

Postby nlaporte » 29 May 2011

Here it is
Cheers,

N.

Code: Select all

inputs:
Length(14),
OverSold(-45),
OverBought(45);

variables:
var_CCI(0),
current_CCI_High(0),
cum_CCI(0),

open_bars_count(0),

var1(0),
var2(0);

var_CCI = CCI(Length);

// ====================================
{ Opening/Closing Long Position }

{ Open Long: CCI crosses above the Overbought level }
condition1 = (currentBar > 1) and (MarketPosition = 0) and (var_CCI crosses above OverBought);
if condition1 then
var1 = 1
else
var1 = 0;

condition2 = (CurrentBar > 1) and (MarketPosition = 1) and (cum_CCI >= 5);
if condition2 then
var2 = 1
else
var2 =0;

{ Enter a long trade }
if (var1 = 1) then begin
Buy ("LE") 100 shares next bar at market;
current_CCI_High = var_CCI;
end;

{ Close Long: 5 down bars since entr}
if (MarketPosition = 1) then begin
cum_CCI = 0;
for open_bars_count = 0 to barssinceentry begin
if (var_CCI[open_bars_count] > current_CCI_High) then begin
cum_CCI = cum_CCI + 1;
current_CCI_High = var_CCI[open_bars_count];
end;
end;
end;

{ Close a long trade }
if (var2 = 1) and (MarketPosition = 1) then begin
Sell ("LX") 100 shares next bar at market;
end;

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Getting nuts with the "Tried to reference back more bars

Postby sptrader » 29 May 2011

I think one problem is, that I think you forgot to define what "cum_CCI " is ..you just have the variable set to 0;

User avatar
Stan Bokov
Posts: 963
Joined: 18 Dec 2009
Has thanked: 367 times
Been thanked: 302 times

Re: Getting nuts with the "Tried to reference back more bars

Postby Stan Bokov » 31 May 2011

Why don't you increase your MaxBarsBack setting in your Strategy Properties?

nlaporte
Posts: 21
Joined: 16 Feb 2011
Location: Switzerland
Has thanked: 1 time
Been thanked: 1 time

Re: Getting nuts with the "Tried to reference back more bars

Postby nlaporte » 01 Jun 2011

Sptrader and Stan,

Thanks for the feedbacks.
The cum_CCI is a trigger: when it reaches the value >=5, there is a sell order.
I increased the barbacks to 100 but it doesnt change anything.
My guess is that one of my loop is not correctly set but I just cant figure it out.
Thanks,

N.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Getting nuts with the "Tried to reference back more bars

Postby TJ » 01 Jun 2011

Sptrader and Stan,

Thanks for the feedbacks.
The cum_CCI is a trigger: when it reaches the value >=5, there is a sell order.
I increased the barbacks to 100 but it doesnt change anything.
My guess is that one of my loop is not correctly set but I just cant figure it out.
Thanks,

N.
barssinceentry is giving you the pain.

nlaporte
Posts: 21
Joined: 16 Feb 2011
Location: Switzerland
Has thanked: 1 time
Been thanked: 1 time

Re: Getting nuts with the "Tried to reference back more bars

Postby nlaporte » 01 Jun 2011

Hi TJ,

Indeed but where I bug: how come it gives me the pain? I just can't figure it out.
By the way, does Powerlanguage enables you to check your code line by line "a la" VBA?
Thanks,

Nicolas


Return to “MultiCharts”