Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Protect Against Orders on Wrong Side of the Market

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

    Protect Against Orders on Wrong Side of the Market

    I would like to protect my strategy from placing orders on the wrong side of the market during fast market conditions. I wrote up this code but I want to share it with the community to see if perhaps there is a better way or if it will even work.

    Also, for NT Support.
    I wanted to know who supplies the OrderState. Rejected. Is is NT or does it come from the broker? When and where does is the order rejected come from? Is the order accepted and Working before it is rejected or auto rejected? What exactly is the flow/life cycle of an Order from start to finish.

    Code:
    private Order orderToConfirm = null;
    private int sumbitOrderRetries = 3;
    
    public void EnterLongLimit()
    {[INDENT]int retryCount = 0;[/INDENT]
                 
                Outside:
                if( retryCount < sumbitOrderRetries)
                {
                    Inside:
                    if(zLimtPriceOk(limitPrice)) // Limit Price is Good to Go
                       {[INDENT=2]try{ 
    orderToConfirm = EnterLongLimit(barsInProgressEnter, ordersLiveUntilCanceled, orderSize * orderMuliplier, limitPrice, longEntryName);
    retryCount++;
    }
    catch (Exception e) {
    Print("Cant Place Order zEnterLongLimit: " + e);
    }
    [/INDENT][INDENT=3]if(orderToConfirm != null &&  orderToConfirm.OrderState != OrderState.Rejected) // Order was accepted[/INDENT][INDENT=3] {
    return;    
    }
    else
    {
    goto Outside;
     }[/INDENT]
                         }
                       else // Limit Price is Not Good
                       {
                        goto Inside;
                       }
                }
                else
                {
                Print("Cant Place Order zEnterLongLimit. Maximun number of retries hit") ;
                return;
                }
    
    
    public bool zLimtPriceOk(double limitPrice)
            {
                bool result = true;
    
                if(limitPrice > GetCurrentAsk() && limitPrice > GetCurrentBid())
                    {
                    result = true;
                    }
                    else result = false;
    
                return result;
            }

    #2
    Hello cutzpr,

    As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.



    Orders are rejected by the brokerage (or by the Simulation Engine for the Sim101 account which pretends to be a brokerage).
    Orders rejected by a brokerage are not accepted (as they are rejected). As they are not accepted they do not become working.
    Orders typically go through pendingsubmit, accepted, working, filled.

    I recommend you use prints to find the answers to the order states.
    Print the order information in OnOrderUpdate() to the output window.

    Print(order.ToString());


    Below is a link to a forum post that demonstrates how to use prints to find information and understand behavior.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    57 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    133 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    73 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