Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"Price is equal to/smaller/larger than"

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

    "Price is equal to/smaller/larger than"

    Seems I can't see the forest for the trees this morning ...

    How do I code "price is equal to an MA"?

    Close == SMA(20).

    and "price is between two MAs"?

    Close < SMA(20) && Close > SMA(50)

    "Close" does not do it. What does?

    Anyone can help me to get out of my jumbled state of mind please?

    sandman
    Attached Files

    #2
    Hello sandman,

    Thanks for your post.

    I think the forest is missing the bar index which means which bar are you comparing (usually, but not always, it is the current bar [0])

    if (Close[0] == SMA(20)[0])
    Is the close price equal to the 20 period SMA. Note that this is likely to be a rare event as you are asking a computer to compare very specific values that likely will never be precisely the same. You might be best to make a comparison of the close price to SMA(20)[0] + 1 *TickSize or SMA(20)[0] - 1 * TickSize. Depends on how precise you need to be.


    if ((Close[0] > SMA(20)[0] && Close[0] < SMA(50)[0]) || (Close[0] < SMA(20)[0] && Close[0] > SMA(50)[0])
    This is checking to see if the close price of the current bar is between the SMA20 and the SMA50 when the SMA20 is below the SMA50 OR if the close price is between the SMA20 and the SMA50 when the SMA20 is above the SMA50. You may not want to have both sets in one but put this together in case you did.

    Comment


      #3
      Paul,

      I was finally able to get back to this piece of coding. Despite your input I haven't been able to make it work. Price hits an SMA 20 and is then supposed to draw a Dot. No dots no matter what I tried so far. Does the draw method not work in this case? Here is what I am using:

      protected override void OnBarUpdate()
      {

      if (CurrentBars[0] < BarsRequired)
      return;

      if (CurrentBar < 1)
      return;

      // Condition =========================

      if (Close[0] == SMA(20)[0] + 3 *TickSize)

      {
      DrawDot("My dot up" + CurrentBar, false, 0, Low[0], Color.Red);
      }
      }

      sandman

      Comment


        #4
        Hello Sandman,

        Thanks for your reply.

        The condition is pretty specific so I would not expect to see a lot of dots. Also, the dots may not be obvious as they may be hidden by the bars themselves.

        I've recreated your code and applied it to the ES 09-16 1 minute bars, add a 20 period SMA for visual reference. At first I also thought there were no dots but on closer inspection I found a few partially hidden by the bars.

        I then modified your code and had it draw two separate dots, one was with the condition of the Close[0] == SMA(20)[0] (red dots) and the other with the condition Close[0] == SMA(20)[0] + 3 * TickSize (Blue dots). In the chart I observed many red dots and a few blue dots. To help see the dots I offset the dot drawing location from Low[0] to Low[0] - 3 * TickSize.

        Please see the attached picture for reference.
        Attached Files

        Comment


          #5
          Thanks Paul, I'll give it another go with your suggestion. Question: Would all the dots show up historically or only now and then?

          sandman

          Comment


            #6
            Hello Sandman,

            Thanks for your reply.

            With your DrawDot tag name of "My dot up" + CurrentBar you will see every dot produced both historically and live.

            Comment

            Latest Posts

            Collapse

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