The MQL4 Execution Logic
Save this as TV_Webhook_Receiver.mq4 in your MQL4/Experts folder. Just like the MT5 version, it relies on the open-source JAson.mqh library (which is fully backwards-compatible with MT4) to parse the JSON string.
Code: Select all
//+------------------------------------------------------------------+
//| TV_Webhook_Receiver.mq4|
//+------------------------------------------------------------------+
#property strict
#include <JAson.mqh> // Download from MQL4/MQL5 CodeBase
string server_url = "http://localhost:5000/get_latest_alert";
string last_processed_signal = "";
int magic_number = 777777; // Unique ID to manage only TV webhook trades
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Enable polling every 1 second
EventSetTimer(1);
Print("EA Initialized. Listening for TradingView signals...");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
string headers = "";
string result_headers = "";
char post[], result[];
// MQL4 uses a slightly different WebRequest parameter structure than MT5
int res = WebRequest("GET", server_url, headers, 500, post, result, result_headers);
if(res == 200)
{
string json_string = CharArrayToString(result);
// Prevent duplicate execution of the same signal
if(json_string != last_processed_signal && json_string != "")
{
ParseAndExecute(json_string);
last_processed_signal = json_string;
}
}
}
//+------------------------------------------------------------------+
//| Parse JSON and Execute Trade |
//+------------------------------------------------------------------+
void ParseAndExecute(string json_payload)
{
CJAVal json;
// Deserialize the JSON string sent by TradingView
if(json.Deserialize(json_payload))
{
string action = json["action"].ToStr();
string symbol = json["symbol"].ToStr();
// Fetch current broker pricing and digit structure
double ask = MarketInfo(symbol, MODE_ASK);
double bid = MarketInfo(symbol, MODE_BID);
int digits = (int)MarketInfo(symbol, MODE_DIGITS);
// Placeholder for Rule 5 position sizing
double lot_size = 0.1;
if(action == "long")
{
// MT4 requires strict price normalization before execution
double sl = NormalizeDouble(json["sl"].ToDbl(), digits);
double tp = NormalizeDouble(json["tp"].ToDbl(), digits);
int ticket = OrderSend(symbol, OP_BUY, lot_size, ask, 3, sl, tp, "TV Long Sweep", magic_number, 0, clrGreen);
if(ticket < 0) Print("OrderSend Error: ", GetLastError());
else Print("Executed Long on ", symbol);
}
else if(action == "short")
{
double sl = NormalizeDouble(json["sl"].ToDbl(), digits);
double tp = NormalizeDouble(json["tp"].ToDbl(), digits);
int ticket = OrderSend(symbol, OP_SELL, lot_size, bid, 3, sl, tp, "TV Short Sweep", magic_number, 0, clrRed);
if(ticket < 0) Print("OrderSend Error: ", GetLastError());
else Print("Executed Short on ", symbol);
}
else if(action == "exit_long" || action == "exit_short")
{
// Iterate backwards through the order pool to safely close positions
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == symbol && OrderMagicNumber() == magic_number)
{
// Determine correct closing price based on order direction
double close_price = (OrderType() == OP_BUY) ? MarketInfo(symbol, MODE_BID) : MarketInfo(symbol, MODE_ASK);
bool closed = OrderClose(OrderTicket(), OrderLots(), close_price, 3, clrGray);
if(!closed) Print("OrderClose Error: ", GetLastError());
else Print("Closed position on ", symbol, " due to TV exit signal.");
}
}
}
}
}
}
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.