money management and lotoptimize

Discussion in 'MetaTrader Expert Advisors' started by univetsity, Jun 14, 2011.

  1. univetsity

    univetsity Trader

    27
    0
    22
    I try to use these code to manage my money,but an problem is I don't know how to descirbe my initital money(priniciple) .my method is cotrol lot :use priniciple 's maximumrisk+3 times about of profit(or loss)'s maximunrisk as lot,it profit>0,it will enlarge,otherwise,the vise.
    Code (MQL5):

    double LotsOptimized()
      {
       double b,lot;//=Lots;
       int    orders=HistoryTotal();    
       int    losses=0,wins=0;  
       int    c,e,f,d;
       b=iATR(NULL,0,60,0);
       c=AccountLeverage();
       
       d=AccountFreeMargin();//I wanna use it describe account's principal,but couldn't how to
       if(orders>=1)
       {e=AccountFreeMargin(); }            
       Print(MarketInfo(Symbol(), MODE_LOTSIZE));
       Print(MarketInfo(Symbol(), MODE_LOTSTEP));
       Print(MarketInfo(Symbol(), MODE_MAXLOT));
       Print("this accountleverage is",c);
       if(d!=0)  
       lot=NormalizeDouble((e*MaximumRisk/2000)*(1+3*(e/d-1)),2);//if profitiable,increase position,otherwise,the vice
       if(lot<0.01) lot=0.01;
       return(lot);
      }
     
    but the first order's position still be 0.01(the mini lot of my MM terminal), these follow will be normal,don't know why. And I know the d==e in my code,so e/d always=1, but I wanna use d stands principal,but how do?TKS!
    And have another method of lotoptimize,if solve this,I will list that one here!:p
     
    Last edited by a moderator: Sep 28, 2015
  2. Enivid

    Enivid Administrator Staff Member

    8,942
    315
    114
    If by principal you mean the initial balance on an account, then you can't code it with some MQL function. You should create an input parameter and the user will have to fill it with the principal value.
     
  3. univetsity

    univetsity Trader

    27
    0
    22
    yes,I mean the initial balance,but problem now is I don't know how to describe the initial balace(also means:how to get this value?)
     
  4. Enivid

    Enivid Administrator Staff Member

    8,942
    315
    114
    You can't. You can only use some input parameter and set it manually before running your EA.
     
  5. univetsity

    univetsity Trader

    27
    0
    22
    Fixed fractional risk method is my target,but if couldn't get the initial balance value,this is impossible to achieve.Are you sure only" set it manually "this way?
    TKS!
     
  6. Enivid

    Enivid Administrator Staff Member

    8,942
    315
    114
    As far as I know, yes. There is no MQL4 variable for initial balance.
    But wait. If by initial balance you mean the balance at the start of the EA running (immediately before attaching it to a chart), then it can be done by reading the current account balance only once in the init() function.
     
  7. univetsity

    univetsity Trader

    27
    0
    22
    TKS! Initial balance I mean is the deposit money of account.Whether array could be used for it or not?e.g. array[0] stands the lastest balance,array[n-1] stands the initial balance, or array[1] stands balance before the lastest trade happend.This is only a hypothesis,actually,I hardly know array clearly.
    Any suggestion would be appreciation!
     
    Last edited: Jun 18, 2011
  8. Enivid

    Enivid Administrator Staff Member

    8,942
    315
    114
    No, there's no such array in MQL.
     
  9. univetsity

    univetsity Trader

    27
    0
    22
    How to get the balance or accountfreemargin before the lastest trade happend.I wanna compare the lastest accountfreemargin with the previous one's value,that could help me confirm lot.
     
  10. Enivid

    Enivid Administrator Staff Member

    8,942
    315
    114
    If your EA trades with OrderSend() without pending orders, then it can get AccountFreeMargin just before sending its next order.
     
  11. univetsity

    univetsity Trader

    27
    0
    22
    Yes,without pending orders,but I am an newbie,don't know the detail about you say in #10.Show the process would more appreciation!:)
     
  12. Enivid

    Enivid Administrator Staff Member

    8,942
    315
    114
    Just insert that line before calling OrderSend() and you'll have the amount of free margin before the last order:
    Code (MQL5):
    FreeMargin = AccountFreeMargin();
     
    Last edited: Sep 28, 2015
  13. DCCS

    DCCS Active Trader

    32
    1
    34
    Hi,

    I believed there is a control of lot size for any particular broker.

    Print("Maximum permitted amount of a lot=",MarketInfo(Symbol(),MODE_MAXLOT));

    May I know how to overcome this code so that the order lot size is not restricted by a particular broker?

    Thanks.
     
  14. Enivid

    Enivid Administrator Staff Member

    8,942
    315
    114
    I don't think there is a way to override this restriction.
     
  15. DCCS

    DCCS Active Trader

    32
    1
    34
    Hi Enivid,

    Ok, I understand.

    So when I place manual trade in demo account the lot can be larger than MODE_MAXLOT. How come like that?

    Is there anyway to convert MT4 mql4 file format to MT5 mql5 file format?

    Thanks.
     
  16. Enivid

    Enivid Administrator Staff Member

    8,942
    315
    114
    I guess you need to contact your broker's support about that. Perhaps, they will also be able to increase the maximum allowed position size for expert advisors. After all, they are interested in higher trading volume by the traders.

    Yes, but it the difficulty of the process varies greatly depending on the code. It can be as easy as copy/paste and fix some minor issues to a completely rewrite hundreds of new lines of code. This article, even being quite old and somewhat outdated, can help you with such a conversion.
     
  17. DCCS

    DCCS Active Trader

    32
    1
    34
    Hi Enivid,

    Ok, I understand.

    Thanks.
     

Share This Page