NexusFi: Find Your Edge


Home Menu

 





Round to interval: code snippet + problem


Discussion in NinjaTrader

Updated
    1. trending_up 4,363 views
    2. thumb_up 1 thanks given
    3. group 3 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623

Hi

In my strategy I need to round a number to multiple of an interval and have a choice of rounding up, down, and to the nearest mulitple of interval. I've found following code snippet that should do the job exactly as I need it:


 
Code
                            
public static decimal RoundToInterval(decimal valuedecimal intervalIntervalRounding rounding)
{
   
// Guard against the divide by zero exception
   
if (interval == 0)
      return 
decimal.MinValue;
 
   
decimal result value interval;
 
   switch (
rounding)
   {
          case 
IntervalRounding.Nearest:
             return (
decimal.Round(result0MidpointRounding.AwayFromZero) * interval);
          case 
IntervalRounding.Up:
             return (
decimal.Ceiling(result) * interval);
          case 
IntervalRounding.Down:
             return (
decimal.Floor(result) * interval);
          default:
             return 
decimal.MinValue;
   }
 

Public enum required would be:


 
Code
                            
public enum IntervalRounding {Nearest 0Up 1Down 2
Now the problem is I can't get it to work. And when I use:

 
Code
                            
RoundToInterval(3.832Down); 

Expected behavior would be number 2 but...

I receive an error:
"The name 'Down' does not exist in the current context"

Am I missing something?


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Iran Peace Expired NO: Ceasefire on Life Support, OPEC a …
Prediction Markets & Event Contracts
$4.5M Floods Russia Nuclear Contract in 24 Hours -- Krem …
Prediction Markets & Event Contracts
May 26 Deadline Dies at 7.5% as US Strikes Iran During C …
Prediction Markets & Event Contracts
Iran Airspace Contract Surges to 33.5% as Project Freedo …
Prediction Markets & Event Contracts
More Than Capable: Hegseths War Warning Validates $114M …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
8 thanks
Darmok and Jalad at Tanagra
1 thanks
  #2 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,758 since Jun 2009
Thanks Given: 3,828
Thanks Received: 4,644

Try with:
 
Code
                            
RoundToInterval(3.832IntervalRounding.Down); 



Success requires no deodorant! (Sun Tzu)
Reply With Quote
Thanked by:
  #3 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623


Thanks Sam,

That was what I was missing.
Now it works for integers (RoundToInterval(3,2,IntervalRounding.Down) but stops when I use the example above (3.83, 2) - it says: Argument 1: cannot convert from double to decimal.

Do you know how to correct this or eventually know any other way of achieving what I need?

Thanks again


Started this thread Reply With Quote
  #4 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623

Well, I have received my C# humility lesson. My programming experience is very limited and I have no experience with strong typing languages at all...

Anyway for my example to work I had to use following code:

 
Code
                            
RoundToInterval(Convert.ToDecimal(3.83), Convert.ToDecimal(2), IntervalRounding.Down


Started this thread Reply With Quote




Last Updated on December 9, 2009


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts