This script is designed for the MetaTrader platform and automatically manages Stop Loss and Take Profit levels for open trading positions. Here's how it works:
The script continuously scans all open orders. It focuses on orders that meet certain criteria: the order must have a valid ticket number, a "magic number" of zero (likely meaning it wasn't placed by another automated system), and no pre-set Stop Loss or Take Profit levels. It also checks that the order hasn't been open longer than a certain time.
If an order matches those criteria, the script then calculates appropriate Stop Loss and Take Profit levels based on a few user-defined settings. The user can specify whether the script should apply to all currency pairs or just the one the script is running on. They can also set the desired Stop Loss and Take Profit distances, which are measured in points (a unit of price movement).
There's also a check to ensure that the calculated Stop Loss and Take Profit distances are not smaller than the minimum allowed distance by the broker. If the desired distances are too small, the script will automatically use the minimum allowed distance instead.
Based on whether the order is a "buy" or a "sell" order, the script calculates the specific price levels for the Stop Loss and Take Profit. For "buy" orders, the Stop Loss will be set below the order's opening price, and the Take Profit will be set above it. For "sell" orders, the opposite is true: the Stop Loss will be set above the opening price, and the Take Profit will be set below.
Finally, the script sends a command to the trading platform to modify the existing order, adding the calculated Stop Loss and Take Profit levels. The Stop Loss and Take Profit lines will appear in blue or red for buy or sell orders.
In essence, this script is an automated tool to quickly apply Stop Loss and Take Profit settings to trades that don't already have them, potentially helping to manage risk and secure profits without manual intervention.
//+------------------------------------------------------------------+
//| AutoSLTP.mq4 |
//| Copyright 2015, MetaQuotes Software Corp. |
//| http://free-bonus-deposit.blogspot.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, dXerof"
#property link "http://free-bonus-deposit.blogspot.com"
#property version "1.00"
input bool AllPairs=True;
input double TakeProfit=400; //40-->4 digits; 400-->5 digits
input double StopLoss=150; //15-->4 digits; 150-->5 digits
input double DurasiTime=60;
//---
int ticket;
double poen;
string pair="";
double iTakeProfit,iStopLoss;
double slbuy;
double tpbuy;
double slsell;
double tpsell;
double stoplevel;
double digi;
double poin;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
for(int cnt=0; cnt<OrdersTotal(); cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
if(OrderTicket()>0 && OrderMagicNumber()==0 && OrderTakeProfit()==0 && TimeCurrent()-OrderOpenTime()<=DurasiTime)
{
if(AllPairs) pair=OrderSymbol(); else pair=Symbol();
//---
stoplevel=MarketInfo(pair,MODE_STOPLEVEL);
digi=MarketInfo(pair,MODE_DIGITS);
poin=MarketInfo(pair,MODE_POINT);
iStopLoss=StopLoss;
iTakeProfit=TakeProfit;
if(StopLoss<stoplevel) iStopLoss=stoplevel;
if(TakeProfit<stoplevel) iTakeProfit=stoplevel;
//---
slbuy=NormalizeDouble(OrderOpenPrice()-iStopLoss*poin,digi);
tpbuy=NormalizeDouble(OrderOpenPrice()+iTakeProfit*poin,digi);
slsell=NormalizeDouble(OrderOpenPrice()+iStopLoss*poin,digi);
tpsell=NormalizeDouble(OrderOpenPrice()-iTakeProfit*poin,digi);
//---
if(OrderSymbol()==pair && (OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP))
{
ticket=OrderModify(OrderTicket(),OrderOpenPrice(),slbuy,tpbuy,0,Blue);
}
if(OrderSymbol()==pair && (OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP))
{
ticket=OrderModify(OrderTicket(),OrderOpenPrice(),slsell,tpsell,0,Red);
}
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments