NAT Documentation


Getting Started

Main NAT Window Interface

Automatic Buy/Sell Button Location

Optimizing MT4 News Sources

About Log Files

Creating
News Scripts

Custom News Sources

 

Custom News Sources


About Custom News Sources

If you have access to other news sources, it is possible to integrate them into NAT, so that they can be used for generating trade signals (programming required).

This feature is available only in NAT Full version.

There are 3 ways you can send news headlines and other data to NAT:

1. Via DLL, by using the NewsAutoTrader.dll function SendMessageToNAT. Multiple applications can send data to NAT.

2. By implementing a Windows console application that outputs/displays the news headlines. Can be useful for Java APIs. NAT will launch the console application and read its redirected console output. Single application limit.

3. By implementing a TCP/IP server application. Useful if you want to send data from a remote location. NAT will function as a TCP/IP client (single client limitation).


DLL Interface


To send data to NAT use NewsAutoTrader.dll function "SendMessageToNAT":

function SendMessageToNAT(Msg:PChar; ProcessByScripts:integer):integer; stdcall;

Msg
is a pointer to NULL terminated NAT message string.

ProcessByScripts
value needs to be 1 for message to be processed by scripts (except for message types that don't support this).

Returns 0 on success.

In NAT Options->News Sources, "Allow using DLL interface" needs to be checked.

NAT news reader needs to be started, otherwise the messages will be ignored.

For NewsAutoTrader.dll to be accessible add the NAT installation directory to system path.

For DLL news sources, the active news source counter (label "S:" in main window) will not be increased.

Msg contains a message string in one of the following formats (without quotes):

"H:<source>:<headline text>"

   
- sends a news headline to NAT and processes it by script, if attached.

"D:<source>:<text>"

   
- use to send other, non-headline data to scripts (works like "H:...", except the text will not be displayed in headline box, not logged, no duplicate checks done like for headlines).
Usage ideas: could be used to send Bid / Ask quote data to log the execution prices (by using WriteLnLog procedure) or to monitor the spreads and not execute the trades if spread is too high.

"I:<text>"

   
- use to send info message to NAT (text displayed in headline box, not logged, not processed by scripts, no time info added).

"TI:<text>"

   
- use to send info message to NAT (text displayed in headline box, not logged, not processed by scripts). Works like "I:...", except time info will be added to headline box, like for headlines (<time>: <text>).


Example MetaTrader 4 indicator that sends Bid / Ask quotes to NAT
(in this case uses TI:.. messages which are not processed by scripts):

 

//+------------------------------------------------------------------+
//| NewsAutoTrader DLL Example.mq4                                   |
//| Xangi Software                                                   |
//| http://www.newsautotrader.com                                    |
//+------------------------------------------------------------------+

#property copyright "
Xangi Software"
#property link "http://www.newsautotrader.com"
#property indicator_chart_window

#import "NewsAutoTrader.dll"
int SendMessageToNAT(string Msg, int ProcessByScripts);

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
  if (ObjectFind("lblNAT")<0) ObjectCreate("lblNAT",OBJ_LABEL,0,0,0);
  ObjectSet("lblNAT",OBJPROP_XDISTANCE,5);
  ObjectSet("lblNAT",OBJPROP_YDISTANCE,15);
  ObjectSet("lblNAT",OBJPROP_CORNER,0);
  ObjectSetText("lblNAT","NAT quote reader",10,"Times New Roman",White);
  return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
  ObjectDelete("lblNAT");
  return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
  SendMessageToNAT("TI:"+Symbol()+" "+DoubleToStr(Bid,5)+" "+DoubleToStr(Ask,5), 1);
  return(0);
}
//+------------------------------------------------------------------+

  

Console Application Interface

 

To send data to NAT by using this method you need to implement a console application that outputs messages to console (standard output, STDOUT). When the news reader is started ('Start' button in NAT) the application will be launched and NAT will read its redirected console output. Single application limit.

The EXE path specified in "Options->News Sources->Custom news sources" can contain parameters, if necessary (complete command line).

Important: for messages to be not delayed, the output buffer needs to be flushed after each message!

If NAT is closed while the console application is running, console application process will be automatically terminated.

constant CRLF = standard Windows newline character sequence = ASCII carriage return (hex. 0x0D) + line feed (hex. 0x0A) character.

NAT Messages (without quotes):

"STARTNEWSPROC" + CRLF

   
- enables headline processing by scripts, enables headline logging for this source, increases the news source count ('S:' label in NAT window).

"STOPNEWSPROC" + CRLF

   
- disables headline processing by scripts for this news source, disables headline logging for this source, decreases the news source count ('S:' label in NAT window).

"H:<source>:<headline text>" + CRLF

   
- sends a news headline to NAT and processes it by script, if attached.

"D:<source>:<text>" + CRLF

   
- use to send other, non-headline data to scripts (works like "H:...", except the text will not be displayed in headline box, not logged, no duplicate checks done like for headlines).


"I:<text>" + CRLF

   
- use to send info message to NAT (displayed in headline box, not logged, not processed by scripts, no time info added).


