How to modify the positionssizearray in backtest

Hi, I use afl-code to loop through the bars with:

for ( i = BarCount - from; i < BarCount;i++ ) {
// if (some condition) Buy[i];
}

For Positionsize I use e.g. : SetPositionSize( 1, spsShares ) to use exactly one share.
If I want on some bars(not on all) and Buy-Signals use 2 shares and then one again, how can I code this?

Thank you.
Heiko

if( condition_to_buy_2_shares) 
{
    // Code logic to buy 2 shares
}
else {
    // Buy one share
}

You means this?
Remember the Buy Array doesnt hold position size, its whether Buy has occurred or not for each bar.

@htrader,
First of all please read here.
It is mandatory in this forum to apply code tags if inserting code.
I'm just reminding because in your other post of few hours ago they are missing too.

It is very easy to apply code tags and it is applied very quickly.


Secondly as for your loop...
Do not do this

 i = BarCount - from;

Read here as for not making assumptions on number of bars.

Also ask yourself whether you really need loop at all.
Reminder to be found here.


buycond1 = Cross(C, MA(C, 20));
buycond2 = Cross(C, MA(C, 50));

SetPositionSize(IIf(buycond2, 2, 1), spsShares);

Buy = buycond1 OR buycond2;
Sell = Cross(MA(C,200), C);

Short = Cover = 0;

345

2 Likes

Hi, thank you for this quick reply and for the tips for inserting afl-code.

I need exactly this from travick, but not only with the buy/sell signals, i need to set the positionssize within a loop through the bararray. I know, the better and faster way is to use the array.
I found my answer, when i combine both replies. My problem was the understanding, that I can on the one hand directly set a buy-signal with buy[i]=1, sell, short...and so on, but for the positionsize I cannot do this directly in the loop, I need a "helper array".
The code has to be like this:



for ( i = 0; i < BarCount;i++ )
{
    if ( buycondition )
    {
        if ( buycond2 )
        {
            Buy[i] = 1;
            Lot_size_helper[i] = 2;
        }
        else
        {
            Buy[i] = 1;
            Lot_size_helper[i] = 1;
        }

    }
}

// For Backtest
SetPositionSize( Lot_size_helper, spsShares );

Many Thanks.