This script is a utility designed to automate the management of your open trading positions based on a specific point on the price chart. It allows you to quickly set "safety nets" or "target goals" by interacting directly with the visual interface of your trading platform.
How it Works
The logic follows a simple, three-step process once activated:
1. Identifying your Target Price The script begins by detecting exactly where you clicked on your trading chart. This click point serves as the "Reference Price." Instead of manually typing in numbers for every trade, the tool uses this visual click as the master value to apply to your active orders.
2. Scanning Active Trades Once the reference price is set, the script automatically scans through all your currently open trades. It looks at every position you have running at that moment—whether you are buying or selling—to determine how to apply the new price information.
3. Smart Order Modification The script then applies the reference price to your trades based on the "direction" of your trade. It follows these specific rules:
-
For Buy Positions:
- If the price you clicked is higher than the current market price, the tool assumes you want to set a Take Profit (your goal).
- If the price you clicked is lower than the current market price, the tool assumes you want to set a Stop Loss (your protection).
-
For Sell Positions:
- If the price you clicked is lower than the current market price, the tool assumes you want to set a Take Profit.
- If the price you clicked is higher than the current market price, the tool assumes you want to set a Stop Loss.
Summary of Behavior
In essence, this script acts as a "one-click" management tool. By clicking a specific spot on the chart, you can instantly update all your open positions so that their exit points align with that visual coordinate. It automatically distinguishes between protection (Stop Loss) and profit-taking (Take Profit) by comparing your click location to the current live market price.
//+------------------------------------------------------------------+
//| StopAndTake.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Melnichenko D.A."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#include <stderror.mqh>
#include <stdlib.mqh>
double price=0;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
price=ChartPriceOnDropped();
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
while(IsTradeContextBusy()){Sleep(1000);}
ModifyOrder();
}
}
//+------------------------------------------------------------------+
void ModifyOrder()
{
switch(OrderType())
{
case OP_BUY:
{
if(price > Bid)
if(!OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), price, 0))
MessageBox("Order change error", "Warning", MB_OK);
if(price < Bid)
if(!OrderModify(OrderTicket(), OrderOpenPrice(), price, OrderTakeProfit(), 0))
MessageBox("Order change error", "Warning", MB_OK);
break;
}
case OP_SELL:
{
if(price < Ask)
if(!OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), price, 0))
MessageBox("Order change error", "Warning", MB_OK);
if(price > Ask)
if(!OrderModify(OrderTicket(), OrderOpenPrice(), price, OrderTakeProfit(), 0))
MessageBox("Order change error", "Warning", MB_OK);
break;
}
}
}
//+------------------------------------------------------------------+
Comments