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

Stoploss and profit targets set using swing indicator not functioning properly

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

    Stoploss and profit targets set using swing indicator not functioning properly

    In an old thread I saw that the following code could be used to set stoploss and profit target based on the value of the swing indicator, however, when I tried implementing this into my custom strategy it resulted in the majority of the trades being exited at breakeven or simply at another incorrect price level. Any help or suggestions will be greatly appreciated!

    Code:
    if(Position.MarketPosition == MarketPosition.Long)
    {
    SetStopLoss(CalculationMode.Price, Swing(2).SwingLow[0]);
    SetProfitTarget(CalculationMode.Price, (Position.AveragePrice + (Position.AveragePrice - Swing(2).SwingLow[0])));
    }

    #2
    Hello PacifistZealot,

    Thanks for your post.

    Set methods prep NinjaTrader to submit protective orders upon the execution of an entry order. Set methods should be called prior to calling the entry order method in the script.

    For example:

    if (condition)
    {
    SetStopLoss();
    SetProfitTarget();
    EnterLong();
    }

    From the SetStopLoss/SetProfitTarget help guide pages: "Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set."

    Also from the help guide: "You may call this method from within the strategy OnBarUpdate() method should you wish to dynamically change the stop loss price while in an open position

    Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position​"


    Here is an example script demonstrating dynamically calling Set methods in OnBarUpdate(): https://ninjatrader.com/support/help...of_stop_lo.htm

    You should also add debugging prints (outside of the condition to place orders) that print out the price you are submitting the orders to. This is so you can confirm the price you are passing into the order methods. Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the help, my stop losses and targets are now being set to the correct price levels. Currently I am calling this method from the strategy OnBarUpdate() method, which is causing the stop loss and target to dynamically update as the swing indicator updates. However, I do not want this to happen, and I want the stop loss and target to remain at the price set by the swing indicator when the trade is first entered. To prevent the prices from updating will I have to call the orders outside of the OnBarUpdate method or is there some other way to do that? Thank you again!

      Comment


        #4
        Hello PacifistZealot,

        Thanks for your notes.

        You could try using a bool to control when trades are made.

        For example, you could create a bool variable named something like "OkToTrade", create a condition that checks if Position.MarketPosition == MarketPosition.Flat, set the bool to true within that condition. Then, check if that bool is true within your condition to place the Entry order method and SetStopLoss/SetProfitTarget methods, and flip the bool to false after calling those methods.

        if (condition && OkToTrade)
        {
        SetStopLoss();
        SetProfitTarget();
        EnterLong();
        OkToTrade = false;
        }

        By doing this, the Entry method and Set methods will only be submitted when the bool is true. After the trades are submitted, the bool is set to false so that the order methods aren't called again. Then, once you are in a flat market position the bool is reset to true so orders can be placed again.

        Ultimately, it will be up to you to come up with the specific logic to accomplish your overall goals.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Haiasi, 04-25-2024, 06:53 PM
        2 responses
        16 views
        0 likes
        Last Post Massinisa  
        Started by Creamers, Today, 05:32 AM
        0 responses
        4 views
        0 likes
        Last Post Creamers  
        Started by Segwin, 05-07-2018, 02:15 PM
        12 responses
        1,786 views
        0 likes
        Last Post Leafcutter  
        Started by poplagelu, Today, 05:00 AM
        0 responses
        3 views
        0 likes
        Last Post poplagelu  
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,407 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Working...
        X