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

Price Decrease Strategy

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

    Price Decrease Strategy

    Hi

    How can I set a condition when the unRealized PnL go bellow a certain amount, I exit the trade?

    example: when the unrealized PnL cross bellow $100, the strategy exit the trade.

    And I don't mean when the unrealized pnL < $100. no, I want the condition when the unrealized PnL cross bellow $100, let's say it reach $105 then it go back bellow $100, that's when it triggers.
    Like when we add MA lines to cross up or cross bellow.

    is there anyway to have this condition?
    Last edited by onlinebusiness; 12-09-2022, 03:52 PM.

    #2
    Hello onlinebusiness,

    Below is a link to an example of a daily loss limit which provides sample logic.
    https://ninjatrader.com/support/foru...ples#post93881

    The PositionAccount.GetUnrealizedProfitLoss() will provide the UnrealizedPnL. You can compare this to be less than 100.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea, thanks for the replay.

      I checked these examples before, I don't think they have the condition I am looking for.
      Can you highlight where I can find that condition exactly?

      Comment


        #4
        Hello onlinebusiness,

        Code:
        // if in a position and the realized day's PnL plus the position PnL is greater than the loss limit then exit the order
        if (Position.MarketPosition == MarketPosition.Long
        && (currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) <= -LossLimit)
        {
        //Print((currentPnL+Position.GetProfitLoss(Close[0], PerformanceUnit.Currency)) + " - " + -LossLimit);
        // print to the output window if the daily limit is hit in the middle of a trade
        Print("daily limit hit, exiting order " + Time[0].ToString());
        ExitLong("Daily Limit Exit", "long1");
        }

        The currentPnL could be removed from this if you only want to focus on the unrealized and not the total PnL, and Position would be replaced with PositionAccount.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Code:
          if (Position.MarketPosition == MarketPosition.Long && (Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) <= -LossLimit)


          Correct me if I am wrong please: this line of code above will give me the condition when the Unrealized PnL is lower or equal to my specifiedLossLimit, correct?

          if that's true, that is not what I want.

          What I want is when the Unrealized PnL Crosses bellow my LossLimit.

          example: at certain point my unraelized PnL reach $105, then after few seconds, it goes back to $99. that moment when it crosses from above $100 to bellow $100. that what I need the condition to detect.
          So it's when it crosses bellow a certain amount, or crosses above a certain amount.

          Comment


            #6
            Hello onlinebusiness,

            In the example I provided, this would only occur once as the position would change from long to flat and the condition would no longer evaluate as true. So it would trigger once at the moment the pnl crosses below the limit level.

            However, you can choose to implement your own strategy. Try using a bool.
            Set a bool to true the first time the value is less than the desired amount. This will be the moment it crosses below.
            Require the bool to be false in the conditions that trigger the action.​
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello NinjaTrader_ChelseaB

              So this is what I did so Far:

              Code:
              //This namespace holds Strategies in this folder and is required. Do not change it.
              namespace NinjaTrader.NinjaScript.Strategies
              {
                  public class MinProfitStrategy: Strategy
                  {
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Enter the description for your new custom Strategy here.";
                              Name                                        = "MinProfitstrategy";
                              Calculate                                    = Calculate.OnBarClose;
                              EntriesPerDirection                            = 1;
                              EntryHandling                                = EntryHandling.AllEntries;
                              IsExitOnSessionCloseStrategy                = true;
                              ExitOnSessionCloseSeconds                    = 30;
                              IsFillLimitOnTouch                            = false;
                              MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                              OrderFillResolution                            = OrderFillResolution.Standard;
                              Slippage                                    = 0;
                              StartBehavior                                = StartBehavior.WaitUntilFlat;
                              TimeInForce                                    = TimeInForce.Gtc;
                              TraceOrders                                    = false;
                              RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                              StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                              BarsRequiredToTrade                            = 20;
                              // Disable this property for performance gains in Strategy Analyzer optimizations
                              // See the Help Guide for additional information
                              IsInstantiatedOnEachOptimizationIteration    = true;
                              MinProfit                                        = 100;
                          }
                          else if (State == State.Configure)
                          {
                          }
                      }
                      private double DayPnL;
              
                      private void PnL()
                          {    
                          if (Position.MarketPosition == MarketPosition.Long && (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) <= MinProfit)
              
                              Account.FlattenEverything();
                          }
              
                      #region Properties
                      [NinjaScriptProperty]
                      [Range(1, double.MaxValue)]
                      [Display(Name="MinProfit", Description="The amount the profit cross bellow", Order=1, GroupName="Parameters")]
                      public double MinProfit
                      { get; set; }
                      #endregion
                  }
              }​
              But when my Unrealized PnL is more than $100 then it goes bellow $100 (like $99, $90 or $80). The condition doesn't trigger to close the trade.

              I am sure I am doing something wrong, can you please check my code and see needs to be edited? I would be very thankful.

              The idea is that I want to set some trails of the accumulated profit, like $100, $150, $200 ....
              once my profit reach any of those trails, and the market reverse on me and I start loosing my accumulated profit, the strategy should close at any of the trail profit I reached.
              So if I reached $100 or more in profit, and the market reversed, the strategy would close the trade the moment the profit go down $100 or $99.

              I hope my explanation is clear enough
              Thank you

              Comment


                #8
                Hello onlinebusiness,

                Enter a position with EnterLong() to open a strategy position.

                Use ExitLong() to exit the long position of the strategy.

                Use Calculate.OnPriceChange() to trigger the exit intra-bar in real-time.

                Use print to understand the behavior.

                Print the time of the bar, print all values compared in the condition, add labels for each value and comparison operator.
                Save the output to a text file and include this with your reply.
                https://ninjatrader.com/support/foru...121#post791121
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by ageeholdings, Today, 07:43 AM
                0 responses
                7 views
                0 likes
                Last Post ageeholdings  
                Started by pibrew, Today, 06:37 AM
                0 responses
                4 views
                0 likes
                Last Post pibrew
                by pibrew
                 
                Started by rbeckmann05, Yesterday, 06:48 PM
                1 response
                14 views
                0 likes
                Last Post bltdavid  
                Started by llanqui, Today, 03:53 AM
                0 responses
                6 views
                0 likes
                Last Post llanqui
                by llanqui
                 
                Started by burtoninlondon, Today, 12:38 AM
                0 responses
                12 views
                0 likes
                Last Post burtoninlondon  
                Working...
                X