Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Realized PnL with ATM in Script

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

    #16
    We do not have reference samples for ATM strategies in NinjaScript. You need to post your code.
    Josh P.NinjaTrader Customer Service

    Comment


      #17
      OK, I THINK I might have found the problem, but not sure. This code is in my strat which was copied from the ATM Sample.

      } // If the strategy has terminated reset the strategy id
      else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
      atmStrategyId = string.Empty;

      Since the GetAtmStrategyPnL uses the atmStrategyId, could this be why it's not adding up the PnL? It LOOKS like this resets it back to zero every time a trade is done. When is says "If the strategy has terminated", is it talking about the ATM strategy and not the NinjaScript strategy terminating?

      Is this a simple matter of taking this reset code out of the strategy?
      Joe

      Comment


        #18
        You would always want to reset it. Just store the value in your own variable before you reset it and aggregate that for your PnL.

        "if strategy was terminated" should be referring to ATM strategies.
        Josh P.NinjaTrader Customer Service

        Comment


          #19
          OK, thanks. Let me see if I can figure out how to do what you just told me. You make it sound so simple but for a non-programmer like me it's like chewing on tin foil.

          Comment


            #20
            In the NinjaScript create yourself a variable.

            Code:
            someVariable += GetAtmStrategyRealizedProfitLoss(atmStrategyId);
            Then you can reset your atmStrategyId after you save out your PnL.
            Josh P.NinjaTrader Customer Service

            Comment


              #21
              That helps a LOT!!! Thank You!!
              I didn't know we could use a += so I guess that's what makes them add together.

              The cummulative trades, cummulative profit works great in a plain old Ninjascript strategy, but not with one that creates an ATM.

              Now if I can just get it to work......
              I appreciate it Josh!

              Comment


                #22
                Right. You need to aggregate them yourself when dealing with ATMs.
                Josh P.NinjaTrader Customer Service

                Comment


                  #23
                  Josh (and NT support) you are so much help i thank you for sticking with us through our noobness. NT is a great product on its own but having support like you provide puts it above the rest imo. Thanks

                  Will give this a go tonight but i believe it will solve my issue.

                  Comment


                    #24
                    Josh,
                    Great news is that it works! I also add my THANKS to cirion's.

                    HOWEVER.........

                    It doesn't seem to be adding (subtracting) a loss to the realized PnL. It looks like it only sees the profitable trades to aggregate. I just ran the strat on yesterdays data.
                    Daily Profit Goal was set to $200.
                    First Trade: - $202.50
                    Second Trade: $95.30
                    Third Trade: $95.30
                    Fourth Trade: $95.30

                    Then it stopped trading. Total Realized PnL was $83.40. Even if it doesn't see commissions it should have kept trading since the PnL limit was $200 and it would have only been $100 to the good.

                    The total for the profitable trades was $285.90, which is over $200, so it quit.

                    Any ideas?? I double checked the Time and it was definitely in bounds and conditions met.
                    Joe
                    PS - when this thing works as advertised I'll post a sample strat that others can use.

                    Comment


                      #25
                      Please post code of what you have. Thank you.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #26
                        OK. I'm posting the part I think you will want to look at.
                        The code you had me put is is just under OnBarUpdate.

                        #region Variables
                        // Wizard generated variables

                        // User defined variables (add any user defined variables below)
                        private double realPnL = 90.000;
                        private string atmStrategyId = string.Empty;
                        private string orderId = string.Empty;
                        private double profit= 0;
                        #endregion

                        protected override void Initialize()
                        {
                        CalculateOnBarClose = true;
                        }

                        protected override void OnBarUpdate()
                        {
                        // Checks for total realized PnL
                        profit += GetAtmStrategyRealizedProfitLoss(atmStrategyId);

                        **** ALL BAR CALCULATION STUFF LEFT OUT****

                        // calculate the highest high for this range
                        double highestHigh = MAX(High, startBarsAgo - endBarsAgo)[endBarsAgo];

                        // calculate the lowest low for this range
                        double lowestLow = MIN(Low, startBarsAgo - endBarsAgo)[endBarsAgo];

                        if (Historical)
                        return;

                        // Check for valid condition and create an ATM Strategy
                        // Condition set 3
                        if (ToTime(Time[0]) >= ToTime(7, 50, 0)
                        && ToTime(Time[0]) <= ToTime(14, 0, 0)
                        && orderId.Length == 0 && atmStrategyId.Length == 0
                        && profit <= realPnL
                        && highestHigh > 0 && Close[0] > highestHigh)

                        {
                        atmStrategyId = GetAtmStrategyUniqueId();
                        orderId = GetAtmStrategyUniqueId();
                        AtmStrategyCreate(Action.Buy, OrderType.Limit, highestHigh, 0,
                        TimeInForce.Day,orderId , "2 Point",
                        atmStrategyId);

                        }

                        // Condition set 4
                        if (ToTime(Time[0]) >= ToTime(7, 50, 0)
                        && ToTime(Time[0]) <= ToTime(14, 0, 0)
                        && Position.MarketPosition == MarketPosition.Flat
                        && orderId.Length == 0 && atmStrategyId.Length == 0
                        && profit <= realPnL
                        && lowestLow > 0 && Close[0] < lowestLow)
                        {
                        atmStrategyId = GetAtmStrategyUniqueId();
                        orderId = GetAtmStrategyUniqueId();
                        AtmStrategyCreate(Action.Sell, OrderType.Limit, lowestLow, 0,
                        TimeInForce.Day,orderId , "2 Point",
                        atmStrategyId);

                        }

                        // Check for a pending entry order
                        if (orderId.Length > 0)
                        {
                        string[] status = GetAtmStrategyEntryOrderStatus(orderId);

                        // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
                        if (status.GetLength(0) > 0)
                        {

                        // If the order state is terminal, reset the order id value
                        if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
                        orderId = string.Empty;

                        }
                        } // If the strategy has terminated reset the strategy id
                        else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
                        atmStrategyId = string.Empty;

                        Thanks Josh, you have the patience of a saint.
                        Joe

                        Comment


                          #27
                          You should not put it up on the top there.

                          Please just try aggregating it as your position is closed.

                          Code:
                          ...
                          else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
                          {
                               profit += GetAtmStrategyRealizedProfitLoss(atmStrategyId);
                               atmStrategyId = string.Empty;
                          }
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #28
                            Josh, it's working perfectly now!!! You are a genius! Case closed.
                            Joe

                            Comment


                              #29
                              If anyone is following this thread, I just posted a Sample RealizedPnLwithATM strategy that folks can use as an example of how to do it.
                              Thanks to Josh!!!!!!!!!
                              Joe

                              Comment


                                #30
                                Exit based on PNL

                                Hello,
                                I want to know if its possible and how it would be good.

                                FLAT position and remove pending orders on a day of trading based on profit trading PNL(+200) or PNL(-1000) incluiding comissionsfor stop trading.

                                Thanks.
                                Delek
                                Last edited by delek77; 06-14-2015, 09:21 PM.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by lightsun47, Today, 03:51 PM
                                0 responses
                                4 views
                                0 likes
                                Last Post lightsun47  
                                Started by 00nevest, Today, 02:27 PM
                                1 response
                                8 views
                                0 likes
                                Last Post 00nevest  
                                Started by futtrader, 04-21-2024, 01:50 AM
                                4 responses
                                44 views
                                0 likes
                                Last Post futtrader  
                                Started by Option Whisperer, Today, 09:55 AM
                                1 response
                                13 views
                                0 likes
                                Last Post bltdavid  
                                Started by port119, Today, 02:43 PM
                                0 responses
                                8 views
                                0 likes
                                Last Post port119
                                by port119
                                 
                                Working...
                                X