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.

13 comments:

  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.

    Ludo

    ReplyDelete
  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

    thanks,
    Chris


    Here is the log I got:
    The first commandfile is
    OrderSend,EURUSD,OP_BUY,1,Ask,5,0,0,Order2,9997,0,Orange

    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:
    OrderClose,Order2,1,Bid,5,Purple'


    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

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete
  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.

    THANKS A LOT :-D

    Cristian

    ReplyDelete
  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.

    ReplyDelete
  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?

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete
  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?

    ReplyDelete
  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. blog.tipster.ca
    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.

    ReplyDelete