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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      137 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      120 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      72 views
      0 likes
      Last Post PaulMohn  
      Working...
      X