The past couple of weeks I have seen several questions looking for help with Scale trading. I suggest @tomasz create a new up to date Official User Base article with several examples. TJ, I know you are busy but this appears to be an area many users are looking for guidance (myself included).

First place for users to start is in the User Guide, https://www.amibroker.com/guide/h_pyramid.html

Her is my initial effort to summarize Scaling code references. I hope anyone with some ideas will contribute to this thread so we can build up a good reference for users.

There appear to be several very different methods of coding scale trading in AmiBroker. Perhaps we could create a thread that gradually adds examples and solves problems for the various possible methods.

As far as I can tell, I have seen three general methods.

1. Using PositionSize
2. Writing loops
3. Using the CBT

I am listing one of my possible examples based heavily on the work done by â€śPaulâ€ť and posted in the User Library, (hey Paul where are you?!) http://www.amibroker.com/members/library/formula.php?id=1380

My first simple example began as I was creating an ensemble of inputs to create a final signal score. Depending on the score then I would decide to Scale Up or Scale Down. My actual score generating process will not be shared (secret sauce) so I am using a Random number generator to create â€śscoresâ€ť to illustrate the Scale trading concept.
Warning, I am not a programmer by trade so this may all be crap, caveat emptor.

``````// Scale trading for score of 0 to 100 PositionPercent v2.0 using the Thresholds

TopThreshold = 75;
MidThreshold = 50;
BottomThreshold = 33;

score = 0;
/////////////////////////////////////////////////////////////////
// Ensemble will ultimately generate a "score" between 0 and 100
/////////////////////////////////////////////////////////////////
// not revealing the secret formula, just use a random number to create a new "score" each bar

for( i = 0; i < BarCount; i++ )
{
score = 100 * mtRandomA(); // generate random scores to test the scaling code
}

PositionPercent = 	IIf( score > TopThreshold, 100,							// 100% invested if score above TopThreshold
IIf( Score > MidThreshold, 66,						//  66% invested if score above MidThreshold
IIf( score > BottomThreshold, 33, 0 ) ) );	//  33% invested if score above BottomThreshold

// due to AmiBroker quirks, the scale UP and the scale DOWN take place in the BuyArray (nothing in the Sell array until total/final exit)
BuyArray = 	IIf( PositionPercent == 0, 0, 																	// Take no action if there is zero position percentage
IIf( PositionPercent > Ref( PositionPercent, -1 ) AND Ref( PositionPercent, -1 ) == 0, 1, 	// Moving to higher percentage from zero, so make an initial buy
IIf( PositionPercent > Ref( PositionPercent, -1 ), sigScaleIn, 						// Moving to higher percentage so scale up
IIf( PositionPercent < Ref( PositionPercent, -1 ), sigScaleOut, 					// Moving to lower percentage so scale down
0 ) ) ) );
SellArray = PositionPercent == 0;	// Exit when there is zero position percentage

SetPositionSize( IIf( BuyArray == sigScaleOut, 100 - PositionPercent, PositionPercent ), spsPercentOfEquity ); // Set position size for each bar

Sell = SellArray;

////////////////////
// Explore
////////////////////
Filter = 1;
AddColumn( score, "score", 1.1, colorDefault, colorLightYellow );
// reveal that Scale Up creates a value of 99,998
// and that Scale Down has a value in the BuyArray of 99,999
dynamic_color = IIf( PositionPercent > Ref( PositionPercent, -1 ), colorLime, IIf( PositionPercent == Ref( PositionPercent, -1 ), colorDefault, colorRose ) );
AddColumn( SellArray, "SellArray", 1.0, colorDefault, IIf( SellArray, colorRed, colorDefault ) );

``````

Unrelated sidenote as I am again sleep deprived and groggy. I don't use charts for much and isn't amazing how many different methods our forum users have for trading? Some use charts and patterns exclusively, I myself don't even need a chart for most of my systems but lord I love the pretty pictures!! So if anyone want to add some charting code to the above please feel free to add it!

The exploration from the above code produces this type of output.

And in a backtest you look for the scaling either here,

or in the detailed log,

There are not that many official examples that I have found but here are a couple of starting points.

A list of numerous resources some of which I will repeat below,
Scale in as percent of allocated equity and that is followed by a nice example by the highly recommended @helixtrader

A CBT Official Knowledge Base example
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

A CBT example

I hope more users will contribute to the thread, but in a few days I will try to add to it if I sense enough interest ( by the number of â€ślikesâ€ť, I need to be loved so tick of those hearts if you want more )

Of course if you think it is not particularly useful there should be a way to tick off some negative symbol for fun,

I look forward to some of our experts adding some looping examples (an area of personal weakness though I have a few to add)

31 Likes

Larry, I have already liked your post, but this time I send some more hearts on behalf of unregistered users

Have a great weekend!

4 Likes

@Milosz thanks for all the love! Wow! You have a great weekend too. I have just been experimenting with scaling and am glad members liked the post. I think the links I posted have many good examples and I was going to post a few of my own here but they are not significantly different than those found in the links.

A slightly different idea below is a somewhat more conventional scale code. It is a modification of an originally simple code by the excellent Howard Bandy.

``````
Float = 100000;
MaxPositions = 10;

////////////////////////
// Backtester Options
///////////////////////
SetOption( "CommissionAmount", 0.0 );
SetOption( "MaxOpenPositions", MaxPositions );
SetOption( "InitialEquity", Float );
SetOption( "AllowSameBarExit", True );
SetOption( "AllowPositionShrinking", True );
SetPositionSize( 100 / MaxPositions, spsPercentOfEquity );

entryDelay = 1;
exitDelay = 1;
SetTradeDelays( entryDelay, exitDelay, entryDelay, exitDelay );
BuyPrice = ShortPrice = IIf( entryDelay == 0, Close, Open );
SellPrice = CoverPrice = IIf( exitDelay == 0, Close, Open );

/* PositionSize reminder from the user guide:
values below -2000 encode share count,
values between -2000 and -1000 encode % of current position
values between -1000 and 0 encode % of portfolio equity
values above 0 encode dollar value

Some examples,
PositionSize = -2010; means you want to buy 10 shares
PositionSize = -10; means you want to buy 10%
PositionSize = 100; means buy \$100 worth
*/

// in this example each Buy is allocated 25% of available equity
PositionSize = -25;

Buy = IIf( H > Ref( HHV( H, 5 ), -1 ), sigScaleIn, 0 );

Sell = IIf( L < Ref( LLV( L, 20 ), -1 ), 1, 0 );

//	Remove the extra Sell signals but NOT the extra Buy signals.
Sell = ExRem( Sell, Buy );
``````

A detailed log will help reveal what type of scaling is taking place,

2 Likes

Oops, form the first post in this thread, I think I made a mistake on one line. An earlier version had the nested immediate IF's in a different order so consider changing,

``````SetPositionSize( IIf( BuyArray == sigScaleOut, 100 - PositionPercent, PositionPercent ), spsPercentOfEquity ); // Set position size for each bar
``````

To a simple,

``````SetPositionSize( PositionPercent, spsPercentOfEquity ); // Set position size for each bar

``````

I don't see this column on my backtest. How can get this column to show?

It is displayed by default when Report style is set to "Trade list". Which version of the program are you using?

I'll tack this question on here rather than start a new thread.

I can't figure out what's wrong with the following set up. Going through the trades manually, things seem ok. Buying every bar's close, and selling according to the sell line or a profit stop. I don't want to remove excess signals with exrem. This is a single instrument backtest.

There will be a simple explanation but it's eluding me. Thanks.

``````
SetBacktestMode( backtestRegularrawMulti );
SetOption("MaxOpenPositions", 5 );
SettradeDelays( 0, 0, 0, 0 );
Sell =  C<Ref(LLV(L,20),-1);
ApplyStop(stoptypeProfit,stopModePercent,5);
``````

You didn't say how this code is not working, but when I ran it I noticed that only one position at a time was being opened, and the single position was using all of my equity because you never specified a position size. Perhaps you want to do something like this:

``````SetPositionSize(20,spsPercentOfEquity);
``````

With that change, 5 positions at a time are open.

1 Like

6.20.0

I don't see it on mine. I'll keep experimenting.

@burger Perhaps your Analysis window columns are not set properly. Review this, http://www.amibroker.com/kb/2014/10/19/how-to-customize-list-view-columns/

2 Likes

That was it! Thank you.