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

Run code on price change only

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

    Run code on price change only

    I'm trying to create code that will limit the code inside to only run if there is a price change from tick to tick. I'm doing this to make the code more CPU efficient. Using the code below isn't working. Its still running each tick even if the price doesn't change.


    if (Close[0] != Last_Close[0] || FirstTickOfBar);
    {
    Some Code here....
    }
    Last_Close[0] = Close[0];


    It compiles fine and there are no errors in the log. Its got me stumped...

    taddypole...

    #2
    I've tried the code in a simple indicator SMA2. It does not limit the code to running only when there is a price change.

    I've created a output file "Change in Price 'Not'.png. You can see that the code is running each tick whether there is a price change or not.

    SMA2.zip is the simple moving average code example attached...

    also .. from the first post I've changed the Last_Close from a DataSeries to a double.
    Attached Files

    Comment


      #3
      Taddypole, the OnBarUpdate() would still be called, but you would not process / update the logic if your condition is not hitting.
      BertrandNinjaTrader Customer Service

      Comment


        #4
        Sorry Bertrand...

        My intent is to not process/update the logic if my condition is hit. That is what it is supposed to do but it doesn't seem to be working. Its still running the process/logic.

        ie.. if the most recent tick Close[0] value is different than the Last_Close[0] then run the process/logic. If the Close[0] is the same as the Last_Close[0] then don't run the process/logic.

        But its not working that way. Any ideas?

        Comment


          #5
          taddypole, you will need to include a print to see whether or not your condition is hit and then debug the condition you're using here, if you're still seeing the calcs being done it would mean the condition has triggered and allowed this step to happen.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Bertrand..

            I do have a print statement in the code and the output of that print statement is in my second post. That is why I know the logic is not working as written. The code and the printout of the "OutPut" window are included in my second post.
            Last edited by Taddypole; 03-03-2011, 11:00 AM.

            Comment


              #7
              #region Variables

              private double Last_Close;

              protected override void Initialize()
              {
              double Last_Close = 0.0;
              }

              if (Last_Close != Close[0]); <<==========
              {
              Print(".......
              }
              Last_Close = Close[0];

              Have I defined my variables incorrectly for the "if" statement? I continue to get the "if" condition to pass even though "Last_Close" is equal to "Close[0]".

              Does Last_Close need to be a "DataSeries" or is a "double" OK?

              Comment


                #8
                Yes, the variable looks ok - have you printed both Last_Close and Close to compare what the variable holds at all points in your code?

                I tried this just here and it triggers as I would expect -

                if (Close[0] == Close[1] && ! FirstTickOfBar)
                {
                Print("Lets Quit");
                return; // if price change or first tick of new bar
                }
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Bertrand, I don't think Taddypole is concerned with the close of the current bar compared to the close of the prior bar. This looks like what you are showing here. He is looking at current price compared to the price at the last tick in the same bar.

                  Dan
                  eDanny
                  NinjaTrader Ecosystem Vendor - Integrity Traders

                  Comment


                    #10
                    If you are looking for the price of the last tick all you have to do is run a double variable that keeps track of it when using CalculateOnBarClose = false.

                    Code:
                    protected override void OnBarUpdate()
                    {
                         if (Close[0] > priorTickClose)
                              // Do something;
                         priorTickClose = Close[0];
                    }
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Time = 3/3/2011 1:10:42 PM TickCount: = 1 CurrentBar = 79738 Close[0] = 101.83 Close[1] = 101.83 Diff = 0.00000000
                      Time = 3/3/2011 1:10:42 PM TickCount: = 2 CurrentBar = 79738 Close[0] = 101.83 Close[1] = 101.83 Diff = 0.00000000

                      You are right eDanny. I want the close of the last tick.

                      And even stranger is with the code below using close and close of bar ago:

                      if (Close[0] != Close[1]);

                      Where Close[0] != Close[1]... the "if" condition should not pass using "Not Equal to".
                      But in the printouts above, I have cases of the two being equal and the condition still passes.This is where Diff = 0.0000000.

                      Strange....
                      Last edited by Taddypole; 03-03-2011, 02:17 PM.

                      Comment


                        #12
                        Just doing a math subtraction is insufficient for determining equality or inequality. Please see this tip here for information discussing this aspect of C#: http://www.ninjatrader.com/support/f...ead.php?t=3929
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Josh...

                          Look at Post #5. That is what I initially tried.

                          if (Last_Close != Close[0]); <<==========

                          Comment


                            #14
                            Sorry? What I am saying is your validity check with your "Diff" print out is insufficient for proving any equality or inequality between two double values.

                            Also, your if-statement line is inaccurate. You will want to remove the semicolon at the end of it.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Josh...

                              Just what I was looking for....



                              if (Instrument.MasterInstrument.Compare(Close[0],Last_Close) != 0)


                              Thank you...

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kevinenergy, 02-17-2023, 12:42 PM
                              117 responses
                              2,766 views
                              1 like
                              Last Post jculp
                              by jculp
                               
                              Started by Mongo, Today, 11:05 AM
                              5 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by SightCareAubetter, Today, 12:55 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post SightCareAubetter  
                              Started by traderqz, Today, 12:06 AM
                              8 responses
                              16 views
                              0 likes
                              Last Post traderqz  
                              Started by SightCareAubetter, Today, 12:50 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post SightCareAubetter  
                              Working...
                              X