-
Rounding or Modulus
I have had only one programming class a long time ago and am in over my head (Shake is pretty dumb.) All I want to do is round down to the nearest 0.25 increment.
Say my number is 1879.92 (closer to 1880 than 1879.75), I would like a value of 1879.75 returned.
If the number is 1764.27, I would like 1764.25 returned.
I think modulus only works for returning whole numbers.
Is there an easier way than using a nested if statement like?
double price;
double roundedprice;
double remainder;
//price is calculated earlier in another section of code
roundedprice = Round(price);
remainder = price - roundedprice;
if (remainder >= 0.75) { price = roundedprice + 0.75 }
if (remainder >= 0.50) { price = roundedprice + 0.50 }
if (remainder >= 0.25) { price = roundedprice + 0.25 }
if (remainder >= 0.00) { price = roundedprice + 0.00 }
Any thoughts?
The actual part of code that I am trying to insert this into follows:
Code:
// Condition set 1
if (DefaultInput[0] > ChandelierSAR(5, 3.5).Chandelier[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
EnterShortStopLimit(DefaultQuantity, ChandelierSAR(5, 3.5).Chandelier[0], ChandelierSAR(5, 3.5).Chandelier[0], "");
Alert("MyAlert0", Priority.High, "", "", 1, Color.White, Color.Black);
}
// Condition set 2
if (DefaultInput[0] < ChandelierSAR(5, 3.5).Chandelier[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
ExitShortStopLimit(ChandelierSAR(5, 3.5).Chandelier[0], ChandelierSAR(5, 3.5).Chandelier[0], "", "");
}
-
Re: Rounding or Modulus
Are all of the numbers to a maximum of 2 decimal places?
-
Re: Rounding or Modulus
Still messy but....
Code:
price = remainder >= 0.75 ? roundedprice + 0.75 :
remainder >= 0.5 ? roundedprice + 0.5 :
remainder >= 0.25 ? roundedprice + 0.25 :
roundedprice;
Or something along the lines of multiplying by 100, converting to integer, taking the modulo 25, doing the business and dividing by 100....
-
Re: Rounding or Modulus
Yes, two number decimals only.
-
Re: Rounding or Modulus
hello, i just give it a try. here's what i did.
remainder = price - Math.Truncate(price);
price = Math.Truncate(price) + (Math.Truncate(remainder / 0.25) * .25);
hope that helps. =)
-
Re: Rounding or Modulus
I guess I left out a valuable bit of information:
for "Condition Set 1" the price needs to be rounded down to the nearest quarter
for "Condition Set 2" the price needs to be rounded UP to the nearest quarter
I guess I need to rethink the code a bit since I have to insert that portion inside the cases now.
I had the following: renaming roundedprice = chandyRounded, remainder = chancyRemainder, and price = ChandelierSAR(5, 3.5).Chandelier[0]
Code:
private double chandyRounded = 1; // Default setting for ChandyRounded
private double chandyRemainder = 1; // Default setting for ChandyRemainder
private double roundUp = 1; // Default setting for RoundUp
private double roundDown = 1; // Default setting for RoundDown
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
chandyRounded = Round(ChandelierSAR(5, 3.5).Chandelier[0];
chandyRemainder = ChanderlierSAR(5, 3.5).Chandelier[0] - chandyRounded;
// Condition set 1
if (DefaultInput[0] > ChandelierSAR(5, 3.5).Chandelier[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
EnterShortStopLimit(DefaultQuantity, ChandelierSAR(5, 3.5).Chandelier[0], ChandelierSAR(5, 3.5).Chandelier[0], "");
Alert("MyAlert0", Priority.High, "", "", 1, Color.White, Color.Black);
}
// Condition set 2
if (DefaultInput[0] < ChandelierSAR(5, 3.5).Chandelier[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
ExitShortStopLimit(ChandelierSAR(5, 3.5).Chandelier[0], ChandelierSAR(5, 3.5).Chandelier[0], "", "");
}
-
Re: Rounding or Modulus
Note... there is an [Edit] button against your post, to save multiple posts...
-
Re: Rounding or Modulus
Could you please repost the code above between code tags eg:
[C0DE]
nicely formatted code goes here
[/C0DE]
the zero in CODE should be a letter O.
-
Re: Rounding or Modulus
Here is what I have so far.
Code:
private double chandyRounded = 1; // Default setting for ChandyRounded
private double chandyRemainder = 1; // Default setting for ChandyRemainder
private double roundUp = 1; // Default setting for RoundUp
private double roundDown = 1; // Default setting for RoundDown
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
chandyRounded = Math.Round(ChandelierSAR(5, 3.5).Chandelier[0]);
chandyRemainder = ChandelierSAR(5, 3.5).Chandelier[0] - chandyRounded;
// Condition set 1
if (DefaultInput[0] > ChandelierSAR(5, 3.5).Chandelier[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
roundDown = chandyRemainder >= 0.75 ? chandyRounded + 0.75 :
chandyRemainder >= 0.5 ? chandyRounded + 0.5 :
chandyRemainder >= 0.25 ? chandyRounded + 0.25 :
chandyRemainder;
EnterShortStopLimit(DefaultQuantity, roundDown, roundDown, "");
Alert("MyAlert0", Priority.High, "", "", 1, Color.White, Color.Black);
}
// Condition set 2
if (DefaultInput[0] < ChandelierSAR(5, 3.5).Chandelier[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
roundUp = chandyRemainder <= 0.25 ? chandyRounded + 0.25 :
chandyRemainder <= 0.5 ? chandyRounded + 0.5 :
chandyRemainder <= 0.75 ? chandyRounded + 0.75 :
chandyRemainder;
ExitShortStopLimit(roundUp, roundUp, "", "")
}