Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Closing ATM Orders

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

    Closing ATM Orders

    I have a working strategy but I am finding that sometimes I have trades still open as the end of the trading day approaches. Normally, since I'm watching the trades happen anyway, I just move a stop up to close to the current price, and let the position get stopped out before the market closes. I would like to do this a little more elegantly but I'm having trouble closing the last open position in my strategy. I added a modified version of the code I use to limit when trading occurs. I'm getting errors in the log when I run this, (the strategy still works, but doesn't close orders). I think I am looking to see if there is an active ATM order, and if so, grab the id and close the order. That is what I am trying to do, however, it isn't working. The error I get in the log is:

    AtmStrategyClose() method error: AtmStrategyId 'orderID' does not exist

    This is the code that is generating the error message in the log.

    /* Checks to see if the time is between 1:05 PM and 1:15 PM EDT and closes any existing trades which are still open. */

    if ((ToTime(Time[0]) >= 160500 && ToTime(Time[0]) < 161500))

    {
    if (atmStrategyId.Length > 0)
    {
    string orderID = GetAtmStrategyUniqueId();
    AtmStrategyClose("orderID");
    }

    #2
    Code below just generated a new order ID, which you stuff in AtmStrategyClose which does not make sense.



    AtmStrategyId
    The unique identifier for the ATM strategy

    Please review the SampleAtmStrategy strategy for how this works.

    Comment


      #3
      Closing ATM Strategy

      I've looked through the Sample ATM Strategy, in fact I lifted it wholesale and it constitutes the core of my strategy. I now understand that "GetAtmStrategyID" actually gets a new id. How do I get the existing strategy id so I can close it, or do I even need to do that?

      In the example the code checks to see if the unrealized profit is > 500 and
      if it is it closes the ATM Strategy.

      if (GetAtmStrategyUnrealizedProfitLoss("idValue") > 500)
      AtmStrategyClose("idValue");

      When I try to construct the code as similarly as I can:

      if(GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat
      AtmStrategyClose("idValue");

      Basically I'm doing a test to see if there is an active position, long or short, and if so I want to close it.

      The code above won't compile and gives an error message that says "The name 'GetAtmStrategyMarketPosition' does not exist in the current context."

      I think I have a problem with either the "atmStrategyId", or the "idValue" or both. How do I get either or both of those values for an ATM strategy that is already running? I don't necessarily know whether it came from the long test or the short test, and in any case I don't know how to get an existing ATM Strategy ID so that I can reference it to close it.
      Thanks
      daven

      Comment


        #4
        >> The code above won't compile and gives an error message that says "The name 'GetAtmStrategyMarketPosition' does not exist in the current context."
        Not sure what you are doing wrong. I suggest creating a copy of the SampleAtmStrategy and starting from there.

        >> How do I get either or both of those values for an ATM strategy that is already running?
        You can't. You need to start the strategy by AtmStrategyCreate.

        Comment


          #5
          Closing ATM Strategyq

          I tried this code which compiled but generated a method error when it ran:

          Code:
          Time Test then

          if (Get AtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
          AtmStrategyClose(atmStrategyId);


          The error message I get is Category "Strategy" and the text is:

          GetAtmStrategyMarketPosition() method error: Missing atmStrategyId parameter.

          I declare atmStrategyId as a private string variable just as in the example and I thought it contained the id for the AtmStrategy when it was launched, but apparently that doesn't work. What is the parameter I'm missing if it isn't the variable value created when the atm strategy was launched?

          thanks

          daven

          Comment


            #6
            Did you do this section of the code?

            Code:
            // Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
            // **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStrategyTemplate' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****
            if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
            {
               atmStrategyId = GetAtmStrategyUniqueId();
               orderId = GetAtmStrategyUniqueId();
               AtmStrategyCreate(Action.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
            }
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Reply

              I basically just lifted it from the sample atm strategy. I use a market order instead of a limit, because I have problems with limit orders being left out there and the trade never being entered. I would like to enter with a limit and then move or cancel the limit if not filled but that is well beyond my current skill level. I'm not sure why you asked since it isn't actually the part of the code that I am working on and asking questions about.
              Dave

              Comment


                #8
                >> GetAtmStrategyMarketPosition() method error: Missing atmStrategyId parameter.
                This error is thrown as the parameter you pass in is empty/blank string.

                You need to debug your strategy as per here in order to understand why that is: http://www.ninjatrader-support.com/v...ead.php?t=3418

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bortz, 11-06-2023, 08:04 AM
                47 responses
                1,602 views
                0 likes
                Last Post aligator  
                Started by jaybedreamin, Today, 05:56 PM
                0 responses
                8 views
                0 likes
                Last Post jaybedreamin  
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                18 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                4 views
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                12 views
                0 likes
                Last Post Javierw.ok  
                Working...
                X