Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT8 Making a script that can adjust an order to the value of a plot at every bar ?

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

    NT8 Making a script that can adjust an order to the value of a plot at every bar ?

    Hello !
    I'm trying to create a script on NT8, that could automaticly trail a stop, without being a strategy, but there is not a lot of threads and answers about this subject, and its pretty annoying to have to wait over 8 hours in front of a screen sometimes, to just press 2 buttons to trail, so i would really aprecciate some help
    I'm sure its a little bit more tricky than an atm strategy trail stop, but i'm sure there is tallented peoples that can figure it out ! So here's the idea :

    Having a script, that can detect if an alert on a chart is declenched (the script know which alert to look for, because the script will only accept an alert with a specific name). Then, the script will look if there is a stop created by an atm strategy, or a limit stop order. (only if the orders are created in the account the script care about, and the account where he will start to adjust the order, like explained below, we can take an Sim101 account for example).

    If there is one (stop order), it will start to adjust automaticly the order (with the change option in the account class maybe ?), to the value of the plot of an indicator (indicator calculate on a certain type of chart), but with an offset of +1 (or -1 if long); (the indicator is just a little thing, that create a dot if the bar on the chart is up or down, to have a trail stop value, to be enable to when; (that's how i actually proceed) i see that the target is hit, i attach the stop to the indicator (actually there is a version for short, and one for long, to have only the type of dot that i need), with an offset of +1 or -1, to trail the position).

    Then, after the first time the order is adjusted (adjusted right after that the alert got declenched and the script see it), the script will wait for every bar close, to see if the value of the plot is different, if yes, it will re-adjust the order to the value of the plot, with an offset of +1 or -1 (i thought, that it would be easier to create 2 versions of the script, one for the shorts, and one for the longs).

    The script will keep adjusting the order, until the order is filled, and when the script see that the order is cancelled, or filled (that the order dont exist anymore), the script is going to stop.


    That's the idea, but there is not a lot of info about making script on NT7 (if it was possible on nt7, i dont know) or NT8, so if anyone have thoughts or ideas on how this can be possible (i created this way of how the script could work, because from what i read, its a way of making the thing that can work on NT8, but i dont know how to program this ).

    Here's some link of the NT8 helpguides that are really related to making this script work : (https://ninjatrader.com/support/help...rderupdate.htm and https://ninjatrader.com/support/help...us/?change.htm ).

    Also, if you think this is not possible to do, first it would be sad, but second, do you think, it would be possible to make the same thing with a strategy and not a script, but having the possibilitie to make the entry for the strategy manually (like if the automated strategy was an atm strategy), i have tried on NT7, without success, maybe its possible on NT8 ?

    Thanks a lot for reading all this topic, and a huge thanks if you can help on this problem !
    Have a great day !

    #2
    Hello Robinson94,

    Welcome to our Support Forums!

    It is currently not possible to access alerts made within a chart from a NinjaScript.

    What your are planning to do would be possible through the context of a NinjaScript strategy. You could have your strategy enter a position with an ATM strategy or follow your own programmed logic. This way you could program your own Auto Trail.

    The SampleOnOrderUpdate strategy demonstrates how you can program an Auto Breakeven in a NinjaScript which is very similar. The Auto Trail would simply alter the logic to move the stop loss more than once.

    It is also possible to modify the stop loss of an ATM strategy from a NinjaScript using the AtmStrategyChangeStopTarget() method. Documentation on this and the other ATM Strategy methods can be found in the help guide. I will provide a link at the end of my post.

    Buttons can be created in a NinjaScript that can be used for manual operations. You could for example, add a button to a strategy and use that to submit orders manually, but in the context of the NinjaScript. This will allow the NinjaScript strategy to manage the orders you wish to place manually.

    In the support department at NinjaTrader we do not create, debug, or modify code for our clients. This is so that we can maintain a high level of service for all of our clients as well as our partners. If you are looking for those services, you can write into platformsupport[at]ninjatrader[dot]com and I can have a member of our Business Development team pass over a list of 3rd party NinjaScript Consultants that would be happy to write the code for you.

    Here are some resources for the items discussed above.

    ATM Strategy Methods - https://ninjatrader.com/support/help...gy_methods.htm

    SampleOnOrderUpdate strategy - http://ninjatrader.com/support/forum...ead.php?t=7499

    WPF modifications to create buttons in NT8 - http://ninjatrader.com/support/forum...327#post499327

    Please let me know if I may be of further assistance.

    Comment


      #3
      Thanks a lot for all this !!

      I'm trying it now, could we keep the thread open a little bit, in case i'm facing a few problems ?
      But so, if i get it well, i can create, pretty much the same thing than an automated strategy in ninjascript for the stop and order handling, but its used as an Atm strategy, so i keep the freedom of manual order ? That's what i undestand from the Samples.

      Anyway, i had no idea that NT8 could do this, this is amazing !

      Thanks a LOT for your time !

      Comment


        #4
        Is it possible, to simply have a simple atm strategy. Then i have an alert that will submit a regular order at any price.
        And then having a automated strategie that will see the order created by the alert, and start to take responsabilitie for the order, and then the strategie can trail this order.

        So i have my atm strategy, and manual entry, i just have to put an alert to create an order, then the strategy take over to adjust this new order, and, if the order is filled, since i would be flat, all my orders made by the ATM strategy, will be removed, like the usual.

        So its possible for a strategy to find a order that she didnt created, and to be enable to handle this order ?

        Edit : I found how to name an order created by an alert, but now that i have an order and the name, can a strategy find the order that has the name, and start to trail it ?
        Maybe with this piece of code in the atm strategy example?

        if (orderId.Length > 0)
        {
        string[] status = GetAtmStrategyEntryOrderStatus(orderId);

        But instead of checking the order status, starting to manage the order, its possible ?
        Last edited by Robinson94; 06-29-2017, 01:57 PM.

        Comment


          #5
          Progress on making automatic trailing stop

          I'm a little bit stuck, so i resume since there has been a lot of progress.

          My idea, is that when an alert is declenched, its going to create a order with a specific name. Longtrailstop in this case. This is done.
          Then the order is a stopmarket order, put at -1 of price, to be sure to not get filled.

          Now that i have the order, i want to trail that order on the plot and value of an other indicator (Longautotrailstop). So what i did, is making a strategy that's going to be activate, and the code is this (not the full code) :

          protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
          int quantity, int filled, double averageFillPrice,
          Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
          {
          if (order.Name == "Longtrailstop" && orderState != OrderState.Accepted);
          {
          AtmStrategyChangeStopTarget(-1, LONGautotrailstop(Close)[0], "Longtrailstop", "autotrailstop");
          }
          }


          So, the idea, is if the order name is the right one, and the order is accepted (and can't get filled is at a negative value), the order its going to be adjust at a -1 of offset, on the value of the indicator called LONGautotrailstop.

          The part where the strategy take the control sadly don't work, maybe its because the order created by the alert is not an atm order ? Or a strategy can't handle a order that she didnt created ?
          After, if we can make this work, the idea would be to add the same code to the bar update, so when there is an order update, we don't have to wait for a new bar for the order to adjust, but after, at every new bar, it will look if it can adjust if the value of the indicator is different.
          PS : everything is compiling well

          I'm sure there is a lot of things wrong in this piece of code , but sadly, i can't figure out why yet, thanks a lot for the help !

          Comment


            #6
            Robinson94,

            Thanks for the reply.

            The ATM strategy order methods are designed to be used with ATM strategies. you will want to use ChangeOrder() to modify Order objects. Also, Alerts cannot initiate ATM strategies.

            ChangeOrder() - https://ninjatrader.com/support/help...hangeorder.htm

            You can use the Strategy Builder to create the same alerts in the context of a NinjaScript strategy. When the code is unlocked, you can add your own ATM strategy methods.

            Strategy Builder 301 - https://www.youtube.com/watch?v=HCyt90GAs9k

            As OnOrderUpdate() is an event driven method and it will only fire for updates on Orders that belong to the strategy. From the help guide:
            Only orders which have been submitted and managed by the strategy will call OnOrderUpdate().


            It is possible to loop through orders in an account if you wish to access orders created outside the strategy.

            Account.Orders - https://ninjatrader.com/support/help...rs_account.htm

            I would recommend to design what you would like to do completely within the context of a strategy. Manually submitted orders are not designed to be easily managed by strategies and there are many complications and implications to consider when attempting to do so.

            Please let me know if I may be of further assistance.

            Comment


              #7
              Error in state realtime

              Hey !

              I have follow your advices, but i have an error at a cetain point.
              So, what i did, is to keep an basic atm strategy that can be used manually like on the dom. Then i created a strategy, that has an variable, where i put the value of my target, and if the value is hit by the price, its going to create an ATM stop market order, and the idea, is onbarupdate, to check if the strategy can adjust the order.
              And normally, i should have the possiblity, to keep manual entry, and manual stop loss, but having an automated trailing stop, and if the trailing stop is hit, my account become flat, and when an atm strategy see that the account is flat, its cancelling all orders, so my stoploss got removed too.

              Its great on the paper, but when the "target" is hit, i receive this error : Stragey 'LONGTrailStop": Ettor on calling "OnBarUpdate" method on bar 0: 'AtmStrategyCreate' method can only be called in state 'Realtime'.

              But i didnt undestand the meaning of Realtime, since my data fee is on realtime, do i need to add something to the code ? Here's the actual code (only the part of OnBarUpdate, and the end of the code about the callback, is only a paste right now, its not configured) :

              protected override void OnBarUpdate()
              {

              if (High[0] >= TargetValue)
              {

              AtmStrategyCreate(OrderAction.Buy, OrderType.StopMarket, -1, -1, TimeInForce.Day, atmStrategyOrderId, "autotrailstop", "autotrailstop", (atmCallbackErrorCode, atmCallbackId) => {

              // checks that the call back is returned for the current atmStrategyId stored
              if (atmCallbackId == atmStrategyId)
              {
              // check the atm call back for any error codes
              if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
              {
              // if no error, set private bool to true to indicate the atm strategy is created
              isAtmStrategyCreated = true;
              }
              }
              });


              Thanks !

              Comment


                #8
                Hello Robinson94,

                Thanks for your reply and for your patience.

                The AtmStrategyCreate() method is not backtestable and will not execute on historical data - to ensure that this is not happening, you can include this check to only run your code on real-time data:

                if(State == State.Historical)
                return;

                You can find more information on AtmStrategyCreate() and the historical state in our help guide here:

                AtmStrategyCreate() - http://ninjatrader.com/support/helpG...tegycreate.htm

                State.Historical - http://ninjatrader.com/support/helpG...n-us/state.htm

                Please let me know if I may be of any further assistance.
                Alan S.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                576 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                334 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X