Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to program StopLoss and Profit Trailing

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

    #16
    Hello njmeyer713,

    Thank you for your reply.

    You could use LowestBar to find the bar with the lowest low since the TrailBar:

    // store the lowest bar ago value
    int lowestBar = LowestBar(Low, CurrentBar - TrailBar);

    //evaluate low price from lowest bar ago value
    double lowestPrice = Low[lowestBar];

    You could then use the lowestPrice when setting the trailing stop.



    Please let us know if we may be of further assistance to you.

    Comment


      #17
      Thank you Kate that was a big help-

      I was overconfident in my coding skills.

      How would I set TrailBar as the bar the trailing stop was first submitted?

      Here is what I have so far but the trailing stop is resetting on a new bar and moved backwards.

      Code:
       if (Position.MarketPosition == MarketPosition.Short
      && (GetCurrentBid(0) + (50 * TickSize)) <= Position.AveragePrice
      && (Position.Quantity ==1))
      
      
      
      {
      
      TrailBar = CurrentBar;
      int lowestBar = LowestBar(Low, CurrentBar - TrailBar);
      double lowestPrice = Low[lowestBar];
      FifteenTrailingStop = ExitShortStopMarket(0, true, Convert.ToInt32(Position.Quantity), lowestPrice + (25 * TickSize), @"15TrailingStop1", @"15Short2U-2D");
      FifteenTrailReached = true;
      CancelOrder(FifteenRunnerBreakeven);
      CancelOrder(FifteenFinalExit);
      }

      Please review my code and let me know if you see any other problems

      Thank you,

      Nick

      Comment


        #18
        Hello njmeyer713,

        Thank you for your reply.

        You only want to set TrailBar to CurrentBar the first time through, and set the lowest price to the low of that current bar (since that's the current low since the order was submitted). Since you're setting FifteenTrailReached to true when that happens, you can check if it's false first like we did before. So, try something like this:

        Code:
         if (Position.MarketPosition == MarketPosition.Short
        && (GetCurrentBid(0) + (50 * TickSize)) <= Position.AveragePrice
        && (Position.Quantity ==1))
        {
        double lowestPrice;
        if (FifteenTrailReached == false)
        {
        TrailBar = CurrentBar;
        lowestPrice = Low[0];
        }
        else
        {
        int lowestBar = LowestBar(Low, CurrentBar - TrailBar);
        lowestPrice = Low[lowestBar];
        }
        FifteenTrailingStop = ExitShortStopMarket(0, true, Convert.ToInt32(Position.Quantity), lowestPrice + (25 * TickSize), @"15TrailingStop1", @"15Short2U-2D");
        FifteenTrailReached = true;
        CancelOrder(FifteenRunnerBreakeven);
        CancelOrder(FifteenFinalExit);
        }
        Please let us know if we may be of further assistance to you.

        Comment


          #19
          Kate-

          Thank you for your help so far.

          I thought I had it running how I wanted it to but I do seem to be running into an issue now. Hopefully you can help me with it. I am getting a reject order because I am trying to place a stop short order below the current market price.

          The strategy is taking the low of the lowest bar displayed on the chart which is including historical data and this could be hundreds of bars ago depending on how far along in the playback I am.

          So for some reason that I cannot figure out the strategy isn't looking for the lowest bar since the trail stop was triggered but looking for the lowest bar on the chart.

          Can you look at the code below and hopefully see where I am making the mistake?

          Code:
          if (Position.MarketPosition == MarketPosition.Short
          && (GetCurrentBid(0) + (50 * TickSize)) <= Position.AveragePrice
          && (Position.Quantity ==1))
          
          
          ///// why does this work
          /// need to get variables at higher classes it think for the other signals
          /// get singals programmed for the other entries
          
          {
          double lowestPrice4;
          
          
          if (CurrentBar > TrailBar)
          {
          FifteenTrailReached4 = true;
          }
          
          if (FifteenTrailReached4 == false)
          {
          TrailBar = CurrentBar;
          lowestPrice4 = (Low[0]);
          }
          
          else
          {
          int lowestBar4 = LowestBar(Low, Convert.ToInt32(CurrentBar - TrailBar));
          lowestPrice4 = (Low[lowestBar4]);
          // FifteenTrailReached = true;
          }
          
          
          FifteenTrailingStop = ExitShortStopMarket(0, true, Convert.ToInt32(Position.Quantity), lowestPrice4 + (25 * TickSize), @"15TrailingStop4", @"15Short2U-2D");
          CancelOrder(FifteenRunnerBreakeven);
          CancelOrder(FifteenFinalExit);
          }
          
          if (Position.MarketPosition == MarketPosition.Flat
          && FifteenTrailReached4 == true)
          
          {
          FifteenTrailReached4 = false;
          }
          Thank you,

          Nick

          Comment


            #20
            Hello njmeyer713,

            Thank you for your reply.

            I'm not seeing anything obvious in the provided code that would look for the lowest price ever on the chart. Could you provide the strategy so I may test on my end to better understand the behavior? You can export it through Tools > Export > NinjaScript AddOn. Do not check the box to export as a compiled assembly or I will be unable to review your logic. Please attach the resulting .Zip file to your reply.

            Thanks in advance; I look forward to assisting you further.

            Comment


              #21
              Kate-

              What is your email? I am assuming this script is deleted when you are done with it? I am not allowed to share the script in its entirely with other people.

              I just want to cover my butt.

              Thank you,

              Nick

              Comment


                #22
                Hello njmeyer713,

                Thank you for your reply.

                That's totally understandable. You can send it to scriptingsupport [at] ninjatrader [dot] com with 2794781 ATTN Kate W. in the subject line. Please include a link to this post in the body of the email. All code will be kept confidential and deleted once testing is completed.

                Thanks in advance; I look forward to assisting you further.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                64 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                139 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                75 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X