//+------------------------------------------------------------------+//| CCI.mq5 |//| Copyright 2024, MetaQuotes Ltd. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2024, MetaQuotes Ltd."#property link"https://www.mql5.com"#property version"1.00"//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+//+------------------------------------------------------------------+//| CCI Bot.mq5 |//| Copyright 2024, MetaQuotes Ltd. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property strictinputint cciLength = 20// CCI Periodinputdouble lotSize = 0.5; // Lot sizeinputint takeProfit = 1000; // Take Profit in pointsinputint stopLoss = 1000; // Stop Loss in pointsinputstring startTradeTime = "07:30"; // Trading start timeinputstring endTradeTime = "09:30"; // Trading end timedouble cciValue;
bool tradeToday = false;
datetime lastTradeDay = 0;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
// Initialization here (if necessary)return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
// Current time and datedatetime currentTime = TimeCurrent();
MqlDateTime strTime;
TimeToStruct(currentTime, strTime);
// Checking if the current time is within the trading windowbool timeAllowed = TimeInRange(strTime, startTradeTime, endTradeTime);
// Calculating CCI value
cciValue = iCCI(_Symbol, PERIOD_M30, cciLength, PRICE_CLOSE);
// Checking if trading has already occurred todayif (lastTradeDay < iTime(_Symbol, 0, 0))
tradeToday = false;
// Trading conditionsbool longCondition = cciValue > 120 && timeAllowed && !tradeToday;
bool shortCondition = cciValue < -120 && timeAllowed && !tradeToday;
// Trade logicif (longCondition)
{
// Open Long Position
OpenTrade(ORDER_TYPE_BUY);
tradeToday = true;
lastTradeDay = currentTime;
}
elseif (shortCondition)
{
// Open Short Position
OpenTrade(ORDER_TYPE_SELL);
tradeToday = true;
lastTradeDay = currentTime;
}
}
//+------------------------------------------------------------------+// Helper functions and further implementation//+------------------------------------------------------------------+// Function to check if the current time is within the specified time intervalbool TimeInRange(MqlDateTime &time, string start, string end)
{
// Time conversionint startTime = ConvertStringToTime(start);
int endTime = ConvertStringToTime(end);
int currentTime = TimeToMinutes(time.hour, time.min);
return (currentTime >= startTime && currentTime <= endTime);
}
// Helper function to convert hours and minutes to minutes since midnightint TimeToMinutes(int hour, int minute)
{
return (hour * 60 + minute);
}
// Helper function to convert a time string to minutes since midnightint ConvertStringToTime(string time)
{
StringReplace(time, ":", "");
long hour = StringToInteger(StringSubstr(time, 0, 2));
long minute = StringToInteger(StringSubstr(time, 2, 2));
return TimeToMinutes(int(hour), int(minute));
}
// Function to open tradesvoid OpenTrade(ENUM_ORDER_TYPE orderType)
{
// Setting order parametersMqlTradeRequest tradeRequest;
tradeRequest.action = TRADE_ACTION_DEAL;
tradeRequest.type = orderType;
tradeRequest.symbol = _Symbol;
tradeRequest.volume = lotSize;
tradeRequest.price = (orderType == ORDER_TYPE_BUY) ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID);
tradeRequest.sl = (orderType == ORDER_TYPE_BUY) ? tradeRequest.price - stopLoss * _Point : tradeRequest.price + stopLoss * _Point;
tradeRequest.tp = (orderType == ORDER_TYPE_BUY) ? tradeRequest.price + takeProfit * _Point : tradeRequest.price - takeProfit * _Point;
tradeRequest.deviation = 10;
tradeRequest.magic = 0;
tradeRequest.comment = "CCI Bot Trade";
// Debugging information before sending the orderPrint("Attempting to open trade: ", (orderType == ORDER_TYPE_BUY) ? "BUY" : "SELL");
Print("Price: ", tradeRequest.price);
Print("SL: ", tradeRequest.sl);
Print("TP: ", tradeRequest.tp);
// Sending the orderMqlTradeResult tradeResult;
if (!OrderSend(tradeRequest, tradeResult))
{
Print("Trade Error: ", tradeResult.comment);
}
else
{
Print("Trade successful: ", (orderType == ORDER_TYPE_BUY) ? "BUY" : "SELL", " Ticket: ", tradeResult.order);
}
}
Hello guys, could anyone tell me why my script doesn't do a trade in the backtest? Need help pleeease. Und same conditions, I can do some random scripts, but my own one doesn't work. (I speak German as well.)
Hello guys, could anyone tell me why my script doesn't do a trade in the backtest? Need help pleeease. Und same conditions, I can do some random scripts, but my own one doesn't work. (I speak German as well.)