Forex Blog

First-hand Forex trading experience and information about foreign exchange market that will be useful to traders

Archives

Script to Place Orders with Position Size Calculator

April 7, 2015 (Last updated on May 18, 2017) by Andriy Moraru

If you have thought that trading based on the accurately calculated position size cannot get any better, think again. Now you can use a MetaTrader script to place orders based on the calculations of the Position Size Calculator indicator. The script will detect the position size calculated by the indicator and will place a trade accordingly, whether it is an instant order or a pending one. For pending orders, it will also detect whether a stop or limit one should be used.

You can download the .mq4 file (or .mq5 if you are using MetaTrader 5) of the script or the same source code in .zip (MT5 in zip). You will have to place it in your MetaTrader’s /MQL4/scripts/ folder (or /MQL5/scripts/ in case of MT5).

Adding a hotkey (keyboard shortcut) to the script will make placing trades even faster. Here is the short tutorial on how to add a hotkey to the script in MetaTrader:

1. Right click on a script in the Navigator subwindow of your MetaTrader:

MetaTrader - How to Set Hotkey for a Script

2. Then set the control key for your shortcut. It is either Ctrl or Alt:

Selecting a Control Key for a Hotkey

3. Now, you can set the main trigger key:

Setting the Main Key for a Keyboard Shortcut

4. Done. You can now press the key combination to invoke the script at any time to open your trade immediately.

PS: Of course, you should enable autotrading in your platform for the script to work whether you start it with a mouse click or a key press:

MetaTrader - Allowing Autotrading for Scripts

If you have any comments or questions about this trade placing script based on Position Size Calculator output, please post them using the form below.

35 Responses to “Script to Place Orders with Position Size Calculator”

  1. Ash

    This is a very nice script. Thanks for sharing it.
    I like to put a comment in my trades. So I am wondering if it is possible to have Order window opened and filled by this script but let me put a comment and then place it myself.

    Reply

    Andriy Moraru Reply:

    Unfortunately, that is not possible. You can only change the comment by editing the script’s source code prior to executing it.

    Reply

  2. David Thompson

    Hello Andriy – I cannot get the position size indicator to display the correct levels for entry, stop loss and take profit – even though they are correctly entered into the indicator settings. I have a screen shot which I can email you to show what has gone wrong. Just need to know where to send it to.

    Reply

    Andriy Moraru Reply:

    Just set DeleteLines input parameter to true. It will fix the problem.

    Reply

  3. David Thompson

    OK – That has fixed the display but now the lot size has disappeared – so what next ?
    Thanks

    Reply

    Andriy Moraru Reply:

    Could you please show a screenshot?

    Reply

  4. David Thompson

    Sorry Andriy – its my fault – it obviously updates the screen when the asset price changes. I was using this when the market was very quiet and very few price changes !!
    Brilliant Job – this will be SO useful . THANKYOU

    Reply

  5. Boh

    Hi Andriy, good day! I downloaded this script but the diamond in the lower right portion of the program icon is in gray.

    I have already downloaded the mq4 file and transferred it to MQL4/Scripts.

    So how do I fix this? Thank you!

    Reply

    Andriy Moraru Reply:

    Can you open it in MQL Editor? If yes, can you compile it there? Are there any errors?

    Reply

  6. Boh

    I right clicked on PSC-trader and clicked modify. It opened MetaEditor. I clicked on the green play button and I was redirected to MT4. However, a prompt showed stating Position Size Calculator not found.

    However, i do have the PositionSizeCalculator_separate under indicator.

    What should I do next? Thank you in advance!

    Reply

    Andriy Moraru Reply:

    To use PSC-trader, you have to attach PositionSizeCalculator_Separate or PositionSizeCalculator_Main to the chart, then launch PSC-trader script (just double-click it, do not go to ‘Modify’).

    Reply

  7. Boh

    Thanks for your reply, Andriy.

    I attached positionsizecalculator_separate to the chart; then added my price parameters.

    Afterwards, I double clicked on psc-trader script but my pending orders are not showing up under the trade tab. Is this normal? Thanks!

    Reply

    Andriy Moraru Reply:

    Does it say anything in the Experts tab of the Terminal window?

    Reply

  8. Boh

    After I attached positionsizecalculator_separate to the chart, the following messages appeared on the experts tab:

    positionsizecalculator_separate gbpusd,h1: initialized
    custom indicator positionsizecalculator_separate gbpusd,h1: loaded successfully

    I then double clicked on psc-trader, and the additional messages appeared on experts tab:

    script psc-trader gbpusd,h1: loaded successfully
    psc-trader gbpusd,h1: initialized
    psc-trader gbpusd,h1: detected position size: 0.11
    psc-trader gbpusd,h1: detected entry level: 1.64400
    psc-trader gbpusd,h1: detected entry type: pending
    psc-trader gbpusd,h1: detected stop-loss level: 1.60000
    psc-trader gbpusd,h1: detected take-profit level: 1.70000
    psc-trader gbpusd,h1: execution failed. error: unknown error
    psc-trader gbpusd,h1: uninit reason 0
    script psc-trader gbpusd,h1: removed

    Is this a result of my using a demo account? I’m using fxcm mt4.

    Thank you for the time answering my questions!

    Reply

    Andriy Moraru Reply:

    Some possible reasons:
    1) Did you enable AutoTrading?
    2) Does your FXCM MT4 account allow opening pending orders with SL and TP? Some brokers only allow setting SL and TP to existing pending orders, not to open pending orders with SL and TP.
    3) Does your FXCM MT4 allows opening orders with size 0.11 (can you open it manually)?

    Reply

  9. Boh

    Hey Andriy,

    1) I have enabled AutoTrading.
    2) I was able to manually place a pending order with SL and TP
    3) I was able to place an order for 0.11

    I think i’ll just switch to another broker. I was wondering if you could tell which broker have you used your script? Thank you!

    Reply

    Andriy Moraru Reply:

    I can use it with EXNESS and Alpari demo accounts without a problem, but the broker should not be causing any such errors. Could you please replace the line #148 in the code:

    Print("Execution failed. Error: ", ErrorDescription(GetLastError()), ".");

    with this:

    Print("Execution failed. Error: ", GetLastError(), ".");

    Then compile (F7) and try to run it again in MT4. Then, please tell me what did it output in the Experts tab.
    Hopefully, knowing the exact error code I will be able to fix this.

    Reply

  10. Boh

    In my PSC-Trader.mq4, Line 148 contains this line:

    else Print(“Order executed. Ticket: “, ticket, “.”);

    Which is different from the line you just posted above (in my PSC-Trader.mq4, it is showing in Line 147).

    I did your instruction but is still not working.

    Reply

    Andriy Moraru Reply:

    What did it say in the Experts tab this time?

    Reply

  11. Boh

    I just reinstalled it and it started working! Your script is great!! Thank you for your fast response!!

    Reply

    Andriy Moraru Reply:

    I am glad that it works for you now!

    Reply

  12. Cris

    Hi,

    How can I save PSC on my MT4 from the start please, I’m using FXCM demo account.
    Thanks a lot.

    Reply

    Andriy Moraru Reply:

    What do you mean by “saving PSC”?

    Reply

  13. Cris

    Position Size Calculator

    Reply

    Andriy Moraru Reply:

    You can save Position Size Calculator by right-clicking on the download link (on this page) with your mouse and choosing ‘Save Link As’. Then you can unzip the downloaded archive and copy the files to /MQL4/Indicators/ of your platform (you can quickly access that folder by clicking File->Open Data Folder from within the platform).

    Reply

  14. Paras Sharma

    Hi Andriy,

    Thanks for sharing your position sizing script & indicator.

    I have something very confidential to discuss with you hence, can I please have your email for that purpose?

    Regards,
    Paras.

    Reply

    Andriy Moraru Reply:

    You can contact me via this form:
    https://www.earnforex.com/contact/

    Reply

  15. Saikat

    Hello andrily i have recently installed your psc on my Mt4 but when i try to double click it to run it it just doesn’t seem to work on mt4. it says position size calculator not found then i installed position size as well on script folder. still now working what have i done wrong.
    thanks alot

    Reply

    Andriy Moraru Reply:

    Position Size Calculator is an indicator and should be installed in Indicators folder. The latest version can be downloaded from this page: https://www.earnforex.com/metatrader-indicators/Position-Size-Calculator

    PSC-Trader is a script that is used to open trades based on the output of Position Size Calculator. The script should be installed into Scripts folder. The latest version can be downloaded from here: https://www.earnforex.com/blog/files/PSC-Trader.mq4

    PSC-Trader script will not work if Position Size Calculator indicator is not attached to the chart when you run the script on it. It will output “Position Size Calculator not found!” It means that you have to attach Position Size Calculator indicator to the same chart before running the script.

    Reply

  16. Saikat

    thanks alot for your unconditional support Andriy. i did as you said now its saying stoploss line not found.

    Thanks Again

    Reply

    Andriy Moraru Reply:

    Did you delete it or rename it?

    Reply

  17. Gary

    Thanks for such a good indicator.

    I want the stop loss based on yesterday’s high or yesterday’s low of the daily candle.

    How do I change this on the indicator?

    Gary

    Reply

    Andriy Moraru Reply:

    If we are talking about Position Size Calculator, you just move the stop-loss line to the yesterday’s high or low. Or you can type it into stop-loss field in the panel.

    Reply

  18. Pier

    Hi Andriy, does the script work with a zero account balance? Thanks!

    Reply

    Andriy Moraru Reply:

    That depends on how you set up your Position Size Calculator indicator. If you set some fixed money risk instead of account percentage, then it will calculate a correct position size value. Then, you can use the PSC-Trader script to try opening that position. Now, it all depends on how your broker functions – does it allow opening trades with zero account balance?

    Reply

Leave a Reply

required
required (will not be published)
optional