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

return in OBU

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

    return in OBU

    Hello,

    If i place
    Code:
    [I][B]if(CurrentBar < 0) return;
    
    
    candle = Close[0];
    
    Print("CANDLE"+candle);[/B][/I]
    at the beginning of my code it will list all prices from 0 to the end of the chart.
    But if i place it at the end of the code its giving me complete different values. I need that part to be at the end of the code and still list from 0 to end of the chart.
    Is there a way to isolate that part at the end of the code and make sure it wont be affected by the rest?


    TY
    Last edited by frankduc; 02-25-2021, 08:56 AM.

    #2
    Hello frankduc,

    Thank you for your post.

    I see that you are calling return in one of your for loops (seen below). Calling return will cause the OnBarUpdate method to stop processing. If you added the section of code you mention to the bottom of the script and the return in your loop returns true, you may not see the same prints as when that section of code is added to the beginning of the script.

    Code:
    for( int valu = firs0; valu <= max0; valu += step0 )
    {
    if(CurrentBar < valu) return;
    
    closePrice6 = Bars.GetClose(valu);
    listQ.Add(closePrice6);
    Print("CQ"+closePrice6);
    }
    Something you could do is use a 'break' statement in your loop when comparing CurrentBar < valu instead of using 'return'.

    Please let us know if we may assist further.

    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello Brandon,

      Thanks for the solution it worked.

      Following the actual code i am trying to apply each value from listMP to this code:

      Code:
      if(CurrentBar < 0) return;
      
      candle = Close[0];
      
      Print("CANDLE"+candle);
      listMP return
      PM-0,25
      PM-11,25
      PM-2
      PM-44,25
      PM-0,25
      PM7,75
      PM-2,75

      The idea is to apply -0.25 to every closeprice from 195 to 1365
      than from 390 to 1365 apply over the first series of price -11.25
      etc
      to make a new list and eliminate the gaps between open and close between each day.


      Code:
      foreach(var item in listMP)
      {
      candle = Close[0] + item;
      ​​​​​​​}
      or i was thinking about :

      Code:
      for( int value = firs; value <= max; value += step )
      {
      if(CurrentBar < value) return;
      candle = Close[value] + item;
      }
      But that wont do either. Any suggestion?

      TY

      Comment


        #4
        Hello frankduc,

        Thank you for that information.

        Please elaborate on what you mean by "to make a new list and eliminate the gaps between open and close between each day" so we may accurately assist.

        Also, please explain your approach in layman's terms so we are able to determine if what you want to accomplish is possible.

        And, provide screenshots describing your ultimate goal.

        I look forward to assisting further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hello Brandon,

          First of all try that code you will understand what i am doing. As you can see it caliber prices from one day to another eliminating the gap between close price at the end of the previous session and the close of the next.

          This caliber the prices for only 2 days. I want to be able to go back 20, 50, 1000 days without having to repeat that part of the code many times:

          Code:
          if(CurrentBar < 1169) return;
          
          double closePrice161 = Bars.GetClose(1169);
          double closePrice91 = Bars.GetClose(1170);
          
          difCP1 = closePrice161 - closePrice91;
          So the first part of my code in post 1 was trying to get every gap between every closing days, that is why i end up with:
          listMP return
          PM-0,25
          PM-11,25
          PM-2
          PM-44,25
          PM-0,25
          PM7,75
          PM-2,75

          Now once i have the gap i must apply it to each bar where there is a gap and that part of the code is doing it:

          Last edited by frankduc; 02-25-2021, 08:57 AM.

          Comment


            #6
            Hello frankduc,

            Thank you for your note.

            We do not understand what you are trying to do and it appears as though you are going down a rabbit hole to accomplish your goal, but we do not know what your goal is. It is not clear that what you are attempting would be a suitable approach for what you are trying to accomplish, and it is not clear what you are trying to accomplish.

            In order for us to give any insight or direction, please describe, in plain English, what you are trying to accomplish. Please also provide screenshots to illustrate what you are trying to do/overcome.

            Thanks in advance, I look forward to further assisting.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Brandon,

              Here's an image attach. You can see the gap between the days. 11 pts and 7.25pts.

              From 9:30 you add +11pts to each prices until you get to the next day at 9:30, than you add 7.25pts to each closeprice until you reach the next 9:30. The result is the yellow line.

              You start from bar 0. bar 4pm = index 1169 and 9:30 = barindex 1170. 3915.75 -3904.75 = 11pts

              I dont get what you dont get people. Sorry i thought it was simple.
              If you just take the last code snippet and copy/paste in NT it will produce the same result.

              TY
              Attached Files

              Comment


                #8
                Hello frankduc,

                Thank you for that information.

                I see your screenshot. Could you please confirm the following:

                Do you want to accomplish changing the prices of the bars on the chart?
                Are you wanting to create a plot line that connects the gaps between the bars?
                Or, are you wanting to add values to your custom list for bars that are not there?

                I look forward to your reply.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Hello,

                  1) No what i did is changing only the price and store the new prices in variable candle, from that point i used addPlot to draw the line and see where are the new prices. Then i use the result of candle in my indicator.
                  I made a mistake in my explanation earlier. I meant from 9:30 you add (-11pts) to each prices until you get to the next day at 9:30. In this case the price must be lower to reach the close of the previous day.

                  2) It connect the gap only between the previous close price at 4pm and the next day close at 9:30. The ajustment concern only bars with day gap. Take this chart for example in attachement, The prices have been lifted from about 17 pts from 16:00 than lifted again by about 29 pts this morning at opening but lifted from the new prices ajusted in the yellow line. The goal is to create a chart where overnight trading does not exist and opening prices always open equal to the previous close in the morning.

                  3) Lets froget the list i created in my first post. What would be the most efficent way to do that? In post 5 i demonstrate how i get the gap between each days for a certain period. Than i try to reproduce the logic of the last code snipet from post 5.

                  When you close all gaps over 3 days. You add or subtract to all close prices to the end of the chart the gap in points. If there is a 11 pts gap, you must caliber all the prices by 11 pts. Than the next gap you encounter from that specific bar (4-9:30) you must caliber from that point all prices by the new amount of that gap (lets say e.g. 5pts). On total it makes 16 pts till the end of the chart, etc.

                  TY
                  Attached Files

                  Comment


                    #10
                    Hello frankduc,

                    Thank you for that information.

                    You could accomplish your goal by looping over bars in OnBarUpdate. The intended loop would do the following:

                    1. Loops from "LastIndexChecked" to CurrentBar. "LastIndexChecked" can be 0 when the script starts.
                    2. Assigns price values to a plot and applies the difference from "offset" variable (currently 0).
                    3. Checks if there is a gap, save the index where the gap was found to a variable, and also save/update the distance of the gap to a variable "offset", and then breaks the loop.

                    The next time the loop is run, it will start where the gap was found and will then adjust with each price by the offset found, and assign the values to the plot.

                    Please let us know if we may assist further.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello,

                      1) I am not sure i follow you. LastIndexChecked represent barIndex?

                      2) Are you talking about listMP? I cannot take the differences between each close price of the bars chart, i only need the differences between days (4-9:30 at specific time period)

                      3) How is it suppose to look?:

                      [CODE]
                      int index;
                      int LastIndexChecked = 0;

                      for(int barIndex = LastIndexChecked; barIndex <= CurrentBar; barIndex++)
                      {
                      DateTime MyDateTime9 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 09, 30, 0);
                      DateTime MyDateTime16 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 16, 00, 0);

                      double closePrice16 = Bars.GetClose(Bars.GetBar(MyDateTime16));
                      double closePrice9 = Bars.GetClose(Bars.GetBar(MyDateTime9));

                      double difCP = closePrice16 - closePrice9;

                      index = barIndex;

                      }
                      /CODE]

                      Will it reach te goal to create a chart where overnight trading does not exist and opening prices always equal to the previous close in the morning?

                      I am a bit confuse.

                      TY

                      Comment


                        #12
                        Hello frankduc,

                        Thank you for your note.

                        The code for the loop that you provided looks correct other than how you are getting the Close value unless the date is what you are wanting. Time[0] will not change for any loop iteration. If you are going to use the times and make it work with the loop, you would need to use Time.GetValueAt(barIndex). If you meant to get the current bars time and use that for every iteration then the loop would be correct. I would suggest printing the indexes to make sure its looping how you expected based on what you need.

                        When the loop is run, it will start where the gap was found and will then adjust with each price by the offset found, and assign the values to the plot to accomplish your goal.

                        GetValueAt() - https://ninjatrader.com/support/help...getvalueat.htm

                        Let us know if we may assist further.
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          Brandon,

                          I dont see how it will return what i want. The addplot in the chart wont show up and i dont think the prices are right. Unless, there is something i didn't understand in your explanation. Could you be more precise or correct what is wrong?


                          For example at bar 1091 its adding from the difCP
                          dif9,5
                          BI1091
                          CDL3918,75 from 3909.25 but not updating from the start. Only adding the distance gap for that specific bar.

                          That if i dont use the break, if i use it :

                          CDL3813,5
                          dif-0,25
                          BI0
                          CDL3813,5
                          dif-0,25
                          BI0
                          CDL3813,5
                          dif-0,25

                          its just repeating this.

                          TY
                          Last edited by frankduc; 02-25-2021, 08:57 AM.

                          Comment


                            #14
                            Hello,

                            I think i have solve the problem but i cant see the yellow line in the chart.
                            Is is possible to use addplot if value is inside a for loop?

                            TY

                            Comment


                              #15
                              Hello frankduc,

                              Thank you for your note.

                              It is possible to call Value[] inside of a loop to assign a value to the plot. I would suggest adding prints before you call Value[] that prints the price being assigned to Value[] to monitor the behavior of the plot. Also, monitor the Log tab of the Control Center to determine if any errors are being thrown.

                              If you are unable to determine why the plot is not placing on the chart, please send us a copy of the code used for the loop so we may investigate.

                              I look forward to assisting further.

                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ageeholdings, 05-04-2024, 03:21 PM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by claxxical, 05-30-2017, 12:30 PM
                              39 responses
                              4,501 views
                              0 likes
                              Last Post fiendtrades  
                              Started by tonynt, 05-21-2019, 06:27 AM
                              12 responses
                              541 views
                              1 like
                              Last Post fiendtrades  
                              Started by alexstox, 10-16-2018, 03:29 PM
                              12 responses
                              355 views
                              0 likes
                              Last Post Tburg1073  
                              Started by truepenny, Today, 03:59 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post truepenny  
                              Working...
                              X