Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Question about BarsSinceEntryExecution()

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

    Question about BarsSinceEntryExecution()

    Hello,

    I'm working on a Ninjascript strategy that uses ATM orders. Is there a way to use BarsSinceEntryExecution() with those orders? If so what do I use for signalName?
    No matter what I try I keep getting a return value of '-1' even several bars after the order has been submitted.

    Thanks!



    From the reference material:
    Definition


    Returns the number of bars that have elapsed since the last entry. When a signal name is provided, the number of bars that have elapsed since that last specific entry will be returned. Method Return Value


    An int value that represents a number of bars. A value of -1 will be returned if a previous entry does not exist.



    Syntax
    BarsSinceEntryExecution()
    BarsSinceEntryExecution(string signalName)



    The following method signature should be used when working with multi-time frame and instrument strategies:



    BarsSinceEntryExecution(int barsInProgressIndex, string signalName, int entryExecutionsAgo)

    My entry method:
    Code:
    #region Order Entry Method
            private void EnterAtmOrder(OrderAction orderAction)
            {
                // Ensure atmStrategyId and atmStrategyOrderId are initialized
                if (atmStrategyId == null) atmStrategyId = string.Empty;
                if (atmStrategyOrderId == null) atmStrategyOrderId = string.Empty;
    
                // Check if the state is less than realtime
                if (State < State.Realtime)
                {
                    Print("A " + orderAction + " order has been placed on historical data.");
                    return;
                }
    
                if (atmStrategyId.Length == 0)
                {
                    // Assign a new GUID to atmStrategyId
                    atmStrategyId = GetAtmStrategyUniqueId();
                    atmStrategyOrderId = GetAtmStrategyUniqueId(); // Ensure this is unique as well
    
                    // Retrieve the current Bid and Ask prices
                    double askPrice = GetCurrentAsk(0);
                    double bidPrice = GetCurrentBid(0);
    
                    // Print both the Bid and Ask prices
                    Print("Current Ask is: " + askPrice);
                    Print("Current Bid is: " + bidPrice);
    
                    // Determine the price based on orderAction
                    double price = 0;
                    if (orderAction == OrderAction.Buy)
                    {
                        price = askPrice; // Use the Ask price for Buy orders
                    }
                    else if (orderAction == OrderAction.Sell)
                    {
                        price = bidPrice; // Use the Bid price for Sell orders
                    }
    
                    // Create the ATM strategy with a limit order at the determined price
                    AtmStrategyCreate(
                        orderAction,
                        OrderType.Limit,
                        price,
                        0,
                        TimeInForce.Day,
                        atmStrategyOrderId,
                        "ATM MADD Custom",
                        atmStrategyId,
                        (atmCallbackErrorCode, atmCallbackId) =>
                        {
                    // checks that the callback is returned for the current atmStrategyId stored
                    if (atmCallbackId == atmStrategyId)
                            {
                        // check the atm callback for any error codes
                        if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
                                {
                            // if no error, set private bool to true to indicate the atm strategy is created
                            isAtmStrategyCreated = true;
                                    orderEntryTime = DateTime.Now; // Record the time of entry
                            hasOrderBeenChecked = false; // Reset the check flag on order creation
                        }
                            }
                        }
                    );
                }
            }
            #endregion​

Latest Posts

Collapse

Topics Statistics Last Post
Started by CarlTrading, 03-31-2026, 09:41 PM
1 response
45 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by CarlTrading, 04-01-2026, 02:41 AM
0 responses
21 views
0 likes
Last Post CarlTrading  
Started by CaptainJack, 03-31-2026, 11:44 PM
0 responses
31 views
1 like
Last Post CaptainJack  
Started by CarlTrading, 03-30-2026, 11:51 AM
0 responses
50 views
0 likes
Last Post CarlTrading  
Started by CarlTrading, 03-30-2026, 11:48 AM
0 responses
42 views
0 likes
Last Post CarlTrading  
Working...
X