PDA

View Full Version : Converting double to int


JangoFolly
09-11-2007, 01:03 PM
Hi. I cannot figure out how to make an int type variable take the value of an integer which is held in a double type variable.

Here is some sample code:


#region Variables
private double tickAltPeriodPro = 0.20;

// User defined variables (add any user defined variables below)
private int tickAltPeriod;
#endregion

protected override void Initialize()
{
tickAltPeriod = Math.Floor(Bars.Period.Value * tickAltPeriodPro);

Add(PeriodType.Tick, tickAltPeriod);}

For example, let's say the variable tickAltPeriodPro takes a value of 0.20. Using a tick chart with a period of 266, I would like tickAltPeriod to be equal to 53 (Floor function return value of the expression [266 * 0.20]).

If I make tickAltPeriod an int variable then I get an error when the Floor function returns a double. If I make tickAltPeriod a double then the Add function returns an error (it's expecting a int input).

Hopefully there's a simple workaround that I'm overlooking. Thank you.


Regards,

JangoFolly
09-11-2007, 01:11 PM
After poking around on the MSDN site I think I just answered my question:

tickAltPeriod = System.Convert.ToInt32(Math.Floor(Bars.Period.Valu e * tickAltPeriodPro));
Thanks again.


Regards,

NinjaTrader_Ray
09-11-2007, 01:14 PM
This shoud also work:

tickAltPeriod = (int) Math.Floor(Bars.Period.Valu e * tickAltPeriodPro);

JangoFolly
09-11-2007, 01:25 PM
Thanks for the prompt reply, Ray. After solving my first problem, I encountered a second one. I received the following error message in my log window:

Strategy Failed to call method 'Initialize' for strategy 'ATRTrade_v8': 'Bars' property can not be accessed from within 'Initialize' method

Is there any way to access the period value in the Initialize block?


Regards,

NinjaTrader_Dierk
09-11-2007, 01:32 PM
Try "BarsPeriod"

JangoFolly
09-11-2007, 01:47 PM
Try "BarsPeriod"

Dierk,

Can you please use that in context? I tried the following:

tickAltPeriod = Math.Floor(BarsPeriod * tickAltPeriodPro);

I received an error that I can't use the "*" operator on the BarsPeriod object.

Thank you.


Regards,

NinjaTrader_Dierk
09-11-2007, 01:49 PM
Try BarsPeriod.Value. Just use the intellisense and you'll see.

JangoFolly
09-11-2007, 01:59 PM
Try BarsPeriod.Value. Just use the intellisense and you'll see.

tickAltPeriod = System.Convert.ToInt32(Math.Floor(BarsPeriod.Value * tickAltPeriodPro));


I get an error when I choose to run a backtest. I can ignore past the dialogue to run a backtest, but the period comes through as 0.

NinjaTrader_Dierk
09-11-2007, 02:01 PM
As the error you got suggests the value can not be 0.

You need to check that tickAltPeriod does not become 0.