Circular Referencing

I have a system where there is a buy signal that depends on the the lowest close since the previous sell and a sell signal that depends on the highest close since the previous buy signal. When I say buy and sell I mean the actual signals after removing the excessive signals.

On paper this is easy however I am not being able to write this in AFL. Can anyone please help me out?

I am attaching an example of what I mean - all blue arrows are signals that should have been buys but were not. This is just an example, I would just to know how AFL handles these cases.

Code:


Buy=0;
Sell=0;

Buy = C>MA(C,20) AND (C>=0.9*HHV(C,40) OR C>=1.2*LLV(C,BarsSince(Sell==1)));
Sell = Cross(HHV(C,40)*0.9,C);

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);

// this will show the missed signals

Y=Flip(Sell,Buy) AND Cross(C,1.2*LLV(C,BarsSince(Sell))) AND C>MA(C,20);
PlotShapes(Y*shapeUpArrow,colorBlue,0,Low);

Plot(C,"C",colorBlack);

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed ,0,High );

Already explained:

The way I understand the issue is because the entire array is processed at once so the buy signal that comes first in the code doesn’t take into account the sell data that came after it in the code which is why this section is avoided completely:

C>=1.2*LLV(C,BarsSince(Sell==1))```

I'm sorry but I still can't figure out a way to make this work. Can you please help?

As written in topic I referenced, exact route to implement circular references depends on details of what you want to achieve. Typically you either use built in functions that implement circular references (again see the post I linked) or you use loop. In your particular case I would use a loop as explained in the Knowledge Base http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/ as it contains exactly the code dealing with buy/sell signals.

1 Like