Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Paint bar, then un-paint it

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

    Paint bar, then un-paint it

    I'm developing an indicator that paints the bar white when certain conditions are met. I have CalculateOnBarClose set to false because I like the indicator to alert me as the bar is being built in real time.

    One of the conditions is based on where the bar closes - so on that final tick when the bar actually closes, sometimes the conditions are no longer met, but the bar remains white. I don't want that. I want it to go back to being a normal bar, and to remove my target and stop dots, if the actual bar close doesn't meet my criteria.

    I hope that makes sense.

    Here's the code:

    // Condition set 1
    if (conditions for long signal)
    {
    BarColor = Color.White;
    DrawDot("Target" + CurrentBar, false, 0, Close[0] + Target * TickSize, Color.Lime);
    DrawDot("Stop" + CurrentBar, false, 0, Close[0] - Stop * TickSize, Color.Red);
    Alert("myAlert", NinjaTrader.Cbi.Priority.High, "Go long", AlertSound, 2, Color.Black, Color.Yellow);
    }

    // Condition set 2
    if (conditions for short signal)
    {
    BarColor = Color.White;
    DrawDot("Stop" + CurrentBar, false, 0, Close[0] + Stop * TickSize, Color.Red);
    DrawDot("Target" + CurrentBar, false, 0, Close[0] - Target * TickSize, Color.Lime);
    Alert("myAlert", NinjaTrader.Cbi.Priority.High, "Go short", AlertSound, 2, Color.Black, Color.Yellow);
    }

    Thanks for your help.

    #2
    Hello bbd_san,

    Thank you for your note.

    If the conditions on that specific bar did not meet the criteria of your if statement, the bars should not be painted. Without the if statements I'm unable to test on my end. I have attached an indicator I used to test, and you can see if you apply it, that no bars that have the same close as the previous bar, also have a dot above, below, or a white bar.

    I would suggest adding print statements to check whether your conditions are becoming true. I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
    Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


    I’ve provided a link covering debugging which you may find helpful.
    Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

    Please let us know if you need further assistance.
    Attached Files
    Last edited by NinjaTrader_AlanP; 03-13-2017, 02:28 PM.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thank you. Here is one of my conditions:

      // Condition for a long signal
      if(Close[0] > EMA(TriggerEMA)[0])
      {
      BarColor = Color.White;
      DrawDot(blah blah blah);
      Alert(blah blah blah);
      }

      I have COBC set to false because I want the alert to play intra-bar, if the price begins to tick above my TriggerEMA. That way I know to start watching that chart. As soon as I get one tick above the TriggerEMA, the bar is painted white and is forever more painted white, even if the bar ends up closing below the TriggerEMA.

      Each tick is treated as if that is the close of the bar (Close[0]), even though it's not really the close until the last tick. On historical bars it is fine. But for bars drawn during the live session, there are multiple bars that are white even though they didn't close above the TriggerEMA.

      I tried using:

      if(FirstTickOfBar && Close[1] > EMA(TriggerEMA)[1])

      But it doesn't seem to work at all. Is it okay to use those terms (Close[1] and EMA(TriggerEMA)[1]) with the 1 instead of the 0?

      Comment


        #4
        Hello bbd_san,

        If Close[0]>EMA(Period)[0] becomes true, the actions are executed and the bar is painted when the indicator has COBC set to false. If the if statement becomes true, it stays true regardless of whether the price came back and closed below the EMA. If you had EnterLong() method call within your actions block, and that if statement became true, a buy order would be submitted even if the price came back and closed below.

        If you want the bar to be painted if the close is above at the end of the bar, you should have COBC set to true.

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Thank you. If the last tick of the bar is < EMA(Period)[0] then the statement is no longer true. So at that point I want the bar to go back to normal. With MultiCharts.NET I am able to use a Reset() as in:

          if(criteriaBuy[0])
          {
          Plot1.Set.....
          }
          else
          {
          if(criteriaSell[0])
          {
          Plot1.Set.....
          }
          else
          {
          Plot1.Reset();
          }
          }

          Works perfectly. But I couldn't find such a command for NT.

          So what I'm asking is can I change the color of the bar to white when my conditions are met, then change it back when my conditions are not met, all while the bar is being built tick by tick?

          Comment


            #6
            I think I may have answered my own question. I can use BarColor = Color.Empty in an else statement at the end. How do I clear the dots drawn by DrawDot("Stop"+CurrentBar....... ?

            Comment


              #7
              RemoveDrawObject("Stop"+(CurrentBar-1));
              eDanny
              NinjaTrader Ecosystem Vendor - Integrity Traders

              Comment


                #8
                Thank you!

                Comment


                  #9
                  I actually had to use RemoveDrawObject("Stop" + CurrentBar); DrawDot("Stop" + CurrentBar, ...); is called on each tick as the bar is being built. But if the next tick no longer meets my criteria, then I need to get rid of those same dots, not the ones on the previous bar.

                  Here's the final version:

                  protected override void Initialize()
                  {
                  Overlay = true;
                  CalculateOnBarClose = false;
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {

                  // Play alert every tick if conditions met
                  // Condition for long signal
                  if (buyconditions && Close[0] > EMA(TriggerEMA)[0])
                  {
                  BarColor = Color.White;
                  DrawDot("Target" + CurrentBar, false, 0, Close[0] + Target * TickSize, Color.Lime);
                  DrawDot("Stop" + CurrentBar, false, 0, Close[0] - Stop * TickSize, Color.Red);
                  Alert("myAlert", NinjaTrader.Cbi.Priority.High, "Go long", AlertSound, 2, Color.Black, Color.Yellow);
                  }
                  else
                  {
                  // Play alert every tick if conditions met
                  // Condition for short signal
                  if (sellconditions && Close[0] < EMA(TriggerEMA)[0])
                  {
                  BarColor = Color.White;
                  DrawDot("Stop" + CurrentBar, false, 0, Close[0] + Stop * TickSize, Color.Red);
                  DrawDot("Target" + CurrentBar, false, 0, Close[0] - Target * TickSize, Color.Lime);
                  Alert("myAlert", NinjaTrader.Cbi.Priority.High, "Go short", AlertSound, 2, Color.Black, Color.Yellow);
                  }
                  else
                  {
                  BarColor = Color.Empty;
                  RemoveDrawObject("Stop" + CurrentBar);
                  RemoveDrawObject("Target" + CurrentBar);
                  }
                  }
                  }

                  Comment


                    #10
                    There are times draw objects will linger after the bar closes and the final condition was false. I always recheck the conditions of the prior bar on FirstTickOfBar of non historical bars and adjust the prior draw object if needed.
                    eDanny
                    NinjaTrader Ecosystem Vendor - Integrity Traders

                    Comment


                      #11
                      Thank you very much for your help! For a while I was trying to use that, FirstTickOfBar, but it didn't seem to work for me. Perhaps you can tell me what I was doing wrong?

                      if(FirstTickOfBar
                      && EMA(FastEMA)[1] > EMA(MedEMA)[1]
                      && Close[1] > EMA(TriggerEMA)[1])
                      {
                      BarColor....
                      DrawDot...
                      }

                      Similarly, I'm wanting to compare stochastics from the current bar and the last bar, but it doesn't seem to work:

                      within OnBarUpdate I have:

                      double LTSSlope = Stochastics(myD, myK, mySmooth).D[0] - Stochastics(myD, myK, mySmooth).D[1];
                      DrawText("LTS" + CurrentBar, "LTS Slope " + LTSSlope.ToString(), 0, Close[0] - ((Stop + 3) * TickSize), Color.Black;

                      It would compile but wouldn't draw anything. It seems like any time I use [1] instead of [0], it doesn't work.

                      Comment


                        #12
                        Hello bbd_San,

                        Without the full code, I’m unable to test on the first question on my end. If you’d like to provide the full code I can take a look on our end.

                        Regarding the stochastics question, are you attempting to write an if statement which compares if the current point is higher than the last point? If so you could write it like the following,

                        Code:
                        if(Stochastics(myD, myK, mySmooth).D[0] > Stochastics(myD, myK, mySmooth).D[1];
                        {
                        DrawText("LTS" + CurrentBar, "LTS Slope " + LTSSlope.ToString(), 0, Close[0] - ((Stop + 3) * TickSize), Color.Black;
                        }
                        If I misunderstood your question please provide more information on what you are trying to do.

                        I look forward to your reply.
                        Alan P.NinjaTrader Customer Service

                        Comment


                          #13
                          Have you tried checking for more than 1 bar before accessing a prior bar?
                          if(CurrentBar < 1) return;
                          or
                          if(CurrentBar >= 1)
                          {
                          //do the stuff
                          }
                          eDanny
                          NinjaTrader Ecosystem Vendor - Integrity Traders

                          Comment


                            #14
                            No eDanny, I did not, wow you are the man! Thank you!!!!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by TraderBCL, Today, 04:38 AM
                            3 responses
                            23 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by WeyldFalcon, 08-07-2020, 06:13 AM
                            11 responses
                            1,423 views
                            0 likes
                            Last Post jculp
                            by jculp
                             
                            Started by RubenCazorla, Today, 09:07 AM
                            0 responses
                            4 views
                            0 likes
                            Last Post RubenCazorla  
                            Started by BarzTrading, Today, 07:25 AM
                            2 responses
                            29 views
                            1 like
                            Last Post BarzTrading  
                            Started by devatechnologies, 04-14-2024, 02:58 PM
                            3 responses
                            21 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Working...
                            X