Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Realized PnL with ATM in Script

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

    Realized PnL with ATM in Script

    Hi,
    I've used the Sample Price Modification strategy code to have an autostrategy stop trading if it accumulated X amount of Real PnL. ie, quit trading if Real PnL > $100.

    Now I have an autostrategy calling an ATM and the Real PnL stop trading limit doesn't work. It looks like the Real PnL is attributed to the ATM it calls instead of the strategy itself, so the strategy doesn't see any Real PnL and just keeps on rolling.

    Is there a way to still have the autostrategy quit trading after X PnL with an ATM being called? I guess it would have to be able to add up the ATM Real PnL and recognize that somehow ???? If so, how would you do that?
    Thanks,
    Joe

    #2
    Hi Joe, you could try incorporating GetAtmStrategyRealizedProfitLoss() in your code and use this to check - http://www.ninjatrader-support.com/H...rofitLoss.html
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thank Bertrand,
      Looks like the example shows a Print function. I'll play with it and see if I can make it turn into a value I can use in the strat so it won't trade if the value is > some amount. The explanation in the Help Guide says what it does but not how to use it. I guess it's for more experienced programmers.
      Joe

      Comment


        #4
        Hey Bertrand,
        Sorry to bother you, but I can't seem to get it to work. I used examples of a similar nature but to no avail. Would you mind looking at this code and see if there's something obviously wrong or missing?

        Under Variables
        (realPnL defined)
        private double realPnL = 90.000;
        private string atmStrategyId = string.Empty;
        private string orderId = string.Empty;

        Under OnBarUpdate
        (I defined PnL to compare against realPnL. Is the GetAtm statement correct? 2 Point is name of the ATM strategy which I assume is the ATM strategyID.)

        double PnL = GetAtmStrategyRealizedProfitLoss("2 Point");

        // 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)
        && PnL <= realPnL
        && Position.MarketPosition == MarketPosition.Flat
        && orderId.Length == 0 && atmStrategyId.Length == 0
        && highestHigh > 0 && Close[0] > highestHigh)

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

        It's calling the ATM just fine and placing orders. I just don't want it to place any orders if the total realized PnL for the ATM on the day is greater than $90.
        Thanks,
        Joe

        Comment


          #5
          double PnL = GetAtmStrategyRealizedProfitLoss("2 Point");
          "2 Point" is not your strategyId. Please use your atmStrategyId.

          Position.MarketPosition == MarketPosition.Flat
          Does not work for ATM orders, designed for NinjaScript orders. Instead use this: http://www.ninjatrader-support.com/H...yPosition.html
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            When i do this in my code all it returns is 0 no matter what the profit or loss for the days trading is.

            Print("PnL is " + GetAtmStrategyRealizedProfitLoss("AtmStrategyId"). ToString());

            What am i doing wrong?

            Comment


              #7
              Josh,
              Where do I find the atmStrategyID? All I can find in Help is that one is generated. It doesn't say how to find it, or when it's generated. In the ATM Sample Strat code I copied, down where it calls the ATM after the conditions are met it says
              atmStrategyId = GetAtmStrategyUniqueId();

              Do I simply put the Word atmStrategyId between the quotes? Or is there somewhere else I have to look for the atmStrategyId?

              IF this is the case and I can simply put the word atmStrategyId between the quotes, do you see anything else obvious that would prevent it from checking the realized PnL before calling the ATM and submitting another order?

              I can remove the marketposition = flat since I don't think it's really needed but it's good to have your reference link in case I do.
              Thanks!!!!!
              Joe

              Comment


                #8
                In your code:
                "AtmStrategyCreate(Action.Buy, OrderType.Limit, highestHigh, 0,
                TimeInForce.Day,orderId , "2 Point",
                atmStrategyId);"

                You've made your own atmStrategyId. Yes, you just put atmStrategyId into there. It is the variable you made to hold your ID. You do not put it between quotes. You just put it inside the parenthesis with no quotes.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Thank You Josh!
                  And thanks for your explanation. It also cleared up something else. I guess I can make atmStrategyId anything I want it to say.
                  It's starting to come together for me now.
                  Again, many thanks!!! I'll give this a shot.
                  Joe

                  Comment


                    #10
                    Hi Trade1953, you are correct. Or just use

                    Code:
                    GetAtmStrategyUniqueId()
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      What would cause it to return 0 no matter what trades the atm takes?

                      Print("PnL is " + GetAtmStrategyRealizedProfitLoss(AtmStrategyId). ToString());

                      What am i doing wrong?

                      Comment


                        #12
                        Hi cirion, are sure you reference the correct AtmStrategyId? Also, you will not access to this historical values in the PnL with this call, you will need to save those yourself in a cumulative variable.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          Hi cirion, are sure you reference the correct AtmStrategyId? Also, you will not access to this historical values in the PnL with this call, you will need to save those yourself in a cumulative variable.
                          Here is my sections of code that apply to my issue. (please tell me if i have missed anything out.)
                          Code:
                          if (orderId.Length == 0 && atmStrategyId.Length == 0 && buypostion == true)
                                      {
                                          atmStrategyId = GetAtmStrategyUniqueId();
                                          orderId = GetAtmStrategyUniqueId();
                                          AtmStrategyCreate(Action.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "AutoATM", atmStrategyId);
                                          exitbar = Close[0];
                                      }
                          Code:
                          if (X == X)
                                      {
                                        Print("The current ATM Strategy realized PnL is: " + GetAtmStrategyRealizedProfitLoss(atmStrategyId));
                                      }
                          It always returns 0 even after a day of trading

                          Comment


                            #14
                            Realized profit won't show up until the trade has closed.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Josh,
                              I have pretty much the same issue as cirion does. My strat trades many times a day yet even after the profit threshold has been reached it just keeps trading. It's almost like it sees every trade as starting with a 0 profit. (I don't have a buy position = true requirement).

                              Would you happen to have a working Sample Strategy that calls an ATM and has the "stop trading if X Realized PnL" working?

                              "A picture is worth a thousand words".
                              Thanks!
                              Joe
                              PS- I hope they are paying you well for dealing with frustrated beginners like me!!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              14 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Yesterday, 08:42 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Yesterday, 07:51 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,982 views
                              3 likes
                              Last Post jhudas88  
                              Working...
                              X