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

Set Breakeven after 1st Profittarget met

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

    Set Breakeven after 1st Profittarget met

    Hy everyone,

    I have a strategy implemented with 2 seperate entries with the same stoploss in the beginning. After my 1st profittarget is met I sell the whole first entry and then I want to set the stoploss of my 2nd entry to breakeven plus some extra. Unfortunately the breakevenpart is not working. I already read some threads but didn't find a solution yet.

    This is my code for long entries.

    Code:
    if (buySignal && Position.MarketPosition == MarketPosition.Flat)
    {
    str_drawID = "Long "+drawID.ToString();
    
    str_buyID1 = "Long1 "+buyID.ToString();
    str_buyID2 = "Long2 "+buyID.ToString();
    SetStopLoss(str_buyID1,CalculationMode.Ticks, StopLoss,true);
    SetProfitTarget(str_buyID1,CalculationMode.Ticks, ProfitTarget);
    EnterLong(ProfitBatch,str_buyID1);
    SetStopLoss(str_buyID2,CalculationMode.Ticks, StopLoss, true);
    SetProfitTarget(str_buyID2,CalculationMode.Ticks, ProfitTarget2);
    EnterLong(TrailBatch,str_buyID2);
    buyID = buyID+1;
    buySignal = false;
    }​
    And this my try to set stoploss to break even after profittarget met

    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    }
    
    else if (Position.MarketPosition==MarketPosition.Long)
    {
    if (Close[0]>Position.AveragePrice + (ProfitTarget * TickSize))
    {
    SetStopLoss(str_buyID2,CalculationMode.Ticks, Position.AveragePrice,true);
    }​
    Last edited by JonesJoker; 04-22-2024, 12:27 PM.

    #2
    Hello JonesJoker,

    Welcome to the NinjaTrader forums!

    The code you have suggested is looking good. The set methods are called before the entry, and then set to the position average price after the position is taken, and is reset after the position is flat. All great!

    However, you will want to use CalculationMode.Price for the SetStopLoss() call in the breakeven condition action block.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello JonesJoker,

      Welcome to the NinjaTrader forums!

      The code you have suggested is looking good. The set methods are called before the entry, and then set to the position average price after the position is taken, and is reset after the position is flat. All great!

      However, you will want to use CalculationMode.Price for the SetStopLoss() call in the breakeven condition action block.
      Thanks for your hint ChelseaB,

      unfortunately it's still not working in Backtesting. Any other thoughts or do you need more code?

      Comment


        #4
        Hello JonesJoker,

        Have you compiled and reloaded the script since changing the code?
        What is the code you changed?

        Add a print within the conditions action block that prints the time and the value of position.averageprice, and enable TraceOrders.


        Provide the output from the output window.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I changed my breakeven block to this:
          Code:
          else if (Position.MarketPosition==MarketPosition.Long)
          {
          if (Close[0]>Position.AveragePrice + (ProfitTarget * TickSize))
          {
          str_buyBEID1 = "BE " +buyID.ToString();
          SetStopLoss(str_buyID2,CalculationMode.Price, Position.AveragePrice,true);
          }
          Compiled and reloaded. Added a print but it only shows the double of the close I guess and no average.price.

          Code:
          else if (Position.MarketPosition==MarketPosition.Long)
          
          {
          Print(Close[0]+Position.AveragePrice);​
          Click image for larger version

Name:	grafik.png
Views:	45
Size:	27.7 KB
ID:	1300636
          Last edited by JonesJoker; 04-22-2024, 02:14 PM.

          Comment


            #6
            Hello JonesJoker,

            These should all be the same action block.

            You need to specify these are strings and not add the numbers together. In general I suggest using string.Format() when combining objects to strings for a print as shown in the support article i linked you.
            Your prints should also have labels.

            else if (Position.MarketPosition==MarketPosition.Long)
            {
            if (Close[0]>Position.AveragePrice + (ProfitTarget * TickSize))
            {
            Print(Time[0].ToString() + " | Close[0]: " + Close[0].ToString() + " > Position.AveragePrice: " + Position.AveragePrice.ToString());
            str_buyBEID1 = "BE " +buyID.ToString();
            SetStopLoss(str_buyID2,CalculationMode.Price, Position.AveragePrice,true);
            }​

            Ensure TraceOrders is enabled.

            Provide the output from the output window saved as a text file.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by JonesJoker View Post
              I changed my breakeven block to this:
              Code:
              else if (Position.MarketPosition==MarketPosition.Long)
              {
              if (Close[0]>Position.AveragePrice + (ProfitTarget * TickSize))
              {
              str_buyBEID1 = "BE " +buyID.ToString();
              SetStopLoss(str_buyID2,CalculationMode.Price, Position.AveragePrice,true);
              }
              Compiled and reloaded. Added a print but it only shows the double of the close I guess and no average.price.

              Code:
              else if (Position.MarketPosition==MarketPosition.Long)
              
              {
              Print(Close[0]+Position.AveragePrice);​
              Click image for larger version  Name:	grafik.png Views:	19 Size:	27.7 KB ID:	1300636
              Thanks again ChelseaB.
              This is the Output after I changed my code to your mentioned one. Looks like the Close[0] is never bigger / equal average.price+(Profittarger*TickSize) although the first batch is closed after hitting the same profittarget. I am confused why the whole block is ignored:
              Code:
              if (Close[0]>=Position.AveragePrice + (ProfitTarget * TickSize))
                              {
                                  Print(Time[0].ToString() + " | Close[0]: " + Close[0].ToString() + " > Position.AveragePrice: " + Position.AveragePrice.ToString());
                                  str_buyBEID1 = "BE " +buyID.ToString();
                                  SetStopLoss(str_buyID2,CalculationMode.Price, Position.AveragePrice,true);
                              }​
              Attached Files

              Comment


                #8
                Hello JonesJoker,

                The conditions are evaluating as false, so the action is not triggered.

                If the close price is not reaching greater than the desired price, what would you like to have happen?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hy ChelseaB,
                  but Close[0] had to be bigger than AveragePrice+Profittarget*TickSize once, otherwise the first batch would not have been closed.

                  Comment


                    #10
                    Hello JonesJoker,

                    May I have print output showing that?
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by wuannetraam, Today, 12:22 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post wuannetraam  
                    Started by TGOG1, Yesterday, 03:02 PM
                    2 responses
                    20 views
                    0 likes
                    Last Post TGOG1
                    by TGOG1
                     
                    Started by AlgoDreamer, Yesterday, 06:12 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post AlgoDreamer  
                    Started by Rafaelseki, Yesterday, 03:36 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post Rafaelseki  
                    Started by rdlans, Yesterday, 05:42 AM
                    2 responses
                    25 views
                    0 likes
                    Last Post rdlans
                    by rdlans
                     
                    Working...
                    X