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

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 wuannetraam, Today, 02:40 AM
    0 responses
    6 views
    0 likes
    Last Post wuannetraam  
    Started by cyberpete76, 03-27-2023, 12:29 AM
    7 responses
    267 views
    1 like
    Last Post slightly  
    Started by renewsaltwater, Today, 01:15 AM
    0 responses
    2 views
    0 likes
    Last Post renewsaltwater  
    Started by slightly, Today, 12:49 AM
    0 responses
    4 views
    0 likes
    Last Post slightly  
    Started by sdauteuil, 09-23-2021, 10:16 AM
    4 responses
    1,210 views
    0 likes
    Last Post jacobpescaia44  
    Working...
    X