Last active
October 30, 2023 15:23
-
-
Save iaminamcom/d12db266f494c98bff7b636482c9763e to your computer and use it in GitHub Desktop.
Auto Stoploss and profit for MT5 - Good For Scalping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+--------------------------------------------------------------------------------------------+ | |
//| AutoStopLoss.mq5 | | |
//| Copyright 2023, iaminam.com | | |
//| ` https:/iaminam.com | | |
//| Made following https://orchardforex.com/mt5-expert-to-apply-an-automatic-stop-loss/ | | |
//+----------------------------------------------------+---------------------------------------+ | |
#property copyright "Copyright 2023, iaminam.com" | |
#property link "https:/iaminam.com" | |
#property version "1.00" | |
#property strict | |
#include <Trade/Trade.mqh> | |
CTrade Trade; | |
input int InpStopLossPoints = 1000; // Stop loss in points | |
input int InpProfitPoints = 1500; // Profit in points | |
int OnInit(){return(INIT_SUCCEEDED);} | |
void OnDeinit(const int reason) {} | |
void OnTick() { | |
datetime checkTime = TimeCurrent()-30; // Only looking at trades in last 30 seconds | |
int cnt = PositionsTotal(); // This won't see limit and stop orders | |
for (int i=cnt-1; i>=0; i--) { | |
ulong ticket = PositionGetTicket(i); | |
if (ticket>0) { | |
if (PositionGetInteger(POSITION_MAGIC)==0 && PositionGetDouble(POSITION_SL)==0) { | |
// magic 0 = manual entry, sl 0 means not set | |
if (PositionGetInteger(POSITION_TIME)>checkTime) { // lets you override after 30 seconds | |
string symbol = PositionGetString(POSITION_SYMBOL); | |
double stopLoss = InpStopLossPoints*SymbolInfoDouble(symbol, SYMBOL_POINT); | |
double stopLossPrice = (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) ? | |
PositionGetDouble(POSITION_PRICE_OPEN)-stopLoss : | |
PositionGetDouble(POSITION_PRICE_OPEN)+stopLoss; | |
stopLossPrice = NormalizeDouble(stopLossPrice, (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS)); | |
Trade.PositionModify(ticket, stopLossPrice, PositionGetDouble(POSITION_TP)); | |
} | |
} | |
if (PositionGetInteger(POSITION_MAGIC)==0 && PositionGetDouble(POSITION_TP)==0) { | |
// magic 0 = manual entry, tp 0 means not set | |
if (PositionGetInteger(POSITION_TIME)>checkTime) { // lets you override after 30 seconds | |
string symbol = PositionGetString(POSITION_SYMBOL); | |
double profit = InpProfitPoints*SymbolInfoDouble(symbol, SYMBOL_POINT); | |
double profitPrice = (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) ? | |
PositionGetDouble(POSITION_PRICE_OPEN)+profit : | |
PositionGetDouble(POSITION_PRICE_OPEN)-profit; | |
profitPrice = NormalizeDouble(profitPrice, (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS)); | |
Trade.PositionModify(ticket, PositionGetDouble(POSITION_SL), profitPrice); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment