Results 1 to 3 of 3
  1. #1
    Standard Member Array
    Join Date
    Dec 2008
    Posts
    23
    Thanks
    1
    Thanked 17 Times in 1 Post

    [req] Help calculating lot size

    I am demo testing and I want to code my EA in a manner that would result in trade sizes of 0.2 lots for every 1000 of Account Balance.

    The closest I could get to that goal is the following, assuming a balance of 5000

    double lot=1;
    lot=NormalizeDouble(MathFloor(AccountFreeMargin()* 200)/1,1);

  2. #2
    Moderator Array
    Join Date
    Dec 2008
    Posts
    541
    Thanks
    6
    Thanked 15 Times in 10 Posts

    Re: [req] Help calculating lot size

    //external variables
    extern bool UseMoneyManagement = false;
    extern double DecreaseFactor = 5;
    extern int Risk = 10;
    extern double InitialLot=0.1;
    extern bool AccountIsMicro=false;


    double Lots()
    {
    if(UseMoneyManagement==false) return(InitialLot);
    double lots;
    int orders=HistoryTotal();
    int losses=0;
    int decimalPlaces=1;
    if(AccountIsMicro==true) decimalPlaces=2;

    lots=NormalizeDouble((AccountFreeMargin()*Risk/1000.0)/100,decimalPlaces);
    StoppedOut = LastTradeStoppedOut();
    if (StoppedOut==true)
    {
    if(AccountIsMicro==false) lots=0.1;
    if(AccountIsMicro==true) lots=0.01;
    }
    if(lots<0.1 && AccountIsMicro==false) lots=0.1;
    if(lots<0.01 && AccountIsMicro==true) lots=0.01;
    double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
    if (MyMaxLot>0)
    {
    if (lots>MyMaxLot)
    return (MyMaxLot);
    }
    if(lots>maxlot) lots=maxlot-1;
    return(lots);
    }

    bool LastTradeStoppedOut()
    {
    int cnt, total;
    datetime NextTime;
    bool Stopped=false;

    NextTime = 0;

    total = HistoryTotal();
    for (cnt = total - 1; cnt >= 0; cnt--)
    {
    OrderSelect (cnt, SELECT_BY_POS, MODE_HISTORY);

    if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic)
    {
    Stopped = false;
    if (OrderType() == OP_BUY)
    {
    if (OrderClosePrice() - OrderOpenPrice() < 0)
    {
    Stopped = true;
    }
    cnt = 0;
    }
    if (OrderType() == OP_SELL)
    {
    if (OrderOpenPrice() - OrderClosePrice() < 0)
    {
    Stopped = true;
    }
    cnt = 0;
    }
    }
    }

    if (Stopped)
    {
    StopTime = OrderCloseTime() + MinutesToDelay*60;
    }

    return (Stopped);
    }

  3. #3
    Gold Member Array Hacker's Avatar
    Join Date
    Jun 2009
    Location
    Never argue with an idiot, they'll drag you down to their level then beat you with experience!!
    Posts
    282
    Thanks
    1
    Thanked 13 Times in 6 Posts

    Re: [req] Help calculating lot size

    Ok here you go.. put it in strategy tester and play with your variables to test..

    [url]http://www.mediafire.com/?sharekey=2f912bd94f9b924a9bf8d6369220dcabe04e75f6 e8ebb871[/url]


    Code:
    //+------------------------------------------------------------------+
    //|                                                    LotTester.mq4 |
    //|                                                                  |
    //|                                                                  |
    //+------------------------------------------------------------------+
    #property copyright ""
    #property link      ""
    
    extern double Lots = 0.2;
    extern double MinBalance = 1000;
    
    //+------------------------------------------------------------------+
    //| expert initialization function                                   |
    //+------------------------------------------------------------------+
    int init()
      {
    //----
       
    //----
       return(0);
      }
      
      
    //+------------------------------------------------------------------+
    //| expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    int deinit()
      {
    //----
       
    //----
       return(0);
      }
      
      
    //+------------------------------------------------------------------+
    //| expert start function                                            |
    //+------------------------------------------------------------------+
    int start()
      {
    //----
       Comment("AccountBalance: " +DoubleToStr(AccountBalance(),2)+"  Lots: "+ DoubleToStr(CalcLots(),2)); 
       return(0);
    //----
       return(0);
      }
      
      
    //+------------------------------------------------------------------+
    
    //+------------------------------------------------------------------+
    //| expert CalcLots function                                            |
    //+------------------------------------------------------------------+
    double CalcLots()
    {
    //----
       double iLots;
       if (AccountBalance()>MinBalance)
       {
        iLots= NormalizeDouble((AccountFreeMargin()*(Lots*100)/1000)/100,2);
    
       } 
        else iLots = Lots;
    //----    
       return (iLots);
    }

Similar Threads

  1. Daryl Guppy - Risk, Stop Loss and Position Size
    By TOUIZI in forum Forex Clips and Movie Tutorial
    Replies: 15
    Last Post: 06-21-2011, 09:50 AM
  2. Options Trading Video Size: (705.44 MB)
    By trduraikamaraj in forum Forex Clips and Movie Tutorial
    Replies: 0
    Last Post: 03-20-2010, 04:38 PM
  3. How to Set Trade Position Size for Maximum Profits
    By manageacc in forum Trading Systems and Strategies
    Replies: 0
    Last Post: 03-12-2010, 04:33 AM
  4. [youTube] Size does matter
    By mr_b34n in forum Jokes & Entertaintment
    Replies: 1
    Last Post: 09-04-2009, 05:13 PM
  5. Changing spread size on backtest
    By Stormin_Norman in forum MetaTrader Expert Advisors Chat
    Replies: 6
    Last Post: 05-18-2009, 03:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •