EXP_Monitoring-Synthetic
Download scriptThis script is designed to automate the tracking and calculation of complex financial data within the MetaTrader platform. Its primary function is to monitor, synthesize, and log price movements and "spread" data for specific currency pairs that are not natively provided as single assets by your broker.
Core Functionality Overview
1. Dynamic Symbol Identification The script begins by analyzing the current asset you are viewing to identify its underlying components. It automatically detects the base currencies and the specific suffixes (like prefixes or suffixes used by different brokers). Based on these components, it identifies two related symbols and determines the correct mathematical relationship needed to calculate a synthetic price. This allows the tool to work across various broker environments without manual configuration for every new pair.
2. Synthetic Price Calculation Because some specific cross-rates or calculated values are not available as standard charts, the script performs real-time calculations on the fly. Depending on the detected symbols, it uses three different mathematical logic paths:
- Division of one price by another.
- Multiplication of two prices.
- The inverse calculation (1 divided by the product of two prices).
This ensures that you are always viewing a mathematically accurate "synthetic" price based on the live feeds of the underlying assets.
3. Real-Time Data Tracking and Logging The script monitors the market continuously for any changes in the bid or ask prices. When it detects a change, it performs several tasks:
- Price Monitoring: It tracks the High, Low, Open, and Close prices of the synthetic asset.
- Spread Analytics: It captures the "spread" (the difference between the buying and selling price), identifying the minimum, maximum, and average spreads over time.
- Automated Logging: It writes this data into external history files on your computer. This creates a permanent record of how the synthetic price and its spread behaved over time.
4. Visual Feedback and Synchronization To keep the user informed, the script provides two types of feedback:
- On-Screen Commentary: It displays a live text update on the chart showing the current spreads for both the original symbol and the calculated synthetic symbol, highlighting the difference between them.
- Chart Refreshing: The script includes logic to signal the platform to refresh its visual data, ensuring that the information displayed remains current with the rapid movements of the market.
Summary of Workflow
When you load this script, it identifies your assets, calculates their combined value, and begins a cycle of monitoring. Every time the market moves, it updates its internal calculations; whenever a new time candle forms, it saves all captured data (price and spread) into a history file and resets the trackers for the next period. This provides a comprehensive way to analyze complex, multi-symbol price actions as if they were a single, standard trading instrument.
#property show_inputs
#import "user32.dll"
int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
#import
#define WM_COMMAND 0x0111
#define PAUSE 100
extern string Currency = "USD";
string Symbol1, Symbol2;
bool Math; // 0 - S1 / S2, 1 - S1 * S2, 2 - 1 / (S1 * S2)
int handle;
string SymbolName;
int time;
double open, low, high, close;
int volume;
double PriceBid, PriceAsk;
double Bid1 = 0, Bid2 = 0, Ask1 = 0, Ask2 = 0;
int Digits1, Digits2;
double MinSpread, MaxSpread, AverageSpread;
bool RealSymbol( string Str )
{
return(MarketInfo(Str, MODE_BID) != 0);
}
void GetSymbols()
{
string Currency1, Currency2;
string SymbolPrefix;
string Str1, Str2;
Currency1 = StringSubstr(Symbol(), 0, 3);
Currency2 = StringSubstr(Symbol(), 3, 3);
SymbolPrefix = StringSubstr(Symbol(), 6, StringLen(Symbol()) - 6);
Str1 = Currency1 + Currency + SymbolPrefix;
Str2 = Currency + Currency1 + SymbolPrefix;
if (RealSymbol(Str1))
{
Symbol1 = Str1;
Str1 = Currency2 + Currency + SymbolPrefix;
Str2 = Currency + Currency2 + SymbolPrefix;
if (RealSymbol(Str1))
{
Symbol2 = Str1;
Math = 0; // S1 / S2
}
else if (RealSymbol(Str2))
{
Symbol2 = Str2;
Math = 1; // S1 * S2
}
}
else if (RealSymbol(Str2))
{
Symbol2 = Str2;
Str1 = Currency2 + Currency + SymbolPrefix;
Str2 = Currency + Currency2 + SymbolPrefix;
if (RealSymbol(Str1))
{
Symbol1 = Str1;
Math = 2; // 1 / (S1 * S2)
}
else if (RealSymbol(Str2))
{
Symbol1 = Str2;
Math = 0; // S1 / S2
}
}
return;
}
bool GetPrices()
{
switch (Math)
{
case 0: // S1 / S2
PriceBid = Bid1 / Ask2;
PriceAsk = Ask1 / Bid2;
break;
case 1: // S1 * S2
PriceBid = Bid1 * Bid2;
PriceAsk = Ask1 * Ask2;
break;
case 2: // 1 / (S1 * S2)
PriceBid = 1 / (Ask1 * Ask2);
PriceAsk = 1 / (Bid1 * Bid2);
break;
}
return;
}
bool SymbolChange()
{
double NewBid1, NewBid2, NewAsk1, NewAsk2;
NewBid1 = MarketInfo(Symbol1, MODE_BID);
NewBid2 = MarketInfo(Symbol2, MODE_BID);
NewAsk1 = MarketInfo(Symbol1, MODE_ASK);
NewAsk2 = MarketInfo(Symbol2, MODE_ASK);
if ((NormalizeDouble(NewBid1 - Bid1, Digits1) != 0) || (NormalizeDouble(NewBid2 - Bid2, Digits2) != 0) ||
(NormalizeDouble(NewAsk1 - Ask1, Digits1) != 0) || (NormalizeDouble(NewAsk2 - Ask2, Digits2) != 0))
{
Bid1 = NewBid1;
Bid2 = NewBid2;
Ask1 = NewAsk1;
Ask2 = NewAsk2;
GetPrices();
return(TRUE);
}
return(FALSE);
}
void WriteBar()
{
FileWriteInteger(handle, time);
FileWriteDouble(handle, open);
FileWriteDouble(handle, low);
FileWriteDouble(handle, high);
FileWriteDouble(handle, close);
FileWriteDouble(handle, volume);
FileFlush(handle);
return;
}
int GetLastTime()
{
int Tmp1, Tmp2;
Tmp1 = iTime(Symbol1, Period(), 0);
Tmp2 = iTime(Symbol2, Period(), 0);
if (Tmp1 > Tmp2)
return(Tmp1);
return(Tmp2);
}
void CreateNewBar()
{
time = GetLastTime();
open = PriceBid;
low = PriceBid;
high = PriceBid;
close = PriceBid;
volume = 1;
return;
}
void ModifyBar()
{
if (PriceBid > high)
high = PriceBid;
else if (PriceBid < low)
low = PriceBid;
close = PriceBid;
volume++;
}
void CreateNewSpread()
{
MinSpread = (PriceAsk - PriceBid) / Point;
MaxSpread = MinSpread;
AverageSpread = MinSpread;
return;
}
void ModifySpread()
{
double Spread = (PriceAsk - PriceBid) / Point;
if (Spread > MaxSpread)
MaxSpread = Spread;
else if (Spread < MinSpread)
MinSpread = Spread;
AverageSpread += Spread;
return;
}
void WriteSpread()
{
int hSpread = FileOpen(SymbolName + Period() + "_Spread.dat", FILE_BIN|FILE_READ|FILE_WRITE);
AverageSpread /= volume;
FileSeek(hSpread, 0, SEEK_END);
FileWriteInteger(hSpread, time);
FileWriteDouble(hSpread, MaxSpread);
FileWriteDouble(hSpread, AverageSpread);
FileWriteDouble(hSpread, MinSpread);
FileClose(hSpread);
return;
}
void CreateHandle()
{
string FileName;
int Tmp[15], TmpTime;
SymbolName = StringSubstr(Symbol(), 0, 6) + "_" + Currency;
FileName = SymbolName + Period() + ".hst";
handle = FileOpenHistory(FileName, FILE_BIN|FILE_READ|FILE_WRITE);
if (FileSize(handle) > 0)
{
if (FileSize(handle) > 148)
{
FileSeek(handle, -44, SEEK_END);
TmpTime = FileReadInteger(handle);
if (TmpTime == time)
{
open = FileReadDouble(handle);
low = FileReadDouble(handle);
high = FileReadDouble(handle);
close = FileReadDouble(handle);
volume = FileReadDouble(handle);
return;
}
}
FileSeek(handle, 0, SEEK_END);
return;
}
FileClose(handle);
handle = FileOpenHistory(FileName, FILE_BIN|FILE_WRITE);
FileWriteInteger(handle, 400);
FileWriteString(handle, "Created by " + WindowExpertName(), 64);
FileWriteString(handle, SymbolName, 12);
FileWriteInteger(handle, Period());
FileWriteInteger(handle, Digits);
FileWriteArray(handle, Tmp, 0, 15);
return;
}
void RefreshChart()
{
int hwnd = WindowHandle(SymbolName, Period());
PostMessageA(hwnd, WM_COMMAND, 33324, 0);
return;
}
string GetComment()
{
int Spread = (Ask - Bid) / Point + 0.1;
double SpreadX = (PriceAsk - PriceBid) / Point;
string Str;
Str = Symbol() + " spread = " + Spread + "\n" + SymbolName + " spread = " + DoubleToStr(SpreadX, 1);
Str = Str + "\nDifference = " + DoubleToStr(Spread - SpreadX, 1);
return(Str);
}
void init()
{
GetSymbols();
Digits1 = MarketInfo(Symbol1, MODE_DIGITS);
Digits2 = MarketInfo(Symbol2, MODE_DIGITS);
SymbolChange();
CreateNewBar();
CreateNewSpread();
CreateHandle();
return;
}
void deinit()
{
FileClose(handle);
Comment("");
return;
}
void start()
{
while (!IsStopped())
{
if (SymbolChange())
{
Comment(GetComment());
if (time < GetLastTime())
{
WriteBar();
WriteSpread();
CreateNewBar();
CreateNewSpread();
RefreshChart();
}
else
{
ModifyBar();
ModifySpread();
}
}
Sleep(PAUSE);
RefreshRates();
}
return;
}
Comments