# Multiple if statements for a sell

Hey guys, hope you're doing well.

I'm trying to test a sell rule where as longer moving averages move above my buy price - I change my sell strategy to a close below them. So starts on a fast moving average and then a slow one.

I've tried to code this condition using the IIF statements as my understanding is they are used for arrays instead of if else statements.

I have done this in two separate ways, both seem to not work and I'm not sure where I am going wrong. I can successfully code this strategy using two moving averages and the one if statement. However, when I try to include more than one if statement it gets messy and not sure why.

Any help would be appreciated,

The two different coding methods below I have tried.

``````SetOption("MaxOpenPositions",20);
SetPositionSize(5,spsPercentOfEquity);

Condition1 C>=HHV(250);

MA10sell = C < MA(C,10);
MA20sell = C < EMA(C,20);
MA50sell = C < MA(C,50);
MA200Sell =C < MA(C,200);

Sell = Sellarray1;

``````

2nd style below,

``````SetOption("MaxOpenPositions",20);
SetPositionSize(5,spsPercentOfEquity);

Condition1 = C >= HHV(C,250);

MA10sell = C < MA(C,10);
MA20sell = C < MA(C,20);
MA50sell = C < MA(C,50);
MA200Sell = C < MA(C,200);

``````

1 Like

Unfortunately I am still having issues. Below is what I have done. Again for both scenarios

``````SetOption("MaxOpenPositions",20);
SetPositionSize(5,spsPercentOfEquity);

Condition1 = C >= HHV(C,250);

MA10sell = C < MA(C,10);
MA20sell = C < EMA(C,20);
MA50sell = C < MA(C,50);
MA200Sell = C < MA(C,200);

Sell = Sellarray1;

``````

2nd example below

``````SetOption("MaxOpenPositions",20);
SetPositionSize(5,spsPercentOfEquity);

Condition1 = C >= HHV(C,250);

MA10sell = C < MA(C,10);
MA20sell = C < MA(C,20);
MA50sell = C < MA(C,50);
MA200Sell = C < MA(C,200);

``````

Thankyou so much! this makes alot of sense

No, don't do that.
Sorry, but this is awful looping code.
It should not be done like that.

https://www.amibroker.com/guide/h_understandafl.html

2 Likes

Agree with @fxshrat using SumSince and other array functions inside loop is big no-no and awful example of how NOT to write loops.

2 Likes

Modify to suit your exact rules.

``````MA_10 = MA(Close, 10);
MA_20 = MA(Close, 20);
MA_50 = MA(Close, 50);
MA_200 = MA(Close, 200);

Buy = Close == HHV(Close, 250) AND Close > Max(MA_10,Max(MA_20,Max(MA_50,MA_200)));
Sell = 0;

entryPrice = 0;
MA_20_Flag = False;
MA_50_Flag = False;
MA_200_Flag = False;

for (i = 0; i < BarCount; i++)
{
{
if (NOT MA_200_Flag AND MA_200 [i] > entryPrice) MA_200_Flag = True;
else if (NOT MA_50_Flag AND MA_50 [i] > entryPrice) MA_50_Flag = True;
else if (NOT MA_20_Flag AND MA_20 [i] > entryPrice) MA_20_Flag = True;

if (MA_200_Flag) adaptiveMA = MA_200 [i];
else if (MA_50_Flag) adaptiveMA = MA_50 [i];
else if (MA_20_Flag) adaptiveMA = MA_20 [i];

{
Sell [i] = True;
SellPrice [i] = Close [i];
entryPrice = 0;
MA_20_Flag = False;
MA_50_Flag = False;
MA_200_Flag = False;
}

}
else
{
{
}
}
}

if (Status("ActionEx") == actionIndicator)
{
PlotShapes(Buy * shapeUpArrow, colorBlue, 0, Low, -20);
PlotShapes(Sell * shapeDownArrow, colorRed, 0, High, -20);
}
``````
1 Like

It's better now but all those `MA_*_Flag` lines can be removed as it's not required because it is things done twice.

So just use

``````		if (MA_200[i] > entryPrice) adaptiveMA = MA_200[i];
else if (MA_50[i] > entryPrice) adaptiveMA = MA_50[i];
else if (MA_20[i] > entryPrice) adaptiveMA = MA_20[i];
``````

-->

``````MA_10 = MA(Close, 10);
MA_20 = MA(Close, 20);
MA_50 = MA(Close, 50);
MA_200 = MA(Close, 200);

Buy = Close == HHV(Close, 250) AND Close > Max(MA_10,Max(MA_20,Max(MA_50,MA_200)));
Sell = 0;

entryPrice = 0;
for (i = 0; i < BarCount; i++)
{
{
if (MA_200[i] > entryPrice) adaptiveMA = MA_200[i];
else if (MA_50[i] > entryPrice) adaptiveMA = MA_50[i];
else if (MA_20[i] > entryPrice) adaptiveMA = MA_20[i];

{
Sell [i] = True;
SellPrice [i] = Close [i];
entryPrice = 0;
}

}
else
{
{
}
}
}

Plot( C, "Price", colorDefault, styleBar );
PlotShapes(Buy * shapeUpArrow, colorBlue, 0, Low, -20);
PlotShapes(Sell * shapeDownArrow, colorRed, 0, High, -20);
``````
1 Like

Thanks @fxshrat,

I put in those flags in case OP wanted to hold the true status of each MA for the life of the trade.

1 Like

It will do the same thing.
The first time flag is true it will exit.
Same with the one I posted. As soon as TRUE it will exit.
There is no difference.
If so then please show example where exit is different.

2 Likes

Thanks guys really appreciate the help!

Thank you fxshrat for sharing your skill and knowledge!

1 Like

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