Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limit trades per direction

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

    Limit trades per direction

    Hey fellas,

    I want to add an input parameter to control the number of trades per direction. I found some code online that should do this. But, the problem I'm running into is that we know the order is filled by an EnterLong or EnterShort statement so we can increment or decrement the counter. In my code, I'm using EnterLongStopMarket and EnterLongStopLimit orders so we're not in an order until it is filled.

    Any suggestions on how to do this? Here's the current sample code:

    protected override void OnBarUpdate()
    {
    //Reset condition

    if (CrossBelow(SMA(20), SMA(89), 1))
    {
    longtrade= 0; //reset long trigger
    }

    //Enter Trade
    if (CrossAbove(CCI(20), 0, 1) && longtrade <= 3)
    {
    EnterLong(1, "Long 1a");
    if (longtrade == 0)
    {
    longtrade = 1;
    }
    if (longtrade >0)
    {
    longtrade = longtrade + 1;
    }
    }
    }

    #2
    Hello PN720,

    Thank you for your note.

    If you are using the managed approach you could use Entries Per Direction and Entry Handling to control this.




    You could see the following sample which stops a strategy after 3 consecutive losers, for how you can pull previous trades and for which you could base your counter off,


    You could also have a counter in OnOrderUpdate on the fill of your entry order, you could see the following example,
    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()


    If neither of these approaches work could you please provide more detail on what you're attempting to do?

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      That code sample with the 3 consecutive losses was exactly what I needed to see.

      Thanks, fellas!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      65 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      139 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X