Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with High & Low

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

    Problem with High & Low

    I am using this code below and have avariable set called newTp and another called newBt which I am setting manully when I open the strategy to be the high and low of the last 5 minute bar, and boxsize is set at 1 pip, However when I print out the results if the High is greater than newTp it should add 1 pip to the value of newTp but in fact it is adding 1 pip to the highest high of the whole chart. This is actually what I was looking for earlier but I cannot see why it is doing it? Nor can I get it to do what I actually want in this instance and just add 1 pip to the new high.

    protectedoverridevoid OnBarUpdate()
    {
    if (High[0]>=(newTp+boxSize))
    {
    newTp = High[
    0];
    }

    if (Low[0]<=(newBt-boxSize))
    {
    newBt = Low[
    0];
    }
    These are my print statements which shows the results, and NewTop and NewBottom do calculate the correct figures but newTp and newBt do not, I am very puzzeled ?? I have also attached the actual startegy if you want to run it to see what I mean. Help!!
    Print((newTp+boxSize));
    Print((newBt-boxSize));
    Print(
    "High" + High[0]);
    Print(
    "Low" + Low[0]);
    Print(
    "Boxsize" + boxSize);
    Print(
    "Box3" + (boxSize*3));
    Print(
    "NewTop" + (High[0] + 0.0001));
    Print(
    "NewBottom" + (Low[0] -0.0001));

    Attached Files

    #2
    Hello,

    I believe the logic in your if statement is not correct. I suggest to use the following:

    if (High[0] >= newTp)
    {
    newTp = newTp + boxsize
    }

    Only when the High is higher than newTp, the boxsize is added to the newTp.
    JasonNinjaTrader Customer Service

    Comment


      #3
      High &amp; Low

      Thanks Jason, but it's not the logic itself but the figures I'm getting, I've used your code in case but still get the same from my print staements viz:-

      1.9971 being the value of newTp
      1.8966 being the value of lowBt

      these are the alltime high showing on the chart and not just the current bar high with 1 boxsize added to it.

      That should be 1.8985 and 1.8962 respectively which it can calculate correctly from this code:-

      Print("NewTop" + (High[0] + 0.0001));
      Print(
      "NewBottom" + (Low[0] -0.0001));

      What I cannot understand is where it is getting the alltime high from and why the logic will not work as planned ??

      Any thoughts

      Comment


        #4
        High &amp; Low

        One of us is missing the point, I can't see why it is finding the alltime high and the alltime low, I'm not asking it to as far as I can see, it is correctly adding 1 pip to that level OK.

        Comment


          #5
          Hello,

          You will want to reset the newTp and the newBt variables once you have reached the point at which you want to start over calculating the values.

          Right now newTp and newBt continue to carry the highest value that has been stored thus far.

          At some point you have to reset these values.

          Do you want to store the high + boxsize for each bar on the chart? If so then I suggest the following approach: using a custom DataSeries to carry your values and associate them with each bar and reference back in the DataSeries to do what you want.
          This link will help you with building a custom DataSeries:
          DenNinjaTrader Customer Service

          Comment


            #6
            High &amp; Low

            Thanks Ben,

            I was not thinking that it would calculate the newBt value right through the chart, I'll have a look at your example.

            Comment


              #7
              Problem with High &amp; Low

              I am using this code to find the highest bar and the lowest bar in the last 20 bars which works fine and gives the correct result:-

              highBar = (HighestBar(Close, 20));
              lowBar = (LowestBar(Close,
              20));
              lastTop = MAX(High, highBar)[
              0];
              lastBottom = MIN(Low, lowBar)[
              0];

              However I only want this to run when I first start the program so I have a variable called first_run set by default to true. WHen I run this code:-

              if (first_run)
              {
              highBar = (HighestBar(Close,
              20));
              lowBar = (LowestBar(Close,
              20));
              lastTop = MAX(High, highBar)[
              0];
              lastBottom = MIN(Low, lowBar)[
              0];
              first_run =
              false;
              }

              I get a totally different answer and the high and low are somewhere way back in history and not within the last 20 bars. can you tell me where I am going wrong please?

              Many thanks.

              Comment


                #8
                What do you mean run on the 1st start of the program?

                For example, if you have a chart with 100 bars on it, OnBarUpdate() will called 100 times when "Calculate on bar close" is set to true. On what bar would you like to run that code?
                RayNinjaTrader Customer Service

                Comment


                  #9
                  High &amp; Low

                  When I first start the strategy I want to find the Highest high in the last 20 bars only, that's why I thought it would do so if using a variable so it only called that routime once?

                  Comment


                    #10
                    You can only determine that if you are on the close of the 20th bar on the chart...

                    So what you could do is:

                    if (CurrentBar == 20)
                    {
                    highBar = (HighestBar(Close,
                    20
                    ));
                    lowBar = (LowestBar(Close,
                    20
                    ));
                    lastTop = MAX(High, highBar)[
                    0
                    ];
                    lastBottom = MIN(Low, lowBar)[
                    0
                    ];
                    }
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      High &amp; Low

                      Sorry Ray I'm not explaining myself well. I have about 5000 bars on the chart it's not the highest high within the first 20 bars which I wnat but the highest high within the the most current 20 bars, ignoring the previous 4880 or so bars?

                      Comment


                        #12
                        Hello,

                        Try using:

                        In your variables make sure you are starting with zero:

                        int highBar = 0;

                        etc...


                        In OnBarUpdate():

                        if(!Historical && highBar == 0)
                        {
                        //returns the index of the bar with the highest close over the
                        //last 21 bars, note current bar does not have a close so use 21 as
                        //the lookback to get 20 bars
                        highBar = (HighestBar(Close, 21));

                        lowBar = (LowestBar(Close, 21));

                        //returns the max high price over the last twenty bars
                        //excluding the currently forming bar
                        lastTop = MAX(High, 20)[1];

                        lastBottom = MIN(Low, 20)[1];
                        }

                        The above assumes you want to exclude using the current bar.

                        Here is a link on Historical:
                        Last edited by NinjaTrader_Ben; 08-14-2008, 05:46 PM.
                        DenNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by prdecast, Today, 06:07 AM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by Christopher_R, Today, 12:29 AM
                        1 response
                        14 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by chartchart, 05-19-2021, 04:14 PM
                        3 responses
                        577 views
                        1 like
                        Last Post NinjaTrader_Gaby  
                        Started by bsbisme, Yesterday, 02:08 PM
                        1 response
                        15 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by i019945nj, 12-14-2023, 06:41 AM
                        3 responses
                        60 views
                        0 likes
                        Last Post i019945nj  
                        Working...
                        X