NinjaScript Little Helpers Code Share - Snippets/UserDefinedMethods - NinjaTrader Programming | futures io social day trading
futures io futures trading


NinjaScript Little Helpers Code Share - Snippets/UserDefinedMethods
Updated: Views / Replies:3,687 / 34
Created: by gregid Attachments:3

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 3  
 
Thread Tools Search this Thread
 

NinjaScript Little Helpers Code Share - Snippets/UserDefinedMethods

  #1 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

NinjaScript Little Helpers Code Share - Snippets/UserDefinedMethods

I've seen many times how a small helper methods can greatly increase productivity. So the idea for this thread is to share such little code snippets that will extend the existing NinjaScript methods.

In NinjaTrader the simplest way to extend its functionality accross the board (ie: for all Indicators or Strategies) is to include the methods and properties in UserDefinedMethods.cs file, where already an empty partial class is defined.

There are 2 separate UserDefinedMethods.cs files - one for Strategies and one for Indicators so if you have a method you would like to see both in Strategies and Indicators you need to paste the code into both of them.

For Indicator you place the code inside of:
 
Code
partial class Indicator
{
    // Your code here
}
For Strategies you place the code inside of:
 
Code
partial class Strategy
{
    // Your code here
}
Also remember to make all methods and properties you would like to access public, eg.
 
Code
public void DoSomething()
{
}

If you have some cool methods, feel free to post them in this thread!


Last edited by gregid; March 27th, 2015 at 07:35 PM.
Reply With Quote
The following 4 users say Thank You to gregid for this post:
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

Extended Print method (PrintMe)


One example where I've realized I spent too much time is with native Print method.

Firstly I wasted time on formatting and adding prefixes like Time[0], or some identifiers to know where the info came from.

Secondly I wasted time on writing Print, testing, commenting out or removing then writing it again and again.
So I wrote the extension to Print called PrintMe with the following functionality:
  1. You can turn printing On and Off
  2. You can add identifiers such as Time, Name, your own Identifier
  3. You can change the separator for the above parts
  4. You can change the time formatting
  5. You can group messages into different categories/level of depth and decide which ones to display

These properties will be included in any Indicator or Strategy once in UserDefinedMethods and from the code instead of old Print("Some Message") you use:
 
Code
PrintMe("Some Message");
// or with grouping:
PrintMe(1,"Some Message");

Here is the code (follow the instructions from first post):
 
Code
        private bool _isPrint = true;
        private bool _printWithName = false;
        private string _uniqueIdentifier = "";
        private bool _printWithTime = true;
        private string _timeFormat = "MM-dd hh:mm:ss";
        private string _groupsIncluded = "0";
        private string _separator = " | ";

        private string[] _groupsArray;
        private int _gInt = 0;
        private string _msg = "";

        /// <summary>
        /// Print if group is to be included
        /// </summary>
        public void PrintMe(int group, string message)
        {
            if (_groupsIncluded == "" || _groupsIncluded == "0" || _groupsIncluded == "*")
                PrintMe(message);
            else
            {
                _groupsArray = _groupsIncluded.Split(',');
                foreach (string g in _groupsArray)
                {
                    int.TryParse(g, out _gInt);
                    if (group == _gInt)
                        PrintMe(message);
                }
            }
        }

        /// <summary>
        /// Print with predefined prefixes (date, time, name)
        /// </summary>
        public void PrintMe(string message)
        {
            if (_isPrint)
            {
                if (_printWithName) _msg += this.Name + _separator;
                if (_uniqueIdentifier != "") _msg += _uniqueIdentifier + _separator;
                if (_printWithTime) _msg += Time[0].ToString(_timeFormat) + _separator;

                _msg += message;
                Print(_msg);
                _msg = "";
            }
        }


        /// <summary>
        /// </summary>
        [Description("Print To Output Window?\n/False disables printing")]
        [Category("Debug")]
        [Gui.Design.DisplayName("Debug Print to Output Window?")]
        public bool PrintToOutputWindow
        {
            get { return _isPrint; }
            set { _isPrint = value; }
        }
        /// <summary>
        /// </summary>
        [Description("Comma separated, '0' to print all groups")]
        [Category("Debug")]
        [Gui.Design.DisplayName("Limit Print to Groups:")]
        public string GroupsIncluded
        {
            get { return _groupsIncluded; }
            set { _groupsIncluded = value; }
        }
        /// <summary>
        /// </summary>
        [Description("Include Name in Print To Output Window?")]
        [Category("Debug")]
        [Gui.Design.DisplayName("Print with Name?")]
        public bool PrintWithName
        {
            get { return _printWithName; }
            set { _printWithName = value; }
        }
        /// <summary>
        /// </summary>
        [Description("Include this Indentifier in Print To Output Window.\nLeave blank to ignore.")]
        [Category("Debug")]
        [Gui.Design.DisplayName("Print with this Identifier:")]
        public string UniqueIdentifier
        {
            get { return _uniqueIdentifier; }
            set { _uniqueIdentifier = value; }
        }

        /// <summary>
        /// </summary>
        [Description("Include Time in Print To Output Window?")]
        [Category("Debug")]
        [Gui.Design.DisplayName("Print with Time?")]
        public bool PrintWithTime
        {
            get { return _printWithTime; }
            set { _printWithTime = value; }
        }
        /// <summary>
        /// </summary>
        [Description("Separate Message Parts With Following Charackters.")]
        [Category("Debug")]
        [Gui.Design.DisplayName("Parts Separator:")]
        public string SeparatorForStrings
        {
            get { return _separator; }
            set { _separator = value; }
        }

        /// <summary>
        /// </summary>
        [Description("Time Format to be used in Print to Output Window")]
        [Category("Debug")]
        [Gui.Design.DisplayName("Time Format")]
        public string DebugTimeFormat
        {
            get { return _timeFormat; }
            set { _timeFormat = value; }
        }
New properties are added to PropertyGrid under category "Debug".

With PrintMe in place I no longer need to comment out any PrintMe, I leave them where they are and if I feel I don't need them for now I simply change the default for PrintToOutputWindow to false in Initialize in the specific indicator. The same with other properties - you can change the default values in Initialize().


Last edited by gregid; March 27th, 2015 at 07:45 PM. Reason: Added info on how to change the defaults
Reply With Quote
The following 9 users say Thank You to gregid for this post:
 
  #4 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

Another overload for PrintMe

If you would rather use names for grouping PrintMe here is another overload to do just that:
 
Code
        /// <summary>
        /// Print if group is to be included using string or characters to identify groups
        /// </summary>
        public void PrintMe(string group, string message)
        {
            if (_groupsIncluded == "" || _groupsIncluded == "0" || _groupsIncluded == "*")
                PrintMe(message);
            else
            {
                _groupsArray = _groupsIncluded.Split(',');
                foreach (string g in _groupsArray)
                {
                    if (group == g)
                        PrintMe(message);
                }
            }
        }
To use:
 
Code
PrintMe("Orders", "Some Message");
You can mix use of all overloads together when requesting them in the list, eg:

 
Code
Limit Print to Groups: Order,1,test,2

Reply With Quote
 
  #5 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

DrawFontSymbol

I recently learned from @cory, a nice trick to draw symbols with graphic fonts (eg. wingdings).

So I added a custom method where I predefine some symbols I want to use and then call it from Indicator like this:
 
Code
DrawFontSymbol("Sun"+Time[0].ToString(), SymbolFontType.Sun, 15, 0,High[0],Color.IndianRed);
Here is a code with 2 predefined symbols (one from Webdings and one from Wingdings), just add it to your partial class (eg. UserDefinedMethod.cs) to have it available for all:
 
Code
        #region DrawFontSymbol
        public enum SymbolFontType
        {
            LightningBolt,
            Sun
        }

        public void DrawFontSymbol(string tag, SymbolFontType symbol, int size, int barsAgo, double y, Color color)
        {
            bool ascale = false;

            switch (symbol)
            {
                case SymbolFontType.LightningBolt:
                    DrawText(tag, ascale, "~", barsAgo, y, 0, color, new Font("Webdings", size), StringAlignment.Center, Color.Empty, Color.Empty, 1);
                    break;
                case SymbolFontType.Sun:
                    DrawText(tag, ascale, "R", barsAgo, y, 0, color, new Font("Wingdings", size), StringAlignment.Center, Color.Empty, Color.Empty, 1);
                    break;
                default:
                    break;
            }
        }
        #endregion

For a list of available symbols in both Webdings and Wingdings go to:
Wingdings and Webdings!

Reply With Quote
The following user says Thank You to gregid for this post:
 
  #6 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

Chart Helper for working on canvas

If you've ever worked with Plot override and custom drawing then you know how painfull the process is with finding the right coordinates. Attached file (ChartHelper) has some nice helper methods to assist you in the process.

PS. I don't know who the original author of the file is.

Attached Files
Register to download File Type: zip ChartHelper.zip (3.3 KB, 38 views)
Reply With Quote
The following user says Thank You to gregid for this post:
 
  #7 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

Ordinal numbers

Another simple helper I use from time to time to display or print the numbers in their ordinal form (1st, 2nd, 3rd, etc.).

To use with integers, eg.:

 
Code
int exampleNumber = 5;
Print(GetOrdinal(exampleNumber) + " Trade")
// Result: 5th Trade
Code:
 
Code
        public static string GetOrdinal(int num)
        {
            if (num.ToString().EndsWith("11")) return num+"th";
            if (num.ToString().EndsWith("12")) return num + "th";
            if (num.ToString().EndsWith("13")) return num + "th";
            if (num.ToString().EndsWith("1")) return num + "st";
            if (num.ToString().EndsWith("2")) return num + "nd";
            if (num.ToString().EndsWith("3")) return num + "rd";
            return num + "th";
        }

