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

Orders tracing indicators

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

    Orders tracing indicators

    Hello

    Is there a way to code a strategy in whch the Stop /Target orders are tracing some indicators (like a moving average, for instance)? Pretty much like the option offered by the DOM.

    Thank you

    #2
    Hello itrader46,

    Thanks for your post.

    Through code, you can call SetStopLoss within OnBarUpdate to update the stop loss dynamically to a new level. Please note that the strategy must set the stop loss to an initial level by calling SetStopLoss when the strategy is flat and before the next entry order is submitted. When the strategy is in a position, you can use SetStopLoss with a CalculationMode of Price and use the indicator value to update the stop loss in association to the indicator plot.

    Please see the SamplePriceModification example below.

    SamplePriceModification - https://ninjatrader.com/support/help...of_stop_lo.htm

    If you are working with ATM strategies, you can call AtmStrategyChangeTargetStop to update the stop loss to an indicator's level. AtmStrategyChangeTargetStop is demonstrated in our SampleAtmStrategy example strategy.

    AtmStrategyChangeTargetStop - https://ninjatrader.com/support/help...stoptarget.htm

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      If I am using an ATM, where do I get the " string orderName " from? It doesn't appear to be the same as the orderId

      AtmStrategyChangeStopTarget(double limitPrice, double stopPrice, string orderName, string atmStrategyId)

      Comment


        #4
        Hello itrader46,

        The order name will be "Target1" "Stop1" "Target2" "Stop2" etc. and will be based on the individual targets and stops for your Atm strategy.

        Code:
        if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
            AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "Stop1", atmStrategyId);
        
        if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
            AtmStrategyChangeStopTarget(High[0] + 3 * TickSize, 0, "Target1", atmStrategyId);
        Please let us know if you have any additional questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          I figured that the stops and targets would be called like that, but when I add that to my code, I get the error " 'GetAtmStrategyMarketPosition' method error: Missing ATM strategy ID parameter " before an order is triggered and " 'AtmStrategyChangeStopTarget' method error: Order name '' is invalid " after the order is triggered

          I must be missing something important, as don't get what you mean by " "Target1" "Stop1" "Target2" "Stop2" etc. and will be based on the individual targets and stops for your Atm strategy."

          My ATM sets the stops and targets as in the template, rather than me setting them up, so the names of orders are not set up by me.

          I'm sorry, but the help guide is very fuzzy in this respect: " AtmStrategyChangeStopTarget(double limitPrice, double stopPrice, string orderName, string atmStrategyId) " and
          " GetAtmStrategyStopTargetOrderStatus(string orderName, string atmStrategyId) " methods are the only ones who refer to " string orderName ", but nowhere is shown how you set the orderName in the first place, as in " EnterLongStopLimit(int quantity, double limitPrice, double stopPrice, string signalName) ", which is for orders placed by the script, where as the ATM methods just call the ATM template to place the orders

          My ATM triggers a position with 3 contracts and 3 targets and I want the stops to follow a T3 moving average I set up in the script , but when I add this code, I get the errors above

          Code:
                         if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat)
                          {
                              AtmStrategyChangeStopTarget(0, T31b2[0], "Stop1", longAtmId);
                              AtmStrategyChangeStopTarget(0, T31b2[0], "Stop2", longAtmId);
                              AtmStrategyChangeStopTarget(0, T31b2[0], "Stop3", longAtmId);
                          }
          I am getting the same errors when I replace Stop1 with longOrderId (which is set up in the script) as well
          Last edited by itrader46; 11-12-2019, 09:35 AM.

          Comment


            #6
            Hello itrader46,

            I suggest testing with a modified version of the SampleAtmStrategy example and then to implement the changes in your script when you know you have it working. I have attached test script and a video testing with an ATM with one target and 3 stops.

            orderName will be Target1/Stop1 if the Atm has 1 target and stop. If the Atm has more than one stop, we will have additional stop orders named Stop2, Stop3, etc.

            The Atm Strategy ID parameter is the AtmStrategyID that you make before creating the AtmStrategy with AtmStrategyCreate. Please see how the atmStrategyId is used in the SampleAtmStrategy example for further direction on how this can be created and used in your script.

            Demo - https://drive.google.com/file/d/1VG2...w?usp=drivesdk

            I look forward to being of further assistance.
            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              It works now, thank you.

              It must have been if (longAtmId.Length > 0) that I was missing

              Comment


                #8
                I got the code below working, and I added (IsRising(T31b2) == true) condition, hoping the Stops will not back-tick with the indicator they are tracing, but they still do.

                What I am trying to achieve on a long position is that, when I have the indicator T31b2[0] reach say 53.9, the stops to stay at that level even when T31b2[0] gets to 53.6, so moving only up in a long position and only down in a short position and I don't get why the code below doesn't do the job.

                Code:
                               if (longAtmId.Length > 0 && longOrderId.Length == 0)
                                {                                    
                                    if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat && (IsRising(T31b2) == true))                    
                                        AtmStrategyChangeStopTarget(0, T31b2[0], "Stop1", longAtmId);
                                    if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat && (IsRising(T31b2) == true))
                                        AtmStrategyChangeStopTarget(0, T31b2[0], "Stop2", longAtmId);
                                    if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat && (IsRising(T31b2) == true))
                                        AtmStrategyChangeStopTarget(0, T31b2[0], "Stop3", longAtmId);
                                    if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat && //(IsRising(T31b2) == true))
                                        AtmStrategyChangeStopTarget(High[0] + 5 * TickSize, 0, "Target3", longAtmId);
                                }
                ...And now I have these prints during live market data testing, which I didn't have so far:

                Short condition at: 56.34 - Current Bid: 56.34 - R = 1 - Open: 56.37 - Current Bar: 4755 @ 13/11/2019 09:22:00
                'GetAtmStrategyEntryOrderStatus' method error: Order ID '6e6b42171d4c417ea4b08f32d69ffa29' does not exist
                Short ATM triggered at: 56.34 Open: 56.37 - Current Bar: 4755 Short order bar = 4755 - 13/11/2019 09:22:00
                ##########
                Cond False Short Reset - Current Bid: 56.34 - Current Bar: 4755
                XX<<-- Short condition false price: 56.34 - Trigger Short Price: 56.34 - Current Bar: 4756 - 13/11/2019 09:23:00
                Cond False Short Reset - Current Bid: 56.32 - Current Bar: 4759
                XX<<-- Short condition false price: 56.32 - Trigger Short Price: 56.34 - Current Bar: 4760 - 13/11/2019 09:27:00
                <<< Short Order ID Reset Price: 56.3 - Open: 56.34 - Current Bar: 4764 - 13/11/2019 09:31:00
                'AtmStrategyChangeStopTarget' method error: Order name 'Stop1' is invalid
                'AtmStrategyChangeStopTarget' method error: Order name 'Stop2' is invalid
                'AtmStrategyChangeStopTarget' method error: Order name 'Stop3' is invalid
                'AtmStrategyChangeStopTarget' method error: Order name 'Target3' is invalid
                'AtmStrategyChangeStopTarget' method error: Order name 'Stop1' is invalid
                'AtmStrategyChangeStopTarget' method error: Order name 'Stop2' is invalid
                'AtmStrategyChangeStopTarget' method error: Order name 'Stop3' is invalid
                'AtmStrategyChangeStopTarget' method error: Order name 'Target3' is invalid
                Last edited by itrader46; 11-13-2019, 06:36 AM.

                Comment


                  #9
                  Hello itrader46,

                  Do you see those errors when testing your strategy with the SampleAtmStrategy strategy or against the test script I have provided? There may be some handling of the atmStrategyID or the orderID that is being overlooked where an old orderID or atmStrategyID is being used. If you do not see the issue with the example scripts, I would suggest checking your implementation, or copying the code used in the examples for the basis of your works.

                  You could also use a variable to keep track of where you updated the stop loss last. Whenever you update the stop loss, update your variable. If the indicator value is greater than your variable, then you can update the stop loss and update your variable to the new level.

                  Please let me know if I can be of further assistance.
                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    I have the same errors with your strategy as well. Check the prints out, please: it looked like the error comes when there is a part fill

                    Prints:

                    Enabling NinjaScript strategy 'SampleAtmTest/178977953' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=By strategy position ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=True CancelExitsOnStrategyDisable=True Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
                    The entry order average fill price is: 53.77
                    The entry order filled amount is: 1
                    The entry order order state is: PartFilled
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop2' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop3' is invalid
                    The current ATM Strategy market position is: Long
                    The current ATM Strategy position quantity is: 1
                    The current ATM Strategy average price is: 53.77
                    The current ATM Strategy Unrealized PnL is: -10.0000000000051
                    The entry order average fill price is: 53.77
                    The entry order filled amount is: 3
                    The entry order order state is: Filled
                    The current ATM Strategy market position is: Long
                    The current ATM Strategy position quantity is: 3
                    The current ATM Strategy average price is: 53.77
                    The current ATM Strategy Unrealized PnL is: -30.0000000000153
                    The current ATM Strategy market position is: Long
                    The current ATM Strategy position quantity is: 3
                    The current ATM Strategy average price is: 53.77
                    The current ATM Strategy Unrealized PnL is: -30.0000000000153

                    But these are my strategy prints... it's not the PartFilled state

                    Enabling NinjaScript strategy 'ATM2/178978024' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=True CancelExitsOnStrategyDisable=True Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes
                    Strategy start bar: 4996 - 14/11/2019 13:19:00
                    ......
                    Long condition at: 57.67 - Current Ask: 57.67 - S = 1 - Open: 57.66 - Current Bar: 5040 @ 14/11/2019 14:03:00
                    'GetAtmStrategyEntryOrderStatus' method error: Order ID '53662971920e40f2a014ae3e9a47e62d' does not exist
                    Long ATM triggered at: 57.67 - Open: 57.66 - Current Bar: 5040 - Long order bar: 5040 - 14/11/2019 14:03:00
                    ##########
                    Cond False Long Reset - Current Ask: 57.67 - Current Bar: 5040
                    The long entry order average fill price is: 57.7
                    The long entry order filled amount is: 3
                    The long entry order order state is: Filled
                    >>> Long Order ID Reset Price: 57.7, | Current Bar: 5040, | Time: 14/11/2019 14:03:00
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop1' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop2' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop3' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Target3' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop1' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop2' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop3' is invalid
                    ....

                    Here Stop1 seems to be OK for some reason

                    Short ATM triggered at: 57.52 Open: 57.56 - Current Bar: 5093 Short order bar = 5093 - 14/11/2019 14:56:00
                    ##########
                    Cond False Short Reset - Current Bid: 57.52 - Current Bar: 5093
                    The short entry order average fill price is: 57.51
                    The short entry order filled amount is: 3
                    The short entry order order state is: Filled
                    >>> Short Order ID Reset Price: 57.5, | Current Bar: 5093, | Time: 14/11/2019 14:56:00
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop2' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop3' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Target3' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop2' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Stop3' is invalid
                    'AtmStrategyChangeStopTarget' method error: Order name 'Target3' is invalid

                    My code is identical to yours:

                    Code:
                                    if (longAtmId.Length > 0 && longOrderId.Length == 0)
                                    {                                    
                                        if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat)                    
                                            AtmStrategyChangeStopTarget(0, T31b2[0], "Stop1", longAtmId);
                                        if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat)
                                            AtmStrategyChangeStopTarget(0, T31b2[0], "Stop2", longAtmId);
                                        if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat)
                                            AtmStrategyChangeStopTarget(0, T31b2[0], "Stop3", longAtmId);
                                        if (GetAtmStrategyMarketPosition(longAtmId) != MarketPosition.Flat)
                                            AtmStrategyChangeStopTarget(High[0] + (int) 10.0 * TickSize, 0, "Target3", longAtmId);
                    Last edited by itrader46; 11-14-2019, 09:00 AM.

                    Comment


                      #11
                      Hello itrader46,

                      Thanks for clarifying. I was able to reproduce by forcing partial fills and applying the strategy against a single tick data series.

                      The error is getting thrown from a race condition. The entry order fills, but the ATM's stops and targets are not yet submitted by the time these methods are called.

                      Since the ATM strategy is submitted as an independent strategy, the errors are not causing the strategy to abort and the ATM strategy is getting submitted with the appropriate targets and stops. We can infer that if we are calling these methods very soon after the ATM entry fills, we may not yet see the targets and stops and we will receive this error.

                      As the ATM strategy does operate as normal and we no longer see these errors after the target and stop are submitted, it would be safe to ignore the error in this context.

                      Please let us know if we can be of further assistance.
                      JimNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by bortz, 11-06-2023, 08:04 AM
                      47 responses
                      1,607 views
                      0 likes
                      Last Post aligator  
                      Started by jaybedreamin, Today, 05:56 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post jaybedreamin  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      6 responses
                      19 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by Jon17, Today, 04:33 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post Jon17
                      by Jon17
                       
                      Started by Javierw.ok, Today, 04:12 PM
                      0 responses
                      15 views
                      0 likes
                      Last Post Javierw.ok  
                      Working...
                      X