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);

Buy = Condition1 ;

Sellarray3 = IIf(EMA(C,20)>=Buy,MA20Sell,MA10Sell);
Sellarray2 = IIf(MA(C,50)>=Buy,MA50Sell,Sellarray3);
Sellarray1 = IIf(MA(C,200)>=Buy,MA200Sell,sellarray2);

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);

Buy = Condition1 ;

Sell = iif(MA(C,200)>=Buy,MA200sell,iif(MA(C,50)>=Buy,MA50Sell,iif(MA(C,20)>=Buy,MA20Sell,MA200sell)));

So use 'BuyPrice' (not '>= Buy') with ValueWhen.

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);

Buy = Condition1 ;
BuyPrice=C;


Sellarray3 = IIf(EMA(C,20)>=ValueWhen(Buy,BuyPrice,1),MA20Sell,MA10Sell);
Sellarray2 = IIf(MA(C,50)>=ValueWhen(Buy,BuyPrice,1),MA50Sell,Sellarray3);
Sellarray1 = IIf(MA(C,200)>=ValueWhen(Buy,BuyPrice,1),MA200Sell,sellarray2);

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);

Buy = Condition1 ;
BuyPrice=Close;

Sell = iif(MA(C,200)>=ValueWhen(Buy,BuyPrice,1),MA200sell,iif(MA(C,50)>=ValueWhen(Buy,BuyPrice,1),MA50Sell,iif(MA(C,20)>=ValueWhen(Buy,BuyPrice,1),MA20Sell,MA200sell)));

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.

Please both re-read this page.
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;

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

for (i = 0; i < BarCount; i++)
{
	if (inTrade)
	{
		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];
		else adaptiveMA = MA_10 [i];
		
		if (Close [i] < adaptiveMA)
		{
			Sell [i] = True;
			SellPrice [i] = Close [i];
			inTrade = False;
			entryPrice = 0;
			MA_20_Flag = False;
			MA_50_Flag = False;
			MA_200_Flag = False;
		}
		
		Buy [i] = False;
	}
	else
	{
		if (Buy [i])
		{
			BuyPrice [i] = Close [i];
			entryPrice = BuyPrice [i];
			inTrade = True;
		}
	}
}

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];
		else adaptiveMA = MA_10[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;

inTrade = False;
entryPrice = 0;
adaptiveMA = 0;
for (i = 0; i < BarCount; i++)
{
	if (inTrade)
	{		
		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];
		else adaptiveMA = MA_10[i];		

		if (Close [i] < adaptiveMA)
		{
			Sell [i] = True;
			SellPrice [i] = Close [i];
			inTrade = False;
			entryPrice = 0;			
		}	
	
		Buy [i] = False;
	}
	else
	{
		if (Buy [i])
		{
			BuyPrice [i] = Close [i];
			entryPrice = BuyPrice [i];
			inTrade = True;			
		}
	}
}

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.