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

stop to break even and trailing stop code help

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

    stop to break even and trailing stop code help

    hello

    would it be possible to have the stop to break even code take place first and then the trailing stop code to take over?
    If you have a solution please let me know. I'm open to new ideas. thanks.

    i have this stop to break even code:


    else if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] > Position.AvgPrice + 3* TickSize)

    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    }
    }

    else if (Position.MarketPosition == MarketPosition.Short)
    {
    if (Close[0] < Position.AvgPrice - 3* TickSize)

    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    }

    Trailing stop code:


    else if (Position.MarketPosition == MarketPosition.Long)
    {
    {
    SetStopLoss(CalculationMode.Price, Close[0] -11*TickSize);
    }
    }

    else if (Position.MarketPosition == MarketPosition.Short)
    {
    {
    SetStopLoss(CalculationMode.Price, Close[0] +11*TickSize);
    }

    How can I use them together in the same strategy?
    please help, im new at this. new ideas welcome.


    Thank you

    #2
    the easiest way would be to set a bool to true when u set ur stop to breakeven. and make this bool to be true a requirement for the trailing if() statement.

    you would have to do this in the onbarupdate bit tho.

    so roughly
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] > Position.AvgPrice + 3* TickSize)

    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    trailing = true;
    }
    }
    and


    else if (Position.MarketPosition == MarketPosition.Long && trailing)
    {
    {
    SetStopLoss(CalculationMode.Price, Close[0] -11*TickSize);
    }
    }
    and finally

    else if (Position.MarketPosition == MarketPosition.Flat && trailing)
    trailing=false;

    EDIT: note tho that the code would maybe readjust the stop again to be below breakeven that way.

    if you dont want that to happen you would also need to include something like.

    else if (Position.MarketPosition == MarketPosition.Long && trailing)
    {
    if (Close[0] -11*TickSize>Position.AvgPrice){
    SetStopLoss(CalculationMode.Price, Close[0] -11*TickSize);
    }
    }
    Last edited by BigRo; 11-17-2015, 10:18 AM.

    Comment


      #3
      Thank you very much for your reply!

      I have been trying to put your info into the strategy and came up with this for the long side.
      please let me know if it is actually ok.

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      SetStopLoss("", CalculationMode.Ticks, 12, false);
      SetTrailStop("", CalculationMode.Ticks, 12, false);

      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, 12);
      }

      else if (Position.MarketPosition == MarketPosition.Long)
      {
      if (Close[0] > Position.AvgPrice + 3* TickSize)

      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
      SetTrailStop == true;
      }

      }
      else if (Position.MarketPosition == MarketPosition.Long && Trailing)
      {
      {
      SetStopLoss(CalculationMode.Price, Close[0] -11*TickSize);
      }
      }
      else if (Position.MarketPosition == MarketPosition.Flat && Trailing)
      SetTrailStop == false;

      The erros i get when compiling are line 136: Expected class, delegate, enum, interface or struct

      and line 143: Type or namespace definition or end of file expected

      #region Properties
      [Description("")]
      [GridCategory("Parameters")]
      136 public bool Trailing
      {
      get { return trailing; }
      set { trailing = value; }
      }
      #endregion
      }
      143 }

      pleae help.
      Thanks a lot

      Comment


        #4
        there are plenty things off in this i think. ok, so. the first thing was actually already wrong in the original code, but i didnt see it.

        if you wanna move a stoploss it has to be a tagged one, so the code knows which to actually move.
        you cant use trailstop and stoploss both at the same time. if you wanna use a trail stop while running a normal stoploss at first, then u have to come up with ur own syntax - which i think u already did. so i dont know why u added that.

        there is futher no point of calling the stoploss in the initalize section. that is only for the purpose of having a static stop. if you trim and move it anyway. then it has no value and need to be called at the time of entry anway.

        also there is no point of adding the bool to the properties since there is no value in changing it from true to false. if anything it will cause problems. its just a conditionbuilder.

        i added a fixed code that displays what u need to do

        hope that helps
        Attached Files
        Last edited by BigRo; 11-18-2015, 11:21 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by mattbsea, Today, 05:44 PM
        0 responses
        3 views
        0 likes
        Last Post mattbsea  
        Started by RideMe, 04-07-2024, 04:54 PM
        6 responses
        31 views
        0 likes
        Last Post RideMe
        by RideMe
         
        Started by tkaboris, Today, 05:13 PM
        0 responses
        2 views
        0 likes
        Last Post tkaboris  
        Started by GussJ, 03-04-2020, 03:11 PM
        16 responses
        3,282 views
        0 likes
        Last Post Leafcutter  
        Started by WHICKED, Today, 12:45 PM
        2 responses
        20 views
        0 likes
        Last Post WHICKED
        by WHICKED
         
        Working...
        X