Indicator value based on condition

Hi
Appreciate your input on this. I am trying to apply an optimised value to the indicator for a specific period.

For example in the code below, if the date range is between 01/01/2015 and 01/01/2016 then assign the value of 14 else assign the value of 10 for the directional indicator.

But I am getting an Error 5 - that the argument has an incorrect type and function expected a number, not an array. In this case, I am not assigning an array but a value on the basis of a condition.

I tried Writeif and strtonum function but nothing seems to cut it. Appreciate if you could guide me here.

If I may have missed any basic coding principle then I apologise for wasting your time.

thank you

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "HoldMinBars", 1 );
PositionSize = 1000;

range = IIf(DateNum()>=1150101 AND DateNum()<1160101,14,10);

PDIValue = PDI(range);
MDIValue = MDI(range);

ADXHisto = PDIValue-MDIValue;
LongEntry  =  Cross(ADXHisto,0);ShortEntry = Cross(0,ADXHisto);
LongExit = Cross(0,ADXHisto) ; ShortExit = Cross(ADXHisto,0);
BuyPrice = SellPrice = C;
Buy = LongEntry; 
Sell = LongExit;
Short = ShortEntry;
Cover = ShortExit;

@beardedinvestor try this

PDIValue = IIf( DateNum() >= 1150101 AND DateNum() < 1160101, PDI(14), PDI(10));
MDIValue = IIf( DateNum() >= 1150101 AND DateNum() < 1160101, MDI(14), MDI(10));

For a look at your variables it is sometimes useful to use the typeof function,

range = IIf( DateNum() >= 1150101 AND DateNum() < 1160101, 14, 10 );
printf( "what is range: %s", typeof( range ) + "\n" );

And if you want to create a variable period version of an indicator that currently does not accept a variable periods, then read this solution.

1 Like

Thank you very much appreciated.
I will take a look at your suggestions. Learning new things everyday.

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