• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit the running trade in a strategy

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

    Exit the running trade in a strategy

    Hello: I would like to know how to program using Strategy Builder to exit the current running trade once it reaches the set daily profit limit by the input parameter. Currently, it continue to reach the set target profit in ticks or $ amount while total Realized profit + current trade P&L have reached the daily profit limit. For example, Daily profit limit is set at 500. and per trade profit target set at 200. It will continue to trade when Realized profit is 400 ( from last two trades that reached per target at 200 each) and last trade currently running has P&L above 100 and continue to run till reach 200 set target profit per trade. Many times the last trade do not compete as per the logic and reverse and takes away realized profit of 400. Please advise. Thanks,

    #2
    Hello mtamaku,

    Save the PnL to a series. On each new session save the PnL to a second series to remember yesterdays PnL.
    Use an offset with the first series to subtract the second series to get the daily PnL.
    Compare this to your input.

    Below is a link to an example.
    https://ninjatrader.com/support/foru...ples#post93881
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi, will it exit the current running trade once it reaches the daily P&L ? thanks

      Comment


        #4
        Hello mtamaku,

        The unlocked script will exit the open position.

        However, the Strategy Builder example made by paul does not. You may be able to use a 3rd series to add the unrealized pnl.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello: I am using the unlocked script logic to exit the trade when reached a daily profit limit but this is what it is doing.

          (1) Set target profit per trade is 100
          (2) Daily profit limit is 200.
          (3) After first two trades, 100 each, reaching daily limit of 200, instead of taking no more trades, it checks the CurrentPNL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) and It exits the long or short on 3rd trade at which point daily profit is more than Daily profit Limit of 200.
          (4) and if the 3rd trade is negative then it continues to trade till I reach the limit of daily profit or loss.

          Basically, how do i stop the trading exactly at amount set in daily profit limit of 200 in this example and avoid next trade which takes all profit from 1st two trade ?
          Please see the attached screen of trade.

          Thanks.
          Attached Files
          Last edited by mtamaku; 07-20-2020, 11:20 AM.

          Comment


            #6
            Hello mtamaku,

            As demonstrated in the DailyLossLimitExample_NT8 example, accumulate the profit and loss to a variable from the SystemPerformance. As the price updates add the accumulated realized pnl to the unrealized pnl with GetUnrealizedProfitLoss(). If the accumulated pnl is greater than a desired amount (or less than a desired amount) call an exit order to exit the position. To place a new order require that the accumlated pnl be less than or greater than the desired amount.

            The example I have provided is a loss limit. This means it looks for the accumulated pnl to be greater than the loss amount to place a new order. Your script to look for the accumulated amount to be less than a profit capture amount to place a new order if you would like.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I am doing that following the sample script. Please see the attached where I have condition for long and short trades for both dailyprofit and dailyloss values provided as input.
              Attached Files

              Comment


                #8
                Here is the result from the script you provided where it has various days where Net Profit or net loss over 500 or -500 after first two days from strategy result.
                I am hoping to see only 500 or -500 in net profit column as the limit set at 500 in the sample script.
                Attached Files
                Last edited by mtamaku; 07-20-2020, 11:57 AM.

                Comment


                  #9
                  Hello mtamaku,

                  The example I have provided is a loss limit example and does not have a profit capture above as this meant to be a demonstration of how this is possible (and not intended to be teaching logic). This example prevents the pnl from being below a certain amount.

                  Use prints to understand the behavior of your script.

                  Below is a link to a forum post that demonstrates using prints to understand behavior.
                  https://ninjatrader.com/support/foru...121#post791121

                  Outside of the condition print the time of the bar, print the position, print the currentPnL, print the GetUnrealizedProfitLoss, print these two values added together, print the DailyProfitLimit.

                  Enable TraceOrders.

                  Attach the output from the prints with your reply.

                  Let me know at what time and date you are expecting the position to be exited.

                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Please see the output of print statement in output window for the last two dates at the end where daily limit of 250 reached but by the time it exits longs it has that last trade that shows the vaue higher than 250 as the set limit.
                    Attached Files

                    Comment


                      #11
                      Hello mtamaku,

                      Its hard to say what is being printed here and where.

                      Also, TraceOrders is not appearing in the output.
                      https://ninjatrader.com/support/foru...121#post791121

                      To confirm these prints are outside of all conditions and are only in OnBarUpdate()?

                      We are wanting to know when the condition is being called and what values it has when it is not evaluating as true.

                      Are these very large bars that only update once every hour or so?

                      Is this real-time or historical?
                      Is the script using Calculate.OnPriceChange?
                      Is TickReplay enabled?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi:
                        I have Traceorders = True but nothing prints in output window except Print commands output.
                        As per the code screen I sent, orders are being placed on OnbarUpdate and orders are being exited as well in same section.
                        Also current PNL and no new order text being checked in OnpositionUpdate.
                        Basically same as your sample script.

                        Bars are Renko bars
                        This is in SIM account in realtime
                        Script using Calculate On bar close.
                        Not sure where and how to enable tick replay.

                        Thank you

                        Comment


                          #13
                          Hello mtamaku,

                          This would mean that the logic is only being checked when the bar closes. Meaning that the position can only be exited when the bar closes, not when the pnl amount is reached exactly.
                          In other words, when the bar closes, it may be a little past the pnl level.

                          Below is a link to the help guide on TickReplay.
                          https://ninjatrader.com/support/help...ick_replay.htm
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            hi, how tick replay would help in the case where I am using On Bar Close ? Do I need to use that feature to stop trading after exact profit reached ? thx

                            Comment


                              #15
                              Hello mtamaku,

                              TickReplay allows OnBarUpdate() to run for Calculate.OnPriceChange and .OnEachTick in historical data.

                              This would be if you wanted to check the logic before the bar closes.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bumbledbee, Today, 04:28 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_Marco  
                              Started by Tmaninc, Today, 02:13 AM
                              3 responses
                              12 views
                              0 likes
                              Last Post Tmaninc
                              by Tmaninc
                               
                              Started by rexmie888, Yesterday, 09:37 PM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_ChrisSch  
                              Started by larkmail, Yesterday, 06:19 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Marco  
                              Started by jvanelli, Yesterday, 09:23 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Marco  
                              Working...
                              X