Navigation:Home > Content >

Code_-_Money_Management.mq4

Time: 2015-09-18 | Download file:Code_-_Money_Management.mq4

//+------------------------------------------------------------------+
//|                                      Code - Money Management.mq4 |
//|                                  Copyright © 2007, GwadaTradeBoy |
//|                                           [email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, GwadaTradeBoy"
#property link      "[email protected]"

//---- Include
#include 
#include 

//---- Money Management
extern bool    MoneyManagement   = true;
extern bool    StrictManagement  = true;
extern bool    AcountIsMini      = false;
extern int     TradePercent      = 1;
extern int     SumTradePercent   = 10;
double         Margin, PF, PipValue, SumTrade;
bool           TradeAllowed      = false;
bool           Allowed           = false;

//---- Optimisation des Lots
extern double  Lots              = 0.1;
//extern double  MaximumRisk       = 0.02;
extern double  DecreaseFactor    = 3;
double         lot;
int            orders, losses, spread, SL;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
   {
//----
//---- Expert Advisor
      spread = MarketInfo(Symbol(),MODE_SPREAD);
      Margin = AccountMargin();
      PF = AccountBalance();
      PipValue = MarketInfo(Symbol(),MODE_TICKVALUE);
//----
      return(0);
   }

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
   {
//----
   
//----
      return(0);
   }
   
//+------------------------------------------------------------------+
//| Calculs preliminaires de l'expert                                |
//+------------------------------------------------------------------+
//**************** Calcul de la posibilitй de trade ****************//
bool TradeAllowed()
   {
      if (StrictManagement)
         {
            SumTrade = ((PF * SumTradePercent)/100);
            if (Margin < SumTrade)
               Allowed = True;
            else
               Allowed = True;
         }
      else
         if (
         (AccountEquity()>(PF+((SumTradePercent/100)*PF))) 
         || (AccountBalance()-AccountEquity()>((TradePercent/100)*PF))
         )
            {
               Allowed = True;
            }
         else
            Allowed = False;
      }
//----
      return(Allowed)
   }
   
//************** Calcul de la taille optimale du lot ***************//
double LotsOptimized()
   {
      if (AcountIsMini)
         Lots = 0.01;
      lot=Lots;
      orders=HistoryTotal();     // Historique des ordres
      losses=0;                  // Nombre de trade perdants consйcutif
/* Selection de la taille du lot */
      //lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000,1);
      if (AcountIsMini)
         lot=NormalizeDouble(AccountFreeMargin()* TradePercent/100,2);
      else
         lot=NormalizeDouble(AccountFreeMargin()* TradePercent/100,1);
/* Calcul du nombre de perte consecutive */
      if(DecreaseFactor>0)
         {
            for(int i=orders-1;i>=0;i--)
               {
                  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==False) 
                     { 
                        Print("Erreur dans l historique!"); 
                        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);
         }
/* Retour de la taille du lot */
      if (AcountIsMini)
         if(lot<0.01) 
            lot=0.01;
      else
         if(lot<0.1) 
            lot=0.1;
//----
      return(lot);
   }
   
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
   {
//----
      
//----
      return(0);
   }
//+------------------------------------------------------------------+

Recommend