Calculating the break even distance in points

 

Hi

I am trying to create a function that will determine the amount of points a position needs to move to hit a break even based on the commission charged by my broker plus an extra amount of the account currency. But I can't get the math right...

Here is the code I am using followed by the result in the history window.


#include <Trade/Trade.mqh>
CTrade trade;

input double lot = 0.1;
input double Commis = 7;
double commis;
int start;

int OnInit(){
   trade.SetExpertMagicNumber(999);
   start = 0;
   commis = Commis*lot;
   return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){
   
}
void OnTick(){
   
   if(start == 0){
      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      double tickSize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
      double tpDist = getTriggerDist(commis,5);
      double slDist = 100*_Point;
      Print("tpDist = ",tpDist);
      tpDist = NormalizeDouble(tpDist,_Digits);
      slDist = NormalizeDouble(slDist,_Digits);
      if(slDist >= tickSize && tpDist >= tickSize){
         trade.Buy(lot,_Symbol,ask,ask-slDist,ask+tpDist);
         trade.Sell(lot,_Symbol,bid,bid+slDist,bid-tpDist);
      }else Print("Incorrect slDist or tpDist...");
   }
   start = 1;
}

//calculate triggerDist for breakeven(from commission) + extra $
double getTriggerDist(double commissionCost, double extra){
   
   double tickSize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double tickValue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
   double triggerDist = 0;
   if(tickValue != 0 && tickSize != 0 && commissionCost != 0){
      triggerDist = (commissionCost+extra)*tickSize/tickValue; //?????
   }
   triggerDist = NormalizeDouble(triggerDist,_Digits);
   return triggerDist;
}


usdcad


Any help would be appreciated.

 

It looks like the core of your code is correct, but there might be a confusion regarding how tick values and tick sizes are used in the calculation. In the context of your getTriggerDist function, you're calculating a distance in points that corresponds to the cost of the commission plus an extra amount in terms of the account's currency.

Updated Code:

#include <Trade/Trade.mqh>
CTrade trade;

input double lot = 0.1;
input double Commis = 7;
input double ExtraAmount = 50.0; // The extra amount in account currency

double commis;
int start;

int OnInit() {
   trade.SetExpertMagicNumber(999);
   start = 0;
   commis = Commis * lot;
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {
   
}

void OnTick() {
   
   if (start == 0) {
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
      double tpDist = getTriggerDist(commis, ExtraAmount);
      double slDist = 100 * _Point;
      Print("tpDist = ", tpDist);
      tpDist = NormalizeDouble(tpDist, _Digits);
      slDist = NormalizeDouble(slDist, _Digits);
      if (slDist >= tickSize && tpDist >= tickSize) {
         trade.Buy(lot, _Symbol, ask, ask - slDist, ask + tpDist);
         trade.Sell(lot, _Symbol, bid, bid + slDist, bid - tpDist);
      } else {
         Print("Incorrect slDist or tpDist...");
      }
   }
   start = 1;
}

// calculate triggerDist for breakeven (from commission) + extra $
double getTriggerDist(double commissionCost, double extra) {
   
   double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
   double triggerDist = 0;
   if (tickValue != 0 && tickSize != 0 && commissionCost != 0) {
      triggerDist = (commissionCost + extra) / tickValue;
   }
   triggerDist = NormalizeDouble(triggerDist, _Digits);
   return triggerDist;
}

Changes I made:

  1. I added the ExtraAmount input variable to allow you to specify the extra amount in the account currency that you want to add to the break-even calculation.

  2. In the getTriggerDist function, I changed the calculation of triggerDist to (commissionCost + extra) / tickValue . This calculation considers the sum of commission cost and extra amount and then converts it to points based on the tick value.

Customize the ExtraAmount input value according to your needs.

Hope this helps!

 
binaryforexea #:

It looks like the core of your code is correct, but there might be a confusion regarding how tick values and tick sizes are used in the calculation. In the context of your getTriggerDist function, you're calculating a distance in points that corresponds to the cost of the commission plus an extra amount in terms of the account's currency.

Updated Code:

Changes I made:

  1. I added the ExtraAmount input variable to allow you to specify the extra amount in the account currency that you want to add to the break-even calculation.

  2. In the getTriggerDist function, I changed the calculation of triggerDist to (commissionCost + extra) / tickValue . This calculation considers the sum of commission cost and extra amount and then converts it to points based on the tick value.

Customize the ExtraAmount input value according to your needs.

Hope this helps!

Thanks for the help. I ran your code on the tester but still got incorrect values. Turn out the correct formula for triggerDist is:

triggerDist = (commissionCost + extra) / tickValue * tickSize / lot;

This is because the tickValue is for 1 whole lot. Now everything is working as I wanted it.

Thanks again.
 

Reason: