Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Get ATM Result inside BarUpdate

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

    Get ATM Result inside BarUpdate

    Hello,

    I've been running examples of the AtmStrategySample.cs file given in Ninjatrader, and I see I must wait for the next bar update to check if the Atm has been created successfully,
    is there a way that I can block, lock or invoke on the main thread when isAtmStrategyCreated is set to true instead of waiting for next tick/bar?

    Code:
    protected override void OnBarUpdate() { if (CurrentBar < BarsRequiredToTrade) return; // HELP DOCUMENTATION REFERENCE: Please see the Help Guide section "Using ATM Strategies" under NinjaScript--> Educational Resources--> http://ninjatrader.com/support/helpGuides/nt8/en-us/using_atm_strategies.htm // Make sure this strategy does not execute against historical data if(State == State.Historical) return; // 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; }); } // Check that atm strategy was created before checking other properties if (!isAtmStrategyCreated) return; // Check for a pending entry order 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) { // Print out some information about the order to the output window Print("The entry order average fill price is: " + status[0]); Print("The entry order filled amount is: " + status[1]); Print("The entry order order state is: " + status[2]); // 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 else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat) atmStrategyId = string.Empty; if (atmStrategyId.Length > 0) { // You can change the stop price if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat) AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId); // Print some information about the strategy to the output window, please note you access the ATM strategy specific position object here // the ATM would run self contained and would not have an impact on your NinjaScript strategy position and PnL Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId)); Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId)); Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ; Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ; } }

    #2
    Also nonrelated but how can I paste code in a friendly way with the CODE tags, all format is gone when pasting here.

    Comment


      #3
      Hello Waxavi,

      Thank you for your note.

      You could monitor in an Account level Order Update for when that actually gets updated. I'm attaching a simple script that demonstrates, however, your mileage may vary here as to whether that's faster or not depending on the Calculate setting of the script.

      As far as copying and pasting code, something that works for me quite well is to paste the text into a notepad file, then recopy it and paste it into the code tags. For some reason that tends to work a bit better.

      Please let us know if we may be of further assistance to you.
      Attached Files
      Kate W.NinjaTrader Customer Service

      Comment


        #4
        I think it's working, will test it a bit more.
        Thanks

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kevinenergy, 02-17-2023, 12:42 PM
        118 responses
        2,778 views
        1 like
        Last Post kevinenergy  
        Started by briansaul, Today, 05:31 AM
        0 responses
        9 views
        0 likes
        Last Post briansaul  
        Started by traderqz, Yesterday, 12:06 AM
        11 responses
        28 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by PaulMohn, Today, 03:49 AM
        0 responses
        8 views
        0 likes
        Last Post PaulMohn  
        Started by inanazsocial, Today, 01:15 AM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Jason  
        Working...
        X