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.
//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.