Announcement

Collapse
No announcement yet.

[req] Help calculating lot size

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [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
    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);
    }

    Comment


    • #3
      Re: [req] Help calculating lot size

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

      http://www.mediafire.com/?sharekey=2...4e75f6e8ebb871


      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);
      }

      Comment

      Top Active Users

      Collapse

      There are no top active users.
      Working...
      X