Can this be coded without loop?

I am finding it difficult to code a Buy/Sell where the sell bar is not known at the time of buy. As a result I am not able to exrem(buy, sell) at the beginning of the code. Since scaleout targets are dependent on the BuyPrice i need to know this upfront. Many buy signals are generated within the trade due to which the buyprice keeps on changing with every bar. I can exrem but what will i exrem with? The psuedo code is below.

Buy=(previousBarHigh - previous3BarLow)>=2 && priceabovecloud;
InitialStopLoss=BuyPrice -1;
ScaleoutTarget1= BuyPrice + 2;
stopLoss1=BuyPrice;
scaleoutTarget2=BuyPrice+4;
stopLoss2=ScaleoutTarget1;
Sell=IF(ScaleoutTarget1NOTReached) then cross(InitialStopLoss, C)
else if (ScaleoutTarget1Reached) then cross(stopLoss1, C)
else if (ScaleoutTarget2Reached) then cross(stopLoss2, C)

Just want a direction from experienced afl writters.

Code what you want using loop and you will answer your question

Ok I have written everything with loops. Please see below.


_TRACE("!CLEAR!"); 
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle(), Null, Null, 0, 0, 2); 
_SECTION_END();

SetBarsRequired(1000);

_SECTION_BEGIN("Signal");

buyOpen=0;priceAtBuy=scaleoutPrice=0;HighestSinceBuy=0;Buy=sell=Short=Cover=0;
TrailActive=ScaledOutOnce=BuyCondition=ShortCondition=0;
sl=1;
for (i=4;i<BarCount-1;i++){
BuyCondition[i]=H[i-1]- L[i-3]>=1; 
ShortCondition[i]=H[i-3] - L[i-1] >=1; 
sl[i]=1;
if (buyopen[i-1]==1){
 if (High[i]>HighestSinceBuy[i-1])
	HighestSinceBuy[i]=High[i];
	else
	HighestSinceBuy[i]=HighestSinceBuy[i-1];
	
	
	buyopen[i]=buyopen[i-1];
	priceAtBuy[i]=priceAtBuy[i-1];
	slPrice[i]=slPrice[i-1];
	scaleoutPrice[i]=scaleoutPrice[i-1];
	ScaledOutOnce[i]=ScaledOutOnce[i-1];
	TrailTriggerPrice[i]=priceAtBuy[i]+4;
	TrailActive[i]=TrailActive[i-1];
 }
 
if (buyopen[i]!=1 && BuyCondition[i]){
buyopen[i]=1;
Buy[i]=1;
priceAtBuy[i]=O[i];
slPrice[i]=priceAtBuy[i]-sl[i];
scaleoutPrice[i]=priceAtBuy[i]+3;
HighestSinceBuy[i]=0;
ScaledOutOnce[i]=0;
TrailActive[i]=0;
}

if (buyopen[i] && !Buy[i]){
//StopLoss Hit
if (L[i]<slPrice[i]){// || (H[i-3]<cloudSupportSecondary[i]&&H[i-2]<cloudSupportSecondary[i]&&H[i-1]<cloudSupportSecondary[i])){
buyopen[i]=0;
Sell[i]=1;
}

//ScaleOut
if(H[i]>=scaleoutPrice[i] && !ScaledOutOnce[i]){
Buy[i]=sigScaleOut;
ScaledOutOnce[i]=1;
slPrice[i]=priceAtBuy[i];
}

if(H[i]>=TrailTriggerPrice[i] && ScaledOutOnce[i] && !TrailActive[i])
TrailActive[i]=1;

//trailing stop
if(TrailActive[i])
slPrice[i]=HighestSinceBuy[i]-2;

}
}
_TRACE("Buy="+Buy+", Sell="+Sell+", buyopen="+buyopen+", HighestSinceBuy="+HighestSinceBuy+", TrailActive="+TrailActive);

shape = (Buy==1) * shapeUpArrow + (Buy==sigScaleOut) * shapeDownArrow + (Buy==sigScaleIn) * shapeUpArrow
+ (Sell>0) * (shapeCircle+shapePositionAbove) 
+ (Short==1)* shapeDownArrow + (Short==sigScaleOut) * shapeUpArrow + (Short==sigScaleIn)* shapeDownArrow 
+ (Cover>0) * shapeCircle;

PlotShapes(shape,  IIf( Buy||Sell, colorWhite, colorYellow), 0, IIf( Buy==1||Cover, Low, High));

_SECTION_END();

How will you write this without loops?

Sorry, I did not express myself correctly.
I did not say it was possible to code it using only arrays, what I wanted to say is that if you code using loop you might find out the exrem condition or discover that you need to use loop.
The use of loops is not prohibited, if they exist it is because not everything is possible to be done with arrays.

I am sure there must be a way to write above without loop. My system runs on tick tf and uses multitimeframe which is why i am trying to avoid loops.

Can someone please look at the code above and tell if it can be done without loop? I just need a hint.

Start with replacing your loop with HighestSince http://www.amibroker.com/f?highestsince

Sorry I tried HighestSince but failing to come with the right code. The problem is this buy/sell is not based on cross of indicators. I have seen many examples where buy is triggered at the bullish cross and sell is triggered on bearish cross. In such cases we can easily exrem
buy=exrem(buy, bearishCross)
But in my case there is no such condition. Can you please provide me an example of a system written without loops where buy/sell are based purely on price action and not on some indicator.

Can anyone post an example, written without loop, where buy/sell is based on purely price action and not on cross of indicator. I think this is an important topic that requires discussion. I am yet to see such a system written without loops. Tomasz please comment.

@Jefforey: it would be helpful if you could express your entry and exit rules in English (i.e. not in AFL or other pseudo-code). Otherwise your question is too generic for anyone to give a useful response.

Depending on your rules, you may or may not be able to accomplish everything without loops.

1 Like
  1. Buy When price moves 2 points in the last 3 bars i.e Ref(H, -1) - Ref(L, -3)>=2
  2. Initial Stop at 1 point below BuyPrice
  3. scaleout when price hits buyPrice + 2 points. On scaleout move stop to buyPrice
  4. If price hits BuyPrice+4 points move stop to BuyPrice+2 points.

I would typically implement that kind of logic in the CBT. I’m not sure you can do it in your phase 1 AFL without a loop, because there’s no other way to identify the “first” buy (i.e. the one that actually results in an entry) with your current rules.

1 Like

I am surprised something this simple cannot be done without loops in amibroker. I reference multiple time frames , multiple (foreign) instruments and run my code on tick timeframe Which makes using loops an untenable option for me. anyway Thank you.

It would be simple, if you had any well-defined upper bound on your trades, but you don’t. Consider the rules you presented. Once you’ve entered a trade, if you’re lucky enough for the price to increase by 4 points, then your ONLY exit is 2 points above the Buy Price. So if that’s your best case scenario, then you shouldn’t just be scaling out at BuyPrice+2, you should be totally exiting the trade because you have no opportunity to ever achieve more profit than that. But with your rules, you might stay in that trade forever.

Well defined upper bound is when time hits 15 minutes before closing time.

OK, that’s a good start. Now we know that your first Buy signal of the day should be considered an actual entry. Do you allow multiple entries for the symbol in a single day, i.e. can you re-enter after being stopped out?

Yes any number of trades. It’s a day trading system. All trades must be closed 15 minutes before market close

It seems to be impossible without loops. Ok let me ask you this. Is there a way to limit the execution of loops to just once for bars other than current bar? I have coded the system using loops but it takes 21 minutes to perform backtest the system for 2 months tick data.

Do one loop save in static variable, test if static exists , if not do the loop, if exits use it.