Commenting Profit or loss to chart

To add comments, please log in or register
Pete318
48
Pete318  

I am trying to put a comment on a chart for the profit or loss of the last trade for the chart the indicator is on.

I have brought some programming together, but it just gives a zero value.

I probably need to tell it what trade / currency pair to look at. Not sure how, or if needed.

I have all my history open and available for the indicator to look at. The last being -0.90 loss.

Any help with the following code appreciated. An explanation of what I am doing wrong or missing would be very helpful too.

This code will not be used in a EA. It is only for an indicator on each chart. The EA I drop on the chart, auto activates if last trade was a loss, therefore I want this to warn me prior to dropping an EA onto the chart.

Message edited;

NB:The code below is now updated to where it does what I want.


//+------------------------------------------------------------------+
//|                                                   PnLcomment.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, PC."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
                
//--- Comment to Chart
  {
  string pnl;
  if(lastTradeTicket()>0) pnl="PROFIT";
  else if(lastTradeTicket()<0) pnl="LOSS"; 
  
   Comment("LAST CLOSED TRADE =  "+pnl+"  @ $"+DoubleToStr(lastTradeTicket()/100,2));// Needs some lines to tell what order to look at in history
   return(rates_total);
  }
//---Selecting last order for this chart.

double lastTradeTicket()
  {
   int last=0;
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        if(OrderSymbol()==Symbol())
        {
         last = int(OrderProfit()*100);
         break;
        }
     }
   return(last);
  }
//+------------------------------------------------------------------+




Thank you in advance

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. Coding help Hi, Mr. Guru could you...
William Roeder
20208
William Roeder  
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  3.  ticket=OrderProfit(); ticket is an int, OrderProfit() is a double, profit=OrderProfit()+OrderSwap()+OrderCommission()
Pete318
48
Pete318  
whroeder1:
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  3.  ticket=OrderProfit(); ticket is an int, OrderProfit() is a double, profit=OrderProfit()+OrderSwap()+OrderCommission()

Thank you for point 1. I have learnt something and will abide with this in future. (Hopefully edited correctly)

Point 2. This is not for an EA. My EA has a way of creating its own value on each chart it is placed on. My problem is my EA when dropped onto a new chart will react to the last trade being a losing trade. Therefore for the meantime, I want a simple indicator to leave a comment on the screen to pre-warn me of the condition of the last trade made on this currency pair. (One currency pair per chart: and I generally remove my EA at end of days trading.)

Point 3. I guessed OrderProfit() was a double, that is why I changed ticket from a int to a double. Unfortunately putting either in does not help with Indicator not functioning.

Thanks for your help.

William Roeder
20208
William Roeder  
Pete318: My problem is my EA when dropped onto a new chart will react to the last trade being a losing trade.

Then it shouldn't find any last trade. Read the link in #1.2

Pete318
48
Pete318  
whroeder1:

Then it shouldn't find any last trade. Read the link in #1.2

Unfortunately in practice it does find the last trade. It is designed to put in further trades if the last one was a losing trade.

That being said, I have completed the Indicator to where I want it. (I have updated the CODE, in the original post, so as not to fill this post with too much information).

A Proper programmer will laugh at the mess I have left. However it has no error messages now and comments to the chart the correct information from my history. Also it clears the comment from the screen  on deleting the indicator.

Long term I will have to address my EA, but for now I have a visual on the chart.

I thank you for reply's. It makes trying to do this sort of thing a little less lonely when someone like yourself will take time to help where they can.

To add comments, please log in or register