Round to interval: code snippet + problem - NinjaTrader Programming | futures io social day trading
futures io futures trading


Round to interval: code snippet + problem
Updated: Views / Replies:1,971 / 3
Created: by gregid Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

Round to interval: code snippet + problem

  #1 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

Round to interval: code snippet + problem

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?

Reply With Quote
 
  #2 (permalink)
Administrator: Retired Backtester
 Vendor: speedytradingservers.com 
Rennes France
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: IB/Kinetick
Favorite Futures: Futures
 
sam028's Avatar
 
Posts: 3,366 since Jun 2009
Thanks: 3,573 given, 3,982 received

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



Success requires no deodorant! (Sun Tzu)
Reply With Quote
The following user says Thank You to sam028 for this post:
 
  #3 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received


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

Reply With Quote
 
  #4 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

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

Reply With Quote

Reply



futures io > > > > > Round to interval: code snippet + problem

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Code snippet for calculating slope Saroj NinjaTrader Programming 2 April 6th, 2011 10:44 AM
MC: Simple strategy code problem / Easy Language true EasyLanguage Programming 4 December 19th, 2010 09:48 AM
On finding the optimal settings for a range interval trendisyourfriend The Elite Circle 5 December 7th, 2010 02:53 PM
Reading Interval setting in Strategy leduc NinjaTrader Programming 4 May 26th, 2010 07:48 PM
Possible to Add/Change Default Interval Settings in NT? CDesai13 NinjaTrader 1 December 14th, 2009 08:29 AM


All times are GMT -4. The time now is 06:52 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-13 in 0.09 seconds with 19 queries on phoenix via your IP 54.90.92.204