Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to make stop activate when order is filled?

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

    How to make stop activate when order is filled?

    I am trying to make a strategy using the strategy builder where I enter trades using stop market orders. I would like to have a stop order for protection when my entry order is filled, but when I set it and try to run the strategy it gives me the error "Error on calling 'OnStateChange' method." Do I have to change something in the script?? thanks

    #2
    Welcome to the forums behindthewheel!

    We typically would not expect the Strategy Builder to generate code that results in run time errors like this. I would like to take a look at a small strategy that only contains the logic to encounter this error.

    Could you create a copy of the strategy and then test the strategy after removing sections of the strategy logic until we are left with what is needed to reproduce this error? Copies of Strategy Builder strategies can be made from the Welcome page of the Strategy Builder, placing your mouse over the strategy and then clicking Save As.

    Once we are left with a barebones strategy that reproduces this error, please attach it to this thread. Instructions for exporting indicators in source code format is included below.



    I look forward to hearing back.
    JimNinjaTrader Customer Service

    Comment


      #3
      hi Jim

      All works fine until I change my stop settings. If I set my stop to place a certain number of ticks from my entry point it works. But when I set my stop to be from a point on the current bar, say the high + 1 tick it does not work.

      My strategy works on a tick by tick basis, while a bar is being formed. Maybe this is why it is confused? I made sure to set calculate to on each tick.

      I separated long and short orders into two different strategies as well to make sure that wasn't why it was confused either,but the error still happens.

      SetStopLoss("", CalculationMode.Ticks, (High[0] + (1 * TickSize)) , false);

      this would be the correct code to place the stop loss from the high of the current bar that is being formed plus one tick right?

      also would you recommend any resource for learning how to understand ninjascript

      thank you very much for your help

      Comment


        #4
        Hello, behindthewheel,

        Thanks for your reply.

        The stops and targets section adds code in State.Configure to set stop losses and profit targets. As this is processed in OnStateChange before data is processed in OnBarUpdate where the strategy logic resides, the Stops and Targets will not be able to reference these prices.

        You could design logic in Conditions and Actions to use Exit methods instead, or you can follow the example below for using SetStopLoss in OnBarUpdate.

        Exiting a Market Position in Conditions and Actions - https://ninjatrader.com/support/help...MarketPosition

        Modifying Stop Loss and Profit Targets - https://ninjatrader.com/support/help...of_stop_lo.htm

        As for learning NinjaScript, I suggest using the Strategy Builder to create simple logic and click View Code to see the resulting syntax. From there, I suggest reviewing our NinjaScript Editor 401 course which helps to gets further involved with unlocked strategies.

        NinjaScript Editor 401 - https://www.youtube.com/watch?v=BA0W4ECyVdc

        Next, I would recommend reviewing our samples and educational resources in the Help Guide.

        Educational Resources - https://ninjatrader.com/support/help..._resources.htm

        Reference Samples - https://ninjatrader.com/support/help...ce_samples.htm

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by terofs, Today, 04:18 PM
        0 responses
        0 views
        0 likes
        Last Post terofs
        by terofs
         
        Started by nandhumca, Today, 03:41 PM
        0 responses
        4 views
        0 likes
        Last Post nandhumca  
        Started by The_Sec, Today, 03:37 PM
        0 responses
        3 views
        0 likes
        Last Post The_Sec
        by The_Sec
         
        Started by GwFutures1988, Today, 02:48 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by ScottWalsh, 04-16-2024, 04:29 PM
        6 responses
        33 views
        0 likes
        Last Post ScottWalsh  
        Working...
        X