Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to make AtmStrategyCreate run only once per strategy?

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

    how to make AtmStrategyCreate run only once per strategy?

    Hi,

    I am having a bit of a problem.

    I have MYSTRATEGY some thing like

    if(event == true) {
    AtmStrategyCreate(....)
    }

    and when 'event' occurs, AtmStrategyCreates creates an ATM strategy instance 1 correctly. However, before ths ATM strategy gets filled, 'event' happen again and it runs the ATM strategy instance 2. Sometimes both
    these ATM strategy are created in same direction sometimes in opposite directions. (hope u get the idea :-)

    Now, I would like

    1) to limit that ONLY 1 instance of the ATM strategy should run at any one time per instrument (I tried setting unique ID etc etc., does not seem to work with ATM strategy)

    2) I want to have an option to choose
    a)MYSTRATEGY continues to run and execute AtmStrategyCreate again after a sucessful FILL of AtmStrategyCreate(...)
    b)MYSTRATEGY should terminate after a sucessful execution of AtmStrategyCreate

    Thanks in advance.

    #2
    Hi rambkk,

    To achieve the limiting of AtmStrategyCreate() instances you need to use the orderId property. Please take a look at SampleAtmStrategy. In there you will see how you could run a check to see if the orderId is available (it is the part with orderId.Length == 0). In the parts after, you will see how you can reset the orderId to allow for the check to work properly based on an updated state of the order status. This should solve your 1) issue. For your 2) issue you can set Running to false to terminate a strategy. Insert this into your strategy with whatever logic you wish to terminate your strategy with:
    Code:
    Running = false;
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Yep it works thx... another question

      Hello,

      Thx so mush (Josh) it works great. Now another question I have
      is if it would be possible to make NinjaScript create a Simulated order
      like clicking the middle-mouse-button on the SuperDOM.


      Here is the solution to my problem copied a lot of it from ATMSample:


      //check if ATM is already running if not then goahead=1
      //if running but not completed then goahead=0
      //if running but completed then -just- clean up while goahead remain 0


      if (orderId.Length == 0 && atmStrategyId.Length == 0)
      {
      goahead=1;
      //Print("Ok!");
      } else {
      //Print("Not ok!");
      goahead=0;

      //free up if prior ATM has been completed
      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
      elseif (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
      atmStrategyId = string.Empty;
      }


      ...

      then at the AtmStrategyCreate part:

      if(goahead!=0) {

      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();

      //use orderId and atmStrategyId as corresponding parameters for AtmStrategyCreate(....)

      }

      Cheers,
      -ram

      Comment


        #4
        I know you can do simulated orders with the normal strategy order method (the non-ATM method. e.g. SetStopLoss(), etc.), but am not sure about the ATM strategy method. Alternatively, you could program your simulated order logic into your NinjaScript code though and submit live orders only when your simulated order would become a live order.
        Last edited by NinjaTrader_JoshP; 09-10-2007, 02:02 PM.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Setting quanity for AtmStrategyCreate

          Hello,

          Is there a way to set quantity for AtmStrategyCreate?
          I tried setting in the 'New Strategy' section by setting it
          to 'by default quantity' and it still executes as 1 contract
          all the time.

          Another thing is that sometimes I get this error
          after executing AtmStrategyCreate using a template
          with Stop-loss then Auto-Trail:

          ---9/11/2007 19:44 Strategy Error on calling the 'OnOrderStatus' method for strategy 'AtmStrategy': Invalid order state.

          ---9/11/2007 19:44 Order Buy stop or buy stop limit orders can't be placed below the market. Affected Order: Buy 1 Stop @ 7476.5

          This happens when there is major fluctuation in the market and then the
          ATM strategy terminates and does not go for a fill which could be VERY
          funny .

          Plz advice.

          -ram
          Last edited by rambkk; 09-11-2007, 06:14 AM.

          Comment


            #6
            Hi,

            You can only do this by changing the quantity specified in the ATM strategy template.

            The second issue, please let me know if you can reproduce this. I will research further.

            The last issue happens when your strategy places an order that is for an invalid price due to the current market price. Strategy termination is expected behaviour. You can try to resolve this by checking the market price (GetCurrentAsk() and GetCurrentBid()) before placing an order. This does not guarantee that an order could be placed at an invalid price but should reduce this situation.
            RayNinjaTrader Customer Service

            Comment


              #7
              Reproducing the error for invalid order

              Hi,

              I managed to reproduce the error several times. Actually it's
              produced from ATM strategy that's being called from MYSTRATEGY
              so actually those checking you mentioned should have been
              built into the ATM strategy as it should be running flawlessly.

              You can find the log file in the attachment.

              Also, when a strategy gets terminated due to some error would
              it be possible to make sure a fill happens at the same time?
              There would be chances that I would be on coffee and listening
              to mp3 and checking news while a strategy terminates forcefully
              and still have position open in the market .

              Please advice.

              Thanks in advance,
              -ram
              Attached Files

              Comment


                #8
                The error is coming from the simulator. In one example from your log, you were filled at 1996.50 and the ATM Strategy submitted the stop loss at 1995.75 (your stop loss offset was 3 ticks) and at that time the market price went lower real quick rendering the stop price invalid and the order was then rejected.

                This is expected behaviour.
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Error from the ATM strategy

                  Hi,

                  Thanks for the reply. To be on the safe side do you think it would be
                  safer to write my own buy/sell/stoploss/limit etc order and a custom code of multi-step trailing by not using ATM strategy from MYSTRATEGY?

                  -ram

                  Comment


                    #10
                    I am not sure that it would be safer but it would provide more control.
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      SetTrailStop() giving errror

                      Hi,

                      I tried changing my code from using ATM strategy to using
                      SetTrailStop and then EnterLong and EnterShort
                      and I get the following error from time to time as the Strategy terminates:

                      Strategy 'MyCustomStrategy' submitted an order that generated the following error 'OrderRejected'. Strategy has sent cancel requests attempted to close the position and terminated itself.

                      Buy stop or buy stop limit orders can't be placed below the market. Affected Order: BuyToCover 1 Stop @ 7486

                      All I do in my code is very simple:
                      SetTrailStop follow by either EnterLong or EnterShort.

                      Does this mean that simple SetTrailStop is not a safe thing to do?

                      My code looks like:
                      SetTrailStop(orderId,CalculationMode.Ticks,2,false);
                      EnterLong(orderQuantity, orderId);


                      Please advice.

                      -ram


                      PS. Log in the attached file
                      Attached Files
                      Last edited by rambkk; 09-13-2007, 06:59 AM. Reason: adding attachment

                      Comment


                        #12
                        Your trail stop offset is 2 ticks, that is the problem. On a fill of your entry, the SetTrailStop() will submit a trail stop order that is 2 ticks from entry. That is way too tight since there are may times and markets where the market will move that much against you (on entry of market, you are already down a tick at minimum) and thus the order is submitted at an invalid price and is rejected.

                        Try using a wider offset and then tighten it in.

                        Here is a tutorial that gives you an idea on how to dynamically change stop loss values once in a trade.

                        RayNinjaTrader Customer Service

                        Comment


                          #13
                          Is SetStopLoss() safer?

                          Dear Ray,

                          Thank you for your reply. It is very useful. If I switch to SetStopLoss()
                          like in the example you have given and use it to modify the SetStopLoss
                          to make it work like SetTrailStop() would the same problem occur if
                          the offset is too small (like 2 ticks)? If so, would there be any way
                          to avoid this? Could it execute a stop with a limit if the price is equal or
                          below the maximum loss allowed for a Long order?

                          -ram

                          Comment


                            #14
                            For the most part it should be fine most times since the problem really arises on initial stop submission, not modification. If my memory serves me correctly, if you change an order to an invalid price, you would not get a "Rejected" state which is what triggers the termination of the strategy.
                            RayNinjaTrader Customer Service

                            Comment


                              #15
                              SampleATMStrategy

                              Hello, I'm having the same issue noted below, the createatmstrategy executes while an existing atm strategy is still running. I can't find the SampleATMStrategy mentioned in this post. Can you point me in the right direction? Thanks.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              651 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              577 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X