CrossArbitr

Download script
Author: Scriptong
CrossArbitr

This script is designed to identify and monitor "Cross Arbitrage" opportunities by calculating the synthetic price of a currency pair based on other related market pairs. Instead of just looking at one chart, it mathematically links multiple instruments to determine if there is a pricing discrepancy between them.

How the Logic Works

1. Automated Pair Identification When you load the script on a specific chart, it automatically analyzes the name of the currency pair you are trading. It specifically looks for "cross pairs" (for example, GBP/JPY) where none of the three symbols in the pair are the US Dollar (USD).

Once it identifies the base and quote currencies from your current chart, the script automatically searches your market watch to find two other related currency pairs that involve those same currencies. It determines how these pairs relate to one another mathematically to build a "synthetic" price.

2. Synthetic Price Calculation The core logic of this tool is its ability to calculate a theoretical price (a "Synthetic Bid" and a "Synthetic Ask"). Depending on which currencies are involved, the script applies different mathematical formulas—ranging from multiplication and division to reciprocal calculations—to derive what the price should be based on the combined value of the other market pairs.

3. Real-Time Visualization The script performs these calculations instantly with every market tick. If you enable the display settings, it will draw horizontal lines directly on your chart representing these synthetic prices. This allows you to see at a glance how the "calculated" price compares to the actual "live" price of the pair you are currently watching.

4. Discrepancy Alerts The primary purpose of this logic is to detect "inefficiencies." The script constantly measures the distance between the live market price and the synthetic price it calculated from the other pairs.

If the difference between these two prices exceeds a specific threshold (which you can set in the parameters), the script triggers an audible alarm. This serves as a signal that the current market price may be deviating significantly from its underlying components, potentially indicating an arbitrage opportunity.

Miscellaneous
It plays sound alerts
7 Views
0 Downloads
0 Favorites
CrossArbitr
//+------------------------------------------------------------------+
//|                                                  CrossArbitr.mq4 |
//|                                                        Scriptong |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Scriptong"
#property link      ""

#property indicator_chart_window
//---- input parameters
extern bool      ShowBid=false;
extern bool      ShowAsk=false;
extern color     BidColor = Lime;
extern color     AskColor = Yellow;
extern int       AlarmIfPointDifference = 10;
extern string    AlarmFile = "wait.wav"; 


bool Activate = False;
string FP, SP;
double Tick;
int WayForCross;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   string S = Symbol();
   if(StringFind(S, "USD", 0) != -1)
     {
      Comment("Èíäèêàòîð ðàáîòàåò òîëüêî ñ êðîññîâûìè ïàðàìè (GBPJPY, EURJPY, EURGBP è ò. ä)!");
      return(0);
     }

   if(StringLen(S) != 6)
     {
      Comment("Èíäèêàòîð ðàáîòàåò òîëüêî ñ âàëþòíûìè ïàðàìè, ó êîòîðûõ íàçâàíèå ñîñòîèò èç øåñòè ñèìâîëîâ!");
      return(0);
     }
     
   // Ïîèñê ïåðâîé âàëþòíîé ïàðû êðîññà
   FP = StringSubstr(S, 0, 3)+"USD";
   MarketInfo(FP, MODE_BID);
   if(GetLastError() > 0)
     {
      FP = "USD"+StringSubstr(S, 0, 3);
      MarketInfo(FP, MODE_BID);
      if(GetLastError() > 0)
        {
         Comment("Íåâîçìîæíî íàéòè èíñòðóìåíò ", FP, " èëè ", StringSubstr(S, 0, 3)+"USD. Íåîáõîäèìî äîáàâèòü åãî â Îáçîðå ðûíêà è ïåðåçàïóñòèòü èíäèêàòîð.");
         return(0);
        } 
     } 
   // --------------------------------------  
     
   // "Ñîçäàíèå" âòîðîé âàëþòíîé ïàðû êðîññà
   SP = "USD"+StringSubstr(S, 3, 3);
   MarketInfo(SP, MODE_BID);
   if(GetLastError() > 0)
     {
      SP = StringSubstr(S, 3, 3)+"USD";
      MarketInfo(SP, MODE_BID);
      if(GetLastError() > 0)
        {
         Comment("Íåâîçìîæíî íàéòè èíñòðóìåíò ", SP, " èëè ", StringSubstr(S, 3, 3)+"USD. Íåîáõîäèìî äîáàâèòü åãî â Îáçîðå ðûíêà è ïåðåçàïóñòèòü èíäèêàòîð.");
         return(0);
        } 
     } 
   // --------------------------------------  

   // Îïðåäåëåíèå ïóòè ñèíòåçà êðîññà
   if (StringFind(FP, "USD") == 3 && StringFind(SP, "USD") == 0)
     WayForCross = 1;
   if (StringFind(FP, "USD") == 0 && StringFind(SP, "USD") == 0)
     WayForCross = 2;
   if (StringFind(FP, "USD") == 3 && StringFind(SP, "USD") == 3)
     WayForCross = 3;
   if (StringFind(FP, "USD") == 0 && StringFind(SP, "USD") == 3)
     WayForCross = 4;
   // -------------------------------

   Tick = MarketInfo(Symbol(), MODE_TICKSIZE);
   
   Activate = True;  
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
   if(ObjectFind("CrossBid") == 0)
     ObjectDelete("CrossBid");
   if(ObjectFind("CrossAsk") == 0)
     ObjectDelete("CrossAsk");
   
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Ïðèâåäåíèå çíà÷åíèé ê òî÷íîñòè îäíîãî òèêà                       |
//+------------------------------------------------------------------+
double ND(double A)
{
 return(NormalizeDouble(A, Digits));
}  

