# Conditional Scalein for every symbol

Let's consider this example rules:
Buy = RSI(14) < 30 ;
Short = RSI(14) > 70 ;

Exit condition is just a time based condition: after one month we exit the trade:
ApplyStop(stopTypeNBar, stopModeBars, 22, 1);

Now comes the most difficult part

At day10, for example, i enter a long trade on symb1. we know that we will be in the trade for one month. In the meanwhile, if my position go down of -5% i want to scalein and i want to scalein too if my position go down at -7% and -9%.
This for every symbols and until the sell\cover rules will be met.

Concerning position size, it's very simple:
Qty = 4;
SetPositionSize(100/Qty,spsPercentOfEquity);
SetOption("MaxOpenPositions", Qty);

Position size for scalein is 5% for every trade. It follows that max possible leverage should be 160%

So i've used SetOption("MarginRequirement",40);

Below is my attempt to code this logic.

I think that something is wrong because sometime scalein doesn't arrive even if i see a MAE large enough.
I can't understand why.

``````
Qty = 4;
SetOption("InitialEquity",50 * 1000);
SetPositionSize(100/Qty,spsPercentOfEquity);
SetBacktestMode( backtestRegular );
SetOption("MaxOpenPositions", Qty );
SetOption("CommissionMode", 1);
SetOption("CommissionAmount", 0.1);
SetOption("MaxOpenPositions", Qty);
SetOption("InterestRate",0);
SetOption("MarginRequirement",10);
SetOption("UsePrevBarEquityForPosSizing",True);
SetOption("AllowPositionShrinking",True);
SetOption("AllowSameBarExit" , False);

RoundLotSize = 0;

BuySignal =   RSI(14) < 30 ;
ShortSignal =   RSI(14) > 70 	 ;

Short= 0;
Sell = ShortSignal;
Cover = 0;   ;

ApplyStop(stopTypeNBar, stopModeBars, 22, 1);

ShortPrice = C;

PositionScore=(1000-RSI(14));
PositionScore = IIf( Buy, PositionScore, 1/PositionScore );

SetCustomBacktestProc("");
SetOption("UseCustomBacktestProc", True );

EachPosPercent = 100/Qty;
dt = DateTime() ;

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );

if ( Status("stocknum") == 0 )
{
StaticVarRemove( "value*" );

for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
{
StaticVarSet (  "value1"  +  symbol, -5 );
// _TRACE(  " Symbol bla: " + Symbol );
}
}

if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();

bo.PreProcess(); // Initialize backtester

for(bar=0; bar < BarCount; bar++)
{

CurEquity = bo.Equity;

for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() )
{
posprofit = pos.GetPercentProfit();
// _TRACE("posprofit  " + posprofit);
price = pos.GetPrice( bar +1 , "O" );
valuein = (EachPosPercent/100 * 0.20 * CurEquity) ;
//_TRACE(DateTimeToStr( dt[bar]) + "  valuein  " + valuein  + "  posprofit  " + posprofit + "  CurEquity " + curequity + "  EachPosPercent  " + EachPosPercent );
// _TRACE(DateTimeToStr( dt[bar]) + " Symbol: " + pos.Symbol );
//  _TRACE(DateTimeToStr( dt[bar]) + " Static Var Get: " + StaticVarGet("value1" + pos.Symbol) );
//  _TRACE(DateTimeToStr( dt[bar]) + " PosProfit: " + posprofit	);

if(  posprofit < StaticVarGet("value1" + pos.Symbol) AND posprofit > -10 )
{
bo.ScaleTrade( bar, pos.Symbol, True, price, valuein );
StaticVarSet("value1" + pos.Symbol, StaticVarGet("value1" + pos.Symbol) - 2);
//   _TRACE(DateTimeToStr( dt[bar]) + " Static Var Get: " + StaticVarGet("value1" + pos.Symbol) );
}
}

}
bo.PostProcess(); // Finalize backtester
}
``````

Below i show a picture that point out the problem.

In the first underlined trade you saw a MAE of -14% and 1 scalein. But this is not what i would expect.
In my logic, i should take the first scalein at -5%

``````
StaticVarSet (  "value1"  +  symbol, -5 );
``````

and subsequent scalein at -7% and -9%

``````
if(  posprofit < StaticVarGet("value1" + pos.Symbol) AND posprofit > -10 )
{
bo.ScaleTrade( bar, pos.Symbol, True, price, valuein );
StaticVarSet("value1" + pos.Symbol, StaticVarGet("value1" + pos.Symbol) - 2);
}
``````

Please can someone help me to find a solution?

Is not of general interest to find a correct way to code a realistic scalein procedure?

The logic is simple.
I have a signal and enter a trade.
I want to scalein 3 times if this trade go down -5%, -7% and -9%

Thank you

When I run your code, I get trades with up to three scale-ins. You did not include any picture or trade list as referenced in your message, so there's no way for anyone to comment on that.

Regarding the logic in your AFL, it doesn't match what you have described. You said you wanted to scale in when the position has declined in value by 5, 7, and 9%. But in the AFL, you are looking at whether the percent profit of the trade is between -5% and -10%.

You also need to consider what happens when you scale in. Let's say you enter a position at a price of 100. The next day, the price has fallen to 92, an 8% decline. You scale in one time at 92, so your average entry price is now 96. If the price does not fall further, you will not scale in again on the next bar, because 92 is 95.83% of 96, i.e. the decline is no longer more than 5%. If you want to scale in at 5, 7 and 9% below the initial entry price (not the average entry price), then you will need to do some of your own bookkeeping using static variables so that you can compare the current price against the first entry price.

Sorry, i forgot to attach the picture. You can see sometime MAE is very high (-20% and lower) but not even one scalein is generated.

You're right, as always.
Amibroker think in term of average price, but what i'm working for is percentage from the first buy price.