Sunday, December 13, 2009

A crude "API" for MetaTrader 4

In response to Ludosm's request to publish this utility, here it is.  Using this API, any other software can drive trade functions in MT4 by generating simple one line CSV files in a similar format to the original MQL4 trade commands.

So where you would write the following in an EA ....
OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color);

.... the CSV file looks like this
OrderSend, symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color

A price of Bid or Ask is interpreted correctly.

An important difference is that open orders are located using their Comment, meaning that comments should have a numerical sequence.

So OrderClose(ticket); becomes OrderClose,Comment

There are additional comments at the top of the script.  Hope it's useful.


  1. Thank you Paul, I looked at the code without running it yet and it sounds exactly what I was expecting with good code qualily.

    I'll use it in the coming week.


  2. Hello Paul,
    this is an interesting utility!! I installed it in MT4 and it was able to post the first trade with no problem, but it does not accept further commandfiles.

    Could you please offer some insight on this, please?
    THANKS in advance


    Here is the log I got:
    The first commandfile is

    16:50:07 TradeFromCsvFile2 EURUSD,M1: loaded successfully
    16:50:26 TradeFromCsvFile2 EURUSD,M1 inputs: Trade_Debug=true; Repeat_Delay_Seconds=10;
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadComment() returning true and "EURUSD"
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadCmd() returning true and 0
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadVolume() returning true and 1
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadPrice() returning true and 1.3193
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadSlippage() returning true and 5
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadStopLoss() returning true and 0
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadTakeProfit() returning true and 0
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadComment() returning true and "Order2"
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadMagic() returning true and 9997
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadExpiration() returning true and 0
    16:50:26 TradeFromCsvFile2 EURUSD,M1: Function ReadArrowColor() returning true and 42495
    16:50:49 TradeFromCsvFile2 EURUSD,M1: open #11410782 buy 1.00 EURUSD at 1.31926 ok
    16:50:49 TradeFromCsvFile2 EURUSD,M1: Function CSVfileOrderSend() returning true after sending cmd (EURUSD,0,1,1.3193,5,0,0,"Order2",9997,0,42495)
    16:50:49 TradeFromCsvFile2 EURUSD,M1: Function ReadAndExecuteCommand() returning true for OrderSend

    Here is the result when I try a second commandfile:

    16:54:17 stdlib EURUSD,M1: loaded successfully
    16:54:17 TradeFromCsvFile2 EURUSD,M1: cannot open file reading command from TradeFromCsvFile\commandfile.txt
    16:54:17 TradeFromCsvFile2 EURUSD,M1: Unknown command Order2 in TradeFromCsvFile\commandfile.txt
    16:54:17 TradeFromCsvFile2 EURUSD,M1: Unknown command 1 in TradeFromCsvFile\commandfile.txt
    16:54:17 TradeFromCsvFile2 EURUSD,M1: Unknown command Bid in TradeFromCsvFile\commandfile.txt
    16:54:18 TradeFromCsvFile2 EURUSD,M1: Unknown command 5 in TradeFromCsvFile\commandfile.txt
    16:54:18 TradeFromCsvFile2 EURUSD,M1: Alert: Problem executing TradeFromCsvFile\commandfile.txt
    16:54:19 TradeFromCsvFile2 EURUSD,M1: shutdown by timeout

  3. Hi Cristian, thanks for trying out the utility. I checked it myself and discovered that the treatment of GetLastError() must have changed since I programmed it. I have added a precautionary GetLastError() to clear old error codes, and it seems to be working well now. I have called the new version V1.1, but the filename is still the same for downloading.

  4. Hi Paul,

    I've compiled your script as said in expert/script, I've created a commandfile.txt in expert/files then I dragged the script on the chart. I manually wrote the line in the txt file : OrderSend,EURUSD,OP_BUY,1,Ask,5,0,0,Order1,27130,0,Orange

    But nothing happens. Did I do something wrong ?

    Thanks for your support.

  5. ok it works, I didn't saw that commandfile.txt needs to be in the TradeFromCsvFile folder.

    I'll test this. Thanks for the great work.

  6. Thank you Paul!!! The API works excellent right now :-)

    At the beginning I had problems again, and I realized that if MetaTrader 4 is installed on the Program Files or Program Files (x86) directory, there are problems related to permissions to write or delete files. I made a new directory for Forex in the root of my HDD and everything works great :-)

    BTW have you tried to specify stop-loss in your API? It seems to work with numbers but it does not seem to recognize statements such as "Ask-30*Points" Am I doing something wrong?
    Anyway that is just something very little.



  7. Hi Cristian,

    glad you like the API. What you suggest for stoploss is not supported but is a very good idea. I'm rather busy right now but might add that feature in the future.

  8. Hello Paul, thank you very much for publicizing this utility! at the moment I lack just one commend: close all pending orders, can you add it?

  9. One more question, is it possible o send several orders at once? It looks like MT4 takes only one order per sth like 7 sec.

  10. Paul, This is a fantastic solution to taking pretty much any signals from anywhere (program, twitter source, etc.) and getting MT4 to execute them. I've too had a need to accept multiple entries at the same time - to do that, just rejigged the script to look for different file names, and get it running on as many charts/interations as possible. Thanks for making this public.

  11. this post is so good ,thnx. Hello Paul, thank you very much for publicizing this utility! at the moment I lack just one commend: close all pending orders, can you add it?

  12. Nice job! I'm working on some AFL to send trades from Amibroker to MT4. I'll publish it as I move along, on my blog.
    IMO- this shouldnt be used to scalp, its too slow, not to mention the data flow from MT4 to your other platform included in the delay time. I'm using this to place limit orders only.

  13. Goodday Paul,

    it seems that the ea do not integrate stoploss and target.

    Could you confirm this?

    I've tried the below and is not implemented in metatrader and it is returned as a failed command.


    Could you tell me if a developpment should be done to integrate stoploss and target or maybe it's an error on my side.

    In everything tks a lot for this ea, it is very helpfull.



  14. Please help i don't know how to use it.

    Note: i installed it in expert/script with commandfile.txt
    Not thing happens after executing it to chart

  15. Paul. Thank you so much you are a life saver.
    Just one wish... It's about your using the comment field to identify orders. This is a great ideas! Just one problem... it seams that if we only partially close an existing order position identified by our comment, that the remaining position after the partial close will be given a new order number and a comment generated by the metaTrader system. Of Course, Since we are not able to label the remaining order position with our own comment, we will not be able to identify it automatically so we can close it. Can anything be done?
    I believe the original comment I labelled my order with was still left as a part of the new system generated comment, so perhaps you can implement a search sequence to find it, and thus find the order.
    Does anybody have any ideas?

  16. This is a great EA, thank you very much :)
    but I can get the EA to make a simple BUY and SELL command/order
    a SELL order with a S/L -5 like this: (works too)
    but when I try this:
    MT4 - gives an error message and will not open the trade....
    does it work on your copy of MetaTrader 4??

  17. Hi Paul Is there a fix for this EA so that it works on the MQL4 Buil 600 (MT4) Platform

  18. I'm using the script for more than 1.5 years now without any problems. The current mt4 build is 825 (not updated yet to 860) and still works fine! Guys, this is a script not an indicator / ea, just put it in the scripts folder! Btw it works for me with TP / SL / Order expiration. Great work, many thanks Paul.

    1. I'm using it since 09/2014 and it's really a great script. Thank you Paul!
      I'm wondering if it is possible to handle this case:
      sending an order for buy limit or sell limit and when the order reach the MT4 the price is already above "sell limit" or below "buy limit" price. This will generate an error... and the order will be missed even if a better price.

    2. This should work if you set parameter for "Repeat_Delay_Seconds" to 1 except strong move and your entry level is too close to market price already.

  19. Can't get it to work, i'm using MT4 build 840.
    I have put the script and a commandfile.txt in the script folder, but no order appears in MT4.
    Also have debug option true, but no errors are in experts log..
    I have MT4 installed in a folder where i have all permissions to read/write/delete.
    Anybody who can help me?

    1. Never mind, got it, forgot to add the "Tradefromcsvfile" folder

  20. Are you looking for Best Forex Robots and Automated Forex Software’s? Drive Gold is the one stop solution for Forex Software’s and Best MT4 EA.
    Gold is known to be the most precious of metals. Gold may be an asset against economical crisis. When the market is declining or in time of inflation, national debt, war and social unrest and in currency failure; gold can be your harbor. In the last decade, a huge rise has been observed in the prices of gold. It has gone up to $1,800 an ounce from around $350. It was in August 2011 that gold prices reached its peak. However, a decline in the prices has been observed in recent weeks. It has declined to $1,600.

  21. Hello Paul, i am from germany and i have looked very hard to find some thing like your code.
    Wtdh your code I can execute my excel signals in MT4...
    I try it the next days on a demo - account and hope that ist works like this.

  22. it seems it doesn't work anymore with the build 950 or w7. not sure?

  23. the journal message is: TradefromCsvFile is not expert and cannot be executed.

  24. Hi Paul and everyone that stumble upon this page,
    Great work on the script.

    Currently the script only picks up one line from txt file and place the order.

    Just wondering is it possible for the script to loop through the txt file for multiple lines of orders?

    Any ideas and suggestion from everyone is appreciated!

  25. Hi, this file is not available anymore.
    Can someone share this file again ?

  26. Thank you for your great post. It's really very informative and really helpful. Please Keep posting. Thanks again.
    23Traders Tutorial

  27. I would like to advise that you go with the ultimate Forex broker - eToro.

  28. I would like to say thank you Paul, it is amazing api that really cool.
    Here i upload again due to Paul link was broken

  29. Replies
    1. Hi can someone please upload this file..I would like to use something like this.
      Thank you

  30. I see that this article was posted in 2009 so readers have had a lot of time to test it. So does it work well in linking live data to other guest trading programs like Amibroker. Will this code allow trades to be made from the guest trading platform?