ReverseSymbol

ReverseSymbol

This script is designed to automate the process of exporting trading data from your MetaTrader platform into a structured historical file. It essentially acts as a bridge, continuously watching price movements and recording them in real-time so that you have a consistent log of market activity for specific currency pairs and timeframes.

Core Functionality

The logic operates by monitoring the "live" price action on your chart and translating that data into an external binary file. Rather than just taking a snapshot of the price once an hour or a day, it systematically captures the open, high, low, and close prices, along with trading volume, every time a new price movement occurs.

How the Logic Works

To ensure accuracy and efficiency, the script follows a specific sequence of operations:

  • Initialization and File Setup: When you first run the script, it identifies exactly which asset (e.g., EURUSD) and timeframe (e.g., H1) you are viewing. It then creates a unique historical file dedicated to that specific pair and timeframe. This ensures that your data remains organized and doesn't get mixed up with other charts.
  • Continuous Data Monitoring: The script constantly checks the clock. It distinguishes between two types of updates:
    1. New Bar Creation: When a new candle (time period) begins, the script identifies the completed candle and writes its final statistics into your history file.
    2. Live Price Updates: While a candle is still "active" and moving, the script focuses on the most recent price point. It updates the current candle's data in the file to reflect the latest high, low, or close.
  • Data Transformation: Interestingly, the logic applies a mathematical inversion to the prices as they are being recorded. This indicates that the script is preparing the data for a specific type of analysis or custom calculation required by your secondary tools.
  • System Synchronization: To keep the interface responsive, the script includes a "refresh" command. This forces the trading platform to acknowledge the new data being written, ensuring that the visual information you see on your screen remains consistent with the data being saved in the background.

Summary of Operation

In simple terms, this script serves as a real-time data logger. It watches the market 24/7, captures every price fluctuation, translates those numbers into a specific format, and saves them into a dedicated file on your computer. It handles both the "closing" of old data and the "updating" of live data automatically, providing you with a continuous stream of saved history without requiring any manual intervention.

Miscellaneous
It writes information to fileIt writes information to fileIt writes information to fileIt writes information to file
6 Views
0 Downloads
0 Favorites
ReverseSymbol
#property indicator_chart_window
#property indicator_buffers 0

#import "user32.dll"
  int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
#import

#define WM_COMMAND 0x0111

#define LB_OFFSET -32
#define BEGIN_OFFSET 148

#define REFRESH_CODE 33324

double open, low, high, close;
int handle, PrevTime;
string SymbolName;

void GetSymbolName()
{
  SymbolName = StringSubstr(Symbol(), 3, 3) + StringSubstr(Symbol(), 0, 3);
  
  return;
}

bool GetBarPrices( int Pos )
{  
  open = 1 / Open[Pos];
  high = 1 / Low[Pos];
  low = 1 / High[Pos];
  close = 1 / Close[Pos];
  
  return;
}

void WriteBar( int STime, double SOpen, double SHigh, double SLow, double SClose, double SVolume )
{
  FileWriteInteger(handle, STime);
  FileWriteDouble(handle, SOpen);
  FileWriteDouble(handle, SLow);
  FileWriteDouble(handle, SHigh);
  FileWriteDouble(handle, SClose);
  FileWriteDouble(handle, SVolume);
  
  FileFlush(handle);
  
  return;
}

void WriteBars( int Pos )
{
  Pos--;
  
  while (Pos >= 0)
  {
    GetBarPrices(Pos);
    WriteBar(Time[Pos], open, high, low, close, Volume[Pos]);
    Pos--;  
  }
  
  return;
}

void ModifyLastBar( int Pos )
{
  GetBarPrices(Pos);
  
  FileSeek(handle, LB_OFFSET, SEEK_CUR);

  FileWriteDouble(handle, low);
  FileWriteDouble(handle, high);
  FileWriteDouble(handle, close);
  FileWriteDouble(handle, Volume[Pos]);
  
  FileFlush(handle);
  
  return;
}

void CreateHandle()
{
  string FileName;
  int Tmp[15], TmpTime;

  GetSymbolName();
  FileName = SymbolName + Period() + ".hst";
  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, REFRESH_CODE, 0);
    
  return;
}

void init()
{  
  CreateHandle();
  
  PrevTime = Time[0];

  return;
}

void deinit()
{
  FileClose(handle);
  
  return;
}

void start()
{
  static int PrevBars = 0;
  static int Pos;
  
  if (PrevTime != Time[0])
  {
    Pos = iBarShift(Symbol(), Period(), PrevTime);
    
    ModifyLastBar(Pos);
    WriteBars(Pos);
    
    PrevTime = Time[0];
    PrevBars = Bars;
  }
  else
  {
    if (PrevBars != Bars)
    {
      FileSeek(handle, BEGIN_OFFSET, SEEK_SET);
      
      WriteBars(Bars);
      
      PrevBars = Bars;
    }
    else
      ModifyLastBar(0);
  }

  RefreshChart();        

  return;
}

Comments