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

Strategy that allows for a percentage of events

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

    #16
    Hello ArmKnuckle,

    Thanks for your reply.

    What I would suggest is to remove this statement: int longCount = 0; // declare and reset counter

    At the class level ( below the statement "public class..."), add a statement like private int longCount = 0;

    In your code somewhere you can add:

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    longCount = 0; // reset counter when flat
    }


    Those changes will allow the counter to accumulate until you are flat.
    Paul H.NinjaTrader Customer Service

    Comment


      #17
      Thanks! That made a big difference. One more tweak and I should be in good shape.

      I am using a multi-time frame strategy. (3, 6, and 15 minute.) There are multiple times an hour when these BarsArray overlap each other at the same "time of day". This has the effect of have the counter adding multiple increments at the same "time of day" if my code is true.

      Time: 5/14/2018 10:45:00 AM Count: 0 03 min. bar
      Time: 5/14/2018 10:45:00 AM Count: 0 15 min. bar
      Time: 5/14/2018 10:48:00 AM Count: 1 03 min. bar
      Time: 5/14/2018 10:48:00 AM Count: 2 06 min. bar **Duplicate**
      Time: 5/14/2018 10:51:00 AM Count: 2 03 min. bar
      Time: 5/14/2018 10:54:00 AM Count: 2 03 min. bar
      Time: 5/14/2018 10:54:00 AM Count: 2 06 min. bar
      Time: 5/14/2018 10:57:00 AM Count: 2 03 min. bar
      Time: 5/14/2018 11:00:00 AM Count: 2 03 min. bar
      Time: 5/14/2018 11:00:00 AM Count: 2 06 min. bar
      Time: 5/14/2018 11:00:00 AM Count: 2 15 min. bar
      Time: 5/14/2018 11:03:00 AM Count: 2 03 min. bar


      Could you provide help on creating one of the following:
      1) Can you have the counter reference events on a specific BarsArray so it only increments once per "time of day"? (Example: Only increment the 06 min. BarsArray.)

      2) Can you have the counter increment only once per "time of day", even if there are duplicates?


      Thanks again for all your help.
      Last edited by ArmKnuckle; 05-27-2018, 08:39 PM.

      Comment


        #18
        Hello ArmKnuckle,

        Thank you for your response.

        You can use the BarsInProgress check to ensure it only updates on the required BarsArray. Please visit the following link for details on using multiple time frames in your code: https://ninjatrader.com/support/help...nstruments.htm

        Please let me know if you have any questions.

        Comment


          #19
          Could you provide your expertise for another counter question? I am learning, and every bit of help is greatly appreciated.

          I am using the counter to iterate when the PSAR flips above/below the price during a trading day.

          My strategy is set to trade OnEachTick. This is necessary so I can exit a trade the instant the PSAR flips sides when certain parameters are met.

          The counter and exit code is below:

          Code:
          	if (Position.MarketPosition == MarketPosition.Long)  // only enter when LONG
          {
          	{
          	if (BarsInProgress == 0)
          		{	
          		if (testPsar(BarsArray[0], 0.01, 0.2, 0.01)[1] <= Lows[0][1] && testPsar(BarsArray[0], 0.01, 0.2, 0.01)[0] >= Highs[0][0])
          			PSARFlipLong++; // increment the counter
          			{
          				Print (Instrument.FullName + "    "+Times[0][0].ToString()+"      LONG Flip Count: "+PSARFlipLong);	
          			}	
          
          		if (((testPsar(BarsArray[0], 0.01, 0.2, 0.01)[1] <= Lows[0][1] && testPsar(BarsArray[0], 0.01, 0.2, 0.01)[0] >= Highs[0][0])
          			&&
          			PSARFlipLong == 2 
          			&& 
          			MACD(BarsArray[0], 12, 26, 9).Diff[1] > 0)
          			||
          			(ToTime(Times[0][0]) >= 155700))
          			{
          				ExitLong("Enter Long 8.4");
          			}
          		}	
          	}
          }
          This works, but the counter keeps iterating on every tick that transpires for the duration of the current bar after the PSAR has flipped.

          What code could I use so the counter could iterate OnEachTick anytime during the current bar, but could only count a maximum of ONCE per bar?

          (One PSAR flip OnEachTick "live" in the current bar = one iteration of the counter.)

          Thanks in advance.
          Last edited by ArmKnuckle; 06-15-2018, 03:25 AM.

          Comment


            #20
            Hello ArmKnuckle,

            Thanks for your reply.

            In the case of OnEachTick processing and the need to track a single event per bar, the best approach would be to use the current bar number (through CurrentBar), save the current bar number when the event occurs and use a check in your conditions to see if the current bar number is the same as the saved bar number.

            CurrentBar is a system property that will contain the bar number that increments on each new bar.
            In your variables area create an int variable called savedBar to be used to put the CurrentBar number into when needed.

            An example of use would be

            if (your existing conditions && CurrentBar != savedBar)
            {
            // your existing actions
            savedBar = CurrentBar; // save the current bar on the first event in this bar to prevent further actions on subsequent ticks, until the next bar.
            }

            In the above code we are checking to see if the variable savedBar is not equal to the CurrentBar which it would not be on the first event of a new bar so the condition would be true and if your other conditions are true then the actions related to the conditions would be performed and the one action would be to save the CurrentBar into the variable savedBar.

            On any further ticks where your conditions are true, the actions would not execute because savedBar is now equal to CurrentBar and the condition CurrentBar != savedBar is not true preventing the actions from running until a new bar has been created (and CurrentBar would now have a different bar number).
            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by RookieTrader, Today, 09:37 AM
            3 responses
            13 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by kulwinder73, Today, 10:31 AM
            0 responses
            5 views
            0 likes
            Last Post kulwinder73  
            Started by terofs, Yesterday, 04:18 PM
            1 response
            22 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by CommonWhale, Today, 09:55 AM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by Gerik, Today, 09:40 AM
            2 responses
            7 views
            0 likes
            Last Post Gerik
            by Gerik
             
            Working...
            X