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

Please help me with an arraylist part.

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

    Please help me with an arraylist part.

    Click image for larger version  Name:	image_65357.jpg Views:	3 Size:	210.5 KB ID:	1146783 Hi All,

    I am trying to create an indicator for NT7 from the logic as below:

    lenF = 12; // amount of bars used as sample to calculate fast moving average

    lenS = 26; // amount of bars used as sample to calculate slow moving average

    lenT = 9; // amount of bars used to calculate signal for VPCI

    mult = 2.0;


    // Functions

    PriceFun(VPC,VPR,VM,src) => //function calculating stop-loss step in relation with Volume and minimal price

    VPCI=VPC*VPR*VM

    lenV = if VPC <0
    int(round(abs(VPCI-3)))
    else if VPC>=0
    round(VPCI+3)
    else
    1

    VPCc = if (VPC > -1 and VPC <0)
    -1
    else if (VPC < 1 and VPC >= 0)
    1
    else
    VPC

    Price=0.0
    for i=0 to lenV - 1
    Price = Price+(src[i]*1/VPCc[i]*1/VPR[i])
    PriceV=Price/lenV/100

    PriceV

    // Calculations

    VWmaS = vwma(Close, lenS) // Fast volume weighted moving average
    VWmaF = vwma(Close, lenF) // Slow volume weighted moving average
    AvgS = sma(Close, lenS) // Slow Volume average
    AvgF = sma(Close, lenF) // Fast Volume average
    VPC = VWmaS - AvgS // Volume-Price Confirmation/Contradication VPC+/-
    VPR = VWmaF/AvgF // Volume-Price Ratio
    VM = sma(volume,lenF)/sma(volume,lenS) // Volume Multipler
    VPCI=VPC*VPR*VM // Volume-Price Confirmation indicator

    DeV = mult*VPCI*VM // Deviation
    StopLoss = sma(low - PriceFun(VPC,VPR,VM,low) + DeV , lenS)

    But when I plotted the indicator kind of weird curve and far away from Price. Could anyone please take a look and point out what is wrong with it?

    Any help will be appreciated !
    Last edited by mailonline38; 03-16-2021, 12:18 AM.

    #2
    Hello mailonline38,

    Thanks for your post.

    I would suggest debugging your script by checking the values being generated at various stages in your script to ensure they are as expected. You can add print statements to your script to view the output of the values in the Tools>Ninjascript output window.

    We will leave this topic open for any forum members who would like to assist.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply. I will do as your suggestion.

      Here is the code that I just added those Print statements and Output window as shown below.
      Thanks for anyone could help.
      Last edited by mailonline38; 03-16-2021, 03:01 PM.

      Comment


        #4
        Here is update file with better plot but the logic is still not correct. Any help?
        Attached Files
        Last edited by mailonline38; 03-17-2021, 01:34 AM.

        Comment


          #5
          I downloaded your script, took a good look at it, but decided not to continue.

          Why?
          Because, to me, right away, I am confused.

          You are using CalculateOnBarClose=False, which means your OnBarUpdate
          is being called for every tick -- so your two ArrayLists VPCc and VPRr are
          going to become quite large.

          Why? Because you are adding new elements to VPCc and VPRr each time
          PriceFunc is called -- and PriceFunc is being called by OnBarUpdate on every
          tick, because COBC=False. Thus, your ArrayLists will become huge over time.

          Can you provide additional details on the intent of your logic?
          Explain the purposes of your ArrayLists -- why are they needed?
          Are they supposed to be caching values for every tick?
          Last edited by bltdavid; 03-17-2021, 10:06 PM.

          Comment


            #6
            Click image for larger version  Name:	Off Chart 3.jpg Views:	0 Size:	186.5 KB ID:	1147145
            Originally posted by bltdavid View Post
            I downloaded your script, took a good look at it, but decided not to continue.

            Why?
            Because, to me, right away, I am confused.

            You are using CalculateOnBarClose=False, which means your OnBarUpdate
            is being called for every tick -- so your two ArrayLists VPCc and VPRr are
            going to become quite large.

            Why? Because you are adding new elements to VPCc and VPRr each time
            PriceFunc is called -- and PriceFunc is being called by OnBarUpdate on every
            tick, because COBC=False. Thus, your ArrayLists will become huge over time.

            Can you provide additional details on the intent of your logic?
            Explain the purposes of your ArrayLists -- why are they needed?
            Are they supposed to be caching values for every tick?
            Sorry making you confused about delete the old script due to my code is not right and missing the print statement as NinjaTrader_PaulH's suggestion above.

            The logic is about array calculation:
            Price=0.0
            for i=0 to lenV - 1
            Price = Price+(src[i]*1/VPCc[i]*1/VPR[i])

            1. As you see, src[i], VPCc[i] and VPR[i] are all arrays.
            If I am using array then I have to know the size of array. That's is why I am using arraylist.
            Now, you are point it out that arraylist VPCc and VPRr are quite large. Thanks for that help.

            Moreover, if I am using for i=0 to lenV-1 then it is not compile and have the error
            "Error on calling 'OnBarUpdate' method for indicator 'StopLoss' on bar 26: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

            2. About CalculateOnBarClose=False, I want to see the indicator plot in realtime. You can set it into true but you have to wait until the current bar complete and move to the next bar appear. Now you can see the plot. It is normal.
            "Are they supposed to be caching values for every tick?[/QUOTE]" - No, it is not need to catch everytick values.

            3. About the Price problem, it makes the indicator plots off away of the price chart.

            This is the first time for me to deal with array, now arraylist. Any help please.
            Thanks bltdavid to dig deep into the problem and point it out.
            Last edited by mailonline38; 03-18-2021, 12:46 AM.

            Comment


              #7
              Hi @bltdavid,
              I am remember why I am using arraylist now, because Price = Price + (src[i] * (1/vpcc[i]) * (1/vpr[i])); causes the error CS0021
              "Cannot apply indexing with[] to an expression of type "int"
              "Cannot apply indexing with[] to an expression of type "double"
              I am trying to put VPCc and VPR outside the function PriceFunc as your suggestion to avoid arraylist inside that function and the error CS0021 are coming back in
              the StopLoss4.cs below. Any advise please.
              Attached Files

              Comment


                #8
                OK. I bring everything inside the function to outside and take out the function but the errors are still the same CS0021.
                Attached Files

                Comment


                  #9
                  Originally posted by mailonline38 View Post
                  [
                  1. As you see, src[i], VPCc[i] and VPR[i] are all arrays.
                  If I am using array then I have to know the size of array. That's is why I am using arraylist.
                  Now, you are point it out that arraylist VPCc and VPRr are quite large. Thanks for that help.
                  No.

                  src is not an array. It is a DataSeries. Big difference.

                  Whereas VPCc and VPRr are ArrayLists, and will contain as many
                  elements as there are ticks received (because COBC=F means OBU is
                  called once per tick), the src DataSeries is not anywhere near as big.

                  Why?
                  By definition, all DataSeries have the same number of elements as there
                  are bars on the charts -- this is by definition -- every DataSeries is said to
                  be "synced" to a bars series (usually the primary bars series) which is just
                  the bars on the chart.

                  Well, "synced to the primary bar series" means there is 1 element in the
                  DataSeries for each bar on the chart. Such a DataSeries grows at the
                  same exact rate as bars are added to the chart, regardless of COBC setting.

                  So, while your ArrayLists are the same size (and both huge), your src
                  DataSeries is much smaller, since it has the same number of elements
                  as bars on the chart. As time passes the disparity in size of the ArrayLists
                  vs the number of elements in the DataSeries increases greatly.

                  Are you aware of these differences?
                  src is absolutely not an array.
                  DataSeries != array.

                  Why does your logic need 1 DataSeries and 2 ArrayLists?
                  Last edited by bltdavid; 03-23-2021, 05:23 PM.

                  Comment


                    #10
                    Originally posted by mailonline38 View Post
                    The logic is about array calculation:
                    Price=0.0
                    for i=0 to lenV - 1
                    Price = Price+(src[i]*1/VPCc[i]*1/VPR[i])

                    1. As you see, src[i], VPCc[i] and VPR[i] are all arrays.
                    See how 'i' is used as index for all three?

                    That still confuses me -- you're looping over recent bars with src[i],
                    but each element VPRr[i] and VPCc[i] is from a recent tick.

                    I still don't understand what you're trying to do.
                    Can you break down your logic more?
                    Where are these formulas coming from?
                    Book? Magazine article?

                    Comment


                      #11
                      Click image for larger version  Name:	Off Chart 6.jpg Views:	0 Size:	241.1 KB ID:	1147261
                      Originally posted by bltdavid View Post
                      Are you aware of these differences?
                      src is absolutely not an array.
                      DataSeries != array.

                      Why does your logic need 1 DataSeries and 2 ArrayLists?
                      WoW, that is deep knowledge!
                      As you see in the post #1, the idea and the logic are from pine script (tradingview) and it looks so simple. But .....

                      Price=0.0
                      for i=0 to lenV - 1
                      Price = Price+(src[i]*1/VPCc[i]*1/VPR[i])

                      When I coded to this part, I can not get it compiled and gave the errors CS0021.
                      So I have trying by errors to make it compile, I got 1 DataSeries and 2 ArrayLists so far since I see [ ] and i I think they are arrays.
                      Now, you tell me that DataSeries != array. I see your point.

                      Therefore, I am trying to get rid off the function and DataSeries src. I got the indicator close to the price chart but its plot is not quite the same as tradingview chart. Thanks for that help.

                      If you see this logic and get error CS0021 then how you do you code in NT7. Please advise.
                      Last edited by mailonline38; 03-18-2021, 12:15 PM.

                      Comment


                        #12
                        Originally posted by mailonline38 View Post
                        As you see in the post #1, the idea and the logic are from pine script (tradingview) ...
                        Huh? What?
                        There was no such background information in your post #1.

                        Do you have a link for the original tradingview pine script?

                        Comment


                          #13
                          Originally posted by bltdavid View Post

                          Huh? What?
                          There was no such background information in your post #1.

                          Do you have a link for the original tradingview pine script?
                          Here you go https://www.tradingview.com/script/GpvwlRQm. Thanks for asking.

                          Comment


                            #14
                            Ok, I think I see where you've gone wrong.

                            Don't uses ArrayLists.
                            Everything should be DataSeries.

                            Stand by ... I'll set a 30 min timer and see how far I can get

                            Comment


                              #15
                              Originally posted by bltdavid View Post
                              Ok, I think I see where you've gone wrong.

                              Don't uses ArrayLists.
                              Everything should be DataSeries.

                              Stand by ... I'll set a 30 min timer and see how far I can get
                              Sure... I am reading about Series<double> now to avoid using ArrayLists. Thanks.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Max238, Today, 01:28 AM
                              5 responses
                              42 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by giulyko00, Yesterday, 12:03 PM
                              3 responses
                              12 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by habeebft, Today, 07:27 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_ChristopherS  
                              Started by AveryFlynn, Today, 04:57 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by r68cervera, Today, 05:29 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X