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

Daily Profit Target

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

    Daily Profit Target

    Team,

    Would like to add condition to my strategy to STOP trading once I reach $500 profit for a day (including before market open)?

    How can I do that?

    #2
    Hello surya,

    Thanks for writing in.

    Please see the example scripts in the forum thread linked below. These example scripts demonstrate how to set a Daily Loss Limit. However, you could use this same concept to set a Daily Profit Limit for your strategy. In the script, we reset the currentPnL for a new day of trading at the start of a new session. Then, we check if we are in a flat position and below the loss limit of the day and enter long. Next, we check if we are in a position and check if the realized day's PnL plus the position PnL is greater than the loss limit then exit the order.

    https://ninjatrader.com/support/foru...limit-examples

    This could also be set up using the Strategy Builder. My colleague Paul was clever enough to realize the Strategy Builder custom series can be used with offsets to do math and subtract the total PnL from yesterday's PnL to get the current PnL and provided an example of how to set a Daily Loss Limit using the Strategy Builder. This same concept could be used to set a Daily Profit Limit.

    Let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Brandon,

      I followed your above post and this is what I got but some how its not working. Can you guide me here

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

      YesterdaysPNL[0] = YesterdaysPNL[1];
      StrategyTotallPNL[0] = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;
      if (CurrentBars[0] < 1)
      return;

      // Set 2
      if (Bars.IsFirstBarOfSession == true)
      {
      YesterdaysPNL[0] = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;
      Print(@"*** First Bar of Session ***");
      Draw.Text(this, @"DailyPNLexample Text_1 " + Convert.ToString(CurrentBars[0]), @"TotalRealizedPNL= " + Convert.ToString(SystemPerformance.AllTrades.Trade sPerformance.Currency.CumProfit), 0, (Low[0] + (-9 * TickSize)) );
      Draw.Text(this, @"DailyPNLexample Text_2 " + Convert.ToString(CurrentBars[0]), @"Prior SessionPNL = " + Convert.ToString(DailyPNL), 0, (Low[0] + (-5 * TickSize)) );
      DailyPNL = 0;
      }

      // Set 3
      if (Bars.IsFirstBarOfSession == false)
      {
      DailyPNL = (StrategyTotallPNL[0] - (YesterdaysPNL[0])) ;
      }

      Print(Convert.ToString(Times[0][0]) + @" TotalPNL: " + Convert.ToString(StrategyTotallPNL[0]) + @" YestredaysPNL: " + Convert.ToString(YesterdaysPNL[0]) + @" DailyPNL: " + Convert.ToString(DailyPNL));
      // Set 5
      if ((High[1] > Open[1])
      && (Open[1] > Close[1])
      && (Close[0] < Open[0])
      && (DailyPNL < DailyProfit))
      {
      Draw.ArrowUp(this, @"MyCustomStrategy7 Arrow up_1 " + Convert.ToString(CurrentBars[0]), false, 1, (Low[0] + (-5 * TickSize)) , Brushes.Red);
      EnterShort(Convert.ToInt32(EntryQuantity), @"EnterShort");
      Print(@"enter short " + Convert.ToString(DailyPNL) + @" > " + Convert.ToString(DailyProfitNew));
      }

      // Set 6
      if ((Low[1] < Open[1])
      && (Open[1] < Close[1])
      && (Close[0] > Open[0])
      && (DailyPNL < DailyProfit))
      {
      Draw.ArrowDown(this, @"MyCustomStrategy7 Arrow down_1 " + Convert.ToString(CurrentBars[0]), false, 1, (High[0] + (5 * TickSize)) , Brushes.LimeGreen);
      EnterLong(Convert.ToInt32(EntryQuantity), @"EnterLong");
      Print(@"enter long " + Convert.ToString(DailyPNL) + @" > " + Convert.ToString(DailyProfitNew));
      }

      }

      Comment


        #4
        Hello surya,

        Thanks for your note.

        I don't see anything specific in your shared code that would cause an issue. Please elaborate on what exactly isn't working so we may accurately assist you. For example, are you not seeing orders place when expected?

        If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.

        To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Below is a link to a forum post that demonstrates using prints to understand behavior and including a link to a video recorded using the Strategy Builder.
        https://ninjatrader.com/support/foru...121#post791121

        Please let me know if I may further assist.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Brandon,

          Thank you. Now my strategy stops after reaching profit target but in calculating profit target it is considering the trades which are met before live trades
          For ex: In below chart as soon as I enabled strategy, profit 1/2/3 plotted on chart (20$ each) and its profit is calculated and when 4 target met the strategy stopped taking trades.
          In real, what I was expecting is it should have skipped 1,2,3 and counter should only start when I enable the strategy.
          Click image for larger version

Name:	Capture.PNG
Views:	1237
Size:	33.3 KB
ID:	1163777

          Comment


            #6
            Hello surya,

            Thanks for your note.

            When a strategy is enabled, it processes historical data to determine trades that the strategy would have made on the data that is already on the PC/chart and to determine what position the strategy is in. (Strategy positions are separate from actual Account positions.)

            Wait Until Flat will wait until this virtual/historical position is closed before live orders can be submitted. It ensures that the strategy starts trading live from a flat position. If you enable the strategy when the account is flat, the strategy will wait until this position calculated from historical data is closed so it is logically making trades starting from an entry signal.

            Immediately Submit automatically submits working orders from when the strategy processed historical data, and assumes the strategy position and account position are where you want it when you enable the strategy. This is typically used to have a strategy resume a position after disabling/enabling. If the strategy already had live orders running, the orders will resume with the new enablement of the strategy if they match the historically calculated orders. If the orders calculated from historical data do not match the live working orders, the live working orders will be cancelled and replaced by those calculated from historical data.

            Sync Account Positions is an additional option that has NinjaTrader submit an order to sync the account position to the position calculated by the strategy. (Not the other way around.)
            Adopt Account Position would be used if you want the strategy to inherit the Account Position upon enablement. This requires additional programming.

            If you do not want the strategy to calculate a position from processing historical data. Simple add if (State == State.Historical) return; to the top of your strategy logic so historical processing is skipped. The strategy will then always start from a flat position because it has not calculated any orders.

            Strategy vs. Account Position — https://ninjatrader.com/support/help..._account_p.htm
            Start Behaviors — https://ninjatrader.com/support/help..._positions.htm

            Additional information could be found in this forum link - https://ninjatrader.com/support/foru...ion#post811541

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Brandon,

              Thank you.
              I am going through builder as stated below. Can you let me know how can I skip historical and where should I add (State == State.Historical) return Click image for larger version

Name:	Capture.PNG
Views:	1227
Size:	11.0 KB
ID:	1163809 ?

              Comment


                #8
                Hello surya,

                Thanks for your note.

                You would not be able to skip historical processing using the Strategy Builder. To skip historical processing, you would need to unlock the code of your script using the 'Unlock code' button. Then, you would add if (State == State.Historical) return; to the top of the OnBarUpdate() method.

                Please let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  PNL from the previous trades can be limited by lowering the amount of candles used in order to trade?

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by judysamnt7, 03-13-2023, 09:11 AM
                  4 responses
                  59 views
                  0 likes
                  Last Post DynamicTest  
                  Started by ScottWalsh, Today, 06:52 PM
                  4 responses
                  36 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by olisav57, Today, 07:39 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post olisav57  
                  Started by trilliantrader, Today, 03:01 PM
                  2 responses
                  22 views
                  0 likes
                  Last Post helpwanted  
                  Started by cre8able, Today, 07:24 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X