//+------------------------------------------------------------------+
//| Ðàñ÷åò çíà÷åíèÿ ñèíòåòè÷åñêîãî áèäà                              |
//+------------------------------------------------------------------+
double CalcBidWayForCross()
{
 switch (WayForCross)
   {
    case 1: return(ND(MarketInfo(FP, MODE_BID)*MarketInfo(SP, MODE_BID)));
    case 2: return(ND(MarketInfo(SP, MODE_BID)/MarketInfo(FP, MODE_ASK)));
    case 3: return(ND(MarketInfo(FP, MODE_BID)/MarketInfo(SP, MODE_ASK)));
    case 4: return(1/(ND(MarketInfo(FP, MODE_ASK)*MarketInfo(SP, MODE_BID))));
   }
}

//+------------------------------------------------------------------+
//| Ðàñ÷åò çíà÷åíèÿ ñèíòåòè÷åñêîãî àñêà                              |
//+------------------------------------------------------------------+
double CalcAskWayForCross()
{
 switch (WayForCross)
   {
    case 1: return(ND(MarketInfo(FP, MODE_ASK)*MarketInfo(SP, MODE_ASK)));
    case 2: return(ND(MarketInfo(SP, MODE_ASK)/MarketInfo(FP, MODE_BID)));
    case 3: return(ND(MarketInfo(FP, MODE_ASK)/MarketInfo(SP, MODE_BID)));
    case 4: return(1/(ND(MarketInfo(FP, MODE_BID)*MarketInfo(SP, MODE_ASK))));
   }
}

  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   if(!Activate)
    return(0);

   // Ðàñ÷åò ñèíòåòè÷åñêèõ Bid è Ask
   double CalcBid = CalcBidWayForCross();  
   RefreshRates();
   double CalcAsk = CalcAskWayForCross();  
   // -------------------------------

   // Ðèñóåì óðîâåíü ñèíòåòè÷åñêîãî Bid
   if (ShowBid)
     {
      if(ObjectFind("CrossBid") == -1)
        {
         ObjectCreate("CrossBid", OBJ_HLINE, 0, 0, CalcBid); 
         ObjectSet("CrossBid", OBJPROP_COLOR, BidColor);
         ObjectSet("CrossBid", OBJPROP_STYLE, STYLE_DOT);
        } 
       else
        if(!ObjectMove("CrossBid", 0, 1, CalcBid))
           Print("Îøèáêà ïåðåìåùåíèÿ ¹", GetLastError());
     }
   // ----------------------------------        

   // Ðèñóåì óðîâåíü ñèíòåòè÷åñêîãî Ask
   if(ShowAsk)
    {
     if(ObjectFind("CrossAsk") == -1)
       {
        ObjectCreate("CrossAsk", OBJ_HLINE, 0, 0, CalcAsk); 
        ObjectSet("CrossAsk", OBJPROP_COLOR, AskColor);
        ObjectSet("CrossAsk", OBJPROP_STYLE, STYLE_DOT);
       } 
      else
       ObjectMove("CrossAsk", 0, 1, CalcAsk);
    }
   // ----------------------------------        

   Comment("Ðåàëüíûé BID = ", DoubleToStr(Bid, Digits), ", ðåàëüíûé ASK = ", DoubleToStr(Ask, Digits), 
           "\nÑèíòåòèê BID = ", DoubleToStr(CalcBid, Digits), ", ñèíòåòèê ASK = ", DoubleToStr(CalcAsk, Digits));

   // Âûäàåì çâóêîâîé ñèãíàë î ïðåâûøåíèè ìèíèìàëüíîé ðàçíîñòè
   if(ND(MathAbs(CalcBid-Bid)) >= ND(AlarmIfPointDifference*Point))
     PlaySound(AlarmFile);
   // --------------------------------------------------------  
    
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments