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

Cancel any pending orders when flat?

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

    Cancel any pending orders when flat?

    Hi, I have strategies that execute a stop loss order when the order is filled. But the strategies also have exit conditions that just exit the position with ExitLong/Short when certain conditions are met. This morning it had built a position of 8 contracts then the exit condition was triggered. It filled the orders in a block of 5 and a block of 3. The 5 stop losses cleared off the chart, but a stop loss for 3 stayed on even though the position was flat.
    So what I am trying to do is make a check for when the position is flat to cancel any pending orders. I used ChatGPT to come up with this but when I try to compile it it gives me quite a few errors. I will include a screenshot of them. Here is the code:

    // Check if the position is flat
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Get all open orders
    List<IOrder> openOrders = GetOrders();

    // Loop through each order and cancel it if it is a stop order
    foreach (IOrder order in openOrders)
    {
    if (order.OrderType == OrderType.Stop)
    {
    CancelOrder(order.OrderId);
    }
    }
    }​
    Click image for larger version

Name:	Screenshot_1.jpg
Views:	117
Size:	29.5 KB
ID:	1249748

    How can I rectify this or accomplish this goal?
    Thanks

    #2
    Hello

    While I can provide some details about the errors you are seeing I do want to point out the following disclaimer about chatgpt below. The code being used is not valid NT8 code which are the reasons from the error. IOrder was for nt7 and there is no method named GetOrders in NinjaScript. The ordertype stop also is not a valid type of order type. You can find a sample of cancelling an order in the following link, you would need to use the Order object and pass that to CancelOrder();



    From our experience at this time, ChatGpt is not quite adequate to generate valid compilable NinjaScripts that function as the user has intentioned. We often find that the generated code will call non-existent properties and methods, use improper classes or inheritance, and may have incorrect logic. We highly encourage that you create a new NinjaScript yourself using the NinjaScript Editor, and use the code generated by ChatGpt as more as suggestions and guide than the actual code generated.

    While It would not be within our support model to correct these scripts at user request, we would be happy to provide insight for any direct specific inquiries you may have if you would like to create this script yourself. Our support is able to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.​
    JesseNinjaTrader Customer Service

    Comment


      #3
      I have definitely realized that ChatGPT is completely full of errors but I try it for ideas sometimes. That being said, what of my original intent though? I have looked through that sample file and it does not address what I am trying to do, which is cancel any pending stopmarket orders left over after a position was closed.

      Comment


        #4
        Hello RaygunWizzle,

        That sample would apply to any kind of order that you can cancel, the same concept is used for cancelling a target after an entry filled. That sample just cancels an entry order after it does not fill. If you wanted to cancel a target you would have to use OnExecutionUpdate to know when the entry filled and then supply the Order variables for your targets that you had stored. Cancel order can only be used if you are storing Order variables, you can find a sample of using Order variables here: https://ninjatrader.com/support/help...and_onexec.htm
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,789 views
        0 likes
        Last Post aligator  
        Started by Jimmyk, 01-26-2018, 05:19 AM
        6 responses
        837 views
        0 likes
        Last Post emuns
        by emuns
         
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        6 responses
        3,294 views
        1 like
        Last Post jgualdronc  
        Started by Touch-Ups, Today, 10:36 AM
        0 responses
        13 views
        0 likes
        Last Post Touch-Ups  
        Started by geddyisodin, 04-25-2024, 05:20 AM
        11 responses
        63 views
        0 likes
        Last Post halgo_boulder  
        Working...
        X