Trying to convert an mql4 to mql5 breakeven indicator but nothing is drawn on the chart.

 

Trying to convert an mql4 to mql5 breakeven indicator but nothing is drawn on the chart.  

My mql5 conversion


What is wrong ?


//+------------------------------------------------------------------+
//|                                           Average Price v3.0.mq4 |
//|                                        Joca - nc32007a@gmail.com |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Joca"
#property indicator_chart_window
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;  
//---
color font_color=White;
int font_size=12;
//---
int PipAdjust,NrOfDigits;
double point;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnInit()
  {
   ObjectDelete(ChartID(),"Average_Price_Line_"+Symbol());
   ObjectDelete(ChartID(),"Information_"+Symbol());
//---
   NrOfDigits=Digits();
//---
   if(NrOfDigits==5 || NrOfDigits==3) PipAdjust=10;
   else
      if(NrOfDigits==4 || NrOfDigits==2) PipAdjust=1;
//---
   point=Point()*PipAdjust;
//---
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit()
  {
   ObjectDelete(ChartID(),"Average_Price_Line_"+Symbol());
   ObjectDelete(ChartID(),"Information_"+Symbol());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int Total_Buy_Trades;
   double Total_Buy_Size;
   double Total_Buy_Price;
   double Buy_Profit;
//---
   int Total_Sell_Trades;
   double Total_Sell_Size;
   double Total_Sell_Price;
   double Sell_Profit;
//---
   int Net_Trades;
   double Net_Lots;
   double Net_Result;
//---
   double Average_Price;
   double distance;
   double Pip_Value=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE)*PipAdjust;
   double Pip_Size=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE)*PipAdjust;
//---
   int total=PositionsTotal();
//---
   for(int i=0;i<total;i++)
     {
      int ord=m_position.SelectByIndex(i);
        {
         if(m_position.PositionType()==POSITION_TYPE_BUY && m_position.Symbol()==Symbol())

           {
            Total_Buy_Trades++;
            Total_Buy_Price+=m_position.PriceOpen()*m_position.Volume();
            Total_Buy_Size += m_position.Volume();
            Buy_Profit+=m_position.Profit()+m_position.Swap()+m_position.Commission();
           }
         if(m_position.PositionType()==POSITION_TYPE_SELL && m_position.Symbol()==Symbol())
           {
            Total_Sell_Trades++;
            Total_Sell_Size+=m_position.Volume();
            Total_Sell_Price+=m_position.PriceOpen()*m_position.Volume();
            Sell_Profit+=m_position.Profit()+m_position.Swap()+m_position.Commission();
           }
        }
     }
   if(Total_Buy_Price>0)
     {
      Total_Buy_Price/=Total_Buy_Size;
     }
   if(Total_Sell_Price>0)
     {
      Total_Sell_Price/=Total_Sell_Size;
     }
   Net_Trades=Total_Buy_Trades+Total_Sell_Trades;
   Net_Lots=Total_Buy_Size-Total_Sell_Size;
   Net_Result=Buy_Profit+Sell_Profit;
//---
   ObjectDelete(ChartID(),"Average_Price_Line_"+Symbol());
   ObjectDelete(ChartID(),"Information_"+Symbol());
//---
   if(Net_Trades>0 && Net_Lots!=0)
     {
      distance=(Net_Result/(MathAbs(Net_Lots*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE)))*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE));
      if(Net_Lots>0)
        {
         Average_Price= SymbolInfoDouble(_Symbol, SYMBOL_BID)-distance;
        }
      if(Net_Lots<0)
        {
         Average_Price= SymbolInfoDouble(_Symbol, SYMBOL_ASK)+distance;
        }
     }
   if(Net_Trades>0 && Net_Lots==0)
     {
      distance=(Net_Result/((SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE)))*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE));
      Average_Price=SymbolInfoDouble(_Symbol, SYMBOL_BID)-distance;
     }
   ObjectDelete(ChartID(),"Average_Price_Line_"+Symbol());
   ObjectCreate(ChartID(),"Average_Price_Line_"+Symbol(),OBJ_HLINE,0,0,Average_Price);
   ObjectSetInteger(ChartID(),"Average_Price_Line_"+Symbol(),OBJPROP_WIDTH,3);
//---
   color cl=Blue;
   if(Net_Lots<0) cl=Red;
   if(Net_Lots==0) cl=White;
//---
   ObjectSetInteger(ChartID(),"Average_Price_Line_"+Symbol(),OBJPROP_COLOR,cl);
   ObjectCreate(ChartID(),"Information_"+Symbol(),OBJ_LABEL,0,0,0);
//---
   int x,y;
   ChartTimePriceToXY(0,0,iTime(Symbol(),_Period,0),Average_Price,x,y);
//---
   ObjectSetInteger(ChartID(),"Information_"+Symbol(),OBJPROP_XDISTANCE,220);
   ObjectSetInteger(ChartID(),"Information_"+Symbol(),OBJPROP_YDISTANCE,y);
   ObjectSetString(ChartID(),"Avrg= "+DoubleToString(Average_Price,NrOfDigits)+"  "+DoubleToString(distance/(point),1)+" pips ("+Net_Result+" "+AccountInfoString(ACCOUNT_CURRENCY)+")  Lots= "+Net_Lots+"  Orders= "+Net_Trades,OBJPROP_TEXT,White);
//---
   return(0);
  }
//+------------------------------------------------------------------+

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.06.23
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Ok I tried the CODE button but it did not grasp properly. better next time. the mq4 and mq5 code is attached as a file to the post
 
Sander Bruyneel #: Ok I tried the CODE button but it did not grasp properly. better next time.

It doesn't “grasp” anything. You paste your code into it. No next time. This time. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
      General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
          Messages Editor
      Forum rules and recommendations - General - MQL5 programming forum (2023)

 
It's like saying "here's my headache, I want to give my headache to you, so now it's your headache".
I'm not taking the piss, it's just that people will often ignore posts when it is a whole project they have nothing to do with. Otherwise it is a freelance job to get it working
 

iTime(_Symbol, _Period, 0)  won't give the correct time of the latest bar in OnCalculate. Use time[rates_total-1] instead.


Edit:

I'm fixing it for you

 

A mistake was made with ObjectSetString, the object name wasn't put in there. Some other adjustments needed as well.

 
Conor Mcnamara #:

A mistake was made with ObjectSetString, the object name wasn't put in there. Some other adjustments needed as well.

Thank you Conor it works now. 

 
Comments that do not relate to this topic, have been moved to "Discussion about OnCalculate returning 0.".