Another MQL4 question on how to calculate pips, pip price, etc

 

I've read a zillion forum threads and there seems to be long and complex discussions over how to calculate in MQL4 an answer to this question:

"If I want to make $50 in 100pips on a given trade, what lot size should I use?

-- OR --

"If I want to make $50 at 0.5 lots on a given trade, how many pips is that?

-- OR --

"If I want to make 100 pips at 0.5 lots on a given trade, how much profit is that?

I just want to keep it REAL simple for now. How does one calculate the three (very similar) above questions with the following criteria in mind?

  1. MQL4
  2. Account currency = AUD
  3. Trading standard forex currencies only - e.g.; EURUSD
  4. Spreads, commissions, etc... all not relevant to this simple calculation
  5. Finer details like whether to use account balance or free margin, how one shouldn't trade based on stupid, simple calculations, handling accounts in other base currencies, etc... are all not important to me
 
  1. In code: Risk depends on your initial stop loss, lot size, and the value of the pair.
    • 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.
    • Account Balance * 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.)
    • 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 (EUR, in this case).
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    • You must normalize lots properly and check against min and max.
    • 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=0.1 Lots maximum.
  2. Use a EA GUI such as the one for MT4: Indicators: 'Money Manager Graphic Tool' indicator by 'takycard' Forum - Page 5
 
whroeder1:
  1. In code: Risk depends on your initial stop loss, lot size, and the value of the pair.
    • 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.
    • Account Balance * 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.)
    • 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 (EUR, in this case).
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    • You must normalize lots properly and check against min and max.
    • 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=0.1 Lots maximum.
  2. Use a EA GUI such as the one for MT4: Indicators: 'Money Manager Graphic Tool' indicator by 'takycard' Forum - Page 5

Yeh, and actually... you are the reason I'm posting this question. Because in almost all threads I've read on this topic you copy and paste that very text... text which doesn't address the original question at all. Weird.

 
truelifeajf: Yeh, and actually... you are the reason I'm posting this question. Because in almost all threads I've read on this topic you copy and paste that very text... text which doesn't address the original question at all. Weird.

Just because you don't understand the answer given by @whroeder1, does not mean that it is incorrect or that it does not address your question, when in fact it does address and answer your question.

So, in an effort to not create any conflict here and actually try to help you understand it, I am going to try to simplify it for you and steer you in the right direction.

  • In essence it only depends on two variables, the "Tick Value" and the "Stop Size (in ticks)", be that a S/L, or T/P.
  • So, forgetting about what "pips" are, lets assume that your stop size is 100 ticks and that the Tick Value reported by the system is 1.30 AUD per tick. This will automatically reflect the correct value based on your account currency (I have used AUD here to reflect your request). This value can constantly fluctuate and change, so it should be checked every time you need to apply it.
  • So 100 ticks x 1.30 AUD = 130 AUD (per standard lot). So if 100 ticks is worth 130.00 AUD per Lot, then 0.1 Lots is worth 13.00 AUD, and 0.01 Lots is worth 1.30 AUD.

That is the short and skinny version! Obviously, now you have to add some extra layers on top of that, such as how many ticks are in a point, how many points are in a pip, and other complications.

All of this is in @whroeder1's post from a coding point of view. It is up to you now to do the necessary research of basic trading principles (which includes these calculations, see below), as well as learning MQL coding to be able to apply it to your code.

Here are some extra details which I think you should research as well:

Forum on trading, automated trading systems and testing trading strategies

Who can educate me about forex

Fernando Carreiro, 2017.10.30 16:28

There are many sources of "Trading Education" available on the internet. Search and you will find for example:

 

ok, but I'm talking about MQL. The information provided thus far has nothing to do with MQL.

I make a start, and we can go from there. How about this?

double factor = 10000;

if (StringSubstr(Symbol(), 3) == "JPY") {

   factor = 100;

};

double tick_value = MarketInfo(Symbol(),MODE_TICKVALUE);

double lots = 0.5;

risk_percent = 1;

double pips = (risk_percent / 100 * AccountBalance()) / (factor * 10 * tick_value) / lots;


Am I on the right track?

 
The above code I just quickly punched out (I missed adding "double" before "risk_percent)
 
truelifeajf:

ok, but I'm talking about MQL. The information provided thus far has nothing to do with MQL.

I make a start, and we can go from there. How about this?

double factor = 10000;

if (StringSubstr(Symbol(), 3) == "JPY") {

   factor = 100;

};

double tick_value = MarketInfo(Symbol(),MODE_TICKVALUE);

double lots = 0.5;

risk_percent = 1;

double pips = (risk_percent / 100 * AccountBalance()) / (factor * 10 * tick_value) / lots;

Am I on the right track?

Yes, exactly! @whroeder1 gave you an MQL answer and provided links to code samples for the various points, but you demonstrated that you did not understand them. For that reason, I gave a simpler non-MQL answer.

However, unless you dedicate some research into the matter, it will be difficult to explain things to you in more details if you are unable to understand them, or least demonstrate an effort into following up on the points he mentioned.

OK! Here is another example, but this time it is my code and not @whroeder1's, since we all have our own individual styles of coding. It is an old example and uses a different approach, but it is still valid for the exercise in question:

Forum on trading, automated trading systems and testing trading strategies

Need moneymanagement LOT size formula based on SL and Account Risk!

Fernando Carreiro, 2014.01.09 02:37

OK guys! This code was written by me and it is what I use in my own EA's. It is quite complex because, besides the Risk% and StopLoss, it also takes into account the Margin Risk% as well as correct the Lot Size based on Minimum, Maximum and Step Size. It also always uses the minimum value of Current Balance and Equity instead of just using one of them. I feel it is safer that way.

Please note, however, that it does not use the spread in the calculation, because I calculate that separately when calculating the StopLoss to be used. The below function, thus uses a relative stop loss size. In my EA's the argument dblStopLossPips is calculated beforehand depending on various factors such as the strategy, spread, ATR, etc.; so the final value passed on to the dblLotsRisk() function is already a final value in order to calculate the Lot size to be used.

// Function to Determine Tick Point Value in Account Currency

        double dblTickValue( string strSymbol )
        {
                return( MarketInfo( strSymbol, MODE_TICKVALUE ) );
        }
        

// Function to Determine Pip Point Value in Account Currency

        double dblPipValue( string strSymbol )
        {
                double dblCalcPipValue = dblTickValue( strSymbol );
                switch ( MarketInfo( strSymbol, MODE_DIGITS ) )
                {
                        case 3:
                        case 5:
                                dblCalcPipValue *= 10;
                                break;
                }
                
                return( dblCalcPipValue );
        }
        

// Calculate Lot Size based on Maximum Risk & Margin

        double dblLotsRisk( string strSymbol, double dblStopLossPips,
                double dblRiskMaxPercent, double dblMarginMaxPercent,
                double dblLotsMin, double dblLotsMax, double dblLotsStep )
        {
                double
                        dblValueAccount = MathMin( AccountEquity(), AccountBalance() )
                ,       dblValueRisk    = dblValueAccount * dblRiskMaxPercent / 100.0
                ,       dblValueMargin  = AccountFreeMargin() * dblMarginMaxPercent / 100.0
                ,       dblLossOrder    = dblStopLossPips * dblPipValue( strSymbol )
                ,       dblMarginOrder  = MarketInfo( strSymbol, MODE_MARGINREQUIRED )
                ,       dblCalcLotMin
                                = MathMax( dblLotsMin, MarketInfo( strSymbol, MODE_MINLOT ) )
                ,       dblCalcLotMax
                                = MathMin( dblLotsMax, MarketInfo( strSymbol, MODE_MAXLOT ) )
                ,       dblModeLotStep  = MarketInfo( strSymbol, MODE_LOTSTEP )
                ,       dblCalcLotStep  = MathCeil( dblLotsStep / dblModeLotStep ) * dblModeLotStep
                ,       dblCalcLotLoss
                                = MathFloor( dblValueRisk / dblLossOrder / dblCalcLotStep ) * dblCalcLotStep
                ,       dblCalcLotMargin
                                = MathFloor( dblValueMargin / dblMarginOrder / dblCalcLotStep ) * dblCalcLotStep
                ,       dblCalcLot = MathMin( dblCalcLotLoss, dblCalcLotMargin )
                ;
                
                if ( dblCalcLot < dblCalcLotMin ) dblCalcLot = dblCalcLotMin;
                if ( dblCalcLot > dblCalcLotMax ) dblCalcLot = dblCalcLotMax;

                return ( dblCalcLot );
        }
 

I understood his answer but it was a generic copy-paste answer that he puts in all threads and my question was asking.. IN MQL... how would I do this. Babypips doesn't cover anything to do with MQL.


Your code looks great but it's adding to the confusion as all other threads do. My original question is about keeping it simple given the criteria outlined.

Anyway I answered my own 3 questions as outlined in my original post:

double tick_value = MarketInfo(Symbol(),MODE_TICKVALUE);


// I want to risk 1% of my account on 0.05 lots. How many pips is that?

   double lots = 0.05;

   double risk_percent = 1;

   double pips_for_risk = (risk_percent / 100 * AccountBalance()) / tick_value / lots; // substitute "risk_percent / 100" for a dollar amount if preferred

   

   // I want to make $50 in 50 pips. What lot size?

   double pips = 350;

   double dollar_goal = 50;

   double lots_size_for_goal = dollar_goal / (pips * 1 * tick_value);

   

   // I want to make a 50 pip win with 0.05 lots. How much money is that?

   double dollar_result = (pips * 10 * tick_value) * lots;



So from here, I can venture into the next set of questions:

  1. Why do I have to multiple pips by 10 for it to be accurate?
  2. what's wrong with using MODE_TICKVALUE as some threads suggest?
  3. how does one make this work for something like XAUUSD as it doesn't seem to calculate properly for that?

 

We get the Points from the broker and the broker do what they want with number of decimals, contract size, ... (like in a kindergarden)!

A pip has had some time a go that what is nowadays the point.

So I defined (for my self!) a pip is that was has the closest value of ~10,00 Euro or ~10.00 USD no matter what symbol.

 
truelifeajf: I understood his answer but it was a generic copy-paste answer that he puts in all threads and my question was asking.. IN MQL... how would I do this. Babypips doesn't cover anything to do with MQL.

Your code looks great but it's adding to the confusion as all other threads do. My original question is about keeping it simple given the criteria outlined.

Anyway I answered my own 3 questions as outlined in my original post:

So from here, I can venture into the next set of questions:

  1. Why do I have to multiple pips by 10 for it to be accurate?
  2. what's wrong with using MODE_TICKVALUE as some threads suggest?
  3. how does one make this work for something like XAUUSD as it doesn't seem to calculate properly for that?

Yes, we do copy/paste! We offer our "guidance" and "help" freely and ask nothing in return, yet you complain while looking a gift horse in the mouth.

Do you really expect us to offer or produce exact code to your specification, when you yourself are not putting in the required effort to learn the basics and use your own intelligence to extract what is needed from the examples provided?

We are not here to spoon-feed you the answer or to be your "mentor" or "teacher"!

If you are unhappy with what has been shared, then I suggest you use the Freelance section.

 

ok thanks... to keep things simple (to begin with at least)... for so long as I multiply this:

MarketInfo(Symbol(),MODE_TICKVALUE);

by 10, then everything is fine. So for me at least, I just multiple by 10.


So that just leaves:

  1. what's wrong with using MODE_TICKVALUE as some threads suggest?
  2. how does one make this work for something like XAUUSD as it doesn't seem to calculate properly for that?
Reason: