Buy using SigScaleIn with ValueWhen for generating true condition

Hello, Im new with AB. I try many times to undestand how scalein work and already use this site https://www.amibroker.com/guide/h_pyramid.html to undestand , but still its to difficult for me.

and i try use this code

// price
Plot (C, "Price", colorAqua, styleBar );

// rules
Hi = Ref (HHV (H, 10 ), -1);
Lo = Ref (LLV (L, 10), -1);
Plot (Hi, "atas", colorWhite, styleLine );
Plot ( Lo , "bawah ", colorTurquoise, styleLine );

//first buy
Buy1  = L <= lo ;
Sell1 = H >= hi ;
Buy1  = ExRem (buy1, Sell1);
sell1 = ExRem (sell1, buy1);

//second buy
BuyVal  = ValueWhen (Buy1, lo) ;
Buy2    = Buy1 < (BuyVal / 1.05) ; // buy2 when going down -5% from buy1
Sell2   = Sell1;
Buy2    = ExRem (buy2, buy1);

//use scale in
Buy  = IIf(Buy1, 1, IIf(Buy2, sigScaleIn, 0)) ;
Sell = sell1 OR Sell2;

//plot arrow
PlotShapes (IIf (Buy1, shapeUpTriangle, shapeNone), colorWhite, 0,L, -15 );
PlotShapes (IIf (Buy2, shapeUpTriangle, shapeNone), colorYellow, 0,L, -15 );
PlotShapes ( IIf (Sell, shapeDownTriangle, shapeNone), colorBlue, 0, H, -15);

Capture

of course its generating wrong result in deatil log, and i know mycode is wrong, maybe somebody can help me to better code. thank you

Is this what you are trying to achieve?

(I haven't ran this through the backtester as yet - you haven't provided any backtester details or set options).

// price
Plot (C, "Price", colorAqua, styleBar);

// rules
Hi = Ref (HHV (H, 10 ), -1);
Lo = Ref (LLV (L, 10), -1);
Plot (Hi, "atas", colorWhite, styleLine);
Plot (Lo , "bawah ", colorTurquoise, styleLine);

//first buy
Buy1  = L <= Lo;
Sell1 = H >= Hi;

Buy1  = ExRem (Buy1, Sell1);
Sell1 = ExRem (Sell1, Buy1);

BuyVal  = ValueWhen (Buy1, Lo);

//second buy
Buy2    = Low < (BuyVal / 1.05); // Buy2 when going down -5% from Buy1
Sell2   = Sell1;
Buy2    = ExRem (Buy2, Buy1);

//use scale in
Buy  = IIf(Buy1 OR Buy2, sigScaleIn, False);
Sell = Sell1;

//plot arrow
PlotShapes (IIf (Buy1, shapeUpTriangle, shapeNone), colorWhite, 0, L, -15);
PlotShapes (IIf (Buy2, shapeUpTriangle, shapeNone), colorYellow, 0, L, -15);
PlotShapes (IIf (Sell, shapeDownTriangle, shapeNone), colorBlue, 0, H, -15);
1 Like

thanks for your help, from your code the arrow is correct, but the SigScaleIn buy is not perform.

aaa

bbb

ccc

@ibenxs,

This is the problem when we can't see the full code! It has been mentioned to you before to post up your full code.

When forum members are asked to post up their full code there is a reason for it!

For example, to help you fully I need to know your 'SetTradeDelay's, your 'SetPositionSize' etc because those inputs effects Scale-In's & Scale-out's.

See

2 Likes

ah....you are righ, i just forget to set it

SetOption("InitialEquity", 100000000 );
SetOption("AllowPositionShrinking", True );
SetOption ("MaxOpenPositions", 30);
PositionSize = -100/10 ;

and your code it perfectly work ! thank you very much !

1 Like

No worries – happy to help.

That’s good you used the ‘Detail Log’ to see what the Backtester was doing. But a tip for you going forward is don’t skim over the report. In this case if you take a close look at each line on your report you will see at the top with the first entry on the third line it reads “RALS requested size is greater than 100% of the bar volume. Limit applied.” So that is a red flag telling you there is a problem somewhere and what the problem is related to.

1 Like

“RALS requested size is greater than 100% of the bar volume. Limit applied.”

I change it from 10% to 100% , if you have any advice for me ( out of topic ) fell free and i'll be happy for that, thanks

Your code still requires further inputs.

What are using for your 'SetTradeDelays'?

What are using for your 'BuyPrice' & 'SellPrice' price arrays?

1 Like

Here is a more complete code. Adjust to your own settings/parameters.

tradeDelay_nBars = 1;
maxPositions = 10;

SetOption("InitialEquity", 100000);                   
SetOption("CommissionMode", 3);              
SetOption("CommissionAmount", .01); 
SetOption("UsePrevBarEquityForPosSizing", IIf(tradeDelay_nBars, False, True)); 
SetOption("AllowPositionShrinking", True);   
SetOption("AllowSameBarExit", False);
SetOption("AccountMargin", 100);                   
SetOption("ActivateStopsImmediately", IIf(tradeDelay_nBars, False, True)); 
SetTradeDelays(tradeDelay_nBars, tradeDelay_nBars, tradeDelay_nBars, tradeDelay_nBars);                             
SetOption("MaxOpenPositions", maxPositions);                
SetPositionSize(100 / maxPositions, spsPercentOfEquity); 
RoundLotSize = 1;

PositionScore = ROC(Close, 50);

// price
Plot (Close, "Price", colorAqua, styleBar);

