// Define a global variable to store the previous number of bars int prevbars = 0; //…
- User
// Define a global variable to store the previous number of bars int prev_bars = 0;
// Define a global variable to store the last bar for which a trade was opened int last_trade_bar = -1;
// Define an input variable for the user to set the wick size filter percentage input double wick_size_filter = 20; // The percentage of the candle size that the wick size should not exceed input double risk_percentage = 1; // How much to risk on the balance based on this percentage input double TP_X = 2; // The default value is 2, meaning the TP will be twice the size of the stop loss
double SLP = 0; double Risk = 0; // New variable to store the calculated risk double LotSize = 0; // New variable to store the calculated lot size double TP = 0; // New variable to store the calculated TP value
// Define the function OnTick void OnTick() { // Print a message to indicate that the OnTick function is being called
// Get the current number of bars int curr_bars = Bars(_Symbol, _Period);
// Check if a new bar has been formed if (curr_bars != prev_bars) { // Update the previous number of bars prev_bars = curr_bars;
// Check for FU candles and print a message // Define the candle variables double open = iOpen(_Symbol, _Period, 1); // Current candle open price double close = iClose(_Symbol, _Period, 1); // Current candle close price double high = iHigh(_Symbol, _Period, 1); // Current candle high price double low = iLow(_Symbol, _Period, 1); // Current candle low price double prev_open = iOpen(_Symbol, _Period, 2); // Previous candle open price double prev_close = iClose(_Symbol, _Period, 2); // Previous candle close price double prev_high = iHigh(_Symbol, _Period, 2); // Previous candle high price double prev_low = iLow(_Symbol, _Period, 2); // Previous candle low price // Calculate the candle size and the wick sizes double candle_size = high - low; // The distance between the high and the low of the current candle double upside_wick_size = high - MathMax(open, close); // The distance between the high and the maximum of the open and close of the current candle double downside_wick_size = MathMin(open, close) - low; // The distance between the low and the minimum of the open and close of the current candle double min_lot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN); double max_lot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX); double lot_step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP); // Check for bullish FU candle with small upside wick if (close > open && low < prev_low && close > prev_close && high > prev_high && close > prev_high && upside_wick_size / candle_size < wick_size_filter / 100) { Print("Bullish FU found"); last_trade_bar = curr_bars; double current_price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Get the current ask price double cml = NormalizeDouble((current_price - low), 5); Risk = AccountInfoDouble(ACCOUNT_BALANCE) * risk_percentage / 100; // Calculate Risk value here. Print("Risk: ", Risk); SLP = NormalizeDouble((current_price - low) * 10000, 1); Print("SLP: ", SLP); LotSize = NormalizeDouble(Risk / (10 * SLP), 2); // Calculate LotSize value here and round it to 2 decimal places. LotSize = MathMax(LotSize, min_lot); // Make sure LotSize is not less than min_lot LotSize = MathMin(LotSize, max_lot); // Make sure LotSize is not greater than max_lot LotSize = MathRound(LotSize / lot_step) * lot_step; // Make sure LotSize is a multiple of lot_step Print("LotSize: ", LotSize); double SL = low; Print("SL: ", SL); double entry = current_price; Print("Entry: ", entry); TP = NormalizeDouble(current_price + cml * TP_X, 5); Print("TP: ", TP); // Open a buy order MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); request.action = TRADE_ACTION_DEAL; request.symbol = _Symbol; request.volume = LotSize; request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); request.sl = SL - 5 * _Point; // Set Stop Loss level without rounding request.tp = TP; request.deviation = 20; request.type = ORDER_TYPE_BUY; request.magic = 123456L; // Magic number int attempts = 0; // Add this line to keep track of the number of attempts while (attempts < 10) // Add this line to limit the number of attempts { bool sent = OrderSend(request, result); // Add this line to store the return value of OrderSend if (sent) // Add this line to check the return value { if (result.retcode == TRADE_RETCODE_DONE) { Print("BUY order opened : ", result.order); break; // Add this line to exit the loop if the trade request was successful } else if (result.retcode == TRADE_RETCODE_REQUOTE) { Print("Requote received, resending trade request"); attempts++; // Add this line to increment the number of attempts } else { Print("BUY order failed with error: ", GetLastError()); break; // Add this line to exit the loop if an error occurred } } else // Add this else block to handle the case when OrderSend fails { Print("Failed to send trade request with error: ", GetLastError()); break; // Add this line to exit the loop if an error occurred } } } // Check for bearish FU candle with small downside wick if (close < open && high > prev_high && close < prev_close && low < prev_low && close < prev_low && downside_wick_size / candle_size < wick_size_filter / 100) { Print("Bearish FU found"); last_trade_bar = curr_bars; double current_price = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Get the current ask price double cml = NormalizeDouble((high - current_price), 5); Risk = AccountInfoDouble(ACCOUNT_BALANCE) * risk_percentage / 100; // Calculate Risk value here. Print("Risk: ", Risk); SLP = NormalizeDouble((high - current_price) * 10000, 1); Print("SLP: ", SLP); LotSize = NormalizeDouble(Risk / (10 * SLP), 2); // Calculate LotSize value here and round it to 2 decimal places. LotSize = MathMax(LotSize, min_lot); // Make sure LotSize is not less than min_lot LotSize = MathMin(LotSize, max_lot); // Make sure LotSize is not greater than max_lot LotSize = MathRound(LotSize / lot_step) * lot_step; // Make sure LotSize is a multiple of lot_step Print("LotSize: ", LotSize); double SL = high; Print("SL: ", SL); double entry = current_price; Print("Entry: ", entry); TP = NormalizeDouble(current_price - cml * TP_X, 5); Print("TP: ", TP); // Open a sell order MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); request.action = TRADE_ACTION_DEAL; request.symbol = _Symbol; request.volume = LotSize; request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID); request.sl = SL + 5 * _Point; // Set Stop Loss level without rounding request.tp = TP; request.deviation = 20; request.type = ORDER_TYPE_SELL; request.magic = 123456L; // Magic number int attempts = 0; // Add this line to keep track of the number of attempts while (attempts < 10) // Add this line to limit the number of attempts { bool sent = OrderSend(request, result); // Add this line to store the return value of OrderSend if (sent) // Add this line to check the return value { if (result.retcode == TRADE_RETCODE_DONE) { Print("SELL order opened : ", result.order); break; // Add this line to exit the loop if the trade request was successful } else if (result.retcode == TRADE_RETCODE_REQUOTE) { Print("Requote received, resending trade request"); attempts++; // Add this line to increment the number of attempts } else { Print("SELL order failed with error: ", GetLastError()); break; // Add this line to exit the loop if an error occurred } } else // Add this else block to handle the case when OrderSend fails { Print("Failed to send trade request with error: ", GetLastError()); break; // Add this line to exit the loop if an error occurred } } }}
// Check open positions Print("Checking open positions"); // Add this line to print a message before the loop Print("_Symbol: ", _Symbol); // Add this line to print the value of _Symbol Print("123456L: ", 123456L); // Add this line to print the value of 123456L for (int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if (ticket > 0) { string symbol = PositionGetSymbol(ticket); long magic = PositionGetInteger(POSITION_MAGIC); Print("symbol: ", symbol); // Add this line to print the symbol of the position Print("magic: ", magic); // Add this line to print the magic number of the position if (symbol == _Symbol && magic == 123456L) { // Print a message to indicate that a position is being checked Print("Checking position with ticket ", ticket);
double order_open_price = PositionGetDouble(POSITION_PRICE_OPEN); double current_price = PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK); double profit_in_pips = MathAbs(current_price - order_open_price) / SymbolInfoDouble(_Symbol, SYMBOL_POINT); // Use a double value for the point size // Calculate the SLP value for this trade double slp = MathAbs(order_open_price - PositionGetDouble(POSITION_SL)) / SymbolInfoDouble(_Symbol, SYMBOL_POINT); // Use a double value for the point size Print("slp: ", slp); // Check if the position is in profit by the amount of the SLP if (profit_in_pips >= slp) { // Move the stop loss to break even MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); request.action = TRADE_ACTION_SLTP; request.order = ticket; request.sl = order_open_price; request.tp = PositionGetDouble(POSITION_TP); request.symbol = _Symbol; request.magic = 123456L; int attempts = 0; // Add this line to keep track of the number of attempts while (attempts < 10) // Add this line to limit the number of attempts { bool sent = OrderSend(request, result); // Add this line to store the return value of OrderSend if (sent) // Add this line to check the return value { if (result.retcode == TRADE_RETCODE_DONE) { Print("Stop loss moved to break even for order ", result.order); break; // Add this line to exit the loop if the trade request was successful } else if (result.retcode == TRADE_RETCODE_REQUOTE) { Print("Requote received, resending trade request"); attempts++; // Add this line to increment the number of attempts } else { Print("Failed to move stop loss with error: ", GetLastError()); break; // Add this line to exit the loop if an error occurred } } else // Add this else block to handle the case when OrderSend fails { Print("Failed to send trade request with error: ", GetLastError()); break; // Add this line to exit the loop if an error occurred } } } else // Add this else block to print the values of profit_in_pips and slp { Print("Position not in profit by SLP amount"); Print("profit_in_pips: ", profit_in_pips); Print("slp: ", slp); } } }} } this is what i see before a trade is opened:
Checking open positions _Symbol: EURUSD 123456L: 123456
and this is what i see after a trade is open:
Checking open positions _Symbol: EURUSD 123456L: 123456 symbol: magic: 0
- Assistant
It appears that the "symbol" and "magic" values are not being retrieved correctly within the loop. This could be due to incorrect use of the PositionGet functions or missing/incorrect data in the positions.
To investigate the issue further, you can add additional print statements within the loop to check the values of the retrieved symbol and magic numbers.