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 executed

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

    Number of trades executed

    hello,

    I would like to set a limit on a the number of trades to be executed during a particular period. For example upon condition X perform action Y but then if this action performs a number of times Z within a day stop trading.

    Is there a way to do this in Algo development module?

    Thanks

    #2
    Hello space_trader,

    Thank you for your post.

    Yes, you could use a counter variable that you increment each time the action is taken, and check in the conditions for that action whether or not the counter is greater than the number of times you wish to execute the action. You can then reset the counter on the first bar of a new session to have it start doing the action again.

    I'm attaching a very simple example script.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      hello Kate,

      Can this be done in Strategy Builder? Without writing any code.

      Comment


        #4
        Hello space_trader,

        Thank you for your reply.

        Yes, but due to limitations of the Strategy Builder you'll need to use a custom series to hold your count and move the count forward by assigning the previous bar's value to it in the first set, then resetting the most recent value to 0 if it's the first bar of the session, then adding to the series count when an order is placed. Please see the example Strategy Builder script below.

        Please let us know if we may be of further assistance to you.
        Attached Files
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hello. I unlocked my working strategy and added the code above to allow only one trade per day. Unfortunately, it didn't work as expected. I spent some time trying to figure it out myself but there comes a point where you have to accept defeat! Could you have a look and let me know where the error is? Also, in the code, is 1 trade 'in and out' of the position or just the entry, so for one in and out trade, the trade count would need to be set to '2' ?

          Thanks
          Attached Files

          Comment


            #6
            hello,

            Same here. I am trying to build a simple strategy where a Long or Short order is placed when Price is higher or lower than the open price (first bar of session) but didn't work. I am not C# expert so the mistake could be just a minor adjustment.

            Your input is greatly appreciated.
            Attached Files

            Comment


              #7
              Hello michaelc6,

              Thank you for your note.

              Your issue is that you're not containing the entry logic within curly brackets from the if(myCount < TradesPerDay) so this isn't checked for both sets.

              You have this:

              Code:
              if (myCount < TradesPerDay)
              // Set 1
              if ((CrossAbove(Close, AuSuperTrendU111.StopDot, 1))
              && (Times[0][0].TimeOfDay > new TimeSpan(8, 30, 0))
              && (Times[0][0].TimeOfDay <= new TimeSpan(12, 0, 0)))
              {
              EnterLong(Convert.ToInt32(DefaultQuantity), "");
              myCount = myCount + 1;
              }
              // Set 2
              if ((CrossBelow(Close, AuSuperTrendU111.StopDot, 1))
              && (Times[0][0].TimeOfDay > new TimeSpan(8, 30, 0))
              && (Times[0][0].TimeOfDay <= new TimeSpan(12, 0, 0)))
              {
              EnterShort(Convert.ToInt32(DefaultQuantity), "");
              myCount = myCount + 1;
              }
              and you should have this:

              Code:
              if (myCount < TradesPerDay)
              [B]{[/B]
              // Set 1
              if ((CrossAbove(Close, AuSuperTrendU111.StopDot, 1))
              && (Times[0][0].TimeOfDay > new TimeSpan(8, 30, 0))
              && (Times[0][0].TimeOfDay <= new TimeSpan(12, 0, 0)))
              {
              EnterLong(Convert.ToInt32(DefaultQuantity), "");
              myCount = myCount + 1;
              }
              // Set 2
              if ((CrossBelow(Close, AuSuperTrendU111.StopDot, 1))
              && (Times[0][0].TimeOfDay > new TimeSpan(8, 30, 0))
              && (Times[0][0].TimeOfDay <= new TimeSpan(12, 0, 0)))
              {
              EnterShort(Convert.ToInt32(DefaultQuantity), "");
              myCount = myCount + 1;
              }
              [B]}[/B]
              I've bolded the missing curly brackets in the second set of code.

              Please let us know if we may be of further assistance to you.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Hello space_trader,

                Thank you for your reply.

                Your script isn't looking at the opening price of the bar, you've got it set to look at the median price of the opening bar which would be (High + Low) / 2. The open of the first bar of session would be Open[0].

                You're also not comparing the price of what you're saving as the open to the current price, you're comparing to the Median price again. Current Price would be Close[0].

                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  hello Kate,

                  Thanks but I am aware. The problem is that when I use the strategy analyzer, I see that the strategy trades multi times per day, not just one.

                  Comment


                    #10
                    Hello space_trader,

                    Thank you for your note.

                    You're not bringing the prior myCount value to the current bar so that's going to be 0. Try this:

                    Code:
                    myCount[0] = myCount[1];
                    
                    // Set 1
                    if (Bars.IsFirstBarOfSession == true)
                    {
                    Open_Price = Median[0];
                    myCount[0] = 0;
                    Print(@"Resetting to 0 at " + Convert.ToString(Times[0][0].TimeOfDay));
                    }
                    
                    // Set 2
                    if (Open_Price <= (Median[0] + (-10 * TickSize)) )
                    {
                    
                    if ((myCount[0] < TradesPerDay)
                    && (Close[0] > Open[0])
                    && (Position.MarketPosition == MarketPosition.Flat))
                    {
                    EnterShort(Convert.ToInt32(DefaultQuantity), "");
                    if (myCount[0] == 0)
                    {
                    myCount[0] = 1;
                    }
                    else
                    {
                    myCount[0] = Convert.ToInt32((myCount[1] + 1));
                    }
                    Print(Convert.ToString(myCount[0]));
                    }
                    }
                    
                    // Set 3
                    if (Open_Price >= (Median[0] + (10 * TickSize)) )
                    {
                    if ((myCount[0] < TradesPerDay)
                    && (Close[0] > Open[0])
                    && (Position.MarketPosition == MarketPosition.Flat))
                    {
                    EnterLong(Convert.ToInt32(DefaultQuantity), "");
                    if (myCount[0] == 0)
                    {
                    myCount[0] = 1;
                    }
                    else
                    {
                    myCount[0] = Convert.ToInt32((myCount[1] + 1));
                    }
                    Print(Convert.ToString("Count: " + myCount[0]));
                    }
                    }
                    Please let us know if we may be of further assistance to you.
                    Kate W.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by aussugardefender, Today, 01:07 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post aussugardefender  
                    Started by pvincent, 06-23-2022, 12:53 PM
                    14 responses
                    238 views
                    0 likes
                    Last Post Nyman
                    by Nyman
                     
                    Started by TraderG23, 12-08-2023, 07:56 AM
                    9 responses
                    383 views
                    1 like
                    Last Post Gavini
                    by Gavini
                     
                    Started by oviejo, Today, 12:28 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post oviejo
                    by oviejo
                     
                    Started by pechtri, 06-22-2023, 02:31 AM
                    10 responses
                    125 views
                    0 likes
                    Last Post Leeroy_Jenkins  
                    Working...
                    X