Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

GetAtmStrategyMarketPosition()

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

    GetAtmStrategyMarketPosition()

    Hi Support,



    I'm testing the ATM Strategy process and Methods.. I have a strategy that is working using StopLimtOrders.

    At times i need to check if the Market is flat as you would guess... I do this before executing some code but needs to check flat first...

    I'm using the example from your scripts and this it how it looks...


    I have a lot more code but just showing the section in discussion..

    I declared this..
    private string atmStrategyId = string.Empty;



    protected override void OnBarUpdate()

    {

    // Check if market flat
    if (GetAtmStrategyMarketPosition("atmStrategyId") == MarketPosition.Flat)
    Print("ATM Strategy position is currently flat");


    // I do some checks for a LONG signal then submit the order

    AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, limitPrice, stopPrice, TimeInForce.Gtc, orderId, "AtmStrategyTest_Name", 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)
    {

    isLongAtmStrategyCreated = true;

    }


    } // End OnBarUpdate


    I print to the output window the information so I can see whats happening.. and I get this...

    'GetAtmStrategyMarketPosition' method error: ATM strategy ID 'atmStrategyId' does not exist
    ATM Strategy position is currently flat

    and as each OnBarUpdate..keeps saying same thing....

    'GetAtmStrategyMarketPosition' method error: ATM strategy ID 'atmStrategyId' does not exist
    ATM Strategy position is currently flat

    GetAtmStrategyMarketPosition' method error: ATM strategy ID 'atmStrategyId' does not exist
    ATM Strategy position is currently flat



    So the documents for this method state this....

    Method Return Value


    MarketPosition.Flat

    MarketPosition.Long

    MarketPosition.Short


    Syntax


    GetAtmStrategyMarketPosition(string atmStrategyId)



    Does anyone have any ideas?



    Thanks
    Bruce







    #2
    Hello Bruce,

    Thank you for your post.

    I'm not seeing where you're generating the OrderId and AtmStrategyId like in this code sample from Sample ATM Strategy that comes built into the platform:

    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])
    {
    isAtmStrategyCreated = false; // reset atm strategy created check to false
    [B]atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();[/B]
    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;
    });
    }
    Can you confirm you are generating unique Ids for these for each entry?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Hello Bruce,

      You must create the ATM strategy first before making this validation if (GetAtmStrategyMarketPosition("atmStrategyId") == MarketPosition.Flat). You're basically using a method of a non-existing strategy. NinjaTrader_Kate already provided you with the code that you need.

      I hope this explanation helps.

      I build useful software systems for financial markets
      Generate automated strategies in NinjaTrader : www.stratgen.io

      Comment


        #4
        Hi Kate,



        All my code is correct when it comes to a working strategy..I just didn't list it all....

        On OnBarUpdate sometimes I need to I check if the Market Position is FLAT . I know I could probably do by checking the atmStrategyId but I was trying to use the
        method GetAtmStrategyMarketPosition()

        So like this....

        ** Please not not full code, just showing main functions **

        /////////////////// Code Below works except for GetAtmStrategyMarketPosition() method I get that error I stated before ////////////////////////////////////////////////

        OnBarUpdate()
        {


        if (BarsInProgress != 0)
        return;

        if (CurrentBars[0] < 20)
        return;

        // Make sure this strategy does not execute against historical data
        if (State == State.Historical)
        return;


        if (GetAtmStrategyMarketPosition("atmStrategyId") == MarketPosition.Flat)
        Print("ATM Strategy positions are currently flat");




        if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])

        // Code in this section for getting limitPrice and stopPrice


        isAtmStrategyCreated = false;
        atmStrategyId = GetAtmStrategyUniqueId();
        orderId = GetAtmStrategyUniqueId();

        // Must have ATM Strategy Named 'AtmStrategyTemplate' and configure target and stop loss to suit.
        AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, limitPrice, stopPrice, TimeInForce.Gtc, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) =>
        {

        //check that the atm strategy created did not result in error, and that the requested atm strategy matches the id in callback
        if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
        {

        isAtmStrategyCreated = true;


        }


        Print(String.Format("{0} Chart ", Instrument.FullName) + " Long StopLimt StopPrice : " + stopPrice + " LONG StopLimitOrder Submitted");
        Print(" ");
        }
        }
        );

        } // end OnBarUpdate



        So I get this message..

        'GetAtmStrategyMarketPosition' method error: ATM strategy ID 'atmStrategyId' does not exist..

        So does this mean I can't call the GetAtmStrategyMarketPosition() method before the atmStrategyId = GetAtmStrategyUniqueId() code ?


        But the thing is... the strategy is working so when an stoplimitorder is submitted I get the line on the chart... So that means there is a atmStrategyId..so the next OnBarUpdate
        shouldn't give me that error....but it does...


        One thing that I'm not clear on is the atmStrategyId type either STRING or INT ?


        Visual Studio code intelisense shows that it's a string...like the strategyTemplateName is so why isn't in quotations like the name is?

        The syntax for that method shows this as well...

        Syntax


        GetAtmStrategyMarketPosition(string atmStrategyId)


        I'm not sure if it's related to this.....I've tried a few didn't ways of coding this...


        Here is the sample code in the help guide..
        https://ninjatrader.com/support/help...rderstatus.htm

        It states ,....

        Notes:



        1. Changes to positions will not be reflected till at least the next OnBarUpdate() event after an order fill.

        2. If the ATM Strategy does not exist then MarketPosition.Flat returns
        (So I should be able to have the code before atmStrategyCreated)


        Thanks
        Bruce
        Last edited by traderhawk; 07-20-2022, 07:13 PM.

        Comment


          #5
          Originally posted by traderhawk View Post
          So I get this message..

          'GetAtmStrategyMarketPosition' method error: ATM strategy ID 'atmStrategyId' does not exist..

          So does this mean I can't call the GetAtmStrategyMarketPosition() method before the atmStrategyId = GetAtmStrategyUniqueId() code ?


          But the thing is... the strategy is working so when an stoplimitorder is submitted I get the line on the chart... So that means there is a atmStrategyId..so the next OnBarUpdate
          shouldn't give me that error....but it does...
          You used the ATM method GetAtmStrategyMarketPosition("atmStrategyId") before creating the ATM strategy. ATM strategy was basically non-existent at that time when you used its method.

          As I said, ATM strategy must be created first before you can use its method. Here's the fixed version of your code:

          Code:
          //CREATE ATM STRATEGY FIRST
          // 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])
          {
              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;
              });
          }
          
          // USE ATM STRATEGY'S METHOD
          if (GetAtmStrategyMarketPosition("atmStrategyId") == MarketPosition.Flat) Print("ATM Strategy positions are currently flat");
          Let me know if this helps remove the bug you're getting.

          I build useful software systems for financial markets
          Generate automated strategies in NinjaTrader : www.stratgen.io

          Comment


            #6
            Hello traderhawk,

            Thank you for your reply.

            I note you have quotes around atmStrategyId when you're calling GetAtmStrategyMarketPosition, but that's a variable that holds the generated ID and not the string that's generated itself. Try this:

            if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat)
            Print("ATM Strategy positions are currently flat");

            Please let us know if we may be of further assistance to you.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            54 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            72 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            44 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X