Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Arrays ...

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

    Arrays ...

    Hi all,

    Having a problem with an array.

    I wish for the last 10 tick values to be captured in an array. This will allow me to then compare and analyse the data.

    Everything was fine prior to my entering this code. whats wrong with it?

    // Array variables
    double[] tickStore;
    private int tickCount = 0;


    protected override void OnMarketData(MarketDataEventArgs e)
    {
    tickStore[tickCount] = tickPrice; // place current tick price in array
    tickCount = tickCount + 1; // move array pointer
    if
    (
    ( tickCount >3 ) // we have at least 3 ticks in Array
    )

    {

    // loop through the array

    for( int i = 0; i<tickCount;i++)
    {
    Print(String.Format("Array" + i + "= " + tickStore[tickCount]));


    tickCheck = tickStore[i] - tickStore[i-1];

    if( (tickCheck >1) || (tickCheck <-1) ) // difference between adjacent ticks

    {

    #2
    Hello cocopod,

    Thank you for your post.

    What part of the code is not working? Are you receiving any errors when compiling the code? If so, what do the errors report?

    Comment


      #3
      Hi Patrick,

      Compiles fine. No errors. when I activate the strategy nothing happens. There is some other code that creates a Bracket trade for example and this normally works but now doesn't.

      Just seems like my array code is causing everything to stop.

      Comment


        #4
        Hello cocopod,

        Thank you for your response.

        Are you seeing this print in the Output window?
        Code:
        Print(String.Format("Array" + i + "= " + tickStore[tickCount]));
        OnMarketData() is not called on historical data, are you expecting the print immediately after enabling the strategy?

        Comment


          #5
          No its not...

          Comment


            #6
            Originally posted by cocopod View Post
            Hi all,

            Having a problem with an array.

            I wish for the last 10 tick values to be captured in an array. This will allow me to then compare and analyse the data.

            Everything was fine prior to my entering this code. whats wrong with it?

            // Array variables
            double[] tickStore;
            private int tickCount = 0;


            protected override void OnMarketData(MarketDataEventArgs e)
            {
            tickStore[tickCount] = tickPrice; // place current tick price in array
            tickCount = tickCount + 1; // move array pointer
            if
            (
            ( tickCount >3 ) // we have at least 3 ticks in Array
            &nbsp

            {

            // loop through the array

            for( int i = 0; i<tickCount;i++)
            {
            Print(String.Format("Array" + i + "= " + tickStore[tickCount]));


            tickCheck = tickStore[i] - tickStore[i-1];

            if( (tickCheck >1) || (tickCheck <-1) ) // difference between adjacent ticks

            {
            On very first entry into your for loop, you are trying to access a negative index.
            Code:
            tickCheck = tickStore[i] - tickStore[i-1];
            If i is zero, then i-1 is -1, so you are trying to get tickStore[-1]. Arrays cannot be accessed with a negative index. You may want to correct your logic, so that you do not try to access a negative index. Depending on your purpose, you could start your for loop from i = 1.

            Comment


              #7
              Hi there,

              thanks for the response.

              And yes you are correct, I was indeed addressing the array incorrectly. I have changed it to the following - however it still doesn't work

              tickCheck = tickStore[i] - tickStore[i+1];

              if( (tickCheck >1) || (tickCheck <-1) ) // difference between adjacent ticks

              {
              Array.Clear(tickStore, 0, tickCount);
              }

              Comment


                #8
                Hello cocopod,

                OnMarketData() is not called on historical data, are you expecting the print immediately after enabling the strategy?

                Comment


                  #9
                  Originally posted by cocopod View Post
                  Hi there,

                  thanks for the response.

                  And yes you are correct, I was indeed addressing the array incorrectly. I have changed it to the following - however it still doesn't work

                  tickCheck = tickStore[i] - tickStore[i+1];

                  if( (tickCheck >1) || (tickCheck <-1) ) // difference between adjacent ticks

                  {
                  Array.Clear(tickStore, 0, tickCount);
                  }
                  Can I refer to what Koganam pointed out in post #6 that:

                  Code:
                  tickCheck = tickStore[i] - tickStore[i[COLOR=Red][B]-1[/B][/COLOR]];
                  will have a negative index if the loop starts at i = 0.

                  You seem to have replaced the above formula by:

                  Code:
                  tickCheck = tickStore[i] - tickStore[i[COLOR=Red][B]+1[/B][/COLOR]]
                  which will give you a negative tickCheck if the original formula gives a positive.

                  Comment


                    #10
                    Originally posted by cocopod View Post
                    Hi there,

                    thanks for the response.

                    And yes you are correct, I was indeed addressing the array incorrectly. I have changed it to the following - however it still doesn't work

                    tickCheck = tickStore[i] - tickStore[i+1];

                    if( (tickCheck >1) || (tickCheck <-1) ) // difference between adjacent ticks

                    {
                    Array.Clear(tickStore, 0, tickCount);
                    }
                    If you look in your log,you will see that you have an "out of bounds" exception. That is because when your array is full, i+1 will be an index one larger than your array size.

                    Now you need to take care of that logic error.

                    Comment


                      #11
                      Hi Patrick,

                      I am actually using Live Data.

                      Arbuthnot - The code

                      "tickCheck = tickStore[i] - tickStore[i+1]"

                      translates to - Subtract the contents of one array cell from the contents of the next one along the chain. The signage of the result is unimportant

                      Comment


                        #12
                        Koganam- - for( int i = 1; i<tickCount;i++)

                        I don't get an error and when you consider the code you can see that i < Tickcount, so the extreme of the count takes it to tickCount and not over

                        Comment


                          #13
                          Hello cocopod,

                          Can you provide the full script so we may test on our end?

                          Comment


                            #14
                            Of course not Patrick!!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            650 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            370 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            109 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            574 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            577 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X