Trap News MT5
Download scriptThis script is designed for MetaTrader 5 to automate the placement of "Pending Orders"—specifically Buy Stop and Sell Stop orders. These types of orders are commonly used by traders who want to enter a position only when the market moves past a specific price level, such as during high-volatility events like news releases.
Core Functionality
The script acts as an automated assistant that calculates and places two opposite trade triggers simultaneously based on parameters you provide. Instead of opening a trade immediately at the current market price, it sets "traps" at specific distances above and below your current position.
How the Logic Works:
1. User Input Configuration Before the script runs, it allows you to define several key variables:
- Distance: How many points away from the current price the Buy and Sell orders should be placed.
- Volume: The size of the trade (Lot Size).
- Risk Management: Specific targets for your Stop Loss (to limit losses) and Take Profit (to secure gains) for both sides of the trade.
- Identification: A unique "Magic Number" used to identify these specific trades so they don't get mixed up with other manual trades.
2. Market Validation Before placing any orders, the script performs a safety check. It looks at the current live market price (Ask and Bid).
- It ensures that your Buy Stop target is actually above the current price.
- It ensures that your Sell Stop target is actually below the current price. If these conditions aren't met, the script will print an error message rather than placing incorrect orders.
3. Order Execution Once the validation is passed, the script automatically calculates the exact coordinates for four different price points:
- Buy Side: It calculates the entry price (Current Price + your defined distance), the Stop Loss (Current Price - your loss limit), and the Take Profit (Current Price + your profit goal).
- Sell Side: It calculates the entry price (Current Price - your defined distance), the Stop Loss (Current Price + your loss limit), and the Take Profit (Current Price - your profit goal).
4. Automated Placement The script then sends these instructions to the broker's server. Once successful, it provides a confirmation in the log showing that both the Buy and Sell orders have been placed with their respective ticket numbers.
Summary for the User
In simple terms, this tool allows you to "set and forget" your trading strategy for a specific price move. By running this script, you are automatically placing a bracket of orders that wait for the market to break in either direction, ensuring your entries, safety nets, and profit targets are perfectly aligned with your plan the moment you initiate the script.
//+------------------------------------------------------------------+
//| Trap News MT5.mq5 |
//| rezarahmad 2024 |
//| rezarahmad@gmail.com |
//+------------------------------------------------------------------+
#property copyright "rezarahmad 2024"
#property link "rezarahmad@gmail.com"
#property version "1.00"
#property script_show_inputs
// Input parameters for the orders
input double BuyStopPrice = 50; // Price at which to place the Buy Stop order
input double SellStopPrice = 50; // Price at which to place the Sell Stop order
input double LotSize = 0.01; // Size of the orders
input double BuyStopLoss = 25; // Stop Loss level for Buy Stop order
input double BuyTakeProfit = 100; // Take Profit level for Buy Stop order
input double SellStopLoss = 25; // Stop Loss level for Sell Stop order
input double SellTakeProfit = 100; // Take Profit level for Sell Stop order
input double MagicNumber = 1234;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
void OnStart()
{
string symbol = Symbol();
// Get current Ask and Bid prices
double Ask = SymbolInfoDouble(symbol, SYMBOL_ASK);
double Bid = SymbolInfoDouble(symbol, SYMBOL_BID);
double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);
// Define the symbol
// Check if the Buy Stop price is above the current Ask price
if(BuyStopPrice <= Ask)
{
Print("Error: Buy Stop price must be above the current market price.");
}
// Check if the Sell Stop price is below the current Bid price
if(SellStopPrice >= Bid)
{
Print("Error: Sell Stop price must be below the current market price.");
}
// Define the deviation for order placement
int deviation = 10; // The maximum price deviation in points
// Place the Buy Stop order
MqlTradeRequest buy_request;
MqlTradeResult buy_result;
ZeroMemory(buy_request);
ZeroMemory(buy_result);
buy_request.action = TRADE_ACTION_PENDING;
buy_request.symbol = symbol;
buy_request.volume = LotSize;
buy_request.type = ORDER_TYPE_BUY_STOP;
buy_request.price = Bid + (BuyStopPrice*point);
buy_request.sl = Bid - (BuyStopLoss*point);
buy_request.tp = Bid + (BuyTakeProfit*point);
buy_request.deviation = deviation;
buy_request.magic = MagicNumber;
buy_request.comment = "Buy Stop Order";
// Send the trade request for Buy Stop
if(!OrderSend(buy_request, buy_result))
{
Print("Error placing Buy Stop order: ", buy_result.retcode);
}
else
{
Print("Buy Stop order placed successfully. Ticket: ", buy_result.order);
}
// Place the Sell Stop order
MqlTradeRequest sell_request;
MqlTradeResult sell_result;
ZeroMemory(sell_request);
ZeroMemory(sell_result);
sell_request.action = TRADE_ACTION_PENDING;
sell_request.symbol = symbol;
sell_request.volume = LotSize;
sell_request.type = ORDER_TYPE_SELL_STOP;
sell_request.price = Ask - (SellStopPrice*point) ;
sell_request.sl = Ask + (SellStopLoss*point) ;
sell_request.tp = Ask - (SellTakeProfit*point);
sell_request.deviation = deviation;
sell_request.magic = MagicNumber;
sell_request.comment = "Sell Stop Order";
// Send the trade request for Sell Stop
if(!OrderSend(sell_request, sell_result))
{
Print("Error placing Sell Stop order: ", sell_result.retcode);
}
else
{
Print("Sell Stop order placed successfully. Ticket: ", sell_result.order);
}
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
Comments