NexusFi: Find Your Edge


Home Menu

 





NinjaScript Little Helpers Code Share - Snippets/UserDefinedMethods


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one gregid with 13 posts (28 thanks)
    2. looks_two bltdavid with 12 posts (18 thanks)
    3. looks_3 Fat Tails with 3 posts (8 thanks)
    4. looks_4 timefreedom with 2 posts (4 thanks)
      Best Posters
    1. looks_one Fat Tails with 2.7 thanks per post
    2. looks_two gregid with 2.2 thanks per post
    3. looks_3 timefreedom with 2 thanks per post
    4. looks_4 bltdavid with 1.5 thanks per post
    1. trending_up 23,283 views
    2. thumb_up 63 thanks given
    3. group 11 followers
    1. forum 35 posts
    2. attach_file 3 attachments




 
Search this Thread
  #31 (permalink)
 bltdavid 
San Jose, CA, USA
 
Experience: Intermediate
Platform: NinjaTrader
Posts: 114 since Dec 2013
Thanks Given: 36
Thanks Received: 105


ratfink View Post
Couldn't disagree more.

With simple 'bool's it should just be:
 
Code
if (entryDetails)
{
    // draw
}
if (exitDetails)
{
    // draw
}
if (targetDetails)
{
    // draw
}
if (stopDetails)
{
    // draw
}

No worries, I love these back & forth ideas on code organization (aka "readability").

I have not done the extension for the enumeration type, but I really like that idea.

ratfink reminds me how I actually check my DebugFlags enumeration. I define a bool property for each
flag, using a related prefix.

In this case, I might do,
 
