IsConnected

Author: Copyright 2021, Dark Ryd3r
0 Views
0 Downloads
0 Favorites
IsConnected
//+------------------------------------------------------------------+
//|                                                  IsConnected.mq5 |
//|                                       Copyright 2021, Dark Ryd3r |
//|                                    https://twitter.com/DarkRyd3r |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Dark Ryd3r"
#property link      "https://twitter.com/DarkRyd3r"
#property version   "1.00"
#include <Trade/TerminalInfo.mqh>

bool     first             = true;
bool     Now_IsConnected   = false;
bool     Pre_IsConnected   = true;
datetime Connect_Start = 0, Connect_Stop = 0;

CTerminalInfo terminalInfo;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{  

   ResetLastError(); 
   
	Print("Expert initialized");

	while ( !IsStopped() )
	{
		Pre_IsConnected = Now_IsConnected;
		Now_IsConnected = terminalInfo.IsConnected();
		
		if ( first ) { Pre_IsConnected = !Now_IsConnected; }
		
		if ( Now_IsConnected != Pre_IsConnected )
		{
			if ( Now_IsConnected )
			{
				Connect_Start = TimeLocal();
				if ( !first )
				{
					Print("Offline");
				}
				if ( IsStopped() ) { break; }
				Print("Online");
				}
			else
			{
				Connect_Stop = TimeLocal();
				if ( !first )
				{  Print("Online");
			   }
				if ( IsStopped() ) { break; }
				Print("Offline");
				}		
		}

		first = false;
		Sleep(1000);
	}

	if ( Now_IsConnected )
	{  Print("Online");
	}
	else
	{  Print("Offline");
		}		
	Print("IsConnected Expert Removed");
	return(0);
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
//---
   start();

//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---