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, 05-11-2026, 05:56 AM
    0 responses
    52 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    29 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    194 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Started by CaptainJack, 04-24-2026, 11:07 PM
    0 responses
    355 views
    0 likes
    Last Post CaptainJack  
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    274 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X