"TI:<text>" + CRLF

   
- use to send info message to NAT (displayed in headline box, not logged, not processed by scripts). Works like "I:...", except time info will be added to headline box, like for headlines (<time>: <headline>).


If the output line is not recognized as a valid message, it will be ignored  - it's OK for console application to output other stuff, just make sure this output is not mistaken with messages to NAT.

In Java, when using System.
out.println, output buffer flushing works OK as is. Example:
 

public class Test {

public static void main(String args[])
{
  System.
out.println("STARTNEWSPROC");
  int c = 1;
  do {
    System.
out.println("H:TEST:test headline " + String.format("%d",c));
    try {
      Thread.sleep(100);
    }
   
catch (Exception e) {};
    c++;
  }
while (true);
}
}


Visual C++ example:

#include "stdafx.h"
#include "windows.h";

int main()
{
  printf(
"STARTNEWSPROC\n");
  fflush(stdout);
// required
 
int c=1;

 
do {
    printf(
"H:TEST:Test headline %d\n", c);
    fflush(stdout);
// required
   
Sleep(100);
    c++;
  }
while (true);

  return 0;
}


Delphi example:

program ConsoleTest;

{$APPTYPE CONSOLE}

uses SysUtils, Windows;

var AWriteLnBuffer:array[0..1023] of Char;
    hOut:THandle;
    c:integer=1;

procedure AWriteLn(const s:string);
var byteswritten:longword;
begin
  StrPLCopy(AWriteLnBuffer, s+#13#10, SizeOf(AWriteLnBuffer));
  WriteFile(hOut, AWriteLnBuffer, StrLen(AWriteLnBuffer), byteswritten, nil);
end;

begin
  hOut:=GetStdHandle(STD_OUTPUT_HANDLE);
  AWriteLn('STARTNEWSPROC');
  repeat
    // standard WriteLn will not work correctly, because output buffer is not flushed after each call
    AWriteLn('H:TEST:test headline '+IntToStr(c));
    Sleep(100);
    Inc(c);
  until false;
end.

 

TCP/IP Server Interface

To send data to NAT by using this method you need to implement a TCP server. NAT will function as a TCP client (single client limit). NAT connects to TCP server when the news reader is started (when 'Start' is clicked).

The protocol:

1. Logging on:

First, when a connection is established the TCP server should output the string "NAT" with added LF character (ASCII linefeed, 0x0A in hex., 10 in decimal). Delphi example:
TCPServer.WriteLn('NAT');

Next, the TCP server should read the password string + LF. If password is OK, output "OK" + LF, else exit. Delphi example:  password:=TCPServer.ReadLn(#$A,10000); // 10000 - timeout

After that, you can start sending the messages below.

2. NAT Messages (without quotes):

"STARTNEWSPROC" + LF

   
- enables headline processing by scripts, enables headline logging for this source, increases the news source count ('S:' label in NAT window).

"STOPNEWSPROC" + LF

   
- disables headline processing by scripts, disables headline logging for this source, decreases the news source count ('S:' label in NAT window).

"H:<source>:<headline text>" + LF

   
- sends a news headline to NAT and processes it by script, if attached. Delphi example:
TCPServer.WriteLn('H:MYSOURCE:Test headline');

"D:<source>:<text>" + LF

   
- use to send other, non-headline data to scripts (works like "H:...", except the text will not be displayed in headline box, not logged, no duplicate checks done like for headlines). Delphi example:
TCPServer.WriteLn('D:EURUSD:1.2143 1.4145');

"I:<text>" + LF

   
- use to send info message to NAT (displayed in headline box, not logged, not processed by scripts, no duplicate checks, no time info added). Delphi example:
TCPServer.WriteLn('I:Hello');

"TI:<text>" + LF

   
- use to send info message to NAT (displayed in headline box, not logged, not processed by scripts, no duplicate checks). Works like "I:...", except time info will be added to headline box, like for headlines (<time>: <text>). Delphi example:
TCPServer.WriteLn('TI:Hello');


Example TCP server code for Delphi (uses Indy 9):
 

unit TCPServerTest;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdTCPServer;

type TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
  private
  { Private declarations }
  procedure TCPExecute(AThread: TIdPeerThread);
end;

var Form1: TForm1;
    TCPServer: TIdTCPServer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TCPServer:=TIdTCPServer.Create(nil);
  TCPServer.OnExecute:=TCPExecute;
  TCPServer.DefaultPort:=30001;
  TCPServer.Active:=true;
end;

procedure TForm1.TCPExecute(AThread:TIdPeerThread);
var password:string;
    c:integer;
begin
  c:=1;
  with AThread.Connection do
  begin
    try
      WriteLn('NAT');
      password:=ReadLn(#$A,10000);
      // ignores password in this example
      WriteLn('OK'); // password accepted
      WriteLn('STARTNEWSPROC');
      repeat
        WriteLn('H:TEST:Test headline '+IntToStr(c));
        Inc(c);
        Sleep(100);
      until AThread.Terminated;
    finally
      if Connected then Disconnect;
    end;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  TCPServer.Free;
end;

end.