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 NullPointStrategies, Today, 05:17 AM
    0 responses
    50 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X