Error message

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

Error message

Postby nlaporte » 04 Apr 2011

Hello,

I get the following error message:

Error in study "xxxxx": :Tried to reference back more bars (26) than allowed by the current MaxBarsBack setting. Please increase the MaxBarsBack setting.

I increased to 50, 100... doesn't change a thing.
I get it because I added a new condition in a working code, as per below:

Code: Select all

// ======================================================
if (open_long = 1) and (first_entry_long >=1) and (barssinceentry = 4) and (Close >= entry_long) then begin
   Buy ("LEx2") 100 shares next bar at market;
   open_long = 2;
end;
// ======================================================

Reading:
if the backtest has already traded once (first_entry_long >=1)
if a position is already opened (open_long = 1)
if the open position is still on after 4 bars (barssinceentry = 4)
if the position has a positive carry (Close >= entry_long) then begin
then
Buy ("LEx2") x shares next bar at market;
And update trigger (open_long = 2);

Can anyone help me on that?
Thank you very much,

N.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: Error message

Postby TJ » 04 Apr 2011

nlaporte wrote:Hello,

I get the following error message:

Error in study "xxxxx": :Tried to reference back more bars (26) than allowed by the current MaxBarsBack setting. Please increase the MaxBarsBack setting.

I increased to 50, 100... doesn't change a thing.
I get it because I added a new condition in a working code, as per below:

Code: Select all

// ======================================================
if (open_long = 1) and (first_entry_long >=1) and (barssinceentry = 4) and (Close >= entry_long) then begin
   Buy ("LEx2") 100 shares next bar at market;
   open_long = 2;
end;
// ======================================================

Reading:
if the backtest has already traded once (first_entry_long >=1)
if a position is already opened (open_long = 1)
if the open position is still on after 4 bars (barssinceentry = 4)
if the position has a positive carry (Close >= entry_long) then begin
then
Buy ("LEx2") x shares next bar at market;
And update trigger (open_long = 2);

Can anyone help me on that?
Thank you very much,

N.


The problem is not because of the snippet above.
I have compiled the code with no problem; the problem is elsewhere.
Can't tell you more without more information.
You should post the whole study.
Don't worry about people "stealing" your idea, most people change their code 100 times before they can find the right logic.


ps. please use the code tag to wrap your codes when posting. I have done it for you this time. It makes reading codes easier.

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

Re: Error message

Postby nlaporte » 04 Apr 2011

Sure... and by the way, comments welcome.
Am more ashame of its simplicity than anything else...
As per below, and as long as I dont include the part of the code i posted earlier, it works fine.
Thanks for the help,
Best,

N.


Code: Select all

[IntrabarOrderGeneration = false]

{
- Check if better trigger than a fixed number of ticks before going out ?
- Include stop looses at the position level when trading live on the platform
- Entry with a lag... if perf neg, still go on, if perf pos, 2x position
- Multiple trading signals, i.e. not just one in and out and in and out ?
- Exit all positions at closing day minus x minutes ? avoid gap risk
}

inputs:
   Price(Close),
   
   Length(9),
   FastLength(12), SlowLength(26), MACDLength(9),
   LinLenght(4),
   
   BBLength(10), NumDevsDnHigh(3), NumDevsDnLow(1.5),   
   NumATRs( 1.5 ), Length_Kel( 20 );

Variables:
   var1(0), var2(0), var3(0), var4(0), var5(0),
   var1_loop(0),
   
   open_long(0), first_entry_long(0), close_long(20),
   entry_long(0), exit_long(0),
   NegPandL_long(0), NegPandL_wait(5),      // <=== MOVE IT TO VARIABLE TO BE OPTIMIZED
   Barback(0), Barback_trail(0),
   Trail_open(0),
   Trail_max(0),
   Trail_gap(0),
   
   Portfolio_AUM(20000),      // total portfolio aum
   Trade_Size(10),         // alloc dedicated in percentage per trade relative to portfolio size               
   Trade_Shares(0),         // number of shares/contracts to buy
   Trail_cut(2),            // max tolerated percentage drop per trade (per 1'000)
   Trail_negbars(1),
   Last_Trade_Time(1445),      // no more trades after that time
   Close_Time(1530),         // close trade placed at 15:30
   
   var_PandL_wait(0),
   var_MACD(0), var_MACD_MA(0), var_MACDDiff(0),
   var_Fast_MA(0),
   
   varLowerBBands(0), varHigherBBands(0);



// ============================================================
{ Indicators computation}

var_MACD = MACD(Price,FastLength,SlowLength );
var_MACD_MA = XAverage(var_MACD, MACDLength );
var_MACDDiff = var_MACD - var_MACD_MA;

var_Fast_MA = AverageFC(Price, 12);

varLowerBBands = BollingerBand(Price,BBLength, -NumDevsDnLow);
varHigherBBands = BollingerBand(Price,BBLength, NumDevsDnHigh);

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

{ Cross over between close and LowerBBand in the n previous bars }
var1 = 0;
var1_loop = 0;
For Barback = 3 downto 0 begin
   condition1 = (currentBar > 1) and (Close[Barback+1] < varLowerBBands[Barback+1]) and (Close[Barback] >= varLowerBBands[Barback]);
   if condition1 then
      var1_loop = 1
   else
      var1_loop = 0;
   var1 = var1 + var1_loop;
end;

{ Avoid extreme bar move on the entry bar } { == Neutralized == }
condition2 = (currentBar > 1) and
      (absvalue(Close - Open)) < (absvalue(Close[1] - Open[1]))*2;
if condition2 then
   var2 = 1
else
   var2 = 1;      

{ Avoid multiple loosing trades on a short time period }
condition3 = (currentBar > 1) and ((BarsSinceExit(1) >= NegPandL_wait) or (PositionProfit(1) > 0) or (first_entry_long = 0));
if condition3 then
   var3 = 1
else
   var3 = 0;      

{ Plug trade if last n bars are positive }
condition4 = (currentBar > 1) and (Close[2] < Close[1]) and (Close[1] < Close);
if condition4 then
   var4 = 1
else
   var4 = 0;


// ============================================================
{ Opening Short Position }




// ============================================================
{ Open Long Book }

{ Enter a long trade }
if (var1 >= 1) and (var2 = 1) and (var3 = 1) and (var4 = 1) and (open_long = 0) and (Time < Last_Trade_time) then begin
   Portfolio_AUM = Portfolio_AUM + (exitprice(1)-entryprice(1))*Trade_shares;      // adjust AUM for trade P&L
   Trade_shares = round((Portfolio_AUM*(Trade_Size/100))/Price,0);   
   Buy ("LE") Trade_shares shares next bar at market;
   first_entry_long = first_entry_long +1;
   entry_long = entryprice;
   open_long = 1;
   NegPandL_long = 0;
   var1 = 0;
   var2 = 0;
   var3 = 0;
   var4 = 0;
end;

{ Beef-up potentially winning trade }
if (open_long = 1) and (first_entry_long >=1) and (barssinceentry = 4) and (Close >= entry_long) then begin
   Buy ("LEx2") 100 shares next bar at market;
   open_long = 2;
end;

// ============================================================
{ Close Long Book }

{ Cut position if close of a bar is going above the higher BB bands }
if (open_long >= 1) and (Close >= varHigherBBands) then begin
   Sell ("High") from Entry("LE") Trade_shares shares next bar at market;
   exit_long = exitprice;
   NegPandL_long = exit_long - entry_long;
   entry_long = 0;
   exit_long = 0;   
   open_long = 0;         
end;

{ Exit position before close of the day}
if (open_long >= 1) and (Time = Close_Time) then begin
   Sell ("End") from Entry("LE") Trade_shares shares next bar at market;
   exit_long = exitprice;
   NegPandL_long = exit_long - entry_long;
   entry_long = 0;
   exit_long = 0;   
   open_long = 0;
end;

{ Exit a long trade: max holding period}
if (open_long >= 1) and (barssinceentry = close_long) then begin
   Sell ("Max") from Entry("LE") Trade_shares shares next bar at market;
   exit_long = exitprice;
   NegPandL_long = exit_long - entry_long;
   entry_long = 0;
   exit_long = 0;
   open_long = 0;      
end;

{ Trailing stop made in Nico }
Trail_open = barssinceentry;
Trail_max = entry_long;
For Barback_trail = Trail_open downto 0 begin
   if Close[Barback_trail] > Trail_max then
      Trail_max = Close[Barback_trail];
end;
Trail_gap = (Close/Trail_max)-1;
if (open_long >= 1) and ((Trail_gap) <= -0.8/100) then begin
   Sell ("Trail") from Entry("LE") Trade_shares shares next bar at market;
   exit_long = exitprice;
   NegPandL_long = exit_long - entry_long;
   entry_long = 0;
   exit_long = 0;
   open_long = 0;      
end;

{ Close trade if two consecutive negative bars }
if (open_long >= 1) and (barssinceentry >= 1) and (Close[Trail_negbars] > Close) then begin
   Sell ("Neg2x") from Entry("LE") Trade_shares shares next bar at market;
   exit_long = exitprice;
   NegPandL_long = exit_long - entry_long;
   entry_long = 0;
   exit_long = 0;
   open_long = 0;      
end;

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: Error message

Postby TJ » 06 Apr 2011

The problem is with barssinceentry.

if you take out the keyword, the code can be applied with no problem.

I don't know what is holding up this keyword, I have to do some more studying on the subject.

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

Re: Error message

Postby Stan Bokov » 07 Apr 2011

The barssinceentry keyword was not touched. There is a little mistake in another part of the code.

Code: Select all

Trail_open = barssinceentry;
Trail_max = entry_long;
For Barback_trail = Trail_open downto 0 begin
   if Close[Barback_trail] > Trail_max then           
   // Error occurs here since Barback_trail becomes > than the number of historical bars available.
      Trail_max = Close[Barback_trail];
end;

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

Re: Error message

Postby nlaporte » 07 Apr 2011

TJ, Stan,

Thanks for the help
Best,

N.


Return to “MultiCharts”