Dynamically scale-in problem

Hi everybody, I am doing portfolio backtest on 50 stocks and have written the following codes hoping to generate the effect of dynamically adjusting how much percentage of equity I will buy according to three different extreme market environments.

The level_1, level_2 and level_3 are the three extreme market environments. At one bar, only one of these three level will be annotated as 1 and the remaining two will be as zero.

Therefore, the following codes mean that when there is a scale-in buy signal and no extreme market situation is detected (level1,2,3 all equal 0), there will be 5% equity to be purchased at that time. When level 1 extreme situation is detected, only 3.75% equity will be purchased; if level 2 extreme situation is detected, only 2.5% equity will be purchased and only 1.25% equity will be purchased for that trade if level 3 extreme situation is detected.

SetPositionSize( 5 , IIf(level_1 == 0 AND level_2 == 0 AND level_3 == 0 AND Buy==sigScaleIn, 
spsPercentOfEquity, IIf(level_1 == 1 AND Buy == sigScaleIn,spsPercentOfEquity*0.75, IIf(level_2 == 1 
AND Buy == sigScaleIn,spsPercentOfEquity*0.5, IIf(level_3 == 1 AND Buy == 
sigScaleIn,spsPercentOfEquity*0.25,spsNoChange )))));

But during explorer debugging process, I found that the there is only 100% equity entered for one trade action. Would someone advise on where did I go wrong please? Thank you.

Your code is wrong.

You can't multiply spsPercentOfEquity constant by any value, because it looses its meaning.

You should use FIRST argument to specify the value you want, second argument defines TYPE of position sizing, not the value. Therefore it must NOT be multiplied by some random values. Correct code looks like this:

conditions = ( level_1 == 0 AND level_2 == 0 AND level_3 == 0 AND Buy==sigScale );
value_you_want = IIF( conditions, 3.75 /*if conditions are met)*/, 5 /* in other case */);

// must NOT multiply second parameter by anything
SetPositionSize( value_you_want, spsPercentOfEquity ); 
1 Like

Thank you Tomasz. But in this case, I could only adjust the equity purchase percentage in a binary mode (either 3.75 or 5) as there is only one condition to be fulfilled or not.

If I have several conditions (actually 4), is there other way to do it? Thank you.

No, you can have ANY number of conditions, just nest those iifs.

value_you_want = IIF( Condition1, value1,
                 IIF( Condition2, value2,
                 IIF( Condition3, value3,
                 IIF( Condition4, value4, value_if_no_condition_is_met ) ) ) );
1 Like

I have a similar problem.
For regural signals thare is 5 shares and each subsequent signal in the same direction is additionally 1 share.

Buy = Buy + sigScaleIn * IsNextLong; 
Short=Short+sigScaleIn * IsNextShort;

SetPositionSize( IIf(IsNextLong OR IsNextShort,1,5), spsShares );

How to limit the amount of extra sigScaleIn for example, up to 3?

@jcorniko - this is completely different question. You can count scaleIns or any other value in any array. It is unrelated to original poster question:

CountOfScaleIns = Cum( Buy == sigScaleIn ); 

This gives cumulative sum of scaleins from beginning. To count scale-in since regular buy you can use

CountOfScaleInSinceBuy = SumSince( Buy == 1, Buy == sigScaleIn  );

Now you can choose to act only if number of scale ins is less than desired value.


Thank you Tomasz the problem is solved.