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

Math looks right, value is wrong!

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

    Math looks right, value is wrong!

    I am sure many of you are familiar with the following math:

    EntryPrice = (((Input[0] - LowestBar(Low,8)) * .382) + LowestBar(Low,8));

    I should be getting a value close to 1100 but get 437.952?

    #2
    LowestBar would give you a bar number back this occurred, are you instead looking perhaps for the MIN() method?

    BertrandNinjaTrader Customer Service

    Comment


      #3
      When I Print() the value it is wrong, but I can tell from the stop that the value is actually correct! functionally all is well, I am wondering though why I get an erroneous value when I output it...

      Comment


        #4
        Unfortunately not exactly sure what you print / plot and then compare, but if I plot this snippet below I get a value in your reported range for today's ES (451).

        Code:
         
        Plot0.Set(((Input[0] - LowestBar(Low,8)) * .382) + LowestBar(Low,8));
        This code below should do it from my point of view -

        Code:
         
        Plot0.Set(((Input[0] - MIN(Low,8)[0]) * .382) + MIN(Low,8)[0]);
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I am setting my entry point as follows:

          EntryPrice = (((HighestBar(High,8) - Input[0]) * .618) + Input[0]);

          On the next line I set my Stop price as follows:

          StopLong = EntryPrice - 1;

          On the next line I print a debug message:

          Print("Pending Long @ " + EntryPrice);

          The result is:

          Pending Long @ 436.806

          On the next line I print another Message:

          Print("Long Support @ " + StopLong);

          The result is:

          Long Support @ 436.806

          After the order is placed (pending), and elsewhere in my script (when I update my stop) I Print another message:

          Print("Long Support @ " + StopLong);

          The result is:

          Long Support @ 1136

          Do you see the problem? I think the erroneous value is causing my orders to cancel prematurely ("Object reference not set to an instance of the object"), but I have no idea where the problem lies or how it could get such a value for EntryPrice but still get a correct value for my stop.

          I just don't get where the 436.806 is coming from when the current price is 1137...
          Last edited by Sleeping Troll; 04-02-2010, 02:48 PM.

          Comment


            #6
            You print to different calcs, one time EntryPrice, the other time StopLong...are those sharing exactly identical methods / calculations?
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Code:
               
              protected override void OnBarUpdate()
              {
              if(Historical)return;
              if(CurrentBar <1)return;
              if (PeakValue[0] == 0)
              {
              PeakValue[0] = Input[0];
              PeakValue[1] = Input[0];
              PeakValue[2] = Input[0];
              Print(PeakValue[0] + ", " + PeakValue[1] + ", " + PeakValue[2]);
              }
              if (Order != null)
              {
              if (Order.Name == "Long" && Input[0] > StopLong + 1)
              {
              [COLOR=red]if (Input[0] > EntryPrice + 2);[/COLOR]
              [COLOR=red]StopLong = Input[0] - 1;[/COLOR]
              [COLOR=red]Print("Long Support @ " + StopLong);[/COLOR]
              [COLOR=red]}[/COLOR]
               
              if (Order.Name == "Short" && Input[0] < StopShort - 1)
              {
              if (Input[0] < EntryPrice - 2);
              StopShort = Input[0] + 1;
              Print("Short Support @ " + StopShort);
              }
               
              if (Order.Name == "Long" && Input[0] <= StopLong)
              {
              CancelOrder(Order);
              if (Input[0] < PeakValue[1])
              {
              macdFlag = null;
              Order = null;
              }
              }
               
              if (Order.Name == "Short" && Input[0] >= StopShort)
              {
              CancelOrder(Order);
              if (Input[0] > PeakValue[1])
              {
              macdFlag = null;
              Order = null;
              }
              }
              }
              if (Order == null && CrossAbove(MACD_BB(Fast,Slow,Smooth).Value, MACD_BB(Fast,Slow,Smooth).Lower,1))
              {
              macdFlag = true;
              }
               
              [COLOR=red]if (macdFlag == true && CrossAbove(MACD_BB(Fast,Slow,Smooth).Value, MACD_BB(Fast,Slow,Smooth).Upper,1))[/COLOR]
              [COLOR=red]{[/COLOR]
              [COLOR=red]if (Input[0] < PeakValue[1])[/COLOR]
              [COLOR=red]{[/COLOR]
              [COLOR=red]PeakValue[1] = Input[0];[/COLOR]
              [COLOR=red]Print(PeakValue[0] + ", " + PeakValue[1] + ", " + PeakValue[2]);[/COLOR]
              [COLOR=red]}[/COLOR]
              [COLOR=red]EntryPrice = (((Input[0] - LowestBar(Low,8)) * .382) + LowestBar(Low,8));[/COLOR]
              [COLOR=red]StopLong = EntryPrice - 1;[/COLOR]
              [COLOR=red]Print("Pending Long @ " + EntryPrice);[/COLOR]
              [COLOR=red]Print("Long Support @ " + StopLong);[/COLOR]
              [COLOR=red]Order = EnterLongLimit(0, true, 1, EntryPrice,"Long");[/COLOR]
              [COLOR=red]}[/COLOR]
               
              if (Order == null && CrossBelow(MACD_BB(Fast,Slow,Smooth).Value, MACD_BB(Fast,Slow,Smooth).Upper,1))
              {
              macdFlag = false;
              }
               
              if (macdFlag == false && CrossBelow(MACD_BB(Fast,Slow,Smooth).Value, MACD_BB(Fast,Slow,Smooth).Lower,1))
              {
              if (Input[0] > PeakValue[2])
              {
              PeakValue[2] = Input[0]; 
              Print(PeakValue[0] + ", " + PeakValue[1] + ", " + PeakValue[2]);
              }
              EntryPrice = (((HighestBar(High,8) - Input[0]) * .618) + Input[0]);
              StopShort = EntryPrice + 1;
              Print("Pending Short @ " + EntryPrice);
              Print("Short Support @ " + StopShort);
              Order = EnterShortLimit(0, true, 1, EntryPrice, "Short");
              }
              }
              Last edited by Sleeping Troll; 04-02-2010, 01:41 PM.

              Comment


                #8
                Sleeping Troll,

                I would not do the prints from OnBarUpdate(). Should you want to see the correct values as the orders change states/are submitted you should print from OnOrderUpdate() instead. OnBarUpdate() only gets you snapshots of that particular bar update event. Orders are not necessarily updated yet as you continue processing down it while OnOrderUpdate() receives events whenever there is an order event like state changes, etc.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  if the value I submit for the order is 436.80 instead of 1136.00 it will do me no good to submit it as an order. IE: the price ranging around 1136 correctly reflects the current pricing, I do not know where 436.80 comes from.
                  Last edited by Sleeping Troll; 04-02-2010, 02:47 PM.

                  Comment


                    #10
                    You should print your variable along each step of your code to see where that value originates from then. Add prints everywhere and follow it sequentially to see where it comes from.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      It comes from:

                      EntryPrice = (((Input[0] - LowestBar(Low,8)) * .382) + LowestBar(Low,8));

                      I have no idea why, I was hoping you could educate me?

                      Comment


                        #12
                        When this occurs my current price is 1137.00 my lowest price over the last 8 bars is 1136.00 soooo...
                        ((1137-1136)*.618)+1136 = 1136.618 not 435.806

                        Comment


                          #13
                          Sleeping Troll,

                          You are mixing doubles and integers.

                          You may be activating some kind of rounding off.

                          try this

                          ((1137.0-1136.0)*.618)+1136.0 =

                          RJay
                          RJay
                          NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

                          Comment


                            #14
                            I am not entering the values, they are coming from my bars series...

                            This is my script with Prints added:

                            Code:
                             
                            Print("Current Price " + Input[0]);
                            Print("Lowest Price last 8 Bars " + LowestBar(Low,8));
                            Print("Difference " + (Input[0] - LowestBar(Low,8)));
                            Print("Difference * .618 " + (Input[0] - LowestBar(Low,8)) * .618);
                            Print("Difference * .618 + Low " + ((Input[0] - LowestBar(Low,8)) * .618)+LowestBar(Low,8));
                            EntryPrice = (((Input[0] - LowestBar(Low,8)) * .382) + LowestBar(Low,8));
                            StopLong = EntryPrice - 1;
                            Print("Pending Long @ " + EntryPrice);
                            Print("Long Support @ " + StopLong);
                            Order = EnterLongLimit(0, true, 1, EntryPrice,"Long");
                            This is my output:

                            Current Price 1137.25
                            Lowest Price last 8 Bars 3
                            Difference 1134.25
                            Difference * .618 700.9665
                            Difference * .618 + Low 700.96653
                            Pending Long @ 436.2835
                            Long Support @ 435.2835

                            Where the heck did "3" come from?

                            P.S. I am running this script on replay and the lookback is going into previous day, would that have something to do with it?

                            P.S. Just tried above with lookback of 4 which does not involve previous day, same problem!
                            Last edited by Sleeping Troll; 04-02-2010, 05:11 PM. Reason: P.S.

                            Comment


                              #15
                              Got it! Phew!

                              Corrected formula:

                              EntryPrice = (((Input[0] - Low[LowestBar(Low,4)]) * .382) + Low[LowestBar(Low,4)]);

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by reynoldsn, 05-10-2024, 07:04 PM
                              4 responses
                              25 views
                              0 likes
                              Last Post reynoldsn  
                              Started by llanqui, Today, 11:10 AM
                              1 response
                              15 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by llanqui, Today, 10:29 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by Trader146, 05-10-2024, 09:17 PM
                              1 response
                              22 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by bourasrafik, Today, 03:26 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post bourasrafik  
                              Working...
                              X