img

Object file

The NeTtradeX Language (NTL+) provides capabilities of working with files that can be used for saving and reading user information.

With the help of NTL+, it is possible to create new files, read existing ones, append new information to an existing file or delete files.

The files the terminal works with must be placed in the username\Documents\NeTtradeX Advisors\scripts\files folder. If a file is specified with a path that contains folders, the folders will be created automatically.

Before reading information from or writing it to a file, a file object should be created. This can be done as follows: file f;, it is also possible to combine the creation and opening of a file by specifying file name and opening mode in the object constructor file f("data.txt",fmRead|fmText);

When working with any files, it is necessary to specify file name and combination of constants for defining the operation mode. File names must comply with the standard OS Windows rules, for instance, file names must not containing the following symbols: : \, /, :, *, ?, ", <, >, |.

Constants for working with files:

  • fmRead - reading mode
  • fmWrite - writing mode
  • fmBinary - work with binary data
  • fmText - work with text data

Object methods:

MethodDescription
bool Open(const string &in, int mode=0) The method opens a file for subsequent writing and reading operations.
bool IsOpen() The method checks whether a specified file is open for work.
void Close() The method closes a file
void Flush() The method flushes all the data from the buffer to the disk.
bool IsEOF() The method checks if the file pointer is placed at the end of the file.
bool Seek(int offset,int origin) The method moves the file pointer inside the file by a specified number of bytes. The offset parameter sets the shift in bytes; negative values correspond to the shift to the beginning of a file, positive values - to the end of a file. Parameter origin points at the position from which the shift is set. The following values are available:
  • fsSet - offset from the beginning of a file
  • fsCurrent - offset from the current position
  • fsEnd - offset from the end of a file
int Size() The method returns file size in bytes.
int Tell() The method returns the offset of a file pointer's current position from the beginning of a file.
void Delete(const string& in) The method deletes the file whose path is specified in the variable in. For instance, in order to delete the file data.txt located in Documents\NetTradeX Advisors\scripts\files folder, you should write file x; x.Delete("data.txt")
double ReadDouble() The method reads a double number from the current position where the file pointer is placed.
float ReadFloat() The method reads a float number from the current position where the file pointer is placed.
int ReadInteger() The method reads an int number from the current position where the file pointer is placed.
int64 ReadInt64() The method reads an int64 number from the current position where the file pointer is placed.
int16 ReadShort() The method reads an int16(short) number from the current position where the file pointer is placed.
int8 ReadByte() The method reads an int8(Byte) number from the current position where the file pointer is placed.
bool ReadString(string &out str,int sz=0) The method reads a string from the current position where the file pointer is placed and stores it in the string str. Parameter sz determines the string length. If the parameter sz is not set explicitly, the string is read until the end of line symbol.
bool WriteDouble(double val) The method writes the value of a val variable of double type to the file.
bool WriteFloat(float val) The method writes the value of a val variable of float type to the file.
bool WriteInteger(int val) The method writes the value of a val variable of int type to the file.
bool WriteInt64(int64 val) The method writes the value of a val variable of int64 type to the file.
bool WriteShort(int16 val) The method writes the value of a val variable of int16 type to the file.
bool WriteByte(int8 val) The method writes the value of a val variable of int8 type to the file.
bool WriteString(const string &in str) The method writes the string str to the file.

Example 1. Let us create an advisor that will write time, bid and ask prices of the last quote to a file. If the file "ticks.txt" does not exist, our advisor will create it; if the file exists, the advisor will append quotes to the end of the file. To view the contents of "ticks.txt", the advisor has to be stopped.

 file f; int Initialize() { // If the file exists, we open it to append. // If the file does not exist, we create and open it for writing if(!f.Open("ticks.txt",fmRead|fmWrite|fmText)) f.Open("ticks.txt",fmWrite|fmText); f.Seek(0,fsEnd); // moving the pointer to the end of the file in order to append to the end. return(0); } int Run() { // writing date and time, bid and ask prices f.WriteString(""+System.Time+";"+Symbols.LastBid(Chart.Symbol)+";"+Symbols.LastAsk(Chart.Symbol)+"\n"); // flushing the string to the disk f.Flush(); return(0); } int DeInitialize() { f.Close(); return(0); } 

Example 2. Let us create a script that will write data to a file, read the data from the file and output it in the program journal.

 int Run() { file f; //Opening a file for writing f.Open("data.txt",fmWrite|fmBinary); //Writing the current symbol, minimum order distance and last bid f.WriteString(""+Chart.Symbol+"\n"); f.WriteInteger(Symbols.Distance(Chart.Symbol)); f.WriteDouble(Symbols.LastBid(Chart.Symbol)); //Closing the file f.Close(); //Opening a file for reading f.Open("data.txt",fmRead|fmBinary); //Reading a symbol, minimum order distance and written bid price. string s=""; f.ReadString(s); int dst=f.ReadInteger(); double bid=f.ReadDouble(); //Outputting the information on the Journal tab System.Print("Current symbol="+s+" distance="+dst+" bid="+bid); //Closing the file f.Close(); return(0); }