Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't calculate the recent closed bar, instead it calculates the furthest closed bar

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

    Can't calculate the recent closed bar, instead it calculates the furthest closed bar

    i made custom buttons and they place orders based off of the most recent closed candle / bar.

    ive been trying to get it to calculate the most recent bar but it always goes to the furthest one back. idk what to do.



    Code:
            #region Button Click Events
    
            #region Buy High Button 1
            protected void BuyHighButtonClick(object sender, RoutedEventArgs e)
            {
                ForceRefresh();
                double closePrice = Close[0]; // Get the close of the last closed bar
                Print($"Placing Buy Limit order at {closePrice}");
                EnterLongLimit(0, true, 1, closePrice, "Buy Limit @C");
            }
            #endregion
    
            #region Sell Low Button 2
            protected void SellLowButtonClick(object sender, RoutedEventArgs e)
            {
                ForceRefresh();
                double closePrice = Close[0]; // Get the close of the last closed bar
                Print($"Placing Sell Limit order at {closePrice}");
                EnterShortLimit(0, true, 1, closePrice, "Sell Limit @C");
            }
            #endregion
    
            #region Buy Market Button 3
            protected void BuyMarketButtonClick(object sender, RoutedEventArgs e)
            {
                ForceRefresh();
                double highPrice = High[0] + TickSize; // Get the high of the last closed bar + 1 tick
                Print($"Placing Buy Stop Market order at {highPrice}");
                EnterLongStopMarket(0, true, 1, highPrice, "Buy Stop +1T");
            }
            #endregion
    
            #region Sell Market Button 4
            protected void SellMarketButtonClick(object sender, RoutedEventArgs e)
            {
                ForceRefresh();
                double lowPrice = Low[0] - TickSize; // Get the low of the last closed bar - 1 tick
                Print($"Placing Sell Stop Market order at {lowPrice}");
                EnterShortStopMarket(0, true, 1, lowPrice, "Sell Stop -1T");
            }
            #endregion​

    #2
    Hello unpacify,

    In your button event you are no longer in the NinjaScript event driven context so using a BarsAgo there wont work directly. You would need to surround that code in a TriggerCustomEvent to get it back to the NinjaScript context.

    Code:
    private void BuyHighButtonClick(object sender, RoutedEventArgs e)
    {​
        TriggerCustomEvent(o =>
        {​
           // your code in here
        });
    }

    Comment

    Latest Posts

    Collapse

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