can you fix this error that i am getting for my mql5 code? error: possible loss of data…
- User
can you fix this error that i am getting for my mql5 code?
error: possible loss of data due to type conversion on line 215
code: // 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 } } } - Assistant
The error "possible loss of data due to type conversion" occurs because you are assigning a value of type double to a variable of type int. To fix this error, you need to explicitly cast the double value to int.
You can do this by using the static_cast() function to convert the value. Here's the updated code:
// Get the current number of bars int curr_bars = static_cast<int>(Bars(_Symbol, _Period));Repeat the same casting method for any other lines where you assign a double value to an int variable. Additionally, make sure to review and fix any other type conversion errors that might occur throughout the code.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.