各位老大,帮忙看看代码哪里出错了???
- void CheckAndOpenLimitOrdersForSymbol(string symbol, SymbolData &data)
- {
- …………
-
- double volume = GetTradeVolume(symbol);
- bool isRecoveryMode = false;
-
- if(Recovery) // 只有当Recovery为true时才检查恢复模式
- {
- datetime startTime = TimeCurrent() - 30 * 24 * 60 * 60; // 30天前
- HistorySelect(startTime, TimeCurrent());
-
- double lastLossAmount = 0;
- double recoveryProfit = 0;
- double lastLossVolume = 0;
- bool foundLoss = false;
- datetime lastLossTime = 0;
-
- Print("Checking orders for recovery mode from ", TimeToString(startTime), " to ", TimeToString(TimeCurrent()));
-
- for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
- {
- ulong dealTicket = HistoryDealGetTicket(i);
- if(HistoryDealSelect(dealTicket) &&
- HistoryDealGetString(dealTicket, DEAL_SYMBOL) == symbol &&
- HistoryDealGetInteger(dealTicket, DEAL_MAGIC) == InpMagicNumber) // 检查魔术号
- {
- double dealProfit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
- datetime dealTime = (datetime)HistoryDealGetInteger(dealTicket, DEAL_TIME);
-
- Print("Deal found: Time=", TimeToString(dealTime), ", Profit=", DoubleToString(dealProfit, 2));
-
- if(dealProfit < 0)
- {
- foundLoss = true;
- lastLossAmount = MathAbs(dealProfit);
- lastLossVolume = HistoryDealGetDouble(dealTicket, DEAL_VOLUME);
- lastLossTime = dealTime;
- Print("Found loss: Amount=", DoubleToString(lastLossAmount, 2), ", Volume=", DoubleToString(lastLossVolume, 2), ", Time=", TimeToString(lastLossTime));
- break;
- }
- }
- }
-
- if(foundLoss)
- {
- for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
- {
- ulong dealTicket = HistoryDealGetTicket(i);
- if(HistoryDealSelect(dealTicket) &&
- HistoryDealGetString(dealTicket, DEAL_SYMBOL) == symbol &&
- HistoryDealGetInteger(dealTicket, DEAL_MAGIC) == InpMagicNumber)
- {
- double dealProfit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
- datetime dealTime = (datetime)HistoryDealGetInteger(dealTicket, DEAL_TIME);
-
- if(dealTime > lastLossTime)
- {
- recoveryProfit += dealProfit;
- Print("Recovery profit: ", DoubleToString(recoveryProfit, 2), ", Deal Time: ", TimeToString(dealTime));
- }
- }
- }
-
- if(recoveryProfit < lastLossAmount)
- {
- isRecoveryMode = true;
- volume = lastLossVolume * coefficient;
- double maxAllowedVolume = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
- volume = (volume > maxAllowedVolume) ? maxAllowedVolume : volume;
- Print("In recovery mode. New volume: ", DoubleToString(volume, 2), " (coefficient: ", DoubleToString(coefficient, 2), ")");
- }
- else
- {
- Print("Exiting recovery mode. Loss recovered. Recovery profit: ", DoubleToString(recoveryProfit, 2));
- }
- }
- else
- {
- Print("No recent losses found. Using normal volume: ", DoubleToString(volume, 2));
- }
- }
- …………
复制代码
下面是回测里面的日志:
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Checking orders for recovery mode from 2024.03.31 11:00 to 2024.04.30 11:00
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.30 10:33, Profit=3852.96
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.30 10:25, Profit=0.00
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.30 09:07, Profit=4397.40
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.30 09:03, Profit=0.00
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.30 08:30, Profit=5255.94
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.30 08:25, Profit=0.00
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.29 19:24, Profit=1895.07
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.29 19:22, Profit=0.00
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Deal found: Time=2024.04.29 19:13, Profit=-16772.94
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Found loss: Amount=16772.94, Volume=10.47, Time=2024.04.29 19:13
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Recovery profit: 3852.96, Deal Time: 2024.04.30 10:33
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Recovery profit: 3852.96, Deal Time: 2024.04.30 10:25
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Recovery profit: 8250.36, Deal Time: 2024.04.30 09:07
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Recovery profit: 8250.36, Deal Time: 2024.04.30 09:03
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Recovery profit: 13506.30, Deal Time: 2024.04.30 08:30
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Recovery profit: 13506.30, Deal Time: 2024.04.30 08:25
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Recovery profit: 15401.37, Deal Time: 2024.04.29 19:24
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 Recovery profit: 15401.37, Deal Time: 2024.04.29 19:22
2024.09.23 14:03:03.616 Core 01 2024.04.30 11:00:00 In recovery mode. New volume: 20.94 (coefficient: 2.00)
代码在回测中是完全没有问题的。
下面是实盘中EA的打印日志:
2024.09.23 13:00:01.435 Jade Emperor 4.1 (XAUUSD,H1) Checking orders for recovery mode from 2024.08.24 08:00 to 2024.09.23 08:00
2024.09.23 13:00:01.435 Jade Emperor 4.1 (XAUUSD,H1) Deal found: Time=2024.09.20 18:42, Profit=113.43
2024.09.23 13:00:01.435 Jade Emperor 4.1 (XAUUSD,H1) No recent losses found. Using normal volume: 0.58
代码在实盘中只找到最近一个订单,而且还是盈利的。
各位老大,帮忙看看,怎么回测中没问题,而实盘中就出错。问题出现在哪里?谢谢。 |