Just a quick note to trading robot developers and system automators. I successfully wrapped the mtapi.dll (MetaTrader version 3.x API) ages ago for my own use, and now I'm happy to share my hard work for free.
My wrapped MetaTrader v3.xx API for dotNet framework features:
- Object oriented way: much easier to program in .Net.
- Safe multithreading: no more strange error codes returned.
- Pumping Message Mechanism is programmed for you: all you need to do is to handle the messages.
- A method to avoid 'Too frequent requests' error is programmed for you.
- A very basic demo source code in VB.net is included.
- Support Visual Basic .Net, C#, C++ .Net, and more dotNet compatible languages.
You can download from
www.fxfisherman.com/downloads/MT3API_dotNet.zip and feel free to give comment.
Important: I really do suggest you understand how the original api works first; to do so you need to read the C source code provided with the original api.
Requirement: Microsoft .Net Framework version 1.1 or newer.
Disclaimer: The product is provided free of charge, and, therefore, on an "as is" basis, without warranty of any kind, express or implied, including without limitation the warranties that it is free of defects, virus free, able to operate on an uninterrupted basis, merchantable, fit for a particular purpose or non-infringing. In any case, the author will not be responsible or liable for ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, INDIRECT OR ANY OTHER LOSSES caused by using of this software library.
move to members' area?
No, I want to present this goodie to the public to make the best use of it.
First of all, many thanks for providing the the public with this. I've spent too long trying t ofigure out a way to get the mtapi.dll working in .NET!
I've gone through your example and everything works fine until I try managing orders. Looking at the original API and comparing to the methods in your wrapped dll, I am unsure of certain parameters that are required to send orders. For CloseOrder() I'm sure I'm using the correct price (just grabbing the closeprice from the traderecord) but I'm getting a NotEnoughRightsException, same with ModifyOrder(). Also, for SendOrderInfo, I'm unsure as to what the CRC and Req_Code shoudl be. Lastly, do I have to specify a price (current bid/ask) or can I leave that as 0?
Again, many thanks
You'll need to use 2 connections logged into the same account concurrently. The first connection is used to listen for pumping messages, and the second connection is used for calling other methods. The NotEnoughRightsException occured because you used the same connection to call restricted methods while in pumping mode.
CRC and Req_code are not used in SendOrderInfo, and also you have to specify Bid price for sell order and Ask price for buy order. Simple, isn't it?
works a treat! One question though. I'm having a problem with spawning off multiple threads to kick off trades or even modify orders. The first command usually goes through pretty quickyl but the subsequent ones take qutie a few seconds and often result in too frequent requests exceptions.
I'm probably not explainign this very well.
I have written a function that will modify a position by putting the stop to entry. Say I have 5 open positions all onside with no stops. Inside a loop a spwan off 5 threads that place a stop at entry for each one. Why this all doesn't happen in under a second I don't know. However, if I have 5 seperate instances of my application, and modify the stops for each position in a seperate instance of the app, it works fine. Confusing ....
It's good that you have it made some trades so soon.
Your problem: It takes long time to modify the 5 trades, because you use the one same connection. It is a mechanism designed by me to avoid 'Too frequent requests' errors raised by the low-level api. MetaQuotes imposed a rule inside their api to limit 1 trade transaction per 5 to 10 seconds.
The only solution now is to spawn two or three more connections the same way you did in multiple instances. But you should aware that MetaQuotes also imposed a rule to limit the number of connections logged into the same account.
I hope API version 4 doesn't have these limiations.
Thanks for the confirmation. I thought it might be something I was doing wrong in the code. I've had to resort to spawnging off console apps to do the dirty work for now. Do you have any idea as to when the next version of the api is to be released?
MetaQuotes says the API4 will be realeased when MetaTrader 4 is in stable version.
That's good news! Have another question. The GetCurrentRate method does not seem to work with standard accounts, only minis (interbankfx) is this a known issue?
I find it has no problem. Maybe you don't set the Lots correctly, because Lots in mini differs from standard account and also differs from one currency pair to another. Be sure to pay more attention to it.
To know more about Lots of all pairs, you should explore the ConSecurity array returned from GetSecurities() function.