Created
May 25, 2023 22:36
-
-
Save MrSimonC/1801a07a6093a1692d456e56553bfc1c to your computer and use it in GitHub Desktop.
Update to training stop mql5 file to make it work
This file contains 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
// Include necessary files | |
#include <Trade\Trade.mqh> | |
#include <Trade\SymbolInfo.mqh> | |
// Input parameters | |
input double RiskPercentage = 1.0; // Risk percentage per trade | |
input double ProfitPercentage = 2.0; // Profit percentage per trade | |
input double TrailingStopPoints = 50; // Trailing stop points | |
// Declare global variables | |
CTrade trade; | |
CSymbolInfo symbolInfo; | |
// OnInit function | |
int OnInit() | |
{ | |
if (!symbolInfo.Name(Symbol())) | |
{ | |
Print("Error: SymbolInfo not found"); | |
return INIT_FAILED; | |
} | |
if (!symbolInfo.IsSynchronized()) | |
{ | |
Print("Error: Symbol is not synchronized!"); | |
return INIT_FAILED; | |
} | |
if (!SymbolSelect(_Symbol, true)) | |
{ | |
Print("Failed to select symbol!"); | |
return INIT_FAILED; | |
} | |
return INIT_SUCCEEDED; | |
} | |
// OnDeinit function | |
void OnDeinit(const int reason) | |
{ | |
} | |
// OnTick function | |
void OnTick() | |
{ | |
MqlTick tick; | |
if (SymbolInfoTick(_Symbol, tick)) | |
{ | |
Print("Bid =", tick.bid, "Ask =", tick.ask); | |
} | |
else | |
{ | |
Print("No ticks for symbol!"); | |
} | |
CheckTrades(tick); | |
} | |
void CheckTrades(MqlTick &tick) | |
{ | |
double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE); | |
double riskAmount = accountBalance * RiskPercentage / 100.0; | |
double profitAmount = accountBalance * ProfitPercentage / 100.0; | |
for (int i = PositionsTotal() - 1; i >= 0; i--) | |
{ | |
Print("One position found."); | |
ulong ticket = PositionGetTicket(i); | |
if (ticket > 0) | |
{ | |
Print("One ticket found."); | |
string orderSymbol = PositionGetString(POSITION_SYMBOL); | |
if (Symbol() == orderSymbol) | |
{ | |
double orderOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN); | |
double orderStopLoss = PositionGetDouble(POSITION_SL); | |
double orderTakeProfit = PositionGetDouble(POSITION_TP); | |
ENUM_POSITION_TYPE orderType = ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE)); | |
if (orderType == POSITION_TYPE_BUY) | |
{ | |
Print("One buy position found."); | |
double currentProfit = (tick.ask - orderOpenPrice) * symbolInfo.Point(); | |
if (currentProfit >= profitAmount || currentProfit <= -riskAmount) | |
{ | |
Print("Closing position", ticket, "with profit", currentProfit); | |
trade.PositionClose(ticket); | |
} | |
else | |
{ | |
Print("in else statement"); | |
// Update stop loss with a trailing stop | |
double newStopLoss = tick.bid - TrailingStopPoints * symbolInfo.Point(); | |
Print("Bid: ", tick.bid); | |
Print("TrailingStopPoints: ", TrailingStopPoints); | |
Print("symbolInfo.Point(): ", symbolInfo.Point()); | |
Print("newStopLoss: ", newStopLoss); | |
Print("orderStopLoss: ", orderStopLoss); | |
Print("orderOpenPrice: ", orderOpenPrice); | |
if (newStopLoss > orderStopLoss && newStopLoss < orderOpenPrice) | |
{ | |
Print("in if statement"); | |
Print("Updating stop loss for position", ticket, "to", newStopLoss); | |
trade.PositionModify(ticket, newStopLoss, orderTakeProfit); | |
} | |
Print("out of if statement"); | |
} | |
} | |
else if (orderType == POSITION_TYPE_SELL) | |
{ | |
double currentProfit = (orderOpenPrice - tick.bid) * symbolInfo.Point(); | |
if (currentProfit >= profitAmount || currentProfit <= -riskAmount) | |
{ | |
Print("Closing position", ticket, "with profit", currentProfit); | |
trade.PositionClose(ticket); | |
} | |
else | |
{ | |
// Update stop loss with a trailing stop | |
double newStopLoss = tick.ask + TrailingStopPoints * symbolInfo.Point(); | |
if (newStopLoss < orderStopLoss && newStopLoss > orderOpenPrice) | |
{ | |
Print("Updating stop loss for position", ticket, "to", newStopLoss); | |
trade.PositionModify(ticket, newStopLoss, orderTakeProfit); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment