Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop loss issue and trade execution issue

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

    Stop loss issue and trade execution issue

    Hi,

    I'm trying to develop a quite simple strategy but I have to program the stop loss by myself as I would like it to be set at the low of the signal bar which is the previous of the entry one.

    When I first set the stop to a defined number of ticks, the strategy was running right but if I replace the "setstoploss" line in the code, I don't have trade execution any more. In practical terms, I replaced this line:

    SetStopLoss("", CalculationMode.Ticks, 5, false);

    by this one:

    SetStopLoss("", CalculationMode.Price, Low[1]-1*TickSize, false);

    Could you please help me on this?

    Thanks,

    Raph

    #2
    Hello Raph,
    Thanks for your note.

    If you try using the below code then are you able to get the correct price.

    Code:
    if (Position.MarketPosition == MarketPosition.Long)
    {
    	this.SetStopLoss(CalculationMode.Price, Low[BarsSinceEntry() - 1]);
    }
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thanks,

      But I replaced it and I still have zero trades...

      This is the code:

      Code:
              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
      			if (Position.MarketPosition == MarketPosition.Long)
      {
      	this.SetStopLoss(CalculationMode.Price, Low[BarsSinceEntry() - 1]);
      }
                  SetProfitTarget("", CalculationMode.Ticks, 3);
      
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Condition set 1
                  if (KeyReversalUp(5)[0] > KeyReversalUp(5)[1]
                      && Close[1] <= Close[2]
                      && Close[2] <= Close[3])
                  {
                      EnterLong(2, "KeyRevUp");
                  }
      		
              }
      Meanwhile the exactly same code with a 5 ticks stop loss made 801 trades on the past 800 periods.

      Comment


        #4
        Hello rdaune,
        You need to apply the code in the OnBarUpdate section and not in the Initialize section of the code.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Thanks a lot. It is working much better now. (and BTW, sorry for my lack of knowledge but I'm a brand new beginner in programming...)

          But there is still one more thing I don't understand. In the code you gave me, the "Low" is on the "entry Candle" and I would like it based on the "Signal Candle" which is always the one just before the entry bar. I can't figure out how to do that with the BarsSinceEntry condition...

          Comment


            #6
            Hello
            My apologies, it should be plus one and not minus one. Please use the rectified code instead.

            Code:
            if (Position.MarketPosition == MarketPosition.Long)
            {
            	this.SetStopLoss(CalculationMode.Price, Low[BarsSinceEntry() [B]+[/B] 1]);
            }
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              This is still liquidating the position instantaneously because it calculate the low on the wrong bar... Here is a screenshot of the chart and the code:

              Code:
                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize()
                      {
              			
                          SetProfitTarget("", CalculationMode.Ticks, 8);
              
                          CalculateOnBarClose = true;
                      }
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                          // Condition set 1
                          if (KeyReversalUp(5)[0] > KeyReversalUp(5)[1]
                              && Close[1] <= Close[2]
                              && Close[2] <= Close[3])
                          {
                              EnterLong(2, "KeyRevUp");
                          }
              		
              			if (Position.MarketPosition == MarketPosition.Long)
              			{
              				this.SetStopLoss(CalculationMode.Price, Low[BarsSinceEntry()+1]);
              			}
                      }
              Attached Files

              Comment


                #8
                Hello rdaune,
                Seems you are not resetting the stop after each entry as the stop might be still referenced to the old price.

                If you try the below code then are you able to get the correct price.
                Attached Files
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Just a mere stupid question: I would like to add a condition to my strategy which is:

                  Close[1] - Open[3] >= 10 ticks

                  How can I do and where can I find a list of the basic operations in programming language?

                  Thanks,

                  Comment


                    #10
                    Hello rdaune,
                    You can use the TickSize property to do it.

                    A sample code will be like
                    Code:
                    if ((int)(Math.Abs(Close[1] - Open[3]) / TickSize) > 10)
                    {
                       //do something
                    }
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Hi,

                      Actually, I would like to put my stop 1 or 2 ticks lower than the previous bar low but in my code, the CalculationMode is in Price...

                      Code:
                      if (Position.MarketPosition == MarketPosition.Long)
                      			{
                      				SetStopLoss(CalculationMode.Price, Low[this.BarsSinceEntry()+ 1]);
                      So, if the previous bar low is 142.83, I would like my stop to be at 142.82... How can I do?

                      Thanks

                      Comment


                        #12
                        Hello rdaune,
                        You can subtract a tick from the stop loss price to do it. A sample code will be like:
                        Code:
                        SetStopLoss(CalculationMode.Price, Low[this.BarsSinceEntry()+ 1] - TickSize);
                        JoydeepNinjaTrader Customer Service

                        Comment


                          #13
                          Hi,

                          I have a problem when trying to edit Ninja script. It always opens the same code even if I choose to open another strategy.

                          I created 1 strategy and re-named it twice for different versions with unlocked code but cannot see these codes anymore meanwhile they have different names...

                          What did I do wrong?

                          Comment


                            #14
                            Hello rdaune,
                            To assist you further may I know how did you renamed the strategy.

                            To create multiple instance of the strategy please follow the below steps
                            • In Control Center menu bar goto Tools>Edit NinjaScirpt>Strategies..
                            • In the Strategies dialog select the strategy you want to rename and click Ok
                            • In the strategy wizard dialog click on the View code button
                            • In the NinjaScript editor, right click on it
                            • In the context menu click on Save As
                            • In the Save as dialog give a new name and click Ok


                            Now you will have a new instance of the strategy.
                            JoydeepNinjaTrader Customer Service

                            Comment


                              #15
                              Hi,

                              I'm trying to set several Profit targets by entering 2 "EnterLong" orders but it only enters the first one...

                              Where is the problem?

                              Code:
                                      /// <summary>
                                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                                      /// </summary>
                                      protected override void Initialize()
                                      {
                                         EntriesPerDirection = 1;
                                         EntryHandling = EntryHandling.UniqueEntries;
                              	   SetProfitTarget("Long1", CalculationMode.Ticks, 20);
                              	   SetProfitTarget("Long2", CalculationMode.Ticks, 10);
                                         SetStopLoss("", CalculationMode.Ticks, 15, false);
                              
                                          CalculateOnBarClose = true;
                                      }
                              
                                      /// <summary>
                                      /// Called on each bar update event (incoming tick)
                                      /// </summary>
                                      protected override void OnBarUpdate()
                                      {
                                          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 + 2 * TickSize)
                              				{
                              					SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                              				}
                              			}
                              			// Condition set 1
                                          if (Open[0] < Close[0]
                                              && VOL()[0] > 4500
                              		&& VOL()[1] < 4000
                              		&& Close[0] > EMA(14)[1]
                              		&& Close[0] - Open[0] > 0.55*(High[0]-Low[0])
                              		&&(ToTime(Time[0]) >= 100000 
                              		&& ToTime(Time[0]) < 192500))
                                          {
                                              EnterLong("Long1");
                              		EnterLong("Long2");
                                          }
                              
                                      }
                              Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              647 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              369 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X