Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
        });
    }
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by JackyP, 07-25-2023, 08:39 AM
    3 responses
    313 views
    0 likes
    Last Post Lancer
    by Lancer
     
    Started by TiffanyMiller, Yesterday, 10:19 PM
    0 responses
    2 views
    0 likes
    Last Post TiffanyMiller  
    Started by algospoke, 05-13-2024, 06:53 PM
    12 responses
    135 views
    0 likes
    Last Post algospoke  
    Started by psangram, Yesterday, 06:35 PM
    0 responses
    4 views
    0 likes
    Last Post psangram  
    Started by Alaina19, 07-14-2024, 04:38 PM
    2 responses
    16 views
    0 likes
    Last Post Alaina19  
    Working...
    X