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 06-16-2012, 04:38 PM   #1
jeronymite
Junior Member
 
Join Date: Apr 2012
Posts: 16
Thanks: 3
Thanked 3 times in 2 posts
Lightbulb Optimize with bools? Here's how!

I really needed to run the Optimizer and be able to test bools. In other words, optimize with the bool variable false and then try it with true. Currently, this is not supported in NT7.

Here's the simple method I worked out to make this easy:

1. Declare bools as normal in NinjaScript. Use them as normal. Too easy!

private bool MyFavouritebool = false ; // Just a normal bool

2. In the Properties region, declare bools as follows:

// Pseudo-bool -- 0 is false, 1 is true.
public int _MyFavouritebool
{
get { return MyFavouritebool ? 1 : 0 ; }
set { MyFavouritebool = value % 2 == 1 ? true : false ; }
}

3. Set the parameter value to 0 for false and 1 for true. This makes optimization with bools simple. To leave the bool as false, use 0;0;1. To leave it as true, use 1;1;1. To optimize using the bool as false, then true, use 0;1;1.

I hope you find this useful.

Cheers.
jeronymite is offline  
Reply With Quote
The following 2 users say thank you to jeronymite for this post:
Old 06-17-2012, 12:31 PM   #2
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,781
Thanks: 159
Thanked 565 times in 556 posts
Default

jeronymite

Thank you for sharing your work in this area!
NinjaTrader_Matthew is offline  
Reply With Quote
Old 09-18-2012, 08:21 PM   #3
Style
Member
 
Join Date: Jul 2009
Posts: 60
Thanks: 18
Thanked 2 times in 2 posts
Default

I'm trying to implement this code. Since adding it, I'm getting the following errors when I compile:

9/18/2012 10:06:06 PM Default Strategy 'NinjaTrader.Strategy.TrendlineBreakOutDT' could not be deserialized: There is an error in XML document (1, 6787).

9/18/2012 10:06:07 PM Strategy Error on getting/setting property 'MacdExit' for strategy 'TrendlineBreakOutDT/8bacaef5f437438d95661270b6885a6c': Object of type 'System.Boolean' cannot be converted to type 'System.Int32'.

Any ideas?

Despite these errors, the code does appear to work correctly, but it bothers me that it doesn't compile cleanly.

Thanks!
Style is offline  
Reply With Quote
Old 09-19-2012, 06:08 AM   #4
jeronymite
Junior Member
 
Join Date: Apr 2012
Posts: 16
Thanks: 3
Thanked 3 times in 2 posts
Default

Hi Style,

Glad you find the code useful.

I too have experienced de-serialization issues, but they were not specifically caused by the pseudo-boolean construct. I found the issue when I tried to rename a strategy.

In any case, the approach I took to overcome the issue was to create a completely new strategy using the Strategy Wizard that was empty of any content -- essentially a shell strategy. I then copied and pasted my source code into the new strategy and saved/recompiled. No more de-serialization errors since, and I use the pseudo-boolean construct all the time. Try this and see how you go. If you still see the errors, I would suggest it is not the pseudo-boolean construct and you may need to look elsewhere for the source of the issue. Perhaps the NT support folks may have some insights on this?

Cheers.
jeronymite is offline  
Reply With Quote
The following user says thank you to jeronymite for this post:
Old 09-19-2012, 07:19 AM   #5
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,781
Thanks: 159
Thanked 565 times in 556 posts
Default

Style

Can you please post the exact region properties section of the code you are using?
NinjaTrader_Matthew is offline  
Reply With Quote
Old 09-19-2012, 04:57 PM   #6
Style
Member
 
Join Date: Jul 2009
Posts: 60
Thanks: 18
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by jeronymite View Post
Hi Style,the approach I took to overcome the issue was to create a completely new strategy using the Strategy Wizard that was empty of any content -- essentially a shell strategy. I then copied and pasted my source code into the new strategy and saved/recompiled.
That did the trick. Weird. Thanks!

Quote:
Originally Posted by NinjaTrader_Matthew View Post
please post the exact region properties section of the code you are using?
#region Properties

[Description("")]
[GridCategory("Parameters")]
public int StartTime
{
get { return startTime; }
set { startTime = Math.Max(1, value); }
}

[Description("")]
[GridCategory("Parameters")]
public int EndTime
{
get { return endTime; }
set { endTime = Math.Max(1, value); }
}

[Description("")]
[GridCategory("Parameters")]
public int ThreeBarExit
{
get { return threeBarExit ? 1 : 0 ; }
set { threeBarExit = value % 2 == 1 ? true : false ; }
}

[Description("")]
[GridCategory("Parameters")]
public int MacdExit
{
get { return macdExit ? 1 : 0 ; }
set { macdExit = value % 2 == 1 ? true : false ; }
}

[Description("")]
[GridCategory("Parameters")]
public bool StaticStop
{
get { return staticStop; }
set { staticStop = value; }
}

[Description("")]
[GridCategory("Parameters")]
public bool StaticTarget
{
get { return staticTarget; }
set { staticTarget = value; }
}

[Description("")]
[GridCategory("Parameters")]
public bool StaticEntry
{
get { return staticEntry; }
set { staticEntry = value; }
}

[Description("")]
[GridCategory("Parameters")]
public bool SineExit
{
get { return sineExit; }
set { sineExit = value; }
}

[Description("")]
[GridCategory("Parameters")]
public int FasterTimeFrame
{
get { return fasterTimeFrame; }
set { fasterTimeFrame = Math.Max(10, value); }
}

[Description("")]
[GridCategory("Parameters")]
public int MacdSineTimeFrame
{
get { return macdSineTimeFrame; }
set { macdSineTimeFrame = Math.Max(10, value); }
}

[Description("")]
[GridCategory("Parameters")]
public int Strength
{
get { return strength; }
set { strength = Math.Max(1, value); }
}

[Description("")]
[GridCategory("Parameters")]
public double Ratio
{
get { return ratio; }
set { ratio = Math.Max(1, value); }
}
#endregion
Style is offline  
Reply With Quote
Old 09-20-2012, 07:42 AM   #7
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,781
Thanks: 159
Thanked 565 times in 556 posts
Default

The next time this occurs, rather than recreating the strategy, can you please try resetting the database to see if this resolve the issue? You can do this by going to Tools--> Options--> Data tab--> select "Reset DB".
NinjaTrader_Matthew is offline  
Reply With Quote
Reply

Tags
bools, optimizer, optimizer parameters

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
Optimize indicator rzeemeijer Strategy Development 2 10-27-2011 11:43 AM
Using Bars with Bools CaptainAmericaXX General Programming 2 04-14-2011 11:29 AM
Bools and [] indexing kenb2004 Strategy Development 5 02-28-2011 11:41 AM
... no parameters to optimize ThatManFromTexas Strategy Analyzer 10 02-28-2011 10:08 AM
ints and bools??? John833 Strategy Development 2 12-08-2008 09:40 AM


All times are GMT -6. The time now is 03:54 PM.