Jump to content
Medved Trader Forums

SetLine example


Recommended Posts

I'm trying to use SetLine to plot multiple horizontal lines from a single paintbar.  The code is below.

What I'm struggling with is the SetLine ID and X axis values.

ID, I'm not sure what to put, so I copied TradingDay.DayNumber from the support example

X1 and X2, I put CandleNumber -15, Candle Number, to at least see if it would plot.

The result is, it only plots the 4th (blue) SetLine.

 

Do you have any suggestions for the proper ID and X values, so that all 4 lines plot, and starts from the beginning of 3 sessions ago?

___________________________________________________

 

MT2.PNG

Edited by nasdorq
Link to comment
Share on other sites

public void MainCalculation()
{
    var TS     = GetTradingSessionInfo(Timestamp);

    if (DateTime.UtcNow>TS.DayStart.AddDays(3))     // only plot last 3 days
        return;
    
    var Line1 = 4460;    //  prices for horizontal lines
    var Line2 = 4455;
    var Line3 = 4450;
    var Line4 = 4445;

    Color Orange = (Color.Orange);
    SetLine(TradingDay.DayNumber, Orange, CandleNumber - 15, Line1, CandleNumber, Line1, 2, null);
    
    Color Red = (Color.Red);
    SetLine(TradingDay.DayNumber, Red, CandleNumber - 15, Line2, CandleNumber, Line2, 2, null);
    
    Color Yellow = (Color.Yellow);
    SetLine(TradingDay.DayNumber, Yellow, CandleNumber - 15, Line3, CandleNumber, Line3, 2, null);
    
    Color Blue = (Color.Blue);
    SetLine(TradingDay.DayNumber, Blue, CandleNumber - 15, Line4, CandleNumber, Line4, 2, null);

Link to comment
Share on other sites

The ID is the unique ID of the line. What that means is that each SetLine that you are showing in your code is setting the same line - that is, it is re-setting the line to the new values. Because the ID of the line does not change.

For example, If you have a line which starts at a particular candle number, AND you know that it's the only line that will start at that candlenumber AND you know that the line, once started, will stay at that candle number, then it would be logical to use the candle number as the line's ID.

What your code above does is, for each day of the last few days, it draws a blue line (the last SetLine in your code) from the last candle of the day to the 15th candle prior to that. At 4445 - because that is the value have in the last SetLine.

 

 

 

Link to comment
Share on other sites

Thanks Mike.  I set the line ID's to simply #s 1 through 4.  I tried giving them names but that returned an error, so numbered will work.  One last follow up if I may?

1)  I seem to remember reading that paintbars, at least a while back, can't plot beyond the current candle into the future.  I tried entering "CandleNumber 5" for x2, and got a bunch of errors.  However if I put "CandleNumber -(-5)", with the double negative becoming a positive, it does plot to the right of the current candle.  It goes infinitely right, regardless of whether it's -(-1) or -(-100).  You can see the example with the red line in the attached chart and the code.

Do you think having them plot beyond the current candle will cause issues with regards to stability or resource use?  I was hoping to have a bunch of these on different charts.

2)  To have the x1 value start plotting from the open 3 sessions ago, is there an easy way to reference the CandleNumber for the opening bar 3 sessions ago?  (I was hoping to use this on charts with different timeframes, otherwise I could just count the # of bars).  I figure I could save it as a var, and then reference it in the SetLine as CandleNumber -(var).

__________________________________________

public void MainCalculation()
{
    var TS     = GetTradingSessionInfo(Timestamp);

    if (DateTime.UtcNow>TS.DayStart.AddDays(3))     // only plot last 3 days
        return;
    
    var Line1 = 4460;    //  prices for horizontal lines
    var Line2 = 4455;
    var Line3 = 4450;
    var Line4 = 4445;
    var test  = 10;

    Color Orange = (Color.Orange);
    SetLine(1, Orange, CandleNumber -15, Line1, CandleNumber, Line1, 2, null);
    
    Color Red = (Color.Red);
    SetLine(2, Red, CandleNumber -15, Line2, CandleNumber -(-5), Line2, 2, null);
    
    Color Yellow = (Color.Yellow);
    SetLine(3, Yellow, CandleNumber -15, Line3, CandleNumber, Line3, 2, null);
    
    Color Blue = (Color.Blue);
    SetLine(4, Blue, CandleNumber -15, Line4, CandleNumber, Line4, 2, null);
    
}

MT3.PNG

Link to comment
Share on other sites

1. CandleNumber+5 will work. Not "CandleNumber 5" :). And yes you can just do +whatever, it will just go far to the right.

2. In order to start at a particular candle, you have to remember the number of the candle. Since the paintbar evaluates looking at all candles, from the earliest to the latest, in the code you can see whether the candle being looked at is at start of session, and if it is, remember its number to be used later.

 

Link to comment
Share on other sites

Sorry one more quick follow up.   Almost there   :)

I'm not quite sure how to reference and save particular candles within the code.

I figured I'd just set x1 to "-1000".  That way it would cover several days on a 1min chart, and all available data on the 60min chart.  However, -1000 bars on the 60min goes way beyond the # of bars on the chart, and causes the paintbar to stop working.

Is there an easy way to reference the furthest left candle on any chart, so I could use this on multiple timeframes?  If not, I can make do and try to find workarounds.

 

Thanks for your help with this.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...