Amps

Wednesday, January 13, 2016

EA MA Optimal Lot or Trading Money Management

Optimal Lot or Trading Money Management

Money Management that nessary for our trading because this would protect our loss could be happen all the time, so this part should have in our Expert Advisor.

The MQL4 function used for this module detail as below.

Function Feature
double NormalizeDouble Return Value of double type with preset accuracy
double value Value with a floating point
int digits Accuracy format, number of digits after point (0-8)
double AccountFreeMargin(); Free margin value of the current account.
double OrderProfit() Returns profit of the currently selected order The net profit value (without swaps or commissions)
int OrdersHistoryTotal(); The number of closed orders in the account history loaded into the terminal. The history list size depends on the current settings of the "Account history" tab of the terminal

Meaning of Moving Average Money Management MQL4 code

double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//--- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//Calculate account free margin from Maximum risk, which specified in EA properties

//--- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
           {
            Print("Error in history!");
            break;
           }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
            continue;
         //---
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1)
         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }


//This function decrease lot size depend on order pending got loss
//DecreaseFactor would got specified on EA properties

//if(DecreaseFactor>0) would do the lot optimization if specified number greater than 0 
//in EA properties
//----- in DecreaseFactor Function----
//if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
//Check order false status and report

//if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
//            continue;
//order success check
//if(OrderProfit()>0) break;
//if order got profit not do the lot optimization
// if(OrderProfit()<0) losses++;
//     }
//      if(losses>1)
//        lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
//     }

// if order have loss do the lot optimization or decrease lot to next order
//----- end of DecreaseFactor check condition

//--- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }

//---- end of  

Test Run Moving Average EA on youtube
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//--- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//Calculate account free margin from Maximum risk, which specified in EA properties

//--- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
           {
            Print("Error in history!");
            break;
           }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
            continue;
         //---
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1)
         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }


//This function decrease lot size depend on order pending got loss
//DecreaseFactor would got specified on EA properties

//if(DecreaseFactor>0) would do the lot optimization if specified number greater than 0 
//in EA properties
//----- in DecreaseFactor Function----
//if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
//Check order false status and report

//if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
//            continue;
//order success check
//if(OrderProfit()>0) break;
//if order got profit not do the lot optimization
// if(OrderProfit()<0) losses++;
//     }
//      if(losses>1)
//        lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
//     }

// if order have loss do the lot optimization or decrease lot to next order
//----- end of DecreaseFactor check condition

//--- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }

//---- end of  

Test Run Moving Average EA on youtube


No comments:

Post a Comment