NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 09-11-2007, 01:03 PM   #1
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default Converting double to int

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 is offline  
Reply With Quote
Old 09-11-2007, 01:11 PM   #2
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

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,
JangoFolly is offline  
Reply With Quote
Old 09-11-2007, 01:14 PM   #3
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

This shoud also work:

tickAltPeriod = (int) Math.Floor(Bars.Period.Valu e * tickAltPeriodPro);
NinjaTrader_Ray is offline  
Reply With Quote
Old 09-11-2007, 01:25 PM   #4
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

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,
JangoFolly is offline  
Reply With Quote
Old 09-11-2007, 01:32 PM   #5
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

Try "BarsPeriod"
NinjaTrader_Dierk is offline  
Reply With Quote
Old 09-11-2007, 01:47 PM   #6
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Dierk View Post
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,
JangoFolly is offline  
Reply With Quote
Old 09-11-2007, 01:49 PM   #7
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

Try BarsPeriod.Value. Just use the intellisense and you'll see.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 09-11-2007, 01:59 PM   #8
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Dierk View Post
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.
Attached Images
File Type: png NT Error.png (30.3 KB, 10 views)
JangoFolly is offline  
Reply With Quote
Old 09-11-2007, 02:01 PM   #9
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

As the error you got suggests the value can not be 0.

You need to check that tickAltPeriod does not become 0.
NinjaTrader_Dierk is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Double EMA NinjaTrader_Ray NinjaScript File Sharing Discussion 0 07-31-2007 10:20 AM
double to int ? zoltran Indicator Development 3 03-14-2007 01:51 AM
Double moving average ,double time frame Mauro60 Indicator Development 3 01-23-2007 02:11 AM
converting eSignal (javascript) to NinjaScript ATI user General Programming 2 10-24-2006 03:06 AM
Converting TS strategy signals to NT Elfsareus Automated Trading 7 02-15-2006 02:32 PM


All times are GMT -6. The time now is 11:31 AM.