How do I make profit appear above the candle?

 

hi vereyone

How do I make profit appear above the candle?

can anyone help me to get this code plz


 

I coded something


but I'm facing a problem where it won't display over the next trades


might be just a problem in the strategy tester, here is my code:

void OnTick(){

        examineLastTrade();
}


void examineLastTrade(){

    double offset = 50*_Point;

    if (!PositionSelect(Symbol())){
        
      HistorySelect(TimeCurrent() - 3600, TimeCurrent());

         for (int i = 0; i < HistoryDealsTotal(); i++){
         
             ulong ticket = HistoryDealGetTicket(i);      
             
             if (ticket > 0){
             
                double dealProfit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
                long dealTime = HistoryDealGetInteger(ticket, DEAL_TIME); 
                
                int barIndex = iBarShift(_Symbol, _Period, dealTime);     
                double pricePosition = iHigh(_Symbol, _Period, barIndex) + offset;
                datetime positionTime = iTime(_Symbol, _Period, barIndex);
                
                Profit_display(0, "deal_profit", "$" + FormatString(dealProfit), pricePosition, 8, clrSilver, positionTime);                
             }  
        
             else{
                 Print("Failed to get ticket for index ", i);
             }
         }       
    }   
}


void Profit_display(int id, string name, string text, double distance, int fontSize, color fontColor, datetime time){

    if (ObjectFind(0, name) < 0){
      ObjectCreate(id, name, OBJ_TEXT, 0, time, distance);
    } 
    ObjectSetInteger(id, name, OBJPROP_CORNER, 0);
    ObjectSetInteger(id, name, OBJPROP_FONTSIZE, fontSize);
    ObjectSetInteger(id, name, OBJPROP_COLOR, fontColor);
    ObjectSetString(id, name, OBJPROP_TEXT, text);
}


string FormatString(double value) {
    string str = DoubleToString(value, 8);

    // Find the position of the decimal point
    int dotPosition = StringFind(str, ".");
    
    if (dotPosition != -1){
    
        // Check the part after the decimal point
        string fractionalPart = StringSubstr(str, dotPosition + 1);

        // If the fractional part has only one zero, keep it
        if (StringLen(fractionalPart) == 2 && StringSubstr(fractionalPart, 1, 1) == "0"){      
            str = StringSubstr(str, 0, dotPosition + 3); // Keep two decimal places
        } 
        else{
            // Remove trailing zeros
            while (StringLen(str) > 0 && StringSubstr(str, StringLen(str) - 1, 1) == "0"){
            
                str = StringSubstr(str, 0, StringLen(str) - 1); // Remove trailing zero
            }

            // Remove the trailing dot if any
            if (StringSubstr(str, StringLen(str) - 1, 1) == "."){
                str = StringSubstr(str, 0, StringLen(str) - 1);
            }
        }
    }

    return str;
}
 

I fixed the issue, I forgot that each text object should have a unique name.


        
void examineLastTrade(){

    double offset = 50*_Point;

    if (!PositionSelect(Symbol())){
        
      HistorySelect(TimeCurrent() - 3600, TimeCurrent());

         for (int i = 0; i < HistoryDealsTotal(); i++){
         
             ulong ticket = HistoryDealGetTicket(i);      
             
             if (ticket > 0){
             
                double dealProfit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
                long dealTime = HistoryDealGetInteger(ticket, DEAL_TIME); 
                
                int barIndex = iBarShift(_Symbol, _Period, dealTime, true);     
                double pricePosition = iHigh(_Symbol, _Period, barIndex) + offset;
                datetime positionTime = iTime(_Symbol, _Period, barIndex);
                
                Profit_display(0, "deal_profit" + IntegerToString(ticket), "$" + FormatString(dealProfit), pricePosition, 8, clrSilver, positionTime);
                
             }  
        
             else{
                 Print("Failed to get ticket for index ", i);
             }
         }       
    }   
}
 

Hi

Just a small tip – if you want the profit looked similar to what you have on screenshot, try using canvas and create bitmaps with text.

CCanvas canvas;
      canvas.CreateBitmap(objName,TimeCurrent(),SymbolInfoDouble(Symbol(),SYMBOL_ASK),50, 50,COLOR_FORMAT_ARGB_NORMALIZE);
      canvas.Erase(ColorToARGB(clrNONE, 0));    
      canvas.FillRectangle(0,0,200,200,ColorToARGB(clrBlue,200));
      canvas.TextOut(10,5, DoubleToString(profit,2)+"$", ColorToARGB(clrWhite, 255), TA_VCENTER );
      canvas.Update();

Best regards