Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Perform calculations "once"

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

    Perform calculations "once"

    I was able to code calculating some parameters based on the Average True Range. What I would like to do is perform these calculations (and display them with Print") only "once" when I first start a strategy on a stock (which will always be after the open).

    In other words let's say at 9.50a I start a strategy in the strategy tab, I would like to determine my parameters based on the average true range only after I first start this strategy and not on every bar update. Then I can use these same parameters throughout the day even tho they would have changed if they were updating on each onbarupdate. I'm sure there is a simple command but I can't seem to find it. I tried having it perform this only if Bars.FirstBarOfSession == true but it still runs this on each bar update (1 minute chart). Thanks.

    #2
    Hello,

    Bars.FirstBarOfSession is true for every tick of the first bar if you have CalculateOnBarClose = false.

    Try one of these:

    if(CurrentBar == 0 && FirstTickOfBar)

    For when you want to print once the indicator is first attached to the chart.

    Or...

    if(Bars.FirstBarOfSession && FirstTickOfBar)

    For when you want to print at the first bar of the session.
    Last edited by NinjaTrader_Ben; 10-09-2008, 07:00 AM.
    DenNinjaTrader Customer Service

    Comment


      #3
      couldn't you just put it in the Initialize() method?

      Comment


        #4
        Hello,


        The "Bars" object is not guaranteed to be set in the Initialize() method. You need to check this in OnBarUpdate().
        DenNinjaTrader Customer Service

        Comment


          #5
          Yeah that didn't work with putting it in initialize. I think Bens idea will work out. I hope to post the result of this in the demo strategies. I am working on an idea for position sizing based on ATR and I'm close to done.

          Comment


            #6
            yeah, i realized it wouldn't work early this morning. basically, you are concerned with coding an efficient program- you dont want to calculate something every bar you only have to calculate once. recently i've been trying to start to write efficient code, but i'm failing pretty hard. like opening AND closing a mysql database connection EVERY market update. not very nice for my hard drive.

            but as for another idea, you could put your method in OnBarUpdate() inside a if(hasbeencalculated == false) {code}; and then set a flag hasbeencalculated = true after it was done once. but this is stuff you probably already know. just thought i'd toss out an idea.

            Comment


              #7
              I'm new at all this so any suggestions are good. I have to say I'm getting much farther then I imagined back in August when I first loaded NinjaTrader.

              Comment


                #8
                sauer11155,

                If you have any other questions feel free to ask us on the forums.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Well, am working on this ATR thing this weekend. So far I have tried the abve two suggestions and also below, and it still calculates and prints on each bar update. I think I am missing something obvious. I did figure out how to convert a number like 35.3354351351 into 35 which will be more useful for broker orders lol.

                  Here is some code I have so far, I will post this soon as a sample code:

                  protected override void OnBarUpdate()
                  {
                  if(calculateATRonce == false);
                  {
                  // Calculate what percent ATR is of price
                  aTR20DayAmount = ATR(BarsArray[1], 20)[0];
                  percentATRisOfPrice = aTR20DayAmount/CurrentDayOHL().CurrentOpen[0]; ;
                  stochasticTrailStop = percentATRisOfPrice/4;
                  profitTarget2 = percentATRisOfPrice/4;
                  profitTarget8 = profitTarget2*4;
                  rEALTrailingStop = percentATRisOfPrice;
                  totalShareSize = maxLoss/stochasticTrailStop;
                  entry2target = totalShareSize/2;
                  entry8target = totalShareSize/2;
                  Print(Instrument.FullName + Time[0] + " Daily 20 ATR= " + aTR20DayAmount + " percentATRisOfPrice =" + percentATRisOfPrice + " stochasticTrailStop " + stochasticTrailStop + " profitTarget2 " + profitTarget2 + " profitTarget8 " + profitTarget8 + " rEALTrailingStop " + rEALTrailingStop + " shareSize " + totalShareSize);
                  entry2target = System.Convert.ToInt32(entry2target);
                  entry8target = System.Convert.ToInt32(entry8target);
                  entry2target = System.Convert.ToInt32(entry2target);
                  Print(" entry2target " + entry2target + " entry8target " + entry8target);
                  calculateATRonce = true;
                  }


                  }

                  Comment


                    #10
                    The best way to debug is to simplify your code. Strip it down to the bare minimums with only your bool. When you get that working then you can add additional layers of complexity until it breaks. If it doesn't break, great you have it working.

                    If your condition is continually evaluating calculateATROnce as false that means you are resetting it to false somewhere after you've set it to true. This is why it keeps running. You need to find that reset and address it.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      You have an extraneous semi-colon.

                      Remove the semi-colon after the "if" statement. It terminated the processing of the statement, so your block will always run.

                      "if(calculateATRonce == false);"

                      should be

                      "if(calculateATRonce == false)"

                      I only know because I did the same once and had the devil of a time debugging it. That ";" is so second nature that we do not see it when we read over the code.

                      Cheers.

                      Comment


                        #12
                        Thanks Koganam. I knew it was something little and dumb (on my part) like that. It's too bad I'm learning C# like this, I think I need to try making some other programs and see if I can get some correct coding habits. Anyway, without the incorrect ; the other idea of first bar of session works fine. When I added this ATR based sizing to my working strategy I had some other odd stuff, so, I'm going to have to get over another learning hurdle.

                        Comment


                          #13
                          Ok one last question for now. Is there some way to convert some of the price results I get like 85.021232215465 into a more stock tradeable price such as simply 85.02? I found about System.Convert.ToInt32 for some other things but I can't find how to specify only 2 decimal places. I know this has been addressed here I just can't find it on the forum.

                          Comment


                            #14
                            var1 = Math.Round(var2, 2, MidpointRounding.AwayFromZero);

                            Comment


                              #15
                              Please also consider Round2TickSize: http://www.ninjatrader-support.com/H...2TickSize.html
                              Josh P.NinjaTrader Customer Service

                              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