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

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.
    Hello, I've updated the DailyLossLimit and DailyLosLimitMultiTrade examples that were posted on the forum for NinjaTrader 7 for NinjaTrader 8. These are often requested and I felt they are good examples to have for NT8. DailyLossLimitExample_NT7 - http://ninjatrader.com/support/forum...241#post451241 (http://ninjatrader
    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.


                  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.


                      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.
                          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 kevinenergy, 02-17-2023, 12:42 PM
                              115 responses
                              2,699 views
                              1 like
                              Last Post kevinenergy  
                              Started by prdecast, Today, 06:07 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by Christopher_R, Today, 12:29 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by chartchart, 05-19-2021, 04:14 PM
                              3 responses
                              577 views
                              1 like
                              Last Post NinjaTrader_Gaby  
                              Started by bsbisme, Yesterday, 02:08 PM
                              1 response
                              15 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X