Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

sma horizontal line across chart

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    sma horizontal line across chart

    Hi, I'm trying to develop an indicator that draws a horizontal line across a chart at the current daily 21 sma price level. Can i get some advice? I have attached my attempt, which doesn't draw anything yet.
    thanks,
    David
    Attached Files

    #2
    Hello trader3000a,

    Thank you for your note.

    Once you have the desired value that you wish to plot the line at, you'd simply need to set it up to call Draw.HorizontalLine on each pass through OnBarUpdate:

    Code:
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 20)
    return;
    double SMAprice = SMA(20)[0];
    Draw.HorizontalLine(this, "myLine", SMAPrice, Brushes.Yellow);
    }
    Please let us know if we may be of further assistance to you.

    Comment


      #3
      thanks Kate,

      I'm wondering why, in the indicator wizard, we bother with the "plot lines..." section where i created: AddPlot(new Stroke(Brushes.SeaShell, 2), PlotStyle.Hash, "SmaDay21");

      if we are going to write
      Draw.HorizontalLine(this, "myLine", SMAPrice, Brushes.Yellow); anyway?

      also wondering how i add the additional data series (daily) to the string " double SMAprice = SMA(20)[0];" Do I add [1] after [0]?

      thanks,
      David

      Comment


        #4
        Hello trader3000a,

        Thank you for your reply.

        Indicators don't have to have a plot if you don't need one. For what you're asking you can just use a horizontal line drawing tool.

        If you want to calculate an SMA off an added Daily series assuming that daily series is the first/only added data series, you could do that like this:

        Code:
        double SMAprice = SMA(BarsArray[1], 20)[0];
        You'd also want to modify the CurrentBar check to make sure you have at least 20 daily bars before processing:

        Code:
        protected override void OnBarUpdate()
        {
        if (CurrentBars[1] < 20)
        return;
        double SMAprice = SMA(BarsArray[1],20)[0];
        Draw.HorizontalLine(this, "myLine", SMAPrice, Brushes.Yellow);
        }
        Please let us know if we may be of further assistance to you.

        Comment


          #5
          i revised but it still doesn't draw anything. Can you take a look at the attached?
          Attached Files

          Comment


            #6
            Hello trader3000a,

            Thank you for your reply.

            How many days to load are you loading on the chart? You need to load at least as many days as the period as you're using for the SMA. Try changing Days to Load to 30 in the Data Series window and then you'll probably need to adjust the chart scale on the price axis to see the line plotted.

            Please let us know if we may be of further assistance to you.

            Comment


              #7
              Hi again Kate; the indicator only works on a daily chart so far. What I'm aiming for is an indicator that will draw the line at the daily 21sma price on ANY chart (5 minute time series, range chart, etc). I added the daily data series under state.configure but not sure how that talks to the logic.

              Comment


                #8
                Hello trader3000a,

                Thank you for your reply.

                Again, your issue is that you have to have enough days to load specified for the primary series in order for enough data to be loaded to calculate the 21 (in the script you sent it was 20) period SMA. So you will need to ensure you specify in the Data Series window that the chart load enough data to load at least 21 daily bars or the line won't appear. Please see my data series settings and your unaltered script running on my chart below:

                Click image for larger version

Name:	2021-12-10_07-11-00.png
Views:	193
Size:	67.4 KB
ID:	1181199

                Click image for larger version

Name:	2021-12-10_07-12-08.png
Views:	185
Size:	53.5 KB
ID:	1181200

                Please let us know if we may be of further assistance to you.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                603 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                349 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                104 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                560 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                560 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X