Thursday, January 7, 2010

MetaTrader - Account Functions

Welcome back, fans of Automated Forex trading.

In this post, let's take a look at account related functions in MetaTrader programming. Take a look Account Functions and scroll down to Functions Accessing to the Information about the Active Account.

Go ahead and fire up MetaTrader. Right-click on Expert Advisors and select New. Go through the wizard and create a new Expert Advisor called AccountStuff. Go ahead and paste the following code into the Init() function:

int init()
{
Print("Account Balance: ", AccountBalance());
Print("Account Credit ", AccountCredit());
Print("Account Currency: ", AccountCurrency());
Print("Account Equity: ", AccountEquity());
/*
Print("AccountFreeMargin: ", AccountFreeMarginCheck());
Print("AccountFreeMarginMode: ", AccountFreeMarginMode());
*/
Print("AccountLeverage: ", AccountLeverage());
Print("AccountMargin: ", AccountMargin());
Print("AccountName: ", AccountName());
Print("AccountNumber: ", AccountName());
Print("AccountProfit: ", AccountProfit());
Print("AccountServer: ", AccountServer());
Print("AccountStopoutLevel: ", AccountStopoutLevel());
Print("AccountStopoutMode: ", AccountStopoutMode());

return(0);
}

Most of these functions are pretty self explanatory, so I'm not going to go through all of them here. You can step through them one at a time by clicking AccountBalance() then click on the next function name to scroll ahead.

One interesting distinction is between AccountBalance() which returns the amount of actual cash in the account. AccountEquity() returns the cash value of the account plus or minus any gains or losses from open positions and is the value to watch in terms of whether you are up or down. AccountMargin() returns the Account value remaining in the account which can be used to add new positions.

So what does the account balance have to do with trading? A lot as it turns out and since money management is one of the most important factors in all trading and can make the difference between success and failure.

I was hoping to include the code for position sizing in this post, but I ran out of time. Please check back over the weekend for that.

Thanks for reading and come back soon.

4 comments:

  1. Cool some more interesting stuff to help in an EA.
    I have a question about MT4, I don't expect an answer I suppose, but I wonder if whom runs/creates the MT4 platform can kind of spy, so to speak, on what we are creating. Kind of a ridiculous question possibly, but with today's standards, companies are spying on us constantly. They probably carefully word that into their terms and agreements. I guess I could not limit that to MT4, but FXCM can always record a history of what we did when we did it too. Just seems very profitable to have that information. At any rate, not to be a conspiracy theorist... Someday I may get some more time away from my family, work, and such to get back into playing around in MT4. The AccountMargin() call looks to be quite useful.

    ReplyDelete
  2. JT-

    The MT4 platform is created and maintained by Metaquotes software corp (http://www.metaquotes.net/). Eery broker who supports it has to license it from Metaquotes and run their own server side infrastructure to interface with their clients. So if anybody is spying on you, its not Metaquotes, but the FCM.

    One serious and ongoing danger of EA's if they get too popular, the brokers will start to defeat them. In other words, if they see a bunch of customers all making the same trade at the same time, they can conspire to run the price the other way to shake everybody out.

    There are a few defenses against this:

    1) Change the magic number on the orders as often as is possible because the magic number for each order gets transmitted to the broker. If all customers us the same magic #, that will tip the FCM off that they are all using the same EA

    2) Don't broadcast stop and take profit orders over to the FCM, instead hold them until the market price reaches the stop or take profit point then just submit a market order. State of the Art EA's (such as Metadroid) employ these tactics already.

    I think in the end, the only real defense against this is to developed your own EA's or use EA's that won't get too popular. I think Daniel Fernandez's EA's fall into this category because they don't trade often enough to appeal the to trigger happy trading public that wants to see trades every day, high winning rates, low drawdowns, etc, etc.

    I have a pipe-dream of developing an EA that examines market action and learns to trade from the market itself. Will I ever get there? Don't know but it will be fun trying,

    Chris

    ReplyDelete
  3. You just described a problem I have that has given me some concern. I have to place the order as I don't have my EA running and I also can't watch the market all day. So I do broadcast where and when I want my order to take place. I really want to get rid of this.

    On the good side my method has a small amount of market exposure, a topic I have been thinking about a lot lately, I trade a currency pair maybe on average 1 time a month. In a sea of open orders out there ( as I am guessing there are), I highly doubt they will pinpoint mine unless I were going to move a lot of money. (I am also guessing manual orders have random magic numbers, so that is good too)
    Non the less I would still like to make what I am doing more private, though I would not be surprised if others are already doing what I do in my method.

    That would be a tough one indeed to get enough market action data for an EA. The only thing that pops to mind is monitor volume, monitor pace at which trades are made, and monitor price direction. Example: if I could detect a degree of change in volume per time period and see that trades have doubled or tripled in execution, and then note which direction it is going. I might be able to find a point to get in on the direction based on those indications and standards I have placed for those indications.

    At any rate I do find these things fun as well. My wife does not understand :).

    ReplyDelete
  4. Hey Everybody,

    Below is a list of the most recommended forex brokers:
    1. Best Forex Broker
    2. eToro - $50 minimum deposit.

    Here is a list of the best forex instruments:
    1. ForexTrendy - Recommended Odds Software.
    2. EA Builder - Custom Indicators Autotrading.
    3. Fast FX Profit - Secret Forex Strategy.

    Hopefully these lists are benificial to you...

    ReplyDelete