Messaging for NinjaTrader

From Triage Documentation

Jump to: navigation, search

Contents

Send your first message in NinjaTrader 7.0

Please note that NinjaTrader will likely discontinue 6.5 in the next 6 to 12 months. When they do, Triage will also discontinue support for NinjaTrader 6.5. If you need help with NT 6.5 please contact us and we will give you legacy documentation for it.

For NinjaTrader 7.0, you will need to add the Triage Messaging references in your declarations region and custom indicator / strategy class. Add the statement (using TriageNinjaTraderMsg;) to the using declarations region as shown below:

// NinjaTrader 7.0 Sample for sending out a message

#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
using TriageNinjaTraderMsg;
#endregion

In your NinjaTrader 7.0 custom indicator / strategy class add the lines of code as shown below to create a new message instance, to dispose of the instance (with the OnTermination method) when the indicator is done and to send out a message with the OnBarUpdate event.

Please note that in some minor versions of NinjaTrader 7.0 beta and above, you cannot place your m_TriageMsg.SetCondition statements in Initialize (). This will produce errors because NinjaTrader introduced some code-breaking changes for their tick and bar counting methods. We recommend to users to place their set condition statements in OnBarUpdate.

// NinjaTrader 7.0 Sample indicator for sending out a message
namespace NinjaTrader.Indicator
{
   public class TriageSampleMessaging : Indicator
   {
      #region Variables
      // Define message instance variables
      private TriageMsg m_TriageMsg;
      #endregion

      protected override void Initialize ()
      {
         CalculateOnBarClose = false;
         m_TriageMsg = new TriageMsg(this);       // Create message instance 
      }
 
      protected override void OnTermination ()
      {
         m_TriageMsg.Dispose();   // free Triage Messaging resource
      }
 
      protected override void OnBarUpdate ()
      {
         if (CurrentBar == 0) {
         // Message sending condition can be set at any time. We just do it once here.
         m_TriageMsg.SetCondition("once");
         }
         m_TriageMsg.SendMsg ("email", "youraddress@email.com","Hello, World!");
      }
      #region Properties
      #endregion
    }
}

What you have just typed in will send a single test message to your email address, one time only. Don't forget to change "youraddress@email.com" to your own email address, and turn off test mode from the tray icon to send a live message.

Try out universal messaging

The example in the previous section was simple, but we know you are looking for a little more excitement. You can try sending messages using all the supported messaging services (shown for NinjaTrader 6.5). First, open up a new custom indicator and create a message instance for each message you will be sending out in your declarations region as shown below.

#region Variables
// Define message instance variables
private TriageMsg m_TriageMsg1;
private TriageMsg m_TriageMsg2;
private TriageMsg m_TriageMsg3;
private TriageMsg m_TriageMsg4;
private TriageMsg m_TriageMsg5;
private TriageMsg m_TriageMsg6;
private TriageMsg m_TriageMsg7;
private TriageMsg m_TriageMsg8;
private TriageMsg m_TriageMsg9;
private TriageMsg m_TriageMsg10;
private TriageMsg m_TriageMsg11;
#endregion

protected override void Initialize()
{
  CalculateOnBarClose	= false;
  // Create message instances and set message sending conditions
  m_TriageMsg1 = new TriageMsg(this);
  m_TriageMsg1.SetCondition("once");
  m_TriageMsg2 = new TriageMsg(this);
  m_TriageMsg2.SetCondition("once");
  m_TriageMsg3 = new TriageMsg(this);
  m_TriageMsg3.SetCondition("once");
  m_TriageMsg4 = new TriageMsg(this);
  m_TriageMsg4.SetCondition("once");
  m_TriageMsg5 = new TriageMsg(this);
  m_TriageMsg5.SetCondition("once");
  m_TriageMsg6 = new TriageMsg(this);
  m_TriageMsg6.SetCondition("once");
  m_TriageMsg7 = new TriageMsg(this);
  m_TriageMsg7.SetCondition("once");
  m_TriageMsg8 = new TriageMsg(this);
  m_TriageMsg8.SetCondition("once");
  m_TriageMsg9 = new TriageMsg(this);
  m_TriageMsg9.SetCondition("once");
  m_TriageMsg10 = new TriageMsg(this);
  m_TriageMsg10.SetCondition("once");
  m_TriageMsg11 = new TriageMsg(this);
  m_TriageMsg11.SetCondition("once");
}

Then type in the following code in your OnBarUpdate() method. Remove the lines in the code that do not apply to you. If you do not have a msn messaging account, then remove the line: m_TriageMsg6.SendMsg ("msn", "msnname@hotmail.com", "Hello, World!");

protected override void OnBarUpdate()
{
   m_TriageMsg1.SendMsg ("email", "youraddress@email.com","Hello, World!" );
   m_TriageMsg2.SendMsg ("sms", "14155550808", "Hello, World!");
   m_TriageMsg3.SendMsg ("phone", "14155550606", "Hello, World!");
   m_TriageMsg4.SendMsg ("fax", "14155550303", "Hello, World!");
   m_TriageMsg5.SendMsg ("yahoo", "myyahoousername", "Hello, World!");
   m_TriageMsg6.SendMsg ("msn", "msnname@hotmail.com", "Hello, World!");
   m_TriageMsg7.SendMsg ("aim", "myAOLusername", "Hello, World!");
   m_TriageMsg8.SendMsg ("icq", "570102039432", "Hello, World!");
   m_TriageMsg9.SendMsg ("gtalk", "googlename@gmail.com", "Hello, World!");
   m_TriageMsg10.SendMsg ("twitter", "twitteralias", "Hello, World!");
   m_TriageMsg11.SendMsg ("facebook", "facebookalias", "Hello, World!");
}

Don't forget to change the red colored destination numbers and addresses in the sample code above to reflect your own phone numbers and accounts. Also, turn off test mode from the tray icon or the messages will not be sent to the live Triage messaging servers.

In the example we just demonstrated, you should have received the message "Hello, World!" from all your chosen messaging services almost simultaneously, and within seconds. With the Triage Messaging service you'll never miss another critical message again wherever you are in the world.

How to Send a Message

Triage Messaging provides a single comprehensive messaging method to help you send messages from your NinjaTrader program.

The m_TriageMsg.SendMsg NinjaScript method helps you send messages according to your service, destination, message text, interval and time rules. With three simple parameters shown below, you can send messages to any destination in the world from your trading platform: message service, message destination and message text.

  m_TriageMsg.SendMsg (message service, message destination, message text);

Messaging Parameters
Message Method Parameter Description
Message Service Message service type: phone, sms, fax, email, yahoo, msn, aim, icq, gtalk, twitter, facebook
Message Destination Destination address or phone number
Message Text Text that you want to send out to your destination



All parameters may be either literal strings or variable strings.

   // Literal Strings
   m_TriageMsg.SendMsg ("yahoo", "yahoousername", "This is my test message");

   // Variable Strings
   string xservice = "phone";
   string xdestination = "14155551111";
   m_TriageMsg.SendMsg (xservice, xdestination, "This is my test message");

During the execution of your trading code, you can set and change these parameters as many times as you wish. We will explain these message method parameters in the next section.

Message Service Parameter

The first parameter is your <message service> type. Triage Messaging supports the following messaging services: phone, mobile device, instant messengers, e-mail, fax, Twitter™ and Facebook™ accounts.

Messaging Services
Messaging Service Parameter Value
Mobile Phone or Device via Short Message (SMS) Sms
Text-to-Voice Phone Message to any Telephone or Cell Phone Phone
Text Fax to any Facsimile Device Fax
E-mail Relay bypassing tradition SMTP and Mail Clients Email
Yahoo Instant Messenger Yahoo
AOL ("AIM") Instant Messenger aim
Microsoft MSN or Live Instant Messenger msn
Google Talk Instant Messenger gtalk
ICQ Instant Messenger icq
Twitter™ Account twitter
Facebook™ Account facebook


Message Destination Parameter

The second parameter is your <messaging destination>. Message destinations are the telephone numbers, internet addresses and instant messaging addresses to reach your target recipient.

You must be precise in specifying your destination addresses. Only alpha-numeric (including @ + - ) characters are allowed. Plus and minus sign (+ and -) characters are optional. Many common user errors occur when the destination is mistyped or misunderstood.

Messaging Destination Formats
Messaging Service Example Destination
Mobile Phone or Device via Short Message (SMS) 14155551111
Text-to-Voice Phone Message to any Telephone or Cell Phone +14155552222
Text Fax to any Facsimile Device +1-415-555-3333
E-mail Relay bypassing tradition SMTP and Mail Clients me@email.com
Yahoo Instant Messenger yahoousername
AOL ("AIM") Instant Messenger AOLusername
Microsoft MSN or Live Instant Messenger msnname@hotmail.com
Google Talk Instant Messenger googlename@gmail.com
ICQ Instant Messenger 570102039432
Twitter™ Account Twitter Alias
Facebook™ Account Facebook Alias


Message Text Parameter

The third parameter is your <messaging text>. This text will be sent to your recipient's destination address. For SMS and Twitter™ messages, you are limited to 140 characters per message. Phone text-to-voice messages are limited to 250 characters. Facebook™ messages are limited to 420 characters.

