Code: Select all
//+------------------------------------------------------------------+
//| keystroke_symbol_changer.mq5 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
//--- input parameters
input string NextSymbolKey="KEY_RIGHT";
input string PrevSymbolKey="KEY_LEFT";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
#define KEY_LEFT 37
#define KEY_RIGHT 39
int symbolIndex = 0;
int OnInit()
{
ChartSetInteger(ChartID(), CHART_EVENT_OBJECT_CREATE, true);
ChartSetInteger(ChartID(), CHART_EVENT_OBJECT_DELETE, true);
int total = SymbolsTotal(false); // Get the total number of symbols
ArrayResize(symbols, total); // Resize the array to fit all symbols
for(int i = 0; i < total; i++)
{
symbols[i] = SymbolName(i, false); // Get the name of the symbol and store it in the array
}
ChartRedraw();
return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime& time[],
const double& open[],
const double& high[],
const double& low[],
const double& close[],
const long& tick_volume[],
const long& volume[],
const int& spread[])
{
return(rates_total);
}
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
if(id==CHARTEVENT_KEYDOWN){
switch(lparam)
{
case KEY_RIGHT:
{
symbolIndex++;
string nextSymbol = symbols[symbolIndex];
ChartSetSymbolPeriod(0, nextSymbol, PERIOD_D1);
break;
}
case KEY_LEFT:
{
symbolIndex--;
string prevSymbol = symbols[symbolIndex]; // Get the previous symbol
ChartSetSymbolPeriod(0, prevSymbol, PERIOD_D1); // Change the symbol of the chart
break;
}
default:
Print("Some not listed key has been pressed");
}
ChartRedraw();
}
}