ScaleIn in and out the same bar (EOD)

I am trying to do Scale In and Scale Out in the same bar.

Basically I am using Daily Bars. The entry signal generated after previous day's close. I will enter half position at Open, then depends how price develop. If the price drop to certain level I will enter another half position. I asked Tomasz via help desk email. I've been told I cannot do Scale In and Out in the same bar. I think I may find a solution for my Scale In issue(see code below). But I do not know whether it has any logical fault. If there is any, could you point out for me? Thanks.

Buy =  Signal1;

OpenEntryPositionPercent = 0.5
ScaleInPositionPercent = 1 - OpenEntryPositionPercent;

OpenEntryPositionSize = pctPosSize * OpenEntryPositionPercent;
ScaleInPositionSize = pctPosSize * ScaleInPositionPercent;

ScaleInPricePercent = 0.01;
ScaleInPrice = Prec(O*(1-ScaleInPricePercent),2);	

SetPositionSize( IIf(L<= ScaleInPrice, (OpenEntryPositionSize + ScaleInPositionSize), OpenEntryPositionSize), spsPercentOfEquity);

BuyPrice = IIf(L<= ScaleInPrice, Prec(( O * OpenEntryPositionPercent + ScaleInPrice * ScaleInPositionPercent),2), O);

Now the problem come for the Scale Out. I want exit the position a few days later. I wish to Scale Out half of the position if the price reach certain level on that day, then exit the other half at Close that day. The two exits will happen in the same bar. Can I do similar thing for the Scale Out? Or can I achieve this via loop? Or I have to resort to low-level custom backtester ScaleTrade as Tomasz sugguested via email. Thank you very much for your help.

As I wrote you already over support - you can only normally have either scale IN or scale OUT or Buy on the very same (single) bar as documented in manual
The only way to have more than one scale in/out in single bar is to use low-level custom backtest calling Scale method in KB

Generally speaking one should avoid multiple scaling in/out in single daily (EOD) bar because you do not have the knowledge what happens inside the bar (how price move inside bar). So you need to do many assumptions, that are usually not true in real-life. Instead of making such assumptions just use intraday data instead of EOD.
Intraday data would represent exactly what market was doing during the course of the day and backtesting on intraday interval would give you better representation of what would actually happen if you traded the market.

1 Like

I was studying the example above that involve custom backtest.
I can not understand the line:

price = pos.GetPrice( bar, "O" );

Why @Tomasz you scale the trade at the open price?

At the end of the day Amibroker call current equity

CurEquity = bo.Equity;

If condition for rebalance is met, how is possible to trade at the open price of the same bar?

The example given in KB was intended to be used with certain settings, i.e. next bar trade at open. If you trade on close you should adjust code accordingly.

I'm just trying to understand and gain confidence in Amibroker code.

Even if you want trade next day at open is correct to modify the Getprice line in:

price = pos.GetPrice( bar + 1, "O" );
  1. Positionscore make all the ranking work at the close of the day.
  2. Next day at open the trader will place orders on the market.

No because delays are applied in first backtest phase, not second. PositionScore is delayed in first phase and scoring uses previous bar data. So GetPrice line is correct in the example.