Skip to content

Instantly share code, notes, and snippets.

@cnra
Last active August 6, 2019 22:00
Show Gist options
  • Save cnra/4ae9edc53e90e19fa8537b45f62f9188 to your computer and use it in GitHub Desktop.
Save cnra/4ae9edc53e90e19fa8537b45f62f9188 to your computer and use it in GitHub Desktop.
metatrader 5 take profit / stop loss
input int SL = 150; //15 pip
void OnTradeTransaction(const MqlTradeTransaction &txs, const MqlTradeRequest &req, const MqlTradeResult &res)
{
MqlTradeRequest rq={0};
MqlTradeResult tr;
if (HistoryDealGetInteger(txs.deal, DEAL_ENTRY) == DEAL_ENTRY_IN)
{
PositionSelect(txs.symbol);
rq.position = txs.order;
rq.action = TRADE_ACTION_SLTP;
rq.symbol = PositionGetString(POSITION_SYMBOL);
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) {
rq.sl = PositionGetDouble(POSITION_PRICE_OPEN) - SL * SymbolInfoDouble(txs.symbol, SYMBOL_POINT);
rq.tp = PositionGetDouble(POSITION_PRICE_OPEN) + SL * SymbolInfoDouble(txs.symbol, SYMBOL_POINT);
}
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) {
rq.sl = PositionGetDouble(POSITION_PRICE_OPEN) + SL * SymbolInfoDouble(txs.symbol, SYMBOL_POINT);
rq.tp = PositionGetDouble(POSITION_PRICE_OPEN) - SL * SymbolInfoDouble(txs.symbol, SYMBOL_POINT);
}
OrderSend(rq, tr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment