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

Need Help With Auto BreakEven & Trail Code On Second Trade

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

    Need Help With Auto BreakEven & Trail Code On Second Trade

    Support,

    I have looked around the net for a bit and I cannot find code examples that match what I am tryin to do so I hope you can help me out.

    Situation:

    I have a strategy that opens two positions at the same time as market orders, one called "Scalp" and one called "Runner". My strategy both buys and sells, (but not at the same time), so the logic has to work for both buys and sells.
    The scalp trade takes profit and exits at 10 ticks. (This is working just fine).
    The Runner trade I would like to go to BreakEven +1 tick of profit at 10 ticks when my scalp trade closes.
    I would then like the Runner trade to continue to trail price 10 ticks behind price until stopped out. I would also like this parameter to be user defined.

    I am a novice with C# but I have ran some code and logic of runners inside of onbarupdate but I cannot get this to work. I started the shell in the builder but have since unlocked the code to add in this final breakeven logic because I was not able to figure it out inside the builder structure.

    Please help!

    Thanks,
    Justin

    #2
    I got the break even to work nicely but I cant figure out how to trail it after that by 10 ticks. Code snippet below.

    ------------------------------------------------------------------------

    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    SetStopLoss(@"Scalp", CalculationMode.Pips, StopLoss, false);
    SetProfitTarget(@"Scalp", CalculationMode.Pips, ScalpTP);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Pips, TSLRunner);
    }

    // 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+10 ticks, set stop loss to breakeven
    if (Close[0] > Position.AveragePrice + 10 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    }
    }

    // If a short position is open, allow for stop loss modification to breakeven
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // Once the price is less than entry price-10 ticks, set stop loss to breakeven
    if (Close[0] < Position.AveragePrice - 10 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    }
    }

    // Set 1
    if ((CrossBelow(Close, amaSuperTrendU111.StopDot, 1))
    && (Close[0] < EMA1[0])
    && (Volume[0] > 100))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), scalp);
    EnterShort(Convert.ToInt32(DefaultQuantity), runner);
    }

    // Set 2
    if ((CrossAbove(Close, amaSuperTrendU111.StopDot, 1))
    && (Close[0] > EMA1[0])
    && (Volume[0] > 100))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), scalp);
    EnterLong(Convert.ToInt32(DefaultQuantity), runner);
    }

    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="StopLoss", Description="Entry SL", Order=1, GroupName="Parameters")]
    public int StopLoss
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="ScalpTP", Description="Scalp TP", Order=2, GroupName="Parameters")]
    public int ScalpTP
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="TSLRunner", Description="Runner Trailing Stop", Order=3, GroupName="Parameters")]
    public int TSLRunner
    { get; set; }
    #endregion

    }
    }

    Comment


      #3
      Hello justin217,

      Thank you for your reply.

      What I would suggest here would be to use a bool to denote when your breakeven has been triggered, and once that bool is true, add your trailing logic. I'd take a look at this strategy builder strategy that has a trailing stop. I'd use the bool in the conditions for moving the stop. I've attached a good example of the logic necessary to control the trailing part of the logic.

      Please let us know if we may be of further assistance to you.
      Attached Files
      Kate W.NinjaTrader Customer Service

      Comment


        #4
        I am stuck and need help on your above instructions. I am a lifetime license holder, does that entitle me to your personal assistance at all? TIA NinjaTrader_Kate I tried to PM you on here, but it said you disabled that feature.

        Comment


          #5
          Hello justin217,

          Thank you for your reply.

          In the support department at NinjaTrader we do not create, debug, or modify code for our clients. This is so that we can maintain a high level of service for all of our clients as well as our vendors. The samples we provide are intended to give you further direction to move forward, but we cannot create exact examples for all requests.

          If you are interested in services in having the code written for you, I can have a representative of our EcoSystem reach out with more information on NinjaScript consultants who will be happy to do so.

          If you would like to move forward programming on your own, I recommend starting small and working with some basic concepts. I'd recommend reviewing the examples provided and getting comfortable with setting up a trailing stop and moving a stop to breakeven first, then working on combining the logic. Start with just the one order that you want to have the trailing stop and then once you have that working, then add in the second order and a stop loss/profit target to close that out.

          This example from our help guide should help with understanding the logic necessary to move a stop to breakeven:



          Please let us know if we may be of further assistance to you.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            justin217 checkout this link, I have been using it as part of my strategy and it works well and could be adapted to your two entry idea.
            This is a conversion of the NT7 misc ProfitTargetTrailingStop_101b Stop Strategy Framework Please contact the original author for any questions or comments. NT8‐ Added optional plot lines for Profit target and Stop. 7-8-2020 Changed the Calculate.OnBarClose to Calculate.OnPriceChange and relocated entry logic for best practice

            Comment


              #7
              Originally posted by NinjaTrader_Kate View Post
              Hello justin217,

              Thank you for your reply.

              What I would suggest here would be to use a bool to denote when your breakeven has been triggered, and once that bool is true, add your trailing logic. I'd take a look at this strategy builder strategy that has a trailing stop. I'd use the bool in the conditions for moving the stop. I've attached a good example of the logic necessary to control the trailing part of the logic.

              Please let us know if we may be of further assistance to you.
              Would be awesome for Kate or Chelsea to combine the Trail Builder example with the Builder Breakeven example so all can learn to trail after a stop has been moved to breakeven.

              Cheers!!

              Comment


                #8
                Originally posted by justin217 View Post
                I got the break even to work nicely but I cant figure out how to trail it after that by 10 ticks. Code snippet below.

                ------------------------------------------------------------------------

                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {
                SetStopLoss(@"Scalp", CalculationMode.Pips, StopLoss, false);
                SetProfitTarget(@"Scalp", CalculationMode.Pips, ScalpTP);
                }
                }

                protected override void OnBarUpdate()
                {
                if (BarsInProgress != 0)
                return;

                if (CurrentBars[0] < 1)
                return;

                // Resets the stop loss to the original value when all positions are closed
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                SetStopLoss(CalculationMode.Pips, TSLRunner);
                }

                // 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+10 ticks, set stop loss to breakeven
                if (Close[0] > Position.AveragePrice + 10 * TickSize)
                {
                SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                }
                }

                // If a short position is open, allow for stop loss modification to breakeven
                else if (Position.MarketPosition == MarketPosition.Short)
                {
                // Once the price is less than entry price-10 ticks, set stop loss to breakeven
                if (Close[0] < Position.AveragePrice - 10 * TickSize)
                {
                SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                }
                }

                // Set 1
                if ((CrossBelow(Close, amaSuperTrendU111.StopDot, 1))
                && (Close[0] < EMA1[0])
                && (Volume[0] > 100))
                {
                EnterShort(Convert.ToInt32(DefaultQuantity), scalp);
                EnterShort(Convert.ToInt32(DefaultQuantity), runner);
                }

                // Set 2
                if ((CrossAbove(Close, amaSuperTrendU111.StopDot, 1))
                && (Close[0] > EMA1[0])
                && (Volume[0] > 100))
                {
                EnterLong(Convert.ToInt32(DefaultQuantity), scalp);
                EnterLong(Convert.ToInt32(DefaultQuantity), runner);
                }

                }

                #region Properties
                [NinjaScriptProperty]
                [Range(1, int.MaxValue)]
                [Display(Name="StopLoss", Description="Entry SL", Order=1, GroupName="Parameters")]
                public int StopLoss
                { get; set; }

                [NinjaScriptProperty]
                [Range(1, int.MaxValue)]
                [Display(Name="ScalpTP", Description="Scalp TP", Order=2, GroupName="Parameters")]
                public int ScalpTP
                { get; set; }

                [NinjaScriptProperty]
                [Range(1, int.MaxValue)]
                [Display(Name="TSLRunner", Description="Runner Trailing Stop", Order=3, GroupName="Parameters")]
                public int TSLRunner
                { get; set; }
                #endregion

                }
                }
                What you can do is after the stop is moved to breakeven then start the trail calculation and use it in your Stop Loss.

                int trail = Position.AveragePrice - TrailBy*TickSize;

                SetStopLoss("Short #1",CalculationMode.Price,trail,false);

                Comment


                  #9
                  Did that help you justin217?

                  Comment


                    #10
                    Originally posted by Trader17 View Post
                    Did that help you justin217?
                    Thank you all for your kind and valuable input and interest in this topic. I have since abandoned this code and concept and have moved on to other strategies. If I find my way back to this path, I will update you all. I do echo one of the posters here though. I think it would be very cool and nice if NT support would code up an example of auto B/E and then trail as combined into one strategy. This technique is a very useful and powerful risk management tool that is employed by many many manual traders via ATM's. Just my two cents. Thanks!

                    Comment


                      #11
                      I agree. They should make an example of this in the Strategy Builder. Move the stop to breakeven and then start trailing it by X ticks for every Y ticks of movement. Chelsea or Kate should be able to do it unless the Strategy Builder cannot do it.

                      Comment


                        #12
                        Another vote for Breakeven + Trail

                        Comment


                          #13
                          Originally posted by Graci117 View Post
                          Another vote for Breakeven + Trail
                          Thanks for your patience.

                          Your vote has been added to the feature request with tracking ID SFT-2212. Please reference this internal tracking number when contacting Platform Support if you ever have questions regarding this feature request.

                          When a feature request is implemented, you'll find a description of the new feature in the release notes:Thank you for using NinjaTrader.​
                          Emily C.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Pattontje, Yesterday, 02:10 PM
                          2 responses
                          14 views
                          0 likes
                          Last Post Pattontje  
                          Started by flybuzz, 04-21-2024, 04:07 PM
                          17 responses
                          229 views
                          0 likes
                          Last Post TradingLoss  
                          Started by agclub, 04-21-2024, 08:57 PM
                          3 responses
                          17 views
                          0 likes
                          Last Post TradingLoss  
                          Started by TradingLoss, 04-21-2024, 04:32 PM
                          4 responses
                          43 views
                          2 likes
                          Last Post TradingLoss  
                          Started by cre8able, 04-17-2024, 04:16 PM
                          6 responses
                          56 views
                          0 likes
                          Last Post cre8able  
                          Working...
                          X