This EA trading tool is meant to open trades at intervals. My plan is that when I analyze and set up my trades with a target profit (TP) and stop loss (SL), the tool becomes operational with that trade and it should split my TP points by a certain factor and open trades at each successive levels accordingly. But right now, it's opening all trades at the first instance instead of progressively, initiating the entire sequence right from the start. I'm thinking about separating the code for different levels since I suspect the issue lies in the FOR statement, although I haven't pinpointed any errors there yet. below is the code
MQL5:
//+------------------------------------------------------------------+ //| Positive Martingale Levels.mq5 | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property version "1.00" #include <Trade\Trade.mqh> #include <Trade\PositionInfo.mqh> #include <Trade\SymbolInfo.mqh> CTrade trading; CPositionInfo positioning; CSymbolInfo symbolling; //***************************Martingale mode**************************// input bool MartingaleMode = true; input string Symbol_Name = "EURUSD"; // Name Of The Symbol input double MartingaleVolumeMultiplier = 2; // Martingale Volume Multiplier input int MartingaleNum = 4; // Number of Martingale Trading (MAX 5 Times) int BCount = 0; int SCount = 0; int BLevel = 0; // Variable to track the level of buy trades int SLevel = 0; // Variable to track the level of sell trades double MartingaleDis; double BMartingaleVol; double SMartingaleVol; double VOL; // Global variables to store initial SL and TP in points double InitialStopLossPoints = 0; double InitialTakeProfitPoints = 0; double InitialStopLossPrice = 0; double InitialTakeProfitPrice = 0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Initialization code here, if needed return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Deinitialization code here, if needed } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if (MartingaleMode) { InitialTradeBeforeMartingaleStarts(); MartingaleTrading(); MartingaleClosePos(); } } //+------------------------------------------------------------------+ //| Function to retrieve the SL and TP of the initial trade | //+------------------------------------------------------------------+ void InitialTradeBeforeMartingaleStarts() { for (int i = PositionsTotal() - 1; i >= 0; i--) { if (positioning.SelectByIndex(i)) { if (positioning.Symbol() == Symbol_Name) { double stopLoss = positioning.StopLoss(); double takeProfit = positioning.TakeProfit(); double openPrice = positioning.PriceOpen(); double point = SymbolInfoDouble(Symbol_Name, SYMBOL_POINT); double VOL = positioning.Volume(); if (positioning.PositionType() == POSITION_TYPE_BUY) { InitialStopLossPoints = (openPrice - stopLoss) / point; InitialTakeProfitPoints = (takeProfit - openPrice) / point; InitialStopLossPrice = stopLoss; InitialTakeProfitPrice = takeProfit; } else if (positioning.PositionType() == POSITION_TYPE_SELL) { InitialStopLossPoints = (stopLoss - openPrice) / point; InitialTakeProfitPoints = (openPrice - takeProfit) / point; InitialStopLossPrice = stopLoss; InitialTakeProfitPrice = takeProfit; } // Calculate Martingale distance MartingaleDis = InitialTakeProfitPoints / MartingaleNum; BMartingaleVol = VOL; SMartingaleVol = VOL; break; } } } } //+------------------------------------------------------------------+ //| Function to handle Martingale trading | //+------------------------------------------------------------------+ void MartingaleTrading() { double point = SymbolInfoDouble(Symbol_Name, SYMBOL_POINT); for (int i = PositionsTotal() - 1; i >= 0; i--) { if (positioning.SelectByIndex(i)) { if (positioning.Symbol() == Symbol_Name) { if (positioning.PositionType() == POSITION_TYPE_BUY && positioning.Profit() > 0) { HandleBuyMartingale(positioning.PriceOpen(), positioning.PriceCurrent(), point); } else if (positioning.PositionType() == POSITION_TYPE_SELL && positioning.Profit() > 0) { HandleSellMartingale(positioning.PriceOpen(), positioning.PriceCurrent(), point); } } } } } //+------------------------------------------------------------------+ //| Function to handle Buy Martingale trades | //+------------------------------------------------------------------+ void HandleBuyMartingale(double openPrice, double currentPrice, double point) { for (int level = 1; level <= MartingaleNum; level++) { double stopLoss = positioning.StopLoss(); double takeProfit = positioning.TakeProfit(); if (BCount < level && MartingaleDis * level >= (currentPrice - openPrice) / point) { BMartingaleVol = NormalizeDouble(BMartingaleVol * MartingaleVolumeMultiplier, 2); trading.Buy(BMartingaleVol, Symbol_Name, SymbolInfoDouble(Symbol_Name, SYMBOL_ASK), stopLoss, 0, ""); BCount++; } } } //+------------------------------------------------------------------+ //| Function to handle Sell Martingale trades | //+------------------------------------------------------------------+ void HandleSellMartingale(double openPrice, double currentPrice, double point) { for (int level = 1; level <= MartingaleNum; level++) { double stopLoss = positioning.StopLoss(); double takeProfit = positioning.TakeProfit(); if (SCount < level && MartingaleDis * level >= (openPrice - currentPrice) / point) { SMartingaleVol = NormalizeDouble(SMartingaleVol * MartingaleVolumeMultiplier, 2); trading.Sell(SMartingaleVol, Symbol_Name, SymbolInfoDouble(Symbol_Name, SYMBOL_BID), stopLoss, 0, ""); SCount++; } } } //+------------------------------------------------------------------+ //| Function to close Martingale positions when reaching profit/loss | //+------------------------------------------------------------------+ void MartingaleClosePos() { double BTotalProfit = 0; double STotalProfit = 0; double currentBidPrice = SymbolInfoDouble(Symbol_Name, SYMBOL_BID); // Current bid price for checking stop loss and take profit breach double currentAskPrice = SymbolInfoDouble(Symbol_Name, SYMBOL_ASK); // Current ask price for checking stop loss and take profit breach // Check if the current price has moved outside the initial stop loss or take profit if (positioning.Symbol() == Symbol_Name && currentAskPrice >= positioning.TakeProfit() && positioning.PositionType() == POSITION_TYPE_BUY) { CloseAllPositions(POSITION_TYPE_BUY); } else if (positioning.Symbol() == Symbol_Name && currentBidPrice <= positioning.TakeProfit() && positioning.PositionType() == POSITION_TYPE_SELL) { CloseAllPositions(POSITION_TYPE_SELL); } } //+------------------------------------------------------------------+ //| Function to close all positions of a specified type | //+------------------------------------------------------------------+ void CloseAllPositions(int positionType) { for (int i = PositionsTotal() - 1; i >= 0; i--) { if (positioning.SelectByIndex(i)) { if (positioning.Symbol() == Symbol_Name && positioning.PositionType() == positionType) { trading.PositionClose(positioning.Ticket()); } } } }