Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need Help - Stop Loss error.

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

    Need Help - Stop Loss error.

    Hallo.
    I'm new Ninja user. I alredy developed my simple strategy and strategy works well until I tryed to modify stop loss price to breakeven. I used the sample shown here: http://www.ninjatrader-support.com/v...ead.php?t=3222

    Now strategy when moves stop loss to breakeven and then reverse postition do not reset stop loss to original position and place stop order with the price from former turn. This price is over the market price what is case of error and strategy termination.
    Can anybody help me to explan where the problem is???

    Strategy has condition:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, stoplossticks);
    }
    but looks it does not work????

    Rgds
    Czarek

    My strategy below:
    //
    // Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Strategy;
    #endregion
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Sample strategy using StopLoss and ProfitTarget orders.
    /// </summary>
    [Description("Sample strategy using StopLoss and ProfitTarget orders.")]
    public class SamplePriceModification : Strategy
    {
    #region Variables
    private int stoplossticks = 20;
    private int profittargetticks = 100;
    #endregion
    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    /* There are several ways you can use SetStopLoss and SetProfitTarget. You can have them set to a currency value
    or some sort of calculation mode. Calculation modes available are by percent, price, and ticks. SetStopLoss and
    SetProfitTarget will submit real working orders unless you decide to simulate the orders. */
    SetStopLoss(CalculationMode.Ticks, stoplossticks);
    SetProfitTarget(CalculationMode.Ticks, profittargetticks);

    CalculateOnBarClose = true;
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, stoplossticks);
    }

    // If a long position is open, allow for stop loss modification to breakeven
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
    if (Close[0] > Position.AvgPrice + 50 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    }
    }

    // Condition set 1
    if (Close[1] < Close[0]
    && Close[2] < Close[0]
    && Close[3] < Close[0]
    && Close[4] < Close[0]
    && Close[5] < Close[0]
    && Close[6] < Close[0]
    && Close[7] < Close[0]
    && Close[8] < Close[0]
    && Close[9] < Close[0]
    && Close[10] < Close[0]
    && Open[0] < Close[0]
    && Open[1] < Close[0]
    && Open[2] < Close[0]
    && Open[3] < Close[0]
    && Open[4] < Close[0]
    && Open[5] < Close[0]
    && Open[6] < Close[0]
    && Open[7] < Close[0]
    && Open[8] < Close[0]
    && Open[9] < Close[0]
    && Open[10] < Close[0])
    {
    EnterLong(1, "");
    }
    // Condition set 2
    if (Close[1] > Close[0]
    && Close[2] > Close[0]
    && Close[3] > Close[0]
    && Close[4] > Close[0]
    && Close[5] > Close[0]
    && Close[6] > Close[0]
    && Close[7] > Close[0]
    && Close[8] > Close[0]
    && Close[9] > Close[0]
    && Close[10] > Close[0]
    && Open[0] > Close[0]
    && Open[1] > Close[0]
    && Open[2] > Close[0]
    && Open[3] > Close[0]
    && Open[4] > Close[0]
    && Open[5] > Close[0]
    && Open[6] > Close[0]
    && Open[7] > Close[0]
    && Open[8] > Close[0]
    && Open[9] > Close[0]
    && Open[10] > Close[0])


    {
    EnterShort(1, "");

    }
    }
    #region Properties
    /// <summary>
    /// </summary>
    [Description("Numbers of ticks away from entry price for the Stop Loss order")]
    [Category("Parameters")]
    public int StopLossTicks
    {
    get { return stoplossticks; }
    set { stoplossticks = Math.Max(0, value); }
    }

    /// <summary>
    /// </summary>
    [Description("Number of ticks away from entry price for the Profit Target order")]
    [Category("Parameters")]
    public int ProfitTargetTicks
    {
    get { return profittargetticks; }
    set { profittargetticks = Math.Max(0, value); }
    }


    #endregion
    }
    }

    #2
    First you would need to address the issue outlined here: http://www.ninjatrader-support.com/v...ead.php?t=3170
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh.
      I checked link you gave me but looks like this problem appear becouse of other reason.
      The exact messages I got when the strategy reverse is:
      Strategy " NQLimit" submitted and order that generate thde gollowing error 'OrderRejected'. Strategy has sendt cancel requests, attempted to close position and treminated itself.

      Sell stop or sell stop limit orders can't be placed above the market. Affected Irder: Sell 1 Stop @320.

      Can I have some more comments where this problem exist and how to fix it??
      I do not know why strategy do not reetset stop loss price according avg price??
      Maybe I can get suggestion how to programme stop in the other way?
      Any helf will be really appreciated.
      Rgds
      Czarek.

      Comment


        #4
        The problem is you are trying to reset it when the strategy becomes flat. This is all fine if your strategy is exited via a stop loss to begin with, but in reality what could happen for you is you never go flat. You have a long position and then you have a reversal condition so you are reversed in the market directly instead of going into a flat state first. That means you are skipping your reset. What you could do is just submit a reset along with your EnterShort().

        So above your EnterShort() line you would add the SetStopLoss() reset code. You would also probably want to tend to the reverse case of EnterLong() reversing EnterShort()
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh.
          Thanks for help, it fixed error problem but other problem would appeared.
          When I add SetStopLoss(CalculationMode.Ticks, stoplossticks); to the EnterLong() and EnterShort() Like this:
          {
          EnterShort(1, "");
          SetStopLoss(CalculationMode.Ticks, stoplossticks);
          }
          Strategy do not show any error but after move stop loss to breakeven and if the market continue same direction strategy change stop loss from original position to breakeven on each bar close. One bar keep original stop loss price next one breakeven another one back stop loss to original and so on.
          So depeand after whih bar market turn you will get stop loss on breakeven or original price.

          When CalculateOnBarClose is false - change stop loss on each tick (very quickly).

          Can I have any promt how to fix it??
          Rgds
          Czarek

          Comment


            #6
            Czarek,

            You need to debug your strategy. The only reason it will keep resubmitting is if it keeps evaluating your condition to true. Whatever your strategy does is a direct translation of what the code is. Please check out this tip: http://www.ninjatrader-support.com/v...ead.php?t=3418

            You may want to try using a flag variable to limit your trade condition. If you already submitted the entry order once make it stop submitting.

            Also, you want to reset your stop loss before you submit an entry order, not after. Reverse the lines.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Josh.
              Thanks for help.
              The only problem I have now is that I can not limit my trade condition to stop submitting StopLoss.
              Can you provide me code snippet whih limit submitting order ot link to such snippet??
              If I got such snippet I hope I could be able to resolve my stategy problem.
              Rgds
              Czarek

              Comment


                #8
                Try using flags. Create bool variables. Say enteredLong and enteredShort.

                In the Variables region of your code:
                Code:
                private bool enteredLong = false;
                private bool enteredShort = false;
                Then go into your trade conditions.
                Code:
                if (Close[0] > Open[0]... blah blah... && enteredLong == false)
                {
                     SetStopLoss(...);
                     EnterLong();
                     enteredLong = true;
                     enteredShort = false;
                }
                
                if (Close[0] < Open[0]... blah blah... && enteredShort == false)
                {
                    SetStopLoss(...);
                    EnterShort();
                    enteredLong = false;
                    enteredShort = true;
                }
                This is pseudo-code that has not been tested, but it gives you a jumping off point.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Josh.
                  Thanks a lot for your help. I adopted you code to my strategy at it is works, but sometimes ignore trade signal. Maybe I did something wrong I will check it out again.
                  I also find other solution:
                  && Position.MarketPosition != MarketPosition.Long)
                  {
                  SetStopLoss(CalculationMode.Ticks, stop);
                  EnterLong(1, "");

                  and it working well.
                  I'm step forward now but the other problem will appeared and I still need you help.
                  With both solutions strategy allow only 1 enter per direction. Strategy do not allow 2 or more entries. Can you advise me how to enable more entries???
                  Rgds
                  Wesley

                  Comment


                    #10
                    Sure. Please check this post out. http://www.ninjatrader-support.com/v...06&postcount=5

                    You are interested in the EntriesPerDirection one. For more info search the Help Guide for that term.
                    Josh P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    637 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    366 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    107 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    569 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    571 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X