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 is not static

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

    StopLoss is not static

    Hello,

    I have created a strategy, that trades the breakout from a certain range (I use two variables: RangeMax, RangeMin).

    For example, I trade a long position by stop order, if the market breaks over "RangeMax", the StopLoss is at "RangeMin", and the profit target is as far as the Stoploss.

    It works, as long as the strategy doesn`t find another Range (new "RangeMax" and "RangeMin") during an open trade.

    In this case, the strategy changes StopLoss from the first "RangeMin" to the new "RangeMin".

    I`ve tried the following code:

    if (.....)
    {
    EnterLongStopMarket(0, true, KontraktzahlLong, RangeMax, @"Long");
    SetStopLoss(@"Long", CalculationMode.Price, RangeMin, false);
    SetProfitTarget(CalculationMode.Ticks, RangeTicks);
    }

    I`ve tried to write

    SetStopLoss(@"Long", CalculationMode.Price, RangeMin, false);

    in the OnStateChange() method (when the current market position is long or short). After compiling I couldn`t find the strategy in Strategy Analyzer.

    Unfortunately I`m not a programmer (learning by doing), but I hope we will find a solution.

    Best regards




    #2
    Hello Heikoman,

    Thanks for your post.

    Set methods prep NinjaTrader to submit target and stop upon the execution of the associated entry order. As such, we should ensure that the Set methods are called before the entry order is submitted.

    To prevent the code from calling the Set methods again once you are in a position, you can check if the Strategy Position is flat in addition to your other entry conditions.

    Position.MarketPosition - https://ninjatrader.com/support/help...etposition.htm

    If the strategy is not listi89ng in the Strategy Analyzer, please check the log tab of the Control Center for any errors. You may then use debugging prints to find the exact line that is throwing an error after opening the Strategy Analyzer and trying to select the strategy. If you have questions on the error, please include the error from the Log tab of the Control Center (you can use Ctrl + C to copy the error) as well as the line of code that generates the error.

    Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

    If you do not see errors, please confirm that you are looking for the strategy that has the same Name property in OnStateChange under if (State == State.SetDefaults).

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      thank you very much, I`ve added

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

      to my trading conditions, so that trading (and setting orders) is just allowed, if there is no open position, and it works great!

      Moreover, in the backtest I´ve seen, that many trades are ignored. Although all conditions are complied, there is no trade when the market breaks out of the range.

      Is there any correlation between this behaviour and the new code line?

      Best regards

      Comment


        #4
        Hello Heikoman,

        Thanks for your question.

        If your strategy was scaling into new positions, the position check would prevent those additional entries, as the entry condition now requires that you are flat before entering again.

        If you are trying to understand if the code is reaching the order submission methods, debugging prints should be used to confirm the order method is reached. If it is not reached, prints should be used outside of the condition so you can monitor why the parts of the condition are not allowing the action to be taken

        If you see that the order method is reached, but the order is ignored, please test again using TraceOrders and be sure to reference the internal rules of the Managed Approach.

        TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm

        Managed Approach (Internal Rules) - https://ninjatrader.com/support/help...antedPositions

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim,

          thanks for your post.

          I found out, that the orders are ignored, because an active entry order is waiting in the opposite direction.

          Moreover, in Managed Approach I`ve read, that this behaviour is "pre-installed" in Ninjatrader. Is there a way to avoid this?

          Another idea (or maybe in combination) is just to cancel the entry order, when a certain price is reached. I can determine the price, but is possible to cancel an order in this way?

          Best regards

          Comment


            #6
            Hello Heikoman,

            You can use the Unmanaged Approach to work outside the internal rules of the Managed Approach. I have attached an example that demonstrates bracketed entries with the Unmanaged Approach.

            Unmanaged Approach - https://ninjatrader.com/support/help...d_approach.htm

            The example models the behaviors described in the SampleOnOrderUpdate strategy. This is an advanced approach, and I recommend becoming familiar with Order object handling and the usage of OnOrderUpdate and OnExecutionUpdate that is demonstrated in SampleOnOrderUpdate before reviewing this UnmanagedTemplate strategy.

            SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

            If you would like to be able to cancel orders programmatically, this is also possible with the Managed Approach when Order objects are tracked for the order, and calling CancelOrder to cancel the order. An example is included below.

            SampleCancelOrder - https://ninjatrader.com/support/help...thod_to_ca.htm

            Let me know if there is anything else we can do to help.
            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              Hello Jim,

              thanks for your post!

              First of all, I`ve tried to cancel orders, if the market breaks out into the "wrong direction" and added the following to my trading conditions (long trades):

              else if (entryOrder != null && Low[0] < RangeMin)
              {
              CancelOrder(entryOrder);
              }

              Of course, in "public class : strategy", I´ve created a new Order:

              private Order entryOrder = null;

              and under "if (State == State.SetDefaults)" the following:

              else if (State == State.Realtime)
              {
              if (entryOrder != null)
              {
              entryOrder = GetRealtimeOrder(entryOrder);
              }
              }

              Compiling was successful, but it doesn`t work in backtests. So I decided to set Order to null, as shown in the sample (long and short trades):

              protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState,
              DateTime time, ErrorCode error, string comment)
              {
              if(order.Name == "Long")

              entryOrder = order;

              if (entryOrder != null && entryOrder == order)
              {
              if(entryOrder.OrderState == order.State.Cancelled)
              {
              entryOrder = null;
              }
              }
              if(order.Name == "Short")

              entryOrder = order;

              if (entryOrder != null && entryOrder == order)
              {
              if(entryOrder.OrderState == order.State.Cancelled)
              {
              entryOrder = null;
              }
              }
              }

              After this step, it is not possible to compile (something wrong with Order.State)
              Sorry for such a long question, but all my ideas to find a mistake didn`t work. Hope you can find something wrong in my strategy.
              Best Regards

              Comment


                #8
                Hello Heikoman,

                If the code cannot compile, the syntax would be incorrect. Please note exactly how the sample code checks for a cancelled order. You may observe that OrderState.Cancelled is used when order.state.Cancelled would be incorrect.

                I.E.

                if (order.OrderState == OrderState.Cancelled)

                where "order" is the Order object that is passed by parameter from OnOrderUpdate, order.OrderState is the order state of that order, and OrderState.Cancelled is the OrderState value that means cancelled.

                We look forward to assisting.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jim,

                  I can`t believe it, that was the point, compiling was successful.

                  Thank you very much!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Caspersky_, 06-18-2024, 12:22 PM
                  9 responses
                  39 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by Fran888, Today, 11:14 AM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by lcgamarra_pinnacle, 06-15-2024, 09:33 AM
                  8 responses
                  95 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by AH369, Today, 11:09 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Started by wanderingjade, Yesterday, 08:21 AM
                  2 responses
                  28 views
                  0 likes
                  Last Post Tkamp
                  by Tkamp
                   
                  Working...
                  X