Skip to content

Instantly share code, notes, and snippets.

@hoangbits
Last active March 23, 2023 09:11
Show Gist options
  • Save hoangbits/c8c59b2445ab127417e159189625a4ea to your computer and use it in GitHub Desktop.
Save hoangbits/c8c59b2445ab127417e159189625a4ea to your computer and use it in GitHub Desktop.
// write mql5 code to set SL to entry price of all current position when current price of XAUUSD move more than 20pips.
void OnTick()
{
double XAUUSD_Price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
Print("XAUUSD_Price: ", XAUUSD_Price);
double XAUUSD_Pips = SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
Print("XAUUSD_Pips: ", XAUUSD_Pips);
double XAUUSD_PipsToSL = 20 * Point();
Print("XAUUSD_PipsToSL: ", XAUUSD_PipsToSL);
double XAUUSD_SL = 0;
int TotalPositions = PositionsTotal();
Print("TotalPositions: ", TotalPositions);
for (int i = 0; i < TotalPositions; i++)
{
ulong Ticket = PositionGetTicket(i);
if (Ticket > 0)
{
ENUM_POSITION_TYPE Type = PositionGetInteger(POSITION_TYPE);
if (Type == POSITION_TYPE_BUY || Type == POSITION_TYPE_SELL)
{
double EntryPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double StopLoss = EntryPrice - XAUUSD_PipsToSL;
if (Type == POSITION_TYPE_BUY && XAUUSD_Price - EntryPrice > XAUUSD_PipsToSL / XAUUSD_Pips)
{
StopLoss = EntryPrice;
}
else if (Type == POSITION_TYPE_SELL && EntryPrice - XAUUSD_Price > XAUUSD_PipsToSL / XAUUSD_Pips)
{
StopLoss = EntryPrice;
}
if (StopLoss != XAUUSD_SL)
{
bool Result = PositionModify(Ticket, StopLoss, PositionGetDouble(POSITION_TP));
if (Result)
{
Print("Position ", Ticket, " SL set to ", StopLoss);
}
else
{
Print("Position ", Ticket, " SL not set");
}
}
}
}
}
}
bool ModifySL(ulong ticket,double new_sl)
{
MqlTradeRequest request={0};
MqlTradeResult result={0};
request.action=TRADE_ACTION_SLTP;
request.type=ORDER_TYPE_SELL;
request.position=PositionGetInteger(POSITION_IDENTIFIER);
request.symbol=_Symbol;
request.sl=new_sl;
request.magic=123456;
request.ticket=ticket;
bool res=OrderSend(request,result);
return(res);
}
Here is an example of how to use the OrderSend() function to modify an active trade’s SL in MQL5:
bool ModifySL(ulong ticket,double new_sl)
{
MqlTradeRequest request={0};
MqlTradeResult result={0};
request.action=TRADE_ACTION_SLTP;
request.type=ORDER_TYPE_SELL;
request.position=PositionGetInteger(POSITION_IDENTIFIER);
request.symbol=_Symbol;
request.sl=new_sl;
request.magic=123456;
request.ticket=ticket;
bool res=OrderSend(request,result);
return(res);
}
// SL & TP Modification
// ref https://www.mql5.com/en/docs/constants/structures/mqltraderequest
// Trade order to modify the StopLoss and/or TakeProfit price levels. It requires to specify the following 4 fields:
// action
// symbol
// sl
// tp
// position
MqlTradeRequest request;
MqlTradeResult result;
ulong position_ticket=PositionGetTicket(i);// ticket of the position
string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol
// TODO
// double sl =
double tp = PositionGetDouble(POSITION_TP)
request.action =TRADE_ACTION_SLTP; // type of trade operation
request.action=position_symbol; // symbol
request.symbol=position_symbol; // symbol
request.sl =sl; // Stop Loss of the position
request.tp =tp;
request.position=position_ticket; // ticket of the position
//--- zeroing the request and result values
ZeroMemory(request);
ZeroMemory(result);
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code
//--- information about the operation
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment