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

simplify code

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

    simplify code

    Hello to the forum,

    I want to outsource recurring calculations from my strategy and simplify my code. That's why I wrote the following code.

    Code:
            protected override void OnBarUpdate()
            {
                // create dummy signal to evaluate
                barCounter++;
                if (barCounter % 2 == 0) barCounter2 += 2;
                testSeries[0] = barCounter2;
    
                MyHistoricSeries myHistoricSeries = new MyHistoricSeries();
    
                // Does not add!
                methodCounter =   myHistoricSeries.Counter(testSeries);
    
                Print (Time[0]
                + "  barCounter  " + barCounter
                + "  barCounter2 " + barCounter2
                + "  methodCounter  " + methodCounter
                );
            }
        }
    }
    
        public class MyHistoricSeries
        {
            int myCounter;
            // Evaluate testSeries
            public int Counter (Series<int>mySeries)
            {
                if (mySeries[0] > mySeries[1]) myCounter++;
                return myCounter;
            }
        }

    The testSeries is passed to the Counter method and returns the evaluation.
    But I want the methodCounter to cumulate!

    Is that basically possible or what other solution is there for my problem?

    Many thanks for the support!

    #2
    Originally posted by user10 View Post
    Hello to the forum,

    I want to outsource recurring calculations from my strategy and simplify my code. That's why I wrote the following code.

    Code:
    protected override void OnBarUpdate()
    {
    // create dummy signal to evaluate
    barCounter++;
    if (barCounter % 2 == 0) barCounter2 += 2;
    testSeries[0] = barCounter2;
    
    MyHistoricSeries myHistoricSeries = new MyHistoricSeries();
    
    // Does not add!
    methodCounter = myHistoricSeries.Counter(testSeries);
    
    Print (Time[0]
    + " barCounter " + barCounter
    + " barCounter2 " + barCounter2
    + " methodCounter " + methodCounter
    );
    }
    }
    }
    
    public class MyHistoricSeries
    {
    int myCounter;
    // Evaluate testSeries
    public int Counter (Series<int>mySeries)
    {
    if (mySeries[0] > mySeries[1]) myCounter++;
    return myCounter;
    }
    }

    The testSeries is passed to the Counter method and returns the evaluation.
    But I want the methodCounter to cumulate!

    Is that basically possible or what other solution is there for my problem?

    Many thanks for the support!
    Try using ++myCounter instead of myCounter++

    Comment


      #3
      Hi user10, thanks for your note.

      It's not incrementing because you're initializing the counter class withing OnBarUpdate, so it is deleted when OBU is finished calling. Place this at the class level (the one that inherits the Indicator class) and it will work properly.

      Best regards.
      Chris L.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_ChrisL View Post
        Hi user10, thanks for your note.

        It's not incrementing because you're initializing the counter class withing OnBarUpdate, so it is deleted when OBU is finished calling. Place this at the class level (the one that inherits the Indicator class) and it will work properly.

        Best regards.
        Thank you very much!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by ArkansasClint, 04-25-2024, 09:28 AM
        1 response
        22 views
        0 likes
        Last Post Leafcutter  
        Started by NM_eFe, Today, 06:14 AM
        0 responses
        3 views
        0 likes
        Last Post NM_eFe
        by NM_eFe
         
        Started by sgordet, Today, 06:04 AM
        0 responses
        4 views
        0 likes
        Last Post sgordet
        by sgordet
         
        Started by bc24fl, 08-30-2019, 01:58 PM
        4 responses
        260 views
        0 likes
        Last Post PaulMohn  
        Started by sugalt, Today, 04:02 AM
        0 responses
        7 views
        0 likes
        Last Post sugalt
        by sugalt
         
        Working...
        X