Object text at a line position problem

 

For some reason I am having differculty in getting some text to appear on the left of the screen, at the same position that a line was created. I thought the code was simply, but the text keeps appearing at the top of the screen, which means that the Y position passed is not being accepted. Is there an easy fix for this? Here is the code;

// Create a horizontal line to reference other stop loss

ObjectCreate("Stoploss_Line_Ref",OBJ_HLINE,NULL,Time[0],Low[0]); //create stoploss line at the low

ObjectSet("Stoploss_Line_Ref",OBJPROP_COLOR,Ext_SL_LineColorRef);

ObjectSet("Stoploss_Line_Ref",OBJPROP_WIDTH,1);

//Create a label on the stop loss reference line

ObjectCreate("Reference_Line",OBJ_LABEL,0,0,0,0,0);

ObjectSet("Reference_Line",OBJPROP_XDISTANCE,1);

ObjectSet("Reference_Line",OBJPROP_YDISTANCE,Low[0]); //************problem here with Y-Coord not accepting Low

ObjectSetText("Reference_Line","Reference",8,"Tahoma",Ext_SL_LineColorRef);

Is there some way I have to convert the Low to a YDISTANCE? Any tips please.

Thanks ahead of time.

Colin

 

ObjectCreate("Reference_Line",OBJ_LABEL,0,0,0,0,0);

ObjectSet("Reference_Line",OBJPROP_XDISTANCE,1);

ObjectSet("Reference_Line",OBJPROP_YDISTANCE,Low[0]); //************problem here with Y-Coord not accepting Low

ObjectSetText("Reference_Line","Reference",8,"Tahoma",Ext_SL_LineColorRef);

Is there some way I have to convert the Low to a YDISTANCE? Any tips please.

Thanks ahead of time.

Colin

I don't think so. Label coordinates are in pixels, not date/price. Even if you manage to convert it from date/price to x/y your label will disconnect from line on zoom or window scale change.

You can use description (but this will show your label label on right side), or replace OBJ_LABEL with OBJ_TEXT. In the second case you will have to move object to the right side manually when new bars arrive.

HTH,

Luke.

 
  1. at the same position that a line was created

    OBJ_TEXT and use Time[0], Low[0]. The text will move left as new bars are created.


  2. If you're going to reuse object names ("Reference_Line" is a constant name) then you must either 1) try to delete the object and then create with coordinates, or 2) try to create (no error checking) and then set the coordinates (or move object) like you're doing. I.e ObjectCreate(coordinates) will fail after the first time and the object won't be moved.
  3. Simplify code by writing common functions like:
    void TLine( string name, datetime T0, double P0, datetime T1, double P1
              , color clr, bool ray=false ){                #define WINDOW_MAIN 0
        if (!Show.Objects)  return;
        /**/ if(ObjectMove( name, 0, T0, P0 ))      ObjectMove(name, 1, T1, P1);
        else if(!ObjectCreate( name, OBJ_TREND, WINDOW_MAIN, T0, P0, T1, P1 ))
            Alert("ObjectCreate(",name,",TREND) failed: ", GetLastError() );
        else if (!ObjectSet( name, OBJPROP_RAY, ray ))
            Alert("ObjectSet(", name, ",Ray) failed: ", GetLastError());
        if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
            Alert("ObjectSet(", name, ",Color) [2] failed: ", GetLastError());
        string  P0t = PriceToStr(P0);           if (MathAbs(P0 - P1) >= Point)
                P0t = StringConcatenate(P0t, " to ", PriceToStr(P1));
        if (!ObjectSetText(name, P0t, 10))
            Alert("ObjectSetText(",name,") [2] failed: ", GetLastError());
    }
    void HLine(string name, double P0, color clr){  //      #define WINDOW_MAIN 0
        if (!Show.Objects)  return;
        /**/ if (ObjectMove( name, 0, Time[0], P0 )){}
        else if(!ObjectCreate( name, OBJ_HLINE, WINDOW_MAIN, Time[0], P0 ))
            Alert("ObjectCreate(",name,",HLINE) failed: ", GetLastError() );
        if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
            Alert("ObjectSet(", name, ",Color) [1] failed: ", GetLastError() );
        if (!ObjectSetText(name, PriceToStr(P0), 10))
            Alert("ObjectSetText(",name,") [1] failed: ", GetLastError());
    }
    

 

Thanks for that. I new there was a simple solution. The OBJ_TEXT and ObjectMove will solve all. Not sure why I was using a label.

Cheers

Colin

Reason: