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 take profit sometimes wrong

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

    Stoploss and take profit sometimes wrong

    Dear Support-Team,

    I've created a strategy that uses ATR to set stoploss and take profit. In backtests, most trades are executed correctly, but sometimes it doesn't work.

    I made a screenshot of a trade (short) to show you the problems (blue horizontal lines):
    1. Stoploss (1,08822) was triggered but not executed
    2. take profit (1,07922) is calculated wrong

    To determine stoploss and take profit, I use the following code, written under "OnBarUpdate":

    double profitTargetPriceShort = Close[0]-(ATR(ATRperiode)[0]*CRV*2);
    double stopLossPriceShort = Close[0]+(ATR(ATRperiode)[0]*2);​

    if (...my conditions...)

    {
    EnterShortLimit(10000, Close[0], "");
    SetProfitTarget(CalculationMode.Price, profitTargetPriceShort);
    SetStopLoss(CalculationMode.Price, stopLossPriceShort);​
    }

    "CRV" is just a multiplier to test different risk ratios, "ATRperiode" is a variable to test different periods of ATR indicator.

    Hope you can help me,

    Best Regards​
    Attached Files

    #2
    Hello Heikoman,

    As you are using Set methods, which cannot be unset, it very important that you call the Set methods before calling the entry method. (Otherwise it will be using the values from the previous trade)


    If this does not correct the issue, enable TraceOrders and add prints to understand the behavior.

    Below is a link to a forum post on using TraceOrders and prints to understand behavior and includes videos.


    After enabling TraceOrders print the time of the bar and the calculated value you are supplying to the set method.
    In OnOrderUpdate() print the order.ToString() object (so we can see when orders are being submitted, become working, and become filled or cancelled)

    Save the output to a text file and include this with your next post.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      thank you for your post!

      First, I changed the lines in the program of set methods and entry method, so that set methods are called before entry method. Unfortunately it didn't help.

      Before I tried your next idea (TraceOrders...), I started another backtest and found the problem with the Output Window.

      While the entry order is open, there are two more entry signals (all EMAs on top of each other, RSI crosses over 70).

      So, Ninjatrader calculated the prices for Stoploss and take profit new with each entry signal.

      Is it possible to avoid this new calculation, while an order is open?

      Best Regards

      Comment


        #4
        Hello Heikoman,

        Are the Set methods and Entry method only being called when the position is flat?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,

          Entry method is not called but both Set methods.

          I thought the line "StartBehavior = StartBehavior.WaitUntilFlat;" under "protected override void OnStateChange()" excludes this behaviour, but it doesn't.

          Comment


            #6
            Hello Heikoman,

            No the Wait until flat start behavior would not be related to this.

            The Start Behavior options are used for transitioning a strategy from historical to real-time and what to do with any open positions.

            Below is a link to a forum post that includes a video.



            The Set methods should be called with CalculationMode.Ticks one line above where the entry is submitted. After the stop and target are working you can change these to using CalcuationMode.Price with a custom price.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello Chelsea,

              meanwhile I found another solution for the problem. I added:

              && (Position.MarketPosition == MarketPosition.Flat))

              to my entry conditions. So stoploss and profit target will just be calculated new, if the position is flat.

              Thank you very much for your support!

              Best Regards

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by futtrader, 04-21-2024, 01:50 AM
              4 responses
              41 views
              0 likes
              Last Post futtrader  
              Started by Option Whisperer, Today, 09:55 AM
              1 response
              11 views
              0 likes
              Last Post bltdavid  
              Started by port119, Today, 02:43 PM
              0 responses
              1 view
              0 likes
              Last Post port119
              by port119
               
              Started by Philippe56140, Today, 02:35 PM
              0 responses
              3 views
              0 likes
              Last Post Philippe56140  
              Started by 00nevest, Today, 02:27 PM
              0 responses
              2 views
              0 likes
              Last Post 00nevest  
              Working...
              X