Trading Articles
Article Categories
Article Tools
Limit Values to Even Numbers Only
Updated October 10, 2010
Top Posters
looks_one
Big Mike
with 3 posts (1 thanks)
looks_two
DavidHP
with 3 posts (0 thanks)
looks_3
bukkan
with 2 posts (1 thanks)
looks_4
tarantino
with 1 posts (2 thanks)
trending_up
2,700 views
thumb_up
4 thanks given
group
2 followers
forum
9 posts
attach_file
0 attachments
Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
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
(If you already have an account, login at the top of the page)
Limit Values to Even Numbers Only
(login for full post details)
#1 (permalink )
New Orleans, La (Mardi Gras City)
Experience: Advanced
Platform: NinjaTrader
Broker: Ninjatrader / Optimus Futures / AmpFutures
Trading: ES / 6E / 6B / CL
Posts: 1,348 since Aug 2009
Thanks: 9,463 given,
2,360
received
Can anyone tell me how to limit the parameters in an indicator to only EVEN numbers?
I want the min value to be 2.
I want the user to be able to select the Value but if they choose an ODD number I want only EVEN numbers to be allowed.
Kind of like when you try to enter a zero and the Math.Max(1,value) is in the code and an error is given that the value is not allowed.
Here is where the code should go I think:
Code
public int ValueToShow
{
get { return valuetoShow; }
set { valuetoShow = Math.Max(2, value); }
}
Thanks,
Rejoice in the Thunderstorms of Life . . .
Knowing it's not about Clouds or Wind . . .
But Learning to Dance in the Rain ! ! !
Best Threads (Most Thanked) in the last 7 days on futures io
(login for full post details)
#3 (permalink )
Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: My own custom solution
Trading: Emini Futures
Posts: 49,762 since Jun 2009
Thanks: 32,302 given,
97,533
received
A simple unfancy way is to let them input whatever, then just do a check once in your code to see if it's odd, and if so make it even.
How to check if a number is odd or even? (C# Knowledge Base) • Geekpedia
Code
int Num = 5 ;
if ( Num & 1 )
{
// It's odd
}
else
{
// It's even
}
To get it to update instantly as a user enters an odd number in the dialog box you have to use Refresh() or Update() in the get/set or one of those things I've since forgotten after not programming in C# in a while I'm sure someone else can help if needed.
Mike
(login for full post details)
#4 (permalink )
New Orleans, La (Mardi Gras City)
Experience: Advanced
Platform: NinjaTrader
Broker: Ninjatrader / Optimus Futures / AmpFutures
Trading: ES / 6E / 6B / CL
Posts: 1,348 since Aug 2009
Thanks: 9,463 given,
2,360
received
Big Mike
A simple unfancy way is to let them input whatever, then just do a check once in your code to see if it's odd, and if so make it even.
Mike
Thanks,
I used that method when I first wrote the indicator and it does change the value to even.
However it seems as if I'm missing something and there should be a way to accomplish this at user input of the incorrect number.
Like when entering a decimal value in a non-decimal field. An error occurs immediately or the value changes back to the original contents.
Rejoice in the Thunderstorms of Life . . .
Knowing it's not about Clouds or Wind . . .
But Learning to Dance in the Rain ! ! !
(login for full post details)
#5 (permalink )
Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: My own custom solution
Trading: Emini Futures
Posts: 49,762 since Jun 2009
Thanks: 32,302 given,
97,533
received
DavidHP
Thanks,
I used that method when I first wrote the indicator and it does change the value to even.
However it seems as if I'm missing something and there should be a way to accomplish this at user input of the incorrect number.
Like when entering a decimal value in a non-decimal field. An error occurs immediately or the value changes back to the original contents.
Yes. There is. I just can't think of an indicator that does it at the moment so can't post sample code, and I've forgotten how to do it exactly, and am no longer using NT.
Perhaps @bukkan can help, or @tarantino or @gomi or @fluxsmith , or any number of other excellent programmers on the site.
Mike
(login for full post details)
#6 (permalink )
Calcutta, India
Experience: Intermediate
Platform: ArthaChitra
Posts: 278 since Jun 2009
Thanks: 161 given,
269
received
DavidHP
Can anyone tell me how to limit the parameters in an indicator to only EVEN numbers?
I want the min value to be 2.
I want the user to be able to select the Value but if they choose an ODD number I want only EVEN numbers to be allowed.
Kind of like when you try to enter a zero and the Math.Max(1,value) is in the code and an error is given that the value is not allowed.
Here is where the code should go I think:
Code
public int ValueToShow
{
get { return valuetoShow; }
set { valuetoShow = Math.Max(2, value); }
}
Thanks,
try like this
Code
public int ValueToShow
{
get{return valuetoshow;}
set{valuetoshow = Math.Max(2,value); //minimum value 2
if (valuetoshow % 2 != 0) valuetoshow -= 1; } //make sure its even
//or +1 depending on what you want. for eg if you enter 15 and want the figure to be 14 then subtract it by 1 or if you want it to be 16 then add it by 1.
}
The following user says Thank You to bukkan for this post:
(login for full post details)
#7 (permalink )
Willowbrook, IL
Posts: 32 since Sep 2010
Thanks: 71 given,
39
received
How about :
Code
public int ValueToShow
{
get { return valuetoShow; }
set { valuetoShow = Math.Max(2, value % 2 == 0 ? value : value + 1); }
}
Bukkan: thanks, seems like we were answering at the same time
(login for full post details)
#8 (permalink )
Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: My own custom solution
Trading: Emini Futures
Posts: 49,762 since Jun 2009
Thanks: 32,302 given,
97,533
received
Look at that. Ask and ye shall receive
And didn't even need the Update() code I was concerned about.
Mike
The following user says Thank You to Big Mike for this post:
(login for full post details)
#9 (permalink )
Calcutta, India
Experience: Intermediate
Platform: ArthaChitra
Posts: 278 since Jun 2009
Thanks: 161 given,
269
received
tarantino
How about
:
Code
public int ValueToShow
{
get { return valuetoShow; }
set { valuetoShow = Math.Max(2, value % 2 == 0 ? value : value + 1); }
}
Bukkan: thanks, seems like we were answering at the same time
(login for full post details)
#10 (permalink )
New Orleans, La (Mardi Gras City)
Experience: Advanced
Platform: NinjaTrader
Broker: Ninjatrader / Optimus Futures / AmpFutures
Trading: ES / 6E / 6B / CL
Posts: 1,348 since Aug 2009
Thanks: 9,463 given,
2,360
received
Big Mike
Look at that. Ask and ye shall receive
And didn't even need the Update() code I was concerned about.
Mike
WOW,
Thanks guys that was super.
It works just as I wanted.
Rejoice in the Thunderstorms of Life . . .
Knowing it's not about Clouds or Wind . . .
But Learning to Dance in the Rain ! ! !
Last Updated on October 10, 2010
Right now
Ongoing
Right now
February
Coming soon
March
Register to Attend
Elite only