I want to enter in trade based on exit reason

exitreason= buy=sell=0;

buy1= C > MA(C,20);
Buy=buy1 or (buy1 and exitreason==2 and c > ma(c,200) and rsi(14) > 60) ;

Sell1= C < MA(C,20));
sell2= C < MA(C,200));
sell= sell1 or sell2 ;
exitreason=iif(sell1==1,1,2);

I want to Buy if buy1 is true and exit on sell 1 or sell2 but if i exited by sell2 condition i want to enter only when buy1==1 and c > ma(c,200) and rsi(14) > 60

can anybody help me in above code

@Rakesh_Lala, on quick inspection of your code (next time try to post your code using the code blocks </>)...

I think you are close, try this untested

Buy = (buy1 and exitreason ==1) or (buy1 and   exitreason==2 and c > ma(c,200) and rsi(14) > 60) ;

You may have to set exitreason to an initial value at the start or your code.

it gives error exitreason used without initialization

Just rearrange your code so that the Sell and exitreason logic is above the Buy logic.

1 Like

what do you mean by rearrange ?

I mean change the order of the lines of text in your AFL file so that the Sell logic is evaluated before the Buy logic.