Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATM Strategy Create Issue

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

    ATM Strategy Create Issue

    I have a Ninja Script strategy that uses ATM for orders. I have been using playback to test and also did some live trading last Friday, but today when I started to use the Strategy it didn't work correctly. Every time I create an ATM order, I am getting an error now for some reason. Here is my code to create an ATM Order:

    private void Enter_ATM(OrderAction orderAction, int stopDiff)
    {
    //ATM Entry
    isAtmStrategyCreated = false; // reset atm strategy created check to false
    atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();
    string ATMName = Enter_ATM_GetATMName(stopDiff);
    Print("Start ATM Order - Name " + ATMName);
    AtmStrategyCreate(orderAction, OrderType.Market, 0, 0, TimeInForce.Day, orderId, ATMName, atmStrategyId, (atmCallbackErrorCode, atmCallBackId) => {
    //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
    if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
    isAtmStrategyCreated = true;
    else
    Print("ATM Error: " + atmCallbackErrorCode.ToString());
    });

    // Check that atm strategy was created before checking other properties
    Print("ATMStrategyCreated: " + isAtmStrategyCreated);
    if (!isAtmStrategyCreated)
    return;
    }

    In the NinjaScript Output, here is what I am seeing:

    Start ATM Order - Name TNT TS SL 15
    ATMStrategyCreated: False
    ATM Error: NoError

    The order is created and I can see the OCO, but the problem I am running into, is that when I try to call GetAtmStrategyMarketPosition(atmStrategyId) an error is returned saying the ATM Strategy ID "...." does not exist

    When I run the system in playback it is showing ATMStrategyCreated: True and everything works fine.

    I am not sure what may have changed or what the issue may be. Any ideas?

    Thank you,
    Jeremy​
    Last edited by jtaeoalii; 07-12-2023, 08:20 AM.

    #2
    Hello Jeremy,

    Thanks for your post.

    This error message is expected if the method is being called when there is no atmStrategyId that exists. This can happen if you are calling the method and the atmStrategyId has changed in your script or is no longer active.

    The atmStrategyId could be printed out after calling AtmStrategyCreate() method to see what the atmStrategyId being used is. A print could also be added before calling GetAtmStrategyMarketPosition() to see if you are accessing the same atmStrategyId value that was supplied to AtmStrategyCreate().

    See this help guide page for more information about using ATMs in a custom NinjaScript strategy: https://ninjatrader.com/support/help...strategies.htm

    Atm Strategy Methods: https://ninjatrader.com/support/help...tsub=using+atm

    You could consider viewing the SampleAtmStrategy strategy that comes default with NinjaTrader to see how it uses the GetAtmStrategyMarketPosition() method. This reference sample could be compared to your script to see where differences might be. To view the script, open a New > NinjaScript Editor window, open the



    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thanks for the response Brandon. I can definitely understand that this is the expected outcome when the atmStrategyId doesn't exist. What I don't understand is why the atmStrageyId doesn't exist. The code used, is only slightly modified from the Sample to meet my needs and it looks like the error is happening immediately after the call. The other thing that I don't understand is that this only happens on live and it only started happening today. I didn't have the issue last week.

      If you look at my code and the code from the sample, you will see that if the ATM is not created, then it prints the ATM error to the output. Right now in live, the system prints "ATM Error: NoError." and the trade is still executed along with the correct OCO. If the ATM Template name does not exist, it usually prints an error Panic.

      Because the ErrorCode is noError, the most likely culprit is that the atmCallBackId doesn't match the atmStrategyId for some reason and too be honest, I don't quite understand how this works, that part is just copied directly from the Sample. Any ideas why the atmCallBackId is different then the atmStrategyId? I noticed that my ATM template is set up to be GTC and I am passing in TimeInForce.Day. Would that cause an issue?

      If the atmCallBackId is different, should I just update my atmStrategyId to match the atmCallBackId and I will be fine?

      Any other recommendations?

      Thanks again for the help Brandon!
      Jeremy

      Comment


        #4
        Hello Jeremy,

        Thanks for your notes.

        When this error message appears it is likely that something in your logic is causing it to occur and either isn't storing the correct atmStrategyId or is calling the method when it shouldn't be called.

        In your script, ensure that you are checking if atmStrategyId > 0 and printing the GetAtmStrategyMarketPosition() within that condition. An example of this is seen in the SampleAtmStrategy reference sample that comes default with NinjaTrader.

        From the reference sample:

        Code:
        if (atmStrategyId.Length > 0)
        {
            //...
            Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
        {
        You should check if (orderId.Length == 0 && atmStrategyId.Length == 0 && <your condition>) before calling isAtmStrategyCreated = false;, atmStrategyId = GetAtmStrategyUniqueId();, and orderId = GetAtmStrategyUniqueId();

        From the reference sample:

        Code:
        if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
        {
            isAtmStrategyCreated = false; // reset atm strategy created check to false
            atmStrategyId = GetAtmStrategyUniqueId();
            orderId = GetAtmStrategyUniqueId();
            AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) => {
                //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
                if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
                    isAtmStrategyCreated = true;
        });
        }
        And, reset the atmStrategyId if the strategy has terminated.

        From the reference sample:

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

        If the script is not behaving as expected you will need to print out the atmStrategyId value and debug your logic further as mentioned in my previous post.​
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Thanks for the help Brandon! The issue was related to the fact that I was trying to use the GetAtmStrategyMarketPosition with the atmStrategyId before it had updated, so the code:

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

          was actually clearing out the atmStrategyId before it had updated. The ATM documentation shows that you have to wait until the next OnBarUpdate() for it to be updated. So I created a variable to hold the market position after creating the Order until after the OnBarUpdate().

          Thanks again Brandon!​

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          65 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          139 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X