NexusFi: Find Your Edge


Home Menu

 





Round to interval: code snippet + problem


Discussion in NinjaTrader

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




 
Search this Thread

Round to interval: code snippet + problem

  #1 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 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?
What broker to use for trading palladium futures
Commodities
About a successful futures trader who didn´t know anyth …
Psychology and Money Management
Cheap historycal L1 data for stocks
Stocks and ETFs
ZombieSqueeze
Platforms and Indicators
MC PL editor upgrade
MultiCharts
 
  #2 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,765 since Jun 2009
Thanks Given: 3,825
Thanks Received: 4,629

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



Success requires no deodorant! (Sun Tzu)
Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 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: 650 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


© 2024 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 - Privacy Policy - Downloads - Top
no new posts