//+------------------------------------------------------------------+
//| Lesson 16 - Timers, trade events, chart events, notifications |
//+------------------------------------------------------------------+
#property copyright "Strategist Academy"
#property version "1.00"
#property strict
input int TimerSeconds = 60;
int OnInit()
{
EventSetTimer(TimerSeconds);
Print("Timer set to ", TimerSeconds, "s. Press 'B' on the chart to send a test alert.");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
EventKillTimer(); // always - a stray timer is a nasty bug
Comment(""); // clear the on-chart dashboard
}
//+------------------------------------------------------------------+
//| Time-based work: runs whether or not the market is moving |
//+------------------------------------------------------------------+
void OnTimer()
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double equity = AccountInfoDouble(ACCOUNT_EQUITY);
double free = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
Comment("Lesson 16 dashboard",
"\nSymbol: ", _Symbol,
"\nBid: ", DoubleToString(bid, _Digits),
"\nEquity: ", DoubleToString(equity, 2),
"\nFree: ", DoubleToString(free, 2),
"\nPositions: ", PositionsTotal(),
"\nUpdated: ", TimeToString(TimeCurrent(), TIME_SECONDS));
}
//+------------------------------------------------------------------+
//| Fires when anything trade-related happens on the account |
//+------------------------------------------------------------------+
void OnTrade()
{
Print("Trade event. Positions total: ", PositionsTotal(),
" orders: ", OrdersTotal());
// React, but do not place new orders from in here - set a flag instead.
if(PositionSelect(_Symbol))
{
double sl = PositionGetDouble(POSITION_SL);
double tp = PositionGetDouble(POSITION_TP);
Print("Position on ", _Symbol,
" type=", (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ? "BUY" : "SELL"),
" volume=", DoubleToString(PositionGetDouble(POSITION_VOLUME), 2),
" sl=", DoubleToString(sl, _Digits),
" tp=", DoubleToString(tp, _Digits));
}
}
//+------------------------------------------------------------------+
//| Keyboard and object interaction |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
if(id == CHARTEVENT_KEYDOWN)
{
if(lparam == 66) // 'B'
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
string msg = _Symbol + " manual check at " + DoubleToString(bid, _Digits);
Alert(msg);
Print("Alert raised: ", msg);
// Requires push notifications enabled in terminal settings.
if(!SendNotification(msg))
Print("SendNotification failed - enable push notifications in terminal options.");
}
}
}