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 Jimmyk, 01-26-2018, 05:19 AM
      6 responses
      835 views
      0 likes
      Last Post emuns
      by emuns
       
      Started by jxs_xrj, 01-12-2020, 09:49 AM
      6 responses
      3,291 views
      1 like
      Last Post jgualdronc  
      Started by Touch-Ups, Today, 10:36 AM
      0 responses
      10 views
      0 likes
      Last Post Touch-Ups  
      Started by geddyisodin, 04-25-2024, 05:20 AM
      11 responses
      62 views
      0 likes
      Last Post halgo_boulder  
      Started by Option Whisperer, Today, 09:55 AM
      0 responses
      9 views
      0 likes
      Last Post Option Whisperer  
      Working...
      X