Money management

elvis_clarkson
14
elvis_clarkson  
//+------------------------------------------------------------------+
//|                                              MoneyManagement.mqh |
//|                                                     Andrew Young |
//|                                 http://www.expertadvisorbook.com |
//+------------------------------------------------------------------+

#property copyright "Andrew Young"
#property link      "http://www.expertadvisorbook.com"

/*
 Creative Commons Attribution-NonCommercial 3.0 Unported
 http://creativecommons.org/licenses/by-nc/3.0/

 You may use this file in your own personal projects. You
 may modify it if necessary. You may even share it, provided
 the copyright above is present. No commercial use permitted. 
*/


#define MAX_PERCENT 10          // Maximum balance % used in money management


// Risk-based money management
double MoneyManagement(string pSymbol,double pFixedVol,double pPercent,int pStopPoints)
{
        double tradeSize;
        
        if(pPercent > 0 && pStopPoints > 0)
        {
                if(pPercent > MAX_PERCENT) pPercent = MAX_PERCENT;
                
                double margin = AccountInfoDouble(ACCOUNT_BALANCE) * (pPercent / 100);
                double tickSize = SymbolInfoDouble(pSymbol,SYMBOL_TRADE_TICK_VALUE);
                
                tradeSize = (margin / pStopPoints) / tickSize;
                tradeSize = VerifyVolume(pSymbol,tradeSize);
                
                return(tradeSize);
        }
        else
        {
                tradeSize = pFixedVol;
                tradeSize = VerifyVolume(pSymbol,tradeSize);
                
                return(tradeSize);
        }
}


// Verify and adjust trade volume
double VerifyVolume(string pSymbol,double pVolume)
{
        double minVolume = SymbolInfoDouble(pSymbol,SYMBOL_VOLUME_MIN);
        double maxVolume = SymbolInfoDouble(pSymbol,SYMBOL_VOLUME_MAX);
        double stepVolume = SymbolInfoDouble(pSymbol,SYMBOL_VOLUME_STEP);
        
        double tradeSize;
        if(pVolume < minVolume) tradeSize = minVolume;
        else if(pVolume > maxVolume) tradeSize = maxVolume;
        else tradeSize = MathRound(pVolume / stepVolume) * stepVolume;
        
        if(stepVolume >= 0.1) tradeSize = NormalizeDouble(tradeSize,1);
        else tradeSize = NormalizeDouble(tradeSize,2);
        
        return(tradeSize);
}


// Calculate distance between order price and stop loss in points
double StopPriceToPoints(string pSymbol,double pStopPrice, double pOrderPrice)
{
        double stopDiff = MathAbs(pStopPrice - pOrderPrice);
        double getPoint = SymbolInfoDouble(pSymbol,SYMBOL_POINT);
        double priceToPoint = stopDiff / getPoint;
        return(priceToPoint);
}


//+------------------------------------------------------------------+
//| Martingale Class                                                 |
//+------------------------------------------------------------------+

#define HISTORY_DAYS 30

// Undocumented and untested - Use at your own risk
class CMartingale
{
        private:
                int ConsecutiveCount(string pSymbol, long pMagic, int pDays);
                int ConsWins, ConsLoss;
        
        public:
                enum ENUM_PROGRESSION {Martingale,Anti_Martingale};
                double Martingale(string pSymbol, long pMagic, double pBaseVol, double pMultiplier = 2, ENUM_PROGRESSION pProgress = Martingale);
};


double CMartingale::Martingale(string pSymbol, long pMagic, double pBaseVol, double pMultiplier = 2, ENUM_PROGRESSION pProgress = 0)
{
        double tradeSize;
        int consCount = ConsecutiveCount(pSymbol,pMagic,HISTORY_DAYS);
        
        if((pProgress == Martingale && ConsLoss > 0) || (pProgress == Anti_Martingale && ConsWins > 0))
        {
                tradeSize = pBaseVol * MathPow(pMultiplier,consCount);
        }
        else tradeSize = pBaseVol;
        
        tradeSize = VerifyVolume(pSymbol,tradeSize);
        
        return(tradeSize);
}


int CMartingale::ConsecutiveCount(string pSymbol, long pMagic, int pDays)
{
        long days = 86400 * pDays;
        HistorySelect(TimeCurrent() - days,TimeCurrent());

        int winCount = 0, lossCount = 0;
        
        for(int i=HistoryDealsTotal()-1; i>=0; i--)
        {
                ulong ticket = HistoryDealGetTicket(i);
                long entry = HistoryDealGetInteger(ticket,DEAL_ENTRY);
                double profit = HistoryDealGetDouble(ticket,DEAL_PROFIT);
                long magic = HistoryDealGetInteger(ticket,DEAL_MAGIC);
                string symbol = HistoryDealGetString(ticket,DEAL_SYMBOL);
                
                if((entry == DEAL_ENTRY_OUT || entry == DEAL_ENTRY_INOUT) && magic == pMagic && symbol == pSymbol)
                {
                        if(profit > 0 && lossCount == 0) winCount++;
                        else if(profit < 0 && winCount == 0) lossCount++;
                        else break;
                }
        }
        
        ConsWins = winCount;
        ConsLoss = lossCount;
                
        if(winCount > 0) return(winCount);
        else return(lossCount);
}

Hello guys.

Why I get these errors? (Errors are in a pic)

William Roeder
23771
William Roeder  
  1. Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum 2015.02.11

    Why it won't work: Calculate Loss from Lot Pips - MQL5 programming forum 2017.07.11

  2.                 enum ENUM_PROGRESSION {Martingale,Anti_Martingale};
                    double Martingale(string pSymbol, long pMagic, doub…
    You defined “Martingale” as an enumeration value and as a function. Why does it surprise you that the compiler gets confused.

  3.                 double margin = AccountInfoDouble(ACCOUNT_BALANCE) * (pPercent / 100);
                    double tickSize = SymbolInfoDouble(pSymbol,SYMBOL_TRADE_TICK_VALUE);
                    
                    tradeSize = (margin / pStopPoints) / tickSize;

    Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
                Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19

    4. You must normalize lots properly and check against min and max.

    5. You must also check FreeMargin to avoid stop out

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.