Reply With Quote
The following user says Thank You to gregid for this post:
 
  #8 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

It seems to me I vastly overestimated usefullness of such thread - as the only people that have shown any interest (thanks) are the ones that could easily deal without my help, plus my hope was to learn something from others as well.

I think I will stop sharing more for now as I feel that I am sharing it with myself

Next time I will decide to share something I will call the thread something more catchy, how about: "Traders are thieves!!!"


Last edited by gregid; March 31st, 2015 at 09:27 AM.
Reply With Quote
The following user says Thank You to gregid for this post:
 
  #9 (permalink)
Elite Member
Indianapolis, IN USA
 
Futures Experience: Advanced
Platform: Ninjatrader TOS Custom
Broker/Data: Several
Favorite Futures: ES CL ZB
 
Posts: 370 since Dec 2009
Thanks: 218 given, 369 received


gregid View Post
It seems to me I vastly overestimated usefullness of such thread - as the only people that have shown any interest (thanks) are the ones that could easily deal without my help, plus my hope was to learn something from others as well.

I think I will stop sharing more for now as I feel that I am sharing it with myself

Next time I will decide to share something I will call the thread something more catchy, how about: "Traders are thieves!!!"

I think your thread is a great idea. A couple of quick snippets that come to mind... the beautiful code posted by @Fat Tails to convert to treasury format numbers. Also @Silvester17 has posted a treasure trove of little gems as well. Hang in there - right now, indicators and t/a have been dismissed here in favor of using vix, usd/jpy, bonds, and the fed to "form a market opinion" and then based on that you guess what the market will do and trade accordingly. That will pass. What the market is actually doing is far more important than what someone thinks it should do, or might do, etc. Thanks for your contributions.

Reply With Quote
The following 2 users say Thank You to timefreedom for this post:
 
  #10 (permalink)
Elite Member
Indianapolis, IN USA
 
Futures Experience: Advanced
Platform: Ninjatrader TOS Custom
Broker/Data: Several
Favorite Futures: ES CL ZB
 
Posts: 370 since Dec 2009
Thanks: 218 given, 369 received



timefreedom View Post
I think your thread is a great idea. A couple of quick snippets that come to mind... the beautiful code posted by @Fat Tails to convert to treasury format numbers. Also @Silvester17 has posted a treasure trove of little gems as well. Hang in there - right now, indicators and t/a have been dismissed here in favor of using vix, usd/jpy, bonds, and the fed to "form a market opinion" and then based on that you guess what the market will do and trade accordingly. That will pass. What the market is actually doing is far more important than what someone thinks it should do, or might do, etc. Thanks for your contributions.

Here is the code from @Fat Tails that displays treasury formats. Insert #region Miscellaneous after #region Properties.

 
Code
		#region Miscellaneous

		public override string FormatPriceMarker(double price)
		{
			double trunc = Math.Truncate(price);
			int fraction = Convert.ToInt32(320 * Math.Abs(price - trunc) - 0.0001); // rounding down for ZF and ZT
			string priceMarker = "";
			if (TickSize == 0.03125) 
			{
				fraction = fraction/10;
				if (fraction < 10)
					priceMarker = trunc.ToString() + "'0" + fraction.ToString();
				else 
					priceMarker = trunc.ToString() + "'" + fraction.ToString();
			}
			else if (TickSize == 0.015625 || TickSize == 0.0078125)
			{
				if (fraction < 10)
					priceMarker = trunc.ToString() + "'00" + fraction.ToString();
				else if (fraction < 100)
					priceMarker = trunc.ToString() + "'0" + fraction.ToString();
				else	
					priceMarker = trunc.ToString() + "'" + fraction.ToString();
			}
			else
				priceMarker = price.ToString(Gui.Globals.GetTickFormatString(TickSize));
			return priceMarker;
		}		
		#endregion

Reply With Quote
The following 2 users say Thank You to timefreedom for this post:

Reply



futures io > > > > > NinjaScript Little Helpers Code Share - Snippets/UserDefinedMethods

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
vvhg's Little Helpers - Keeping Them In One Place vvhg The Elite Circle 32 January 7th, 2016 11:58 PM
Code TRX Channel for TOS (MT4 Code here) rommark ThinkOrSwim Programming 0 October 1st, 2014 04:55 PM
Code Snippets needed! zeller4 NinjaTrader Programming 11 June 20th, 2011 04:21 PM
code snippets Saroj Feedback and Announcements 1 September 11th, 2009 02:10 PM


All times are GMT -4. The time now is 06:56 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-13 in 0.12 seconds with 20 queries on phoenix via your IP 54.90.92.204