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

Number of Trades in Last X Amount of Time

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

    Number of Trades in Last X Amount of Time

    Is there a way track how many trades in a strategy were executed in the last X minutes or seconds?

    I thought of using something like a data series to track the number of trades for each bar and sum up the last Y bars, but since I use range bars, that won't quite do the trick. And sometimes, with range bars in a quiet market, my strategy can produce quite a number of trades within a single bar.

    Any thoughts on how to accomplish this? Ultimately, what I'm trying to do is say "if there were 5 trades in the last 5 minutes, disable the strategy" or something similar to that.

    #2
    Hi coolmoss,

    For this I would look into information exposed with IExecution. For closed trades, you could check the trade performance collection for specific execution times to compare against your computer clock for example. If you are more interested in open positions, you would need to keep track of "last 5 executions" on your own with a counter, but can still use the time information exposed from execution interface.


    This reference sample can help work with Trade performance collections:


    Untested:
    Code:
    Trade fiveTradesAgo = Performance.RealtimeTrades[Count - 5];
    if (DateTime.Compare(fiveTradesAgo.EntryExecution.Time.AddMinutes(5), DateTime.Now) >= 0) 
    {
    //tells you that the fifth most recent trade was opened more recently than 5 minutes ago. 
    }
    See post 6 for snippet which includes proper checks.
    Last edited by NinjaTrader_RyanM1; 07-13-2012, 07:56 AM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Ah, that makes sense. Thanks for the suggestions, as I was sort of barking up the wrong tree.

      Comment


        #4
        You're welcome -- There's always more than one way to do things but hopefully should get you started.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Hi Ryan,

          I tried a variant of the code you posted and get the following message in the log:


          Error on calling 'OnBarUpdate' method for strategy 'RKSjugJuly12RealTime/5209cb591b5749a9bbb7648f4544de2c': 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.

          Here is the code I currently have running in onBarUpdate (eventually plan on having it in onExecution). The log error occurs when the 5th or 6th trade occurs:

          if(Performance.AllTrades.Count > 5)
          {
          Trade tradesAgo = Performance.RealtimeTrades[Count - 5];
          if (DateTime.Compare(tradesAgo.EntryExecution.Time.Ad dMinutes(1), DateTime.Now) <= 0)
          {
          Disable();
          }
          }
          I have made no other code changes. I can effect when the error arrives by varying the >5 to some other value. Is there something incorrect in the application or syntax of this code snippet?

          Comment


            #6
            Thanks for giving a try. This should work better. It needed checks on the RealtimeTrades.Count property in a couple places. I also flipped operator on the DateTime.Compare which I believe is closer to what you're looking for.

            Code:
            if(Performance.RealtimeTrades.Count > 5)
            {
            	Print("More than 5 RT trades");
            	Trade tradesAgo = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 5];
            
            	if (DateTime.Compare(tradesAgo.EntryExecution.Time.AddMinutes(1), DateTime.Now) >= 0) 
            	{
            		Print("5 trades in a minute");
            		Disable(); 
            	}
            }
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              I'm happy to try or test any suggestions support provides. I always learn something in the process. I know you guys don't have to extend yourself quite this far and I really appreciate it. I'd never be able to get anywhere without your help.

              Comment


                #8
                This worked perfect, thanks!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NJA_MC, 01-03-2021, 10:34 PM
                11 responses
                148 views
                0 likes
                Last Post wzgy0920  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                173 responses
                2,367 views
                0 likes
                Last Post jeronymite  
                Started by Mindset, Yesterday, 10:38 PM
                0 responses
                10 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by JesseOffshore, Yesterday, 09:40 PM
                0 responses
                6 views
                0 likes
                Last Post JesseOffshore  
                Started by WHICKED, 04-26-2024, 12:56 PM
                4 responses
                144 views
                0 likes
                Last Post WHICKED
                by WHICKED
                 
                Working...
                X