IF statement array suboperator

I know that the "if" statement cannot accept arrays, and in order for that it must use [] suboperators

Now, my question is, can the suboperator be an array? (i know that it's a "no", it has to be a number)

But how could I write the following the correct way?

NATRP1 = somenumber
NATRL = somenumber
natr1 = 100 * (HHV(High, NATRP1) - LLV(Low, NATRP1)) / MA(Close, NATRP1);

for i = ....no idea how to write it....)
{
	if ( natr1[i] < NATRL )    
		buyC1 AND buyC2 AND sellC1 AND sellC2 = Null;
	else
	Cover = Buy = buyC1 and buyC2;
	Short = Sell = sellC1 and sellC2;
}

Thank you as always for the valuable help, in the past few months I went from having 0 idea to being able to put a system together and optimize a few things, looking forward to keep learning and improving.

These codes might help:

Need Help in AFL code for pyramiding in Intraday
Weekly backtest start day

1 Like

Thank you! Ill definitely give it a look tomorrow,
I looked quickly and somethings became more clear

I wanted to leave an optimization running overnight, I thought my code was something easy to fix

If anyone else is reading this post, it would be greatly appreciated if they could correct my "if" statement

@Bob,

You need zero looping code for that.


NATRP1 = /*somenumber*/;
NATRL = /*somenumber*/;

natr1 = 100 * (HHV(High, NATRP1) - LLV(Low, NATRP1)) / MA(Close, NATRP1);
natr1_cond = natr1 >= NATRL;

Buy = IIf(natr1_cond, buyC1 AND buyC2, 0);

Sell = IIf(natr1_cond, sellC1 AND sellC2, 0);

Short = Sell;
Cover = Buy;

Or


NATRP1 = /*somenumber*/;
NATRL = /*somenumber*/;

natr1 = 100 * (HHV(High, NATRP1) - LLV(Low, NATRP1)) / MA(Close, NATRP1);
natr1_cond = natr1 >= NATRL;

Buy = natr1_cond AND buyC1 AND buyC2;
Sell = natr1_cond AND sellC1 AND sellC2;

Short = Sell;
Cover = Buy;

2 Likes

thank you so much! will try that
looking forward to keep learning

One question

My system is "stop and reverse"

Either long, or either short, it closes the position completely before taking the other one

would the correct way be the way you wrote it or

cover = buy = natr1_cond AND buyC1 AND buyC2;
short = sell = natr1_cond AND sellC1 AND sellC2;

?

closes the position completely before taking the other one

Just add this line at the top of the code


SetOption("ReverseSignalForcesExit", True);

1 Like

Agreed @fxshrat,

This (without Loop):

//Without Using Loop to generate Buy/Sell Signal
_SECTION_BEGIN( "SAR No-Loop Buy/Sell" );
	 //A Simple PSAR based Demo strategy
	 SetOption( "ReverseSignalForcesExit", True );
	 
	 acc = Param( "Acceleration", 0.02, 0, 1, 0.001 );
	 accm = Param( "Max. acceleration", 0.2, 0, 1, 0.001 );
	 _SAR = SAR( acc, accm );
	 
	 _Buy = _SAR < L;
	 _Short = _SAR > H;
	 
	 Buy = Cover = ExRem( _Buy, _Short );
	 Short = Sell = ExRem( _Short, _Buy );
	 
	 //Plotting
	 Plot( C, "Price", colorDefault, styleCandle );
	 Plot( _SAR, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );	 
	 
	 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -30 ); //Long Entry
	 PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0, H, -15 ); //Long Exit
	 
	 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H, -30 ); //Short Entry
	 PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, -15 ); //Short Exit
_SECTION_END();

Or this (with Loop):

//Using For-Loop to generate Buy/Sell Signal instead of using ExRem
_SECTION_BEGIN( "SAR For-Loop Buy/Sell" );
	 //A Simple PSAR based Demo strategy
	 acc = Param( "Acceleration", 0.02, 0, 1, 0.001 );
	 accm = Param( "Max. acceleration", 0.2, 0, 1, 0.001 );
	 _SAR = SAR( acc, accm );
	 
	 _Buy = _SAR < L;
	 _Short = _SAR > H;
	 
	 //Array Initialization
	 Buy = Sell = Short = Cover = Null;
	 LongFlag = ShortFlag = 0; //Simple Flag arrays to identify whether in a Long or Short position
	 
	 //Using Loop to generate signals
	 for( i = 0; i < BarCount; i++ ) {
		 //Long Positions
		 if( _Buy[ i ] AND LongFlag == 0 ) {
			 Buy[ i ] = 1;
			 LongFlag = 1;  //To record that we are in Long position
		 }		 
		 if( _Short[ i ] AND LongFlag == 1 ) {
			 Sell[ i ] = 1;  //Selling-off the Long position
			 LongFlag = 0;   //Reseting LongFlag back to False, to denote that we are no longer in "Long" position
		 }
		 
		 //Short Positions
		 if( _Short[ i ] AND ShortFlag == 0 ) {
			 Short[ i ] = 1;
			 ShortFlag = 1;	  //To record that we are in Short position
		 } 
		 if( _Buy[ i ] AND ShortFlag == 1 ) {
			 Cover[ i ] = 1;  //Covering the Short position
			 ShortFlag = 0;   //Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
		 }
	 }
	 
	 //Plotting
	 Plot( C, "Price", colorDefault, styleCandle );
	 Plot( _SAR, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );	 
	 
	 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -30 ); //Long Entry
	 PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0, H, -15 ); //Long Exit
	 
	 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H, -30 ); //Short Entry
	 PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, -15 ); //Short Exit
_SECTION_END();

Would yield the same. However, is there a more smarter (loopless) way to counter a situation while backtesting wherein this happens:

Thank you

P.S. @Bob, you did not mention that you are using a SAR system in your first post of this thread.

2 Likes

Thank you for the great resources