// rules
Hi = Ref(HHV (H, 10), -1);
Lo = Ref(LLV (L, 10), -1);
Plot(Hi, "atas", colorBlue, styleLine);
Plot(Lo, "bawah", colorBlue, styleLine);

Buy = Sell = Short = Cover = 0;
BuyPrice = IIf(tradeDelay_nBars, Open, Close);
SellPrice = IIf(tradeDelay_nBars, Open, Close);

// first buy
buySignal_1 = Low <= Lo;
BuyVal = ValueWhen(buySignal_1, BuyPrice);

// sell
sellSignal = High >= Hi;

buySignal_1 = ExRem(buySignal_1, sellSignal);
sellSignal = ExRem(sellSignal, buySignal_1);

// second buy
buySignal_2 = Low < (BuyVal / 1.05) ; // buySignal_2 when going down -5% from buySignal_1
buySignal_2 = ExRem(buySignal_2, buySignal_1);
buySignal_2 = buySignal_2 AND NOT buySignal_1; // If both both buy signals occur on same bar then buySignal_2 is discarded

// use scale in
Buy = IIf (buySignal_1 OR buySignal_2, sigScaleIn, False); // AB treats all scale-ins as 1 open position, AB automactically treats intial buy as buy
Sell = sellSignal;

// plot arrow
PlotShapes(IIf(buySignal_1, shapeUpTriangle, shapeNone), colorBlue, 0, L, -15);
PlotShapes(IIf(buySignal_2, shapeUpTriangle, shapeNone), colorOrange, 0, L, -15);
PlotShapes(IIf(Sell, shapeDownTriangle, shapeNone), colorRed, 0, H, -15);

// Explorer
Filter = 1;

AddColumn(buySignal_1, "Buy Signal 1");
AddColumn(BuyVal, "Buy Signal 1 Value", 1.3);
AddColumn(buySignal_2, "Buy Signal 1");
AddColumn(sellSignal, "Sell Signal");
2 Likes

this is awsome! thanks a lot!:smile::smile::grin:

1 Like

Don't do that for real-time trading!

See

1 Like

Please replace previous code with this one.

Previous code in certain circumstances allowed 'buySignal_2' to occur without 'buySignal_1' occuring first after latest Sell signal.

tradeDelay_nBars = 1;
maxPositions = 10;

//SetOption("InitialEquity", 100000);                   
//SetOption("CommissionMode", 3);              
//SetOption("CommissionAmount", .01); 
SetOption("UsePrevBarEquityForPosSizing", IIf(tradeDelay_nBars, False, True)); 
SetOption("AllowPositionShrinking", True);   
SetOption("AllowSameBarExit", False);
SetOption("AccountMargin", 100);                   
SetOption("ActivateStopsImmediately", IIf(tradeDelay_nBars, False, True)); 
SetTradeDelays(tradeDelay_nBars, tradeDelay_nBars, tradeDelay_nBars, tradeDelay_nBars);                             
SetOption("MaxOpenPositions", maxPositions);                
SetPositionSize(100 / maxPositions, spsPercentOfEquity); 
RoundLotSize = 1;

PositionScore = ROC(Close, 50);

// price
Plot (Close, "Price", colorAqua, styleBar);

// rules
Hi = Ref(HHV (H, 10 ), -1);
Lo = Ref(LLV (L, 10), -1);
Plot(Hi, "atas", colorBlue, styleLine);
Plot(Lo, "bawah", colorBlue, styleLine);

Buy = Sell = Short = Cover = 0;
BuyPrice = IIf(tradeDelay_nBars, Open, Close);
SellPrice = IIf(tradeDelay_nBars, Open, Close);

// first buy
buySignal_1 = Low <= Lo;
BuyVal = ValueWhen(buySignal_1, BuyPrice);

// sell
sellSignal = High >= Hi;

buySignal_1 = ExRem(buySignal_1, sellSignal);
sellSignal = ExRem(sellSignal, buySignal_1);

// inTrade
inTrade = Flip(buySignal_1, sellSignal);

// second buy
buySignal_2 = Low < (BuyVal / 1.05) ; // buySignal_2 when going down -5% from buySignal_1
buySignal_2 = ExRem(buySignal_2, buySignal_1);
buySignal_2 = inTrade AND buySignal_2 AND NOT buySignal_1; // If both both buy signals occur on same bar then buySignal_2 is discarded

// use scale in
Buy = IIf (buySignal_1 OR buySignal_2, sigScaleIn, False); // AB treats all scale-ins as 1 open position, AB automactically treats intial buy as buy
Sell = sellSignal;

// plot arrow
PlotShapes(IIf(buySignal_1, shapeUpTriangle, shapeNone), colorBlue, 0, L, -15);
PlotShapes(IIf(buySignal_2, shapeUpTriangle, shapeNone), colorOrange, 0, L, -15);
PlotShapes(IIf(Sell, shapeDownTriangle, shapeNone), colorRed, 0, H, -15);

// Explorer
Filter = 1;

AddColumn(inTrade, "In Trade", 1.0);
AddColumn(buySignal_1, "Buy Signal 1", 1.0);
AddColumn(BuyVal, "Buy Signal 1 Value", 1.3);
AddColumn(buySignal_2, "Buy Signal 2", 1.0);
AddColumn(sellSignal, "Sell Signal", 1.0);
1 Like

Can you explain to me this code,

BuyPrice = IIf(tradeDelay_nBars, Open, Close);

It means if you are using trade delays of zero, your BuyPrice will be the Close price of the Buy bar otherwise your BuyPrice will be the Open price of the execution bar.

1 Like