Faxes and e-mails are limited to 1,000 characters. Instant messages are limited to the restrictions of the instant messaging service (please check with your instant messenger service for more details). Triage Messaging servers will truncate your message to fit within the requirements of the messaging service you choose. You may specify multiple destinations, but only one text per message instance.

For email messaging sending, you may also use the || separator (two characters) to specify an e-mail title and body. For example, "My email message title || My email message body text".

  m_TriageMsg.SendMsg ("msn", "msnuser1@hotmail.com, msnuser2@hotmail.com", "This is my test message" );

Message Condition Method

Traders often send price and status information from their indicators and strategies on some specific interval, such as once an hour or once every 500 ticks. You can set a condition for when a message is sent with the SetCondition method.

We designed a series of messaging conditions that help you control when messages will be sent without writing the complex code yourself. See Figure 4.4a for the list of messaging condition settings available to you.

Messaging Condition Settings
Message Condition Property Description
"all" Send messages in all cases. There are no restrictions, so you can control when your messages are sent out
"once" Send message only once
"now", "" Send message now in real-time
(e.g. On the last bar on chart and in real-time.)
A "" blank condition parameter also defaults to the "now" condition
"next=x" Send message every x time interval e.g. next=01:00:00
(hh:mm:ss, hh:mm or hh format)
"bar=x" Send message every x bars e.g. bar=10 (every 10 bars)
"tick=x" Send message every x ticks e.g. tick=25 (every 25 ticks)
"time=timestart-timeend" Send message when system time is between timestart and timeend. (Timestart and timeend are in hh:mm:ss, hh:mm or hh format)

All message instances default to the condition "now" if you do not set a message send condition using the SetCondition method.

For clients who want to control their own message condition logic, please use the "all" parameter. The "all" parameter will tell the Triage Messaging method to send messages in all cases. You will be required to form your own conditions for restricting when a message is sent out from your trading platform.

The "all" parameter is for advanced users who know exactly how to write code that will send messages only on desired trading events. Otherwise, you may overload both the network and your own messaging device with too many messages.

You may use the Triage Messaging conditions in addition to your own messaging sending criteria. Shown below is a sample of how the messaging conditions are used in context.

// message sent to a phone destination once
m_TriageMsg1.SetCondition ("once");
m_TriageMsg1.SendMsg ("phone", "14152228888", "Send message once");

// message sent via SMS in real-time (last bar on chart) each time code runs
m_TriageMsg2.SetCondition ("now");      // if condition is "now" then this statement is optional
m_TriageMsg2.SendMsg ("sms", "14153338888", "Send message in real-time");

// message sent to MSN instant message account every 10 bars
m_TriageMsg3.SetCondition ("bar=10");
m_TriageMsg3.SendMsg ("msn", "msn@hotmail.com", "Send message every 10 bars");

// message sent to Yahoo instant message account every 20 minutes if my system is in a trade
If sys_status = 1 {     // my own condition
   m_TriageMsg4.SetCondition ("next=00:20:00");
   m_TriageMsg4.SendMsg ("yahoo", "myyahoouser", "Send message every 20 minutes",);
}

// message sent to e-mail every 300 ticks
   m_TriageMsg5.SetCondition ("tick=300");
   m_TriageMsg5.SendMsg ("email", "me@mail.com", "Send message every 300 ticks");

// message sent to gtalk instant message account during time interval 1AM to 2AM
   m_TriageMsg6.SetCondition ("time=01:00:00-02:00:00");
   m_TriageMsg6.SendMsg ("gtalk", "my@gmail.com", "Send message from 1AM to 2AM"); 

Message Method Return Results

The m_TriageMsg.SendMsg method will return error values as shown below. For error values -101 or lower, please contact Triage support and we will be happy to diagnose and troubleshoot the issue with you. Most traders will get a value of 1 (success) or -100, which means the message condition has not triggered, yet.

Messaging Errors
Error Description Return Value
Message sent out successfully 1
Internal Error: Triage Messaging seems not to be running. Check tray icon 0
No message sent out -1
One or more parameters are empty (service, destination or text) -2
The message condition has not triggered for this call (not an error) -100
Internal error, report to Triage Trading Support -101
Internal error, report to Triage Trading Support -102
Failed to load TriageMessaging.dll. May need reinstallation -200
The input parameter indicator or strategy is invalid -201
NinjaTrader assembly cannot be loaded (install error/version mismatch) -202


The m_TriageMsg.SetCondition method will return error values as shown below.

Condition Errors
Error Description Return Value
A new condition is set. Conditional variables reset. 1
The condition passed in is same as the existing condition. Nothing changes. 0
The message condition parameter is incorrect -103
Personal tools
Navigation