Why i cant acces array element?

hi guys, im learning afl for a couple of weeks now so in the beginning of my learning process. Doing some stuff with existing code and trying to get a better understanding....
so my question is ; why is trailarray [i] empty in/before IF statement to sell ?

StopLevel = 1 - Param("trailing stop %", 10, 0.1, 10, 0.1)/100;

trailARRAY = Null;
trailstop = 0;

for( i = 1; i < BarCount; i++ )
 {

   if( trailstop == 0 AND Buy[ i ] ) 
    { 
       trailstop = High[ i ] * stoplevel;
    }
    else Buy[ i ] = 0; // remove excess buy signals
    _TRACE("i " + i);
    _TRACE(" trailstop " + trailstop );
   if( trailarray[i] > 0 AND Low[ i ] < trailarray[i] )        // trailarray [i] is EMPTY ??
    {
       Sell[ i ] = 1;
       SellPrice[ i ] = trailstop;
       trailstop = 0;
    }

   if( trailstop > 0 )
    {   
       trailstop = Max( High[ i ] * stoplevel, trailstop );
       trailARRAY[ i ] = trailstop;
       //_TRACE(" trailarray " + trailarray[i]);
    
    }

}

Do not re-invent the wheel.

Please do it like it is shown in KB. It already provides optimal code.

So you should use trailstop variable in if statement not trailarray.

trailarray is for plotting trail stop outside of loop.
It has no other purpose.

trailstop is zero if not being in trade. So if trailstop is zero then trailarray is Null (empty/nothing).
trailarray is initialized by Null instead of zero so that if not being in trade then it shows empty result (in chart/exploration/...).


As aside if you copy code from elsewhere then please add link of original source to the code.
If you modify it then please add comment to code that it has been modified (e.g. by you).

1 Like

thank you for your answer.
im not trying to re-invent anything but don't we learn by practising instead of copying ?

There is no doubt for me that KB provides optimal code and next time i will provide link, im sorry for that.

as far as my question goes, i just found it but your answer helped me .
my point/question was trailarray[i] shows empty ( before sell statement) also when trailstop is not zero, so when you are in a trade.
That is because trailstop value has not been assigned yet to trailarray[i]

trailarray[i-1] =trailstop when used for sell statement.

i know it looks stupid and yes im embarrassed

I was basically talking about purpose of original version.

Your modified version is inaccurate.

Yes, you may practice whatever you like to. No one prohibits you from that.
Still you should be remembered that you are driving on wrong track.

ok, well i didn't copy the whole code but only thing changed from KB's code is trailstop with trailarray.
So what is inaccurate exactly ?

and why am i driving on wrong track ? what should i do according to you ?

thank you for your feedback, appreciate it.

Mama mia.

Your code of post # 1 is simply inaccurate and that's why on wrong track.
Simply plot trail line and compare with original one.
Also plot signals. You will see that you only get single entry. And that's it.
You call incorrect element in that modified code. That's why no exit.

But why over-complicating it by calling previous element if you just need to call trailstop variable?
I don't get it.

what should i do according to you ?

Using the original code after being finished with experimentation.

Happy Easter!

Your code of post # 1 is simply inaccurate and that's why on wrong track.
Simply plot trail line and compare with original one.
Also plot signals. You will see that you only get single entry. And that's it.

was just part of code as the rest was not relevant, trail line and signals are being plotted.

You call incorrect element in that modified code. That's why no exit.

yes, as i found out myself and mentioned that in my 2nd post

But why over-complicating it by calling previous element if you just need to call trailstop variable?
I don't get it.

was just for understanding purposes, wouldn't make much sense indeed.

happy easter :egg: :egg:

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.