Oto wskaźnik dla MetaTrader 5, który oblicza spread aktualnego instrumentu w procentach i wyświetla go w lewym dolnym rogu wykresu:
//+------------------------------------------------------------------+
//| SpreadPct.mq5 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"
#property version "1.00"
#property indicator_chart_window
#property indicator_plots 0
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(1);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
//--- delete all objects
ObjectsDeleteAll(0, "SpreadPct_");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
//---
CalculateAndDisplaySpread();
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
CalculateAndDisplaySpread();
}
//+------------------------------------------------------------------+
//| Function to calculate and display spread percentage |
//+------------------------------------------------------------------+
void CalculateAndDisplaySpread()
{
double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
if(ask == 0 || bid == 0) return;
double spread_points = ask - bid;
double spread_pct = (spread_points / bid) * 100;
string text = StringFormat("Spread: %.2f%%", spread_pct);
int x_pos = 10; // 10 pixels from left
int y_pos = (int)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS) - 20; // 20 pixels from bottom
ObjectCreate(0, "SpreadPct_Label", OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_XDISTANCE, x_pos);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_YDISTANCE, y_pos);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_CORNER, CORNER_LEFT_LOWER);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_ANCHOR, ANCHOR_LEFT);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_COLOR, clrWhite);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_BACK, false);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_HIDDEN, true);
ObjectSetInteger(0, "SpreadPct_Label", OBJPROP_FONTSIZE, 10);
ObjectSetString(0, "SpreadPct_Label", OBJPROP_TEXT, text);
}
//+------------------------------------------------------------------+
Instrukcja instalacji:
- Otwórz MetaEditor w MT5 (Ctrl+M)
- Kliknij "Nowy plik" (Ctrl+N)
- Wybierz "Custom Indicator" i kliknij "Dalej"
- Wpisz nazwę (np. "SpreadPct") i kliknij "Dalej"
- Skopiuj powyższy kod i wklej go w edytorze
- Skompiluj (F7)
- Wskaźnik będzie dostępny w oknie "Navigator" i możesz go przeciągnąć na wykres
Działanie wskaźnika:
- Oblicza spread jako procent ceny bid
- Aktualizuje się co 1 sekundę (timer) oraz przy nowych tickach
- Wyświetla wartość w lewym dolnym rogu wykresu
- Wartość jest pokazywana z dokładnością do 2 miejsc po przecinku
Możesz dostosować wygląd (kolor, rozmiar czcionki) zmieniając odpowiednie parametry w funkcji ObjectSetInteger.