• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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.

    https://ninjatrader.com/support/help...t8/?export.htm

    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 qewcool, Today, 10:51 PM
        0 responses
        1 view
        0 likes
        Last Post qewcool
        by qewcool
         
        Started by momchi, Today, 10:09 PM
        0 responses
        2 views
        0 likes
        Last Post momchi
        by momchi
         
        Started by crokusek, Today, 09:56 PM
        0 responses
        2 views
        0 likes
        Last Post crokusek  
        Started by crokusek, Today, 09:43 PM
        0 responses
        2 views
        0 likes
        Last Post crokusek  
        Started by Gregory Arkadin, Today, 03:31 PM
        0 responses
        14 views
        0 likes
        Last Post Gregory Arkadin  
        Working...
        X