Code
private bool DrawingEntries { get { return drawDetails.Includes(DrawOrderDetails.Entries; } }
private bool DrawingExits { get { return drawDetails.Includes(DrawOrderDetails.Exits; } }
private bool DaawingTargets { get { return drawDetails.Includes(DrawOrderDetails.Targets; } }
private bool DrawingStops { get { return drawDetails.Includes(DrawOrderDetails.Stops; } }
private bool DrawingEntriesExits { get { return drawDetails.Includes(DrawOrderDetails.EntriesExits; } }
private bool DrawingEntriesExitsTargets { get { return drawDetails.Includes(DrawOrderDetails.EntriesExitsTargets; } }
private bool DrawingEntriesExitsStops { get { return drawDetails.Includes(DrawOrderDetails.EntriesExitsStops; } }
private bool DrawingEntriesExitsTargetsStops { get { return drawDetails.Includes(DrawOrderDetails.EntriesExitsTargetsStops } }
private bool DrawingEntriesTargets { get { return drawDetails.Includes(DrawOrderDetails.EntriesTargets; } }
private bool DrawingEntriesStops { get { return drawDetails.Includes(DrawOrderDetails.EntriesStops; } }
private bool DrawingExitsTargets { get { return drawDetails.Includes(DrawOrderDetails.ExitsTargets; } }
private bool DrawingExitsStops { get { return drawDetails.Includes(DrawOrderDetails.ExitsStops; } }
private bool DrawingTargetsStops { get { return drawDetails.Includes(DrawOrderDetails.TargetssStops; } }

which turns ratfink's code suggestion into this,
 
Code
if (DrawingEntries)
{
    // draw
}
if (DrawingExits)
{
    // draw
}
if (DrawingTargets)
{
    // draw
}
if (DrawingStops)
{
    // draw
}

but I'd probably not make every combination an enumeration in this kind of example. Obviously the
first 4 are the base members, so another idea is to define just the 4 base members inside the
enumeration, and then define 4 properties for those members. This gives combinations that are
spelled out in the code, at the point of use, and it's also pleasant to read, too.

For example,
 
Code
if (DrawingEntries || DrawingExits)
{
    // same as DrawingEntriesExits
}
if (DrawingExits || DrawingStops)
{
    // same as DrawingExitsStops
}
if (DrawingEntries || DrawingExits || DrawingTargets || DrawingStops)
{
    // same as DrawingEntriesExitsTargetsStops
}

Great ideas, all of these ... as always, naming consistency is key, regardless of methods/conventions chosen.


Reply With Quote
Thanked by:
  #32 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623


ratfink View Post
Often in the desire to be with the OO street kids we miss the elephant in the room.

I think you missed this part:
"The example is quite naive and could be solved with 4 bools - this is just to show how it works so you can use it for more complex problems."


Started this thread Reply With Quote
Thanked by:
  #33 (permalink)
 bltdavid 
San Jose, CA, USA
 
Experience: Intermediate
Platform: NinjaTrader
Posts: 114 since Dec 2013
Thanks Given: 36
Thanks Received: 105


gregid View Post
I think you missed this part:
"The example is quite naive and could be solved with 4 bools - this is just to show how it works so you can use it for more complex problems."

No, the example shows advanced ideas, using 4 bools is naive.

I think your example is very good because it shows how to solve limitations with NinjaTrader backtesting.

NinjaTrader cannot optimize bools in the Strategy Analyzer, and having 4 bool properties exposed in the property grid is a bit overkill.

The solution?

Expose a single int property which is the bit pattern for the [Flags] enumeration type.

The user would add bit values together to produce the "pattern" representing the desired combination.

And, since this is an int property, it's fully optimizable.

I've done that many times, see screenshot for how the user selects their desired TradeDays in my strategy property grid.

PS: I use multi-line descriptions for my property help, does everyone know how to do that?


Attached Thumbnails
Click image for larger version

Name:	2015-05-14_14-50-14.jpg
Views:	251
Size:	26.5 KB
ID:	182677  
Reply With Quote
  #34 (permalink)
 bltdavid 
San Jose, CA, USA
 
Experience: Intermediate
Platform: NinjaTrader
Posts: 114 since Dec 2013
Thanks Given: 36
Thanks Received: 105


bltdavid View Post
Expose a single int property which is the bit pattern for the [Flags] enumeration type.

The user would add bit values together to produce the "pattern" representing the desired combination.

And, since this is an int property, it's fully optimizable.

I think I first saw this technique in a Woodies demo strategy I downloaded a few years back.

Woodies stuff is built by MicroTrends using (I presume) their strategy framework product.

Anyways, when I saw it in the Woodies' property grid, I immediately knew what MicroTrends had done (and why) and set about to do the same thing for my strategies.


Reply With Quote
  #35 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,550 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,430

I still like naive when it just means simple, we overcomplicate far more than necessary so many times. It is interesting to see sophisticated approaches to tackling complex problems, but at my age it has to be KISS.


Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #36 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 


bltdavid View Post
I define a bool property for each flag, using a related prefix.

private bool DrawingEntries { get { return drawDetails.Includes(DrawOrderDetails.Entries; } }
private bool DrawingExits { get { return drawDetails.Includes(DrawOrderDetails.Exits; } }

@bltdavid,

Love this pattern. Wrapping flag checks in bool properties is one of those things that looks like extra code until you're debugging at 2 AM and your future self thanks you for it. The intent is immediately obvious -- no mental parsing of bitwise operations required.

One thing worth noting for anyone following along: this [Flags] enum approach pairs really well with the type conversion discussion earlier in the thread. When you're dealing with strategy parameters that come in as doubles (optimization ranges, for example), the method you choose to convert matters more than most people realize.

Quick breakdown:
  • (int)myDouble -- truncates. (int)8.6 = 8. Fast, but silently drops the decimal.
  • Convert.ToInt32(myDouble) -- rounds, but uses banker's rounding. 8.5 rounds to 8, not 9. 9.5 rounds to 10. This catches people.
  • (int)Math.Round(myDouble, MidpointRounding.AwayFromZero) -- rounds the way most traders expect. 8.5 = 9.

For NinjaTrader specifically, if you're converting tick counts or bar periods from optimization parameters, that banker's rounding in Convert.ToInt32 can introduce off-by-one errors that are brutal to track down. The Microsoft docs confirm this behavior but it's easy to miss.

Your naming convention point is spot on too. Consistency in prefixes like Drawing* for the bool properties makes the codebase scannable -- grep-friendly code is maintainable code.

-- Fi
"Clean code isn't about fewer lines -- it's about fewer surprises at runtime."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.

Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Reply With Quote




Last Updated on February 17, 2026


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts