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

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.

69 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

  19. tan yu kim

    i have downloaded the script but i dont know why i cant use it.

    Reply

    Andriy Moraru Reply:

    What happens when you try using it? Are you using it together with the Position Size Calculator indicator?

    Reply

  20. Claudio

    Hi. This is a great tool! Thanks for sharing.
    My question is, regarding short positions. Are you going to include in future updates?

    Reply

    Andriy Moraru Reply:

    It already works fine with short positions. Did you encounter any specific problems with short trades?

    Reply

  21. Neil

    Hi, amazing indicator and script.
    Would there be any chance of adding a new option/feature. I always place my stop loss ‘X’ pips above or below a high or low. So say I place the indicator Stop Loss at a high or low, then it adds or subtracts ‘X’ pips before doing the calculation. Hope this makes sense.

    Reply

    Andriy Moraru Reply:

    I am not sure if I understand you correctly, but you can move the SL/TP lines wherever you want when using Position Size Calculator.

    Reply

    Neil Reply:

    I wanted the indicator too do calculations and enter the SL ‘X’ amount of pips above or below what the SL placement on the chart is. It’s easier when scalping too see and place the SL at a high or low, then the indicator adds or subtracts ‘X’ pips too the SL depending if it’s a buy or sell order. I have managed too modify the source code I think as you made it open source and it looks like it’s doing the correct calculation. Will test when markets are open .

    Reply

    Neil Reply:

    My plan rules dictate the SL to be 3.5 pips above a high for a sell order and 3.5 pips below a low for a buy order. It’s easier and quicker when scalping too place the indicator SL at the high or low, instead off finding and placing the SL 3.5 pips higher or lower than a high or low, then make the indicator and script think the SL is actually 3.5 pips higher or lower. Hope this makes sense.

    Reply

    Andriy Moraru Reply:

    OK, now I understand. It is easier just to code a script, which when triggered would move the lines to X pips above/below the high/low.

    Reply

    Neil Reply:

    If you only change the script, then it pulls the wrong position size calculation from the indicator though. I have managed too change both since my original post.

    Reply

    Andriy Moraru Reply:

    I did not mean changing the PSC-Trader script. I meant coding a completely separate script, which would adjust the SL and TP lines.

    Reply

  22. Bob

    Hi, this is really great thank you!
    I installed the script on mt5, and also have the position size calculator, however, when i click on PSC Trader, it says position size calculator not found

    Reply

    Andriy Moraru Reply:

    Did you attach the Position Size Calculator indicator to the chart where you try to launch the PSC-Trader script?

    Reply

  23. Sia

    Hi!
    I want to run the script to place order, but it doesn’t work!
    In the experts window, I got this:
    PSC-Trader NZDUSD,M15: Execution failed. Error: trade is disabled.

    I compiled the script and enabled the AutoTrading. I can also manually place the order, but not with the script.

    Another issue is that Position Sizing Calculator settings reset to the default whenever I open it!
    I change the settings from the inputs tab (line colour, line style and so on) for position sizing calculator, (even save it) but when I close the calculator and open it again, it reset and I need to either set again or load what I saved before!
    Why it does not keep the settings I made?!

    Reply

    Andriy Moraru Reply:

    Regarding PSC-Trader: Could you please make a screenshot of the entire platform with Experts tab open when you try to launch PSC-Trader?

    Regarding PSC: The indicator forgets all settings when you close it. There are three ways to overcome this problem:

    1. Save settings into a .set file and load them when you attach the indicator again.
    2. Do not close the indicator but rather minimize it.
    3. Save the indicator with its settings into a chart template and load the chart template when you need it back.

    Reply

    Sia Reply:

    Here you are:
    http://i63.tinypic.com/20aduux.jpg

    Reply

    Andriy Moraru Reply:

    Does your broker allow trading with expert advisors? Are you able to open trade with other scripts or EAs? Otherwise, the symbol is closed for trading, but you say that you are able to open the trades manually, so that should not be the case. Most probably, your account type does not allow EA execution.

    Reply

  24. Sia

    Hello and thanks for your reply.
    A friend of mine with the same broker use this script without any problem!

    Reply

    Andriy Moraru Reply:

    That could be a difference in the account type or account permissions. Are you able to use other trading scripts or expert advisors in that account? If not, you should contact the broker about the issue.

    Reply

  25. Mani

    Hello and many thanks for this great, valuable and useful script.
    I want to know how the default value of Entry, SL and TP will be calculated here?
    Is it possible to link the SL and TP to the entry price? For instance, whatever Entry price assigned, then SL assigned to be 10 pips (for example) below the entry and TP 15 pips above the entry.
    Is this possible?

    In addition, Is it possible to assign the Entry 10 pips (for example) above/below the current price by default?

    Thanks again.

    Reply

    Andriy Moraru Reply:

    The PSC-Trader is meant to be used only with the Position Size Calculator indicator. SL and TP are set using that indicator.

    Reply

  26. Mani

    Thanks for your reply.
    I know the true reason for the position sizing. I just want to know if this is possible or not?

    In this case,

    Please kindly let me know how the position sizing assign the default values to the Entry, SL and TP?

    In addition, is it possible to assign SL and TP based on Pips distance and not Price?
    For instance SL =-10 and TP=15

    Thanks again.

    Reply

    Andriy Moraru Reply:

    The PSC indicator uses the current candle’s Low for SL when you attach it to the chart. If you set UseFixedSLDistance and UseFixedTPDistance to true, you will be able to set both SL and TP in pips vs. price levels.

    Reply

    Mani Reply:

    Hi again!
    I did change the “UseFixedSLDistance” to true and it works perfect.
    Just one more question.
    Now I should enter the SL as fraction pip and not pip.
    For instance, SL=80 results in SL of 8 pips! Am I right?!

    In addition, Can I set a default SL to be always, for example, to be 8 Pips rather than set it each time?
    Thanks.

    Reply

    Andriy Moraru Reply:

    The pips are set in your broker’s pips. If your broker uses fractional pips, it will be in fractional pips.

    There is no way to set the default SL.

    Reply

  27. Ulf

    Hi all, thanks Andriy for this valuable tools.
    I have one question problem and one request.

    1. When I run the PSC-trader to place an order, I got this error:

    Script PSC-Trader GBPJPY,M15: removed
    Script PSC-Trader GBPJPY,M15: removed
    PSC-Trader GBPJPY,M15: Execution failed. Error: trade is disabled.
    PSC-Trader GBPJPY,M15: Execution mode: SYMBOL_TRADE_EXECUTION_MARKET

    or

    Script PSC-Trader AUDUSD,M15: removed
    PSC-Trader AUDUSD,M15: uninit reason 0
    PSC-Trader AUDUSD,M15: Execution failed. Error: trade is disabled.
    PSC-Trader AUDUSD,M15: Execution mode: SYMBOL_TRADE_EXECUTION_MARKET

    This is the case most of the time, but sometimes it works!!!
    I don’t understand why sometimes it places the order and sometimes not! I reinstall the MT4 and positionsizing and its script, but still the same! I am wondering if you know why sometimes my trade is disabled or sometimes enable.

    ————————————————————————————————————

    the position sizing script works perfect when we go long. for the entry, it assigns the high of the current candle.
    When we go short, it should assign the low of the current candle (current price) to the current price, however, still high of the candle will be assigned again which is not correct!
    Would you kindly either fix this issue or let me know where in the code you assign the entry price, so I can fix it myself and not be taking your time.
    Thanks.

    Reply

    Andriy Moraru Reply:

    “Error: trade is disabled.” is caused by your broker turning off either trading for the current symbol completely or just for scripts/EAs. You need to discuss it with your broker.

    PSC-Trader script does not work by itself. It works only with the Position Size Calculator indicator. You set Entry, SL, and TP via that indicator.

    Reply

    Ulf Reply:

    Hello!
    Would you reply to my second question:

    The position sizing script works perfect when we go long. For the entry, it assigns the high of the current candle.

    When we go short, it should assign the low of the current candle (current price) to the current price, however, still high of the candle will be assigned again which is not correct!
    Would you kindly either fix this issue or let me know where in the code you assign the entry price, so I can fix it myself and not be taking your time.
    Thanks.

    Reply

    Andriy Moraru Reply:

    I have answered that question in my previous reply.

    Reply

    Ulf Reply:

    Then I don’t understand your reply. My second question is about PositionSizingCalculater, not Script. On “UseFixedSLDistance” mode and Buy, it works fine. Entry price (Buy @) will be placed at the high of the current candle.
    However, when we switch to Sell, SL and TP will be changed as it should. But the Entry price (Sell @) should be shifted to the low of the current candle, but it still remains on the current candle high.
    Any solution to this? I think it would be very easy for you to fix it.
    However, if you answered this as well, then I am sorry that I don’t understand you. please elaborate.
    Thanks.

    Reply

    Andriy Moraru Reply:

    Why do you believe that Position Size Calculator should switch the Entry price either to candle’s High or Low? The entry price is given by the user. The only time it is set by the indicator is the indicator’s initial attachment because it has to set it somewhere. Switching the Instant order’s type from Buy to Sell should only switch the Entry price from Ask to Bid.

    EDIT: If you want to discuss the issues related to the Position Size Calculator indicator, it is better to do so in the dedicated forum thread.

    Reply

  28. Alexandro

    Ciao,
    This tool is good but I have two issues that need support on.

    First, It places “Buy/Sell Limit” order which is not correct in most of the cases. I want to only place a “Buy/Sell Stop” order. How can I do so?!!

    Second, as I saw that pointed out by another user, it should definitely shift the entry to low of the current candle in short positions.
    This is very very important.
    Grazie.

    Reply

  29. Cian

    Hi,

    This script and indicator have made my life so much easier, thanks a million.

    Have a quick question, it’s not really an enhancement request, I just thought i’d ask here first in case I go down a wrong path.

    Do you know if it would be possible for me to edit this script so that when I place a pending order through the script, this pending order will be cancelled if the Stop Loss level is hit before the Entry Level?

    Thanks,
    Cian

    Reply

    Andriy Moraru Reply:

    No. For that, you would need some kind of expert advisor that would be kept attached to the chart and monitor/cancel the orders.

    Reply

Leave a Reply

required
required (will not be published)
optional