How to average down (buy when position value drops x percent)?

I'm looking for help on how to add to a position if the position suffers a defined % loss (say a 50% loss for the example), and I would like to limit the number of additional purchases to 1 (for a maximum of 2 purchases contributing to an open position).

I explored using EntryPrice = ValueWhen (Buy ,BuyPrice) and then calculate the loss to trigger the 2nd purchase. I also explored using ApplyStop to calculate the loss and trigger the 2nd purchase when the desired loss was achieved.

So far nothing has worked, or I'm using the above incorrectly.

I just need someone to point me in the right direction. It seems like this should be simple and common, so I'm clearly missing something. Thanks.


Only users with "Verified Badge" are allowed to post on this forum.

Search "Verified Badge" for more information on how to get verified.


Thanks. Now verified. Do you have any help to offer regarding my question?

1 Like

We can't see your computer screen!

Post up your full current code.


Read this AB article for a starting point.
Pyramiding (scaling in/out) Portfolio-level back testing (

Be sure to follow the advice of the first sentence in that tutorial, " IMPORTANT: Please read first Tutorial: Backtesting your trading ideas article and Portfolio Backtesting."
You will want an understanding of the Backtester before attempting to master scaling in/out.

@nsm51 @ghanson,

Thanks for responding to my question. I did read those articles, as well as other resources. Here's what I have so far... I can't seem to get the sigScaleIn to fire. Perhaps I'm approaching the problem the wrong way and there's a better solution?

Buy = BuyCondition1;   // Whatever condition you like, as it's beside the point.

Buys = Cum(Buy);   // Counting 'Buy' since I want to limit ScaleIn to occur no more than once 

PurchasePrice = ValueWhen(Buy,BuyPrice);   // Defining the purchase price

AverageDown = 0.5;   // Defining the loss amount for ScaleIn

IIf(Buys == 1 AND Ref(Close, -1) / PurchasePrice <= AverageDown, sigScaleIn, 0);

Sell = SellCondition1;   // Whatever condition you like

Is that your full code?


Here is a set of code. The first purchase gets made, but it doesn't make the second ScaleIn purchase. Any help would be appreciated.

InitialEquity = 100000;
TradeSizeDollars = 1000;
TradeSizePercentEquity = 1;
MaxOpenPositions = 500;
ChartPeriod = 10000;

RoundLotSize = 1;
BuyPrice = Open;
SellPrice = Open;

SetOption( "InitialEquity", InitialEquity );
SetPositionSize( TradeSizeDollars, spsValue );

SetOption( "MaxOpenPositions", MaxOpenPositions );
SetOption( "AccountMargin", 100 );
SetTradeDelays( 1, 1, 1, 1);
SetBacktestMode( backtestRegular );

Buy = 1;

AverageDown = .99;   // Set high to test sigScaleIn, but sigScaleIn doesn't fire
Buys = Cum(Buy);
PurchasePrice = ValueWhen(Buy,BuyPrice);
Buy = Buy + IIf(Buys == 1 AND Ref(Close, -1) / PurchasePrice <= AverageDown, sigScaleIn, 0);

Sell = 0;

That will only be true on the very first bar (because of 'Buys = Cum(Buy)').


1 Like

Thanks, but that's really not what my question is about. Here is my question: Can I use 'ApplyStop' to make a 'Buy' order, not a Sell order? If not, then what approach is recommended to buy more shares when a certain percentage loss is realized?

Do you not want to know why your current code is not working?

You asked this (or am I seeing things?)

I was providing you with the reason why that is the case. For which you could then alter your code to get the desired result.

Refer to the links that have already been provided to you. Do some trial and error to see what works and what doesn't work (use debugging if need be).

Let us know if you still get stuck.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.