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

Setting Stop Loss and Take Profit

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

    Setting Stop Loss and Take Profit

    I am developing a Bollinger Band strategy. I am trying to get the stop loss and take profit orders set correctly. I started in Strategy Builder and have it working pretty well. But I ran into a hiccup. I have the stop loss set at the end of the candle that signals the trade. I do that by creating a variable for a long stop loss and a variable for a short stop loss. I set those variables in Set 1 of Conditions and Actions. I then call those variables in later sets where I set the stop loss and take profit orders after the entry order is triggered. My problem is that the stop loss does not set immediately after the order is placed. It waits for the next candle to close. If the next candle reverses hard and closes beyond my stop loss level, then the stop loss order does not get set and leads to huge losses. I will try to explain to make this clearer:

    Here is how I built it in Strategy Builder:
    Calculate on bar close
    Created variables for long stop loss and short stop loss
    Conditions and Actions Set 1: market position = flat. set the long stop loss as the low of current candle. Set short stop loss as high of current candle.
    Set 2: Long entry conditions creating long entry order
    Set 3: Market position = long. Create stop loss order using long stop loss variable for price. Create take profit order using middle bollinger band as target.
    Set 4: Short entry conditions creating short entry order
    Set 5: Market position = short. Create stop loss order using short stop loss variable for price. Create take profit order using middle bollinger band as target.

    This generally works well, except for the situation identified above. For example, a long order is set to enter at 1.02. That order gets filled early in next candle. Stop loss order should be set at 1.01, but isn't set until the next candle closes. The candle reverses hard and closes at 1.005 preventing a stop loss order from being set.
    The solution to this would be for stop loss and take profit to be set immediately after the entry order fills. I have tried to use the Stops and Targets section of Strategy Builder for this (which does set them immediately), but that did not work either.

    Does anyone know how I can fix this? I imagine it is probably a pretty simple fix, but I just can't figure it out. I expect that I will probably need to make a manual change to the code. I am not an experienced coder, but do have some working knowledge of ninjascript.

    Any help would be greatly appreciated!

    #2
    Hello bdrex95,

    "My problem is that the stop loss does not set immediately after the order is placed."

    When using Exit order methods in the conditions and actions pages, this would be expected using the Strategy Builder as the entry has to fill and change the position and then a new bar must update after the position has changed.

    If you would like the stop order and limit order submitted immediately you would need to unlock the script and submit these orders from OnExecutionUpdate() which will run the exact moment the entry fills.

    Below is a link to the ProfitCasestopTrailExitOrdersExample example which demonstrates.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by TAJTrades, Today, 09:46 AM
    0 responses
    1 view
    0 likes
    Last Post TAJTrades  
    Started by rhyminkevin, Yesterday, 04:58 PM
    5 responses
    62 views
    0 likes
    Last Post dp8282
    by dp8282
     
    Started by realblubb, Today, 09:28 AM
    0 responses
    4 views
    0 likes
    Last Post realblubb  
    Started by AaronKoRn, Yesterday, 09:49 PM
    1 response
    18 views
    0 likes
    Last Post Rikazkhan007  
    Started by ageeholdings, Today, 07:43 AM
    0 responses
    12 views
    0 likes
    Last Post ageeholdings  
    Working...
    X