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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    128 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    65 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    117 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X