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

Ending Ninjascript strategy

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

    Ending Ninjascript strategy

    I still have not been able to come up with a way to exit a strategy, and have the strategy cancel open orders, and stop running, after reaching a certain level of profit or loss, based on both unrealized and realized PnL.
    Does anyone know how to code this?

    #2
    Jim,

    I've already given you the code in a PM. Please use the code provided.

    Code:
    if ((Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 200) || (Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) <= -200))
         ExitLong();
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh, that just exits a position, it doesn't stop the entire strategy.

      Comment


        #4
        Right. The line I pasted is meant to be used as a supplement along with the reference sample I posted. The line above adds calculations for unrealized PnL while the reference sample only shows realized PnL.

        You can review the reference sample for a technique you can employ to halt the strategy here.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          ending ninjascript

          We're making progress Josh.
          The coding below did cause an exit of a long position in the $AUDUSD, with $200 in profit, but it doesn't cancel an order that was in place for reversing and going short, upon certain conditions being met.
          In effect, it still hasn't stopped the strategy.
          Is there any way to have it not only exit, upon reaching the target profit or loss, but cancel any orders that had been put in place, before reaching the target, and then not place any more orders, because the strategy has been stopped from running beyond the return statement.
          Thanks,
          Jim


          //* Prevents further trading if the current session's realized profit exceeds $200.

          if ((Performance.RealtimeTrades.TradesPerformance.Cur rency.CumProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 200)
          || (Performance.RealtimeTrades.TradesPerformance.Curr ency.CumProfit + Position.GetProfitLoss(Close[
          0], PerformanceUnit.Currency) <= -200))
          {
          ExitLong(
          "lg");
          ExitShort(
          "sht");
          return;
          }

          // Conditions below this point for going long or short based on the strategy parameters, which are based on a breakout above of below a certain beginning bars high and low.
          Based on intrabar, and not close of bar.

          Comment


            #6
            ending ninjascript

            More info.
            Please read comments in below post about cancelling orders.
            The reversal order to go short did eventually cancel once changing to the next hourly bar, even though the strategy says "false" on CalculateonBarClose.
            I would like for it to cancel with out waiting on the next bar to start.
            This is based on an hourly chart.
            Jim

            Comment


              #7
              What NT version are you using?
              RayNinjaTrader Customer Service

              Comment


                #8
                ending ninjascript

                6.5 Beta version

                Comment


                  #9
                  For clarification are you saying -

                  - You are in a long position
                  - Have a working entry order to go short
                  - No other stop or target orders working?
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    ending ninjascript

                    Hi Ray,
                    When I am in a long position, there will be a sell stop order to close the position at the low of the bar from which I went long, and there is also a sell short order to reverse and go short at that same low.
                    This morning, the strategy obtained $200 profit at 7:34am est, and it issued a sell market order to exit, based on reaching that profit, and it also cancelled the sell stop order, that was at the low of the entry order bar, at the same time that it exited.
                    However, the sell short order was still working until the opening of the next bar at 8:00am est.
                    It was then cancelled, and no more orders are being placed, which is what I want, if I reach the target profit or loss amount of $200.
                    I just would like to be able to have that reversal order, for possibly going in the other direction, to be cancelled, as soon as I exit, versus waiting for the close of the bar.
                    I am using
                    CalculateOnBarClose =
                    false;
                    Jim

                    Comment


                      #11
                      Thanks for the clarification.

                      Since the internals of our strategy class is quite complex, give me some time to think about this particular scenario and if this behaviour is expected or if something needs to be modified.
                      RayNinjaTrader Customer Service

                      Comment


                        #12
                        ending ninjascript

                        Okay,
                        Thanks for reviewing this.
                        Jim

                        Comment


                          #13
                          Lets try this:

                          Where you "return;" in your code after the PnL condition is met, subsitute "return;" for --> "Running = false;" excluding the quotations of course.

                          This will really terminate the strategy itself. The termination process should cancel out your pending entry stop order.
                          RayNinjaTrader Customer Service

                          Comment


                            #14
                            ending ninjascript

                            Will do.
                            Won't know until tomorrow if it works.
                            Thanks,
                            Jim

                            Comment


                              #15
                              ending ninjascript

                              When making this substitution
                              Running = false
                              What happens is that the strategy will not show on the chart, and does not show under the strategies tab for position, AvgPrice, Unrealized, or Realized.
                              My strategy still sends orders and takes a position, but it acts as if the strategy is not running, right from the start.
                              Jim

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by judysamnt7, 03-13-2023, 09:11 AM
                              4 responses
                              57 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
                              20 views
                              0 likes
                              Last Post helpwanted  
                              Started by cre8able, Today, 07:24 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X