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

AutoBreakEven & Profit

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

    AutoBreakEven & Profit

    Hi, I am struggling with a seemingly simple task. I want to move my stop to Breakeven as soon as 25% of my ProfitTarget (PT) are met and lock in 10 Ticks in Profit as soon as 50% of PT is reached. My stop does adjust in both cases which is great but as soon as the Price moves from 50% of unrealized Profits back to 25% my stop also moves back from +10 (green Diamonds) to Breakeven (magenta Diamonds)... any advice help how I can "fix" my stop at +10 once 50% or ProfitTarget was hit? Here is the code:

    if (IsFlat)
    {
    SetStopLoss(CalculationMode.Ticks, SL);
    }

    else if (IsLong)
    {
    // Once the price is greater than 25% of PT ticks, set stop loss to breakeven
    if (Close[0] > Position.AveragePrice + (0.25 * PT * TickSize))
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    Draw.Diamond(this, ""+CurrentBar, true, 0, Position.AveragePrice - (0), Brushes.Magenta);

    }
    if (Close[0] > Position.AveragePrice + (0.5 * PT * TickSize))
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + 10 * TickSize);
    Draw.Diamond(this, ""+CurrentBar, true, 0, Position.AveragePrice + 10 * TickSize, Brushes.Chartreuse);

    }
    }
    Attached Files

    #2
    Hello JBU1314,

    Thanks for your post.

    You can declare and use a set of bools to control the setting of the stop loss so that it occurs only once for each.

    In each conditional statement you would check for the bool to be true and once the stop has been set, change the bool to false to prevent changing it.

    For example (using a previously declared bool doitonce = true):

    if (Close[0] > Position.AveragePrice + (0.25 * PT * TickSize) && doitonce)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    Draw.Diamond(this, ""+CurrentBar, true, 0, Position.AveragePrice - (0), Brushes.Magenta);
    doitonce = false; // set to false so we only process the stop change here once.
    }


    When flat you would set the bools to back true.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you Paul. But this would also only draw the Diamond once right? I would like to have ongoing visual representation of were my adjusted stops are - so a Diamond at every bar... Is there another way you can think of?

      Comment


        #4
        Hello JBU1314,

        Thanks for your reply.

        You now know how to set the stop prices once for each level. To continue drawing the diamonds what I would do is use a double variable to save the stoploss price level so that you have that available to draw the diamond and use the bools (or perhaps more bools) to tell you when to draw the diamonds. Here is an example for the first section:

        if (Close[0] > Position.AveragePrice + (0.25 * PT * TickSize) && doitonce)
        {
        SetStopLoss(CalculationMode.Price, Position.AveragePrice);
        doitonce = false; // set to false so we only process the stop change here once.
        stoplevel = Position.AveragePrice; // save the stop level
        }

        if (!doitonce) // if it is false...
        {
        Draw.Diamond(this, ""+CurrentBar, true, 0, stoplevel, Brushes.Magenta);
        }

        This example should help you to work out what you need to do. It is likely that you will need two bools to draw the B/E diamond where one is false and the other is true so that the one set of diamonds are drawn when setting the B/E stop. Then when the profit section is set that bool would then be set false inhibiting the B/E diamonds from printing.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thank you very much for the great feedback!!

          Comment


            #6
            Another Rookie question sorry, how do I reset the bool to true? I seem to have all the code right but i does not reset to true when flat? attached the pic? Any advice would be greatly appreciated.
            else if (State == State.Configure)
            {
            SetStopLoss(CalculationMode.Ticks, SL);
            SetProfitTarget(CalculationMode.Ticks, PT);
            bool adjustbe = true;
            }

            }
            protected override void OnBarUpdate()
            {
            if(base.CurrentBar < this.PT)
            return;

            if (Close[0] > Close[1])
            EnterLong();

            if (IsFlat)
            {
            SetStopLoss(CalculationMode.Ticks, SL);
            bool adjustbe = true;
            //bool adjustrealprofit =true;
            }


            else if (IsLong)
            {
            //SET 1 - MOVE SL TO BE!!!
            // Once the price is greater than 25% of PT ticks, set stop loss to breakeven

            if (Close[0] > (Position.AveragePrice + (0.25 * PT * TickSize)) && adjustbe)
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
            adjustbe = false;
            stoplevel = Position.AveragePrice;
            //P("StopLoss adjusted to:" + (Position.AveragePrice - (0)));
            //P("Adjust P/L:" + (Position.AveragePrice - Close[0]));
            }
            if (!adjustbe)
            {
            Draw.Diamond(this, ""+CurrentBar, true, 0, stoplevel, Brushes.Magenta);
            }
            Attached Files

            Comment


              #7
              Hello JBU1314,

              It appears your section:

              if (IsFlat)
              {
              SetStopLoss(CalculationMode.Ticks, SL);
              bool adjustbe = true;
              //bool adjustrealprofit =true;
              }

              is intend to reset bools and reset the stop loss value.

              I don't see where IsFlat is being set anywhere in your code.

              You may want to try:

              if (Position.MarketPosition == MarketPosition.Flat)
              {
              SetStopLoss(CalculationMode.Ticks, SL);
              bool adjustbe = true;
              //bool adjustrealprofit =true;
              }


              Reference: http://ninjatrader.com/support/helpG...etposition.htm
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                I have a helpers region at the bottom where IsFlat is definded as
                bool IsFlat { get { return base.Position.MarketPosition == MarketPosition.Flat; }}

                Comment


                  #9
                  Hello JBU1314,

                  Thanks for your reply.

                  I would advise to debug your code using print statements to validate the various bool conditions. For direction on this, please see: http://ninjatrader.com/support/forum...ead.php?t=3418
                  Paul H.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by bmartz, 03-12-2024, 06:12 AM
                  4 responses
                  31 views
                  0 likes
                  Last Post bmartz
                  by bmartz
                   
                  Started by Aviram Y, Today, 05:29 AM
                  4 responses
                  11 views
                  0 likes
                  Last Post Aviram Y  
                  Started by algospoke, 04-17-2024, 06:40 PM
                  3 responses
                  28 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by gentlebenthebear, Today, 01:30 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by cls71, Today, 04:45 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X