Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy property relative to price

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

    Strategy property relative to price

    Hi,

    I would like to add a dataserie and indicators in my strategy that the time period and other variables would be predetermined and relative to the price of the symbol. For example, for a ticker with a stock price of 20$ the strategy would add a dataserie of 10 minutes and if above 50$ it would add a dataserie of 20 minutes.

    So in summary, I would like the ninjascript to read the symbol current price before the OnBarUpdate method, just once at the launch of the strategy. Is there any way to do this?

    Thanks for your help.

    #2
    Hi RT001, thanks for your question.

    A BarsRequest can be done in State.Configure to get prices for the instrument. After the prices are received you can make the decision of what AddDataSeries parameters to use. I attached my test script here.

    Best regards,
    -ChrisL
    Attached Files

    Comment


      #3
      Oooh nice! Many thanks Chris! I didn't think it was possible.

      Comment


        #4
        Finally, this was super complicated... took me a few hours to figure this one out, I am not that far in C#.

        I was totally unable to return, change or access the values generated by BarsRequest. I seems because of the order of execution, you need to brute-force the completion of the BarsRequest.Request(). I found the answer deep down in another thread. So for anybody not being able to read the values outside of the scope of the BarRequest section, here's how to do it and the thread that explains the reason.

        ----------

        you first declare a variable outside of the scope (before) of the BarsRequest

        Code:
        namespace NinjaTrader.NinjaScript.Strategies
        {
        public class MyStrat : Strategy
        {
        
        private int barsRequestReturnVariable; // this is the variable we want to return

        Then you run the BarsRequest, and the if statement will force the request to end before the code following. If you don't do this, BarsRequest will execute after the rest!!

        Important: the following code is in the State == State.Configure section of a strategy.

        Code:
        object isBarsCompleted = null;
        
        BarsRequest barsRequest = new BarsRequest(Instrument.GetInstrument(Instrument.FullName), 0)
        {
        TradingHours = TradingHours.Get(tradeHours)
        };
        
        // Request the bars
        barsRequest.Request(new Action<BarsRequest, ErrorCode, string>((bars, errorCode, errorMessage) =>
        {
        if (errorCode != ErrorCode.NoError)
        {
        // Handle any errors in requesting bars here
        NinjaTrader.Code.Output.Process(string.Format("Err or on requesting bars: {0}, {1}",
        errorCode, errorMessage), PrintTo.OutputTab1);
        return;
        }
        
        barsRequestReturnVariable = (int)Math.Floor(bars.Bars.GetClose(0));
        isBarsCompleted = true;
        
        }));
        
        if (isBarsCompleted == null) { System.Threading.Thread.Sleep(500); }; } // just a small pause, to let run the BarRequest first
        
        Print(barsRequestReturnVariable);
        This code is for the sake of explaining the problem, I don't recommend using this script as is.

        The thread explaining it (avoid the while statement in it it can lead to system freezes):
        https://ninjatrader.com/support/foru...ope#post719650
        Last edited by RT001; 11-18-2021, 11:39 PM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        41 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        64 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 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