Amibroker: How To Add Position Sizing Using AFL

Line1Beginners Course  |  Intermediate Course  |  Amibroker Course

Setting Position Sizing In Amibroker Formula Language

If you’ve been a part of the stock market for any amount of time, you will know that one of the most important parts to a trading and investment plan is your position sizing.

This video shows us how to add some position sizing rules to any trading system when coding in Amibroker Formula Language (AFL).

How We Enter Our Code:

  • From the main screen in Amibroker, open the formula Editor by going to Analysis > Formula Editor.
  • Type in the following code:

SetOption(“MaxOpenPositions”, 20);

SetPositionSize(5, spsPercentOfEquity);

What This Code Means

The code above is setting our maximum open positions to 20 at a time, and our position size per stock to 5 percent of our total equity.

We can use the “SetOption” command for other things as well – typically if you go through the settings menu in the Automatic Analysis window you can see what they are called, and simply call them the same thing in your code (for example, using our “MaxOpenPositions”).

I hope this helps, happy trending and enjoy!

Dave McLachlan

Beginners Course  |  Intermediate Course  |  Amibroker Course Home

Videos in the FREE Amibroker Course:

  1. Amibroker: How To Import A Watchlist With A CSV File
  2. Amibroker: How To Backtest A Single Stock Or Index
  3. Amibroker: How To Backtest a Portfolio or Group of Stocks
  4. Amibroker: How To Optimize A Trading System
  5. Amibroker: How To Scan The Market For Buy And Sell Signals
  6. Amibroker: How To Use Explore To Find And Filter Stocks
  7. Amibroker: How To Add Position Sizing Using AFL
  8. Amibroker: How to Add An Index Filter To Your Trading System Using AFL
  9. Amibroker: How To Plot Your Trading System On A Chart (incl. Buy/Sell Arrows)
  10. Amibroker: How to Use the If, Then Else Function (IIF)
  11. Amibroker: How to Code the ATR Trailing Stop (Chandelier) using ApplyStop
  12. Amibroker: How to Plot ApplyStop on a Chart
  13. Amibroker: How to Code Van Tharp Position Sizing (Fixed Fractional)
  14. Amibroker: How to “Scale In” to Positions (1) – Buying Once a Month
  15. Amibroker: How to “Scale In” to Positions (2) – Pyramiding Three Times
  16. Amibroker: How to Use “Cross”, When One Thing Crosses Another
  17. Amibroker Advanced: How to Compare Equity Curves

FREE Trading System Video Lessons:

  1. 7 Free Trading Systems and Their Returns From The Last 13 Years
  2. Seven Full Trading Systems Revisited, With Completely New Data
  3. Trading System: How to Create A Buy and Hold Trading System
  4. Trading System: How to Code “Sell In May And Go Away”
  5. Trading System: How to Code a Moving Average Channel That Made 23% p.a
  6. Trading System: How to Code Highest Highs As Used In 52 Week Highs Or Turtle Trading System
  7. Trading System: How to Code a Bollinger Band Breakout Trading System
  8. Trading System: How to Code a Moving Average Crossover That Returned 28% per annum
  9. Trading System: How to Refer to Previous Signals For MAup Trading System
  10. Trading System: How to Code the Leap Of Faith (Gaps) Trading System That Returned 24% p.a.
  11. Trading System: How to Code a Short Selling System (Plus Results)
  12. Trading System: Meb Faber Trading System Using Different Timeframes
  13. Trading System: How to Code the Three Billion Dollar Trading System

FREE Amibroker Q & A Videos:

  1. Amibroker Q & A: How To Do A Monte Carlo Test? (on ALL versions)
  2. Amibroker Q & A: Visualising Monte Carlo Data with Histograms and Scatter Plots
  3. Amibroker Q & A: How to Create the 52 Week High/Low Index with AddToComposite
  4. Amibroker Q & A: How to Code Trend Pilot: Five Consecutive Days Above or Below a Moving Average
  5. Amibroker Q & A: Three “Hacks” To Improve Your Coding Skills
  6. Amibroker Q & A: How to Remove Excess Signals

Read Related Articles:




  And, subscribe for free to stay in touch and receive the latest updates:

Enter Your Email Address:

.
(Check Your Junk Mail, Just In Case)

June 10, 2013  Tags: , ,   Posted in: Amibroker, Amibroker Course

4 Responses

  1. Riccardo - June 18, 2013

    question: what happens when you have 19 slot filled and on the last slot, on a particular day, you have 2 buy signals from 2 different stocks. What stock is chosen? Randomly? or the first one in alphabetical order following the list?
    Thanks – excellent work.

  2. Dave McLachlan - June 18, 2013

    Hey Riccardo,

    Great question – as far as I’m aware it will choose the next trade in sequence alphabetically from your watch-list.

    Thanks – Dave

  3. Dave McLachlan - August 8, 2013

    Great email from reader NC about doing Van Tharp Position Sizing. He came up with the following code (this is based on a 2% position size from entry to stop loss):

    SellSignal = *Your sell signal or initial stoploss here*

    Stopspread = ( C – Sellsignal ) / C;
    SetPositionSize (2 / stopspread, spsPercentOfEquity);

    I wouldn’t recommend using this to optimize at all, but it does give an idea of how Vap Tharp style position sizing can affect your trading system.

    – Dave

  4. Eric - March 3, 2015

    Hi Dave, thanks for providing such a wonderful platform for amibroker newbies. I have learned a lot from watching your videos.

    I have come across several problems and I still couldn’t find out after spending so much time. Hopefully, you can give me a hand.

    I am trying to make reference to a price at a specific time. For instance, when time is at 930am, I want to assign a variable to equal the close of the 930am minute bar. Then I can use this price as reference for later use. I’ve set up something like below,

    refprice = iif (TimeNum() == 093059, C, 0);

    but it doesn’t seem to work. How do you suggest to write it in codes?

    Also, is there a way to “print” the variable, refprice, so that I can see if any value is being assigned to it?

    Lastly, once a trade is entered, is there a function to call the “entry” or “exit” price?

Leave a Reply