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

Using division on a double and System.Timespan

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

    Using division on a double and System.Timespan

    I am trying to create an indicator that plots how far a volume bar traveled Open[0] - Close[0] divided by the time it took to complete Time[0] - Time[1] but I have not been able to figure out how to convert the timespan to a double to complete the calculation

    Code:
     ema = EMA((Open[0] - Close[0]) / (Time[0] - Time[1]), 10);

    #2
    Hello MisterGee,

    Thanks for your post.

    To accomplish this, you would need to save the TimeSpan to a variable called something like "myTime" and then use myTime.TotalHours or myTime.TotalMinutes depending on the most significant measurement you want to get.

    For example, if Time[0] - Time[1] is a value of 1 minute (00:01:00) and you use myTime.TotalMinutes, this will return a double value of 1.

    Another example is if Time[0] - Time[1] is a value of 1 minute and you use myTime.TotalHours, this will return a double value of 0.0166666666666667.

    Further, EMA() requires you to pass in a Series<double> and an int. You would need to create a custom Series<double> variable, assign your calculations to that Series<double> variable, and use the Series<double> when calling EMA.

    For example:

    //class-level variables
    private TimeSpan myTime;
    private Series<double> mySeriesDouble;

    //OnStateChange() State.DataLoaded
    mySeriesDouble = new Series<double>(this);

    //OnBarUpdate()
    myTime = Time[0] - Time[1];

    mySeriesDouble[0] = (Open[0] - Close[0]) / myTime.TotalMinutes;

    Print(EMA(mySeriesDouble, 10)[0]);


    See the help guide documentation below for more information.

    Creating Custom Series: https://ninjatrader.com/support/help...t8/seriest.htm
    EMA: https://ninjatrader.com/support/help...onential_e.htm

    For more information about converting TimeSpans to Doubles, you would need to research this topic more by doing a Google search for something like "Convert TimeSpan to Double C#".



    Last edited by NinjaTrader_BrandonH; 06-13-2023, 06:58 AM.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Have you considered It seems, reading between the lines, that you're either trying to divide the candle body size by some unit of time e.g. the number of minutes or the number of seconds. Have you considered doing it that way e.g. ema = EMA((Open[0] - Close[0]) / (Time[0] - Time[1].TotalSeconds), 10); ? You'll want to use a Math.Max in there to control for if the times are the same so you don't get a divide by zero... or you could use minutes... or hours... or something else appropriate. I would put this into a series first then EMA that.

      Edit: re-reading this thread I see now that NinjaTrader_BrandonH suggested much the same thing here and I read past that at first. The only thing I would add to that is where he's saying "mySeriesDouble = (Open[0] - Close[0] etc." it should be "mySeriesDouble[0] = (Open[0] - Close[0] etc." because you can't assign a series variable to be a double value.
      Last edited by QuantKey_Bruce; 06-12-2023, 05:06 AM.
      Bruce DeVault
      QuantKey Trading Vendor Services
      NinjaTrader Ecosystem Vendor - QuantKey

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by StockTrader88, 03-06-2021, 08:58 AM
      45 responses
      3,992 views
      3 likes
      Last Post johntraderuser2  
      Started by TAJTrades, Today, 09:46 AM
      0 responses
      7 views
      0 likes
      Last Post TAJTrades  
      Started by rhyminkevin, Yesterday, 04:58 PM
      5 responses
      62 views
      0 likes
      Last Post dp8282
      by dp8282
       
      Started by realblubb, Today, 09:28 AM
      0 responses
      8 views
      0 likes
      Last Post realblubb  
      Started by AaronKoRn, Yesterday, 09:49 PM
      1 response
      19 views
      0 likes
      Last Post Rikazkhan007  
      Working...
      X