make a horizontal line that starts now

 

Hi Everyone,


Here is my small piece of code, and I cant figure out how to make the line start from the time the object was created to current bar, and not go all the way from the start entire screen.. Please help? :)


      string dtTimeBegin = TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);
      ObjectCreate("MyLine"+RecentTicket,OBJ_HLINE,0,dtTimeBegin,RecentExit);


// i also tried this but no avail 

ObjectSet("MyLine"+RecentTicket,OBJPROP_TIME1, TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
 

You're trying to put a string var where a datetime var is supposed to go.

Change it to:


ObjectCreate("MyLine"+RecentTicket,OBJ_HLINE,0,TimeCurrent(),RecentExit);

 

Wait what...

No no, hline is meant to go over the entire screen. It's like the horizontal ruler line in metatrader. You can't trim it.

For your purpose use trend line or TREND.

ObjectCreate("MyLine"+RecentTicket,OBJ_TREND,0,time1,RecentExit, TimeCurrent(), RecentExit );

ObjectSet("MyLine"+RecentTicket, OBJPROP_RAY,false);

You can also use TimeLocal() instead of TimeCurrent()...difference should be mostly noticed only when backtesting.... For indis I tend to use TimeLocal().

time1 up there is a datetime variable - date for the starting bar of your horizontal line.

The second line orders the trendline to not draw itself beyond its starting and ending points.

 
forexCoder:

Wait what...

No no, hline is meant to go over the entire screen. It's like the horizontal ruler line in metatrader. You can't trim it.

For your purpose use trend line or TREND.

ObjectCreate("MyLine"+RecentTicket,OBJ_TREND,0,time1,RecentExit, TimeCurrent(), RecentExit );

ObjectSet("MyLine"+RecentTicket, OBJPROP_RAY,false);

You can also use TimeLocal() instead of TimeCurrent()...difference should be mostly noticed only when backtesting.... For indis I tend to use TimeLocal().

time1 up there is a datetime variable - date for the starting bar of your horizontal line.

The second line orders the trendline to not draw itself beyond its starting and ending points.



Thanks so much for your help! =)
 
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());
}