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

Order handling with managed approach

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

    Order handling with managed approach

    The link https://ninjatrader.com/support/help...d_approach.htm was very helpful, but I'm still wondering if the managed approach can accomplish certain tasks.

    Take this example:

    Code:
    protected override void Initialize() 
    { 
        EntriesPerDirection = 5; 
        EntryHandling = EntryHandling.AllEntries; 
    }
    Now pretend I have an indicator XYZ() that outputs one of three signals: Buy (+1), Sell (-1), Neutral (0).

    Code:
    protected override void OnBarUpdate()
    {
        if (XYZ()[0] == 1)
             // should do ONE of the following tasks this bar (i.e. don't reverse in one step):
             //          if currently long < 5 contracts, should long another one
             //          if currently hold short position(s), should close one short position now 
             EnterLongLimit(GetCurrentBid())
    
        if (XYZ()[0] == 0)
             // if hold any contracts (short or long), close 1 position now
             ExitLongLimit(GetCurrentBid());
             ExitShortLimit(GetCurrentAsk());
    
        if (XYZ()[0] == -1)
             // should do ONE of the following tasks this bar (i.e. don't reverse in one step):
             //          if currently short < 5, should short another one
             //          if currently hold a long position, should close one long position now 
             EnterShortLimit(GetCurrentAsk())
    }
    I put in the comments what I want to accomplish. Are the orders I placed below the comments the way to accomplish that? I am trading contracts with low volume, and therefore only want to move one contract per bar. I don't want to reverse a long position to a short position in the same bar.

    #2
    Hello YD777, and thank you for your question.

    With what you are describing, a common approach I have seen in scripts is to structure them as follows :

    Code:
    [FONT=Courier New]protected override void OnBarUpdate()
    {
        if (XYZ()[0] == 1)
        {
             // should do ONE of the following tasks this bar (i.e. don't reverse in one step):
             //          if currently long < 5 contracts, should long another one
             //          if currently hold short position(s), should close one short position now 
             EnterLongLimit(GetCurrentBid());
            [B]return[/B];
        }
    
        if (XYZ()[0] == -1)
        {
             // should do ONE of the following tasks this bar (i.e. don't reverse in one step):
             //          if currently short < 5, should short another one
             //          if currently hold a long position, should close one long position now 
             EnterShortLimit(GetCurrentAsk())
        }
    }
    
    protected override void OnExecution(IExecution execution)
    {
        if (XYZ()[0] == 0)
        {
             // if hold any contracts (short or long), close 1 position now
             ExitLongLimit(GetCurrentBid());
             ExitShortLimit(GetCurrentAsk());
        }
    }[/FONT]
    This works because ExitLongLimit and ExitShortLimit will be ignored if you are not in a long or short position, respectively.

    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      if (XYZ()[0] == 0)
      // if hold any contracts (short or long), close 1 position now
      ExitLongLimit(GetCurrentBid());
      ExitShortLimit(GetCurrentAsk());
      • You need to enclose those statements in a block.
      • If you want to exit a specific quantity, you must specify the quantity. Those statements will exit the entire position.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by andrewtrades, Today, 04:57 PM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by chbruno, Today, 04:10 PM
      0 responses
      6 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      436 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      9 views
      0 likes
      Last Post FAQtrader  
      Started by rocketman7, Today, 09:41 AM
      5 responses
      20 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X