Issue to define additional entries dependant on the first entry

Hello everyone.

I've been facing an issue to implement scaling to my system. The system consists of buying a certain TICKER conditioned to a Bollinger Breakout entry signal and scale in up to two additional entries, which depend on the value of the first entry. The first entry is the breakout of a BBandBot(C,20,2) and two remaining entries would be nothing more than the breakout of BBandBot(?,20,3) and BBandBot(?,20,4), but considering the value of the first entry and not de CLOSE value of the bar. I don't know how to store the first entry value to make these two additional entries dependant on it.

Find my code below:

SetOption( "InitialEquity", 20000 );
MaxPos = 5;
SetOption( "MaxOpenPositions", MaxPos );


BS=BBandTop(C,20,2);
E1=BBandBot(C,20,2);
E2=BBandBot(C,20,3);
E3=BBandBot(C,20,4);

Buy=L<E1;
BuyPrice=E1;
Sell=H>=MA(C,20);
SellPrice =MA(C,20);

//Buy = ExRem( Buy, Sell );  //  clear out excesive signals, if we are already bought
//Sell = ExRem( Sell, Buy ); //  clear out excesive signals, if we are already sold

//PlotShapes(Buy*shapeUpArrow,colorBrightGreen,0);  
//PlotShapes(Sell*shapeDownArrow,colorRed,0); 

Plot(E1,"E1",colorAqua,0);
Plot(E2,"E2",colorYellow,0);
Plot(E3,"E3",colorBrown,0);
Plot(BS,"BS",colorAqua,0);
Plot(MA(C,20),"MA",colorWhite,0);

Any help or comments are appreciated.
RDCAmaral.

Not a complete answer:
Normally things like scale in are handled by the backtest engine but if you want to do it on the chart you could try...
FirstBuyPrice = ValueWhen( Buy, BuyPrice );
...to hold that entry price for the scale in calculations.

@rdcamaral are you trying to backtest or trying to achieve it in a live system ?

Thanks for the suggestion. I've found a topic here at the forum describing the scale in with mutliple criteria. Using the command you suggested I achieved what I wanted.

Many thanks!

I'm trying to backtest for instance. Wih @JohnHT answer and some otther piece of code I was able to solve the issue.

oh well good. Do share your findings and the link(s) here so that it may help someone in the future if he/she ever came across it.

Oh yeah, for sure. The scale in procedure I've adopted something very similar to this discussion:

[Scale-In / Scale-Out : How to implement in AmiBroker Auto-Trading like IB or the other auto-trading platform]

For future reference, find below my code mixing the solution John has suggested with the one presented at the topic above:

FirstEntry = BuySignal1;
InFirstPos = Flip(FirstEntry, Sell);
FirstTrigger = ExRem(InFirstPos, Sell);
BarsSinceFirstTrigger = BarsSince(FirstTrigger);
FirstTriggerPrice = IIf(BarsSinceFirstTrigger < BarsSinceSell, Ref(BI,-1), 0 );


Buy = ExRem( Buy, Sell );  //  clear out excesive signals, if we are already bought
Sell = ExRem( Sell, Buy ); //  clear out excesive signals, if we are already sold

PE = ValueWhen( Buy, Ref(BI,-1)); //entry price
DE=ValueWhen(Buy,STDDEV);


SE=PE-DE;
TE=SE-DE;
STP=TE-DE;

BuySignal2=L<Ref(SE,0);

SecondEntry = BuySignal2 AND InFirstPos AND Ref(InFirstPos,-1);
InSecondPos = Flip(SecondEntry, Sell);
SecondTrigger = ExRem(InSecondPos, Sell);
BarsSinceSecondTrigger = BarsSince(SecondTrigger);
SecondTriggerPrice =IIf(BarsSinceSecondTrigger < BarsSinceSell, Ref(SegundaEntrada,-BarsSinceSecondTrigger), 0 );


BuySignal3=L<Ref(TE,0);

ThirdEntry = BuySignal3 AND InSecondPos AND Ref(InSecondPos,-1);
InThirdPos = Flip(ThirdEntry, Sell);
ThirdTrigger = ExRem(InThirdPos, Sell);
BarsSinceThirdTrigger = BarsSince(ThirdTrigger);
ThirdTriggerPrice = IIf(BarsSinceThirdTrigger < BarsSinceSell, Ref(TerceiraEntrada,-BarsSinceThirdTrigger), 0);


Buy = IIf( FirstTrigger, 1, IIf( SecondTrigger OR ThirdTrigger, sigScaleIn, 0  ) );

BuyPrice = IIf( FirstTrigger, Ref(PE,0),IIf( SecondTrigger , Ref(SE,0),Ref(TE,0)));

Sell = ExRem(Sell,Buy);

Filter=FirstTrigger or SecondTrigger or ThirdTrigger or Sell; 
2 Likes