turns-00027.parquet:7936
6ecc2a4c330a47f6f008ba2a
turn 13/13gpt-4o-2024-05-13ItalianItaly788 words
degenerate_repetitionAbsentFinal dense release
USER
però pensavo potrebbe essere voluto in modo che fintanto che ci sono ordini aperti priceHistory non viene aggiornato. avrebbe un senso considerando anche la funzione closeOrders
ASSISTANT
Capisco il tuo ragionamento e potrebbe effettivamente avere senso. Se il programmatore ha voluto che `priceHistory` non venisse aggiornato fintanto che ci sono ordini aperti, vediamo come questo potrebbe essere implementato all'interno del codice. Ricontrolliamo la logica nel contesto delle funzioni `closeBuyOrders` e `closeSellOrders`.
### Comportamento Intentato
L'EA potrebbe essere progettato per evitare aggiornamenti di `historyPriceLevel` mentre ci sono ordini aperti, per evitare cambiamenti nel livello di riferimento durante il trading attivo.
### Logica e Implementazione
#### Passaggio 1: Verifica degli Ordini Aperti
Determiniamo prima la logica di chiusura degli ordini nella funzione `OnTick` e come influenza l'aggiornamento di `priceHistory`.
```mql
int countTotalOrders() {
int totalOrdersCount = 0;
int ordersCounter = OrdersTotal() - 1;
while (ordersCounter >= 0) {
if (OrderSelect(ordersCounter, SELECT_BY_POS)) {
if (OrderMagicNumber() == MagicID && OrderSymbol() == _Symbol) {
totalOrdersCount++;
}
}
ordersCounter--;
}
return(totalOrdersCount);
}
```
#### Passaggio 2: Determinazione degli Ordini
Esaminiamo dove vengono determinati `currentHistoryIndex` e `priceHistory` nel contesto di ordini aperti.
```mql
void OnTick() {
int totalOrders;
...
if (lastTickTime == Time[0])
return;
lastTickTime = Time[0];
totalOrders = countTotalOrders();
// Solo aggiorna historyPriceLevel se non ci sono ordini aperti
if (totalOrders == 0) {
// Determinazione di historyPriceLevel
currentHistoryIndex = -1;
int historyIndex = 0;
int lastValidIndex = recordsCount - 1;
while (historyIndex < recordsCount && priceHistory[historyIndex][0] < Time[0]) {
currentHistoryIndex = historyIndex;
historyIndex++;
}
if (currentHistoryIndex == -1) {
currentHistoryIndex = recordsCount - 1;
}
historyPriceLevel = NormalizeDouble(priceHistory[currentHistoryIndex, 1], _Digits);
ObjectDelete("level");
string orderComment = "level";
if (ObjectFind(orderComment) != -1) {
ObjectMove(orderComment, 0, iTime(_Symbol, 1, 0), historyPriceLevel);
} else {
ObjectCreate(0, orderComment, OBJ_HLINE, 0, Time[1], historyPriceLevel);
ObjectSet(orderComment, OBJPROP_COLOR, 16776960);
ObjectSet(orderComment, OBJPROP_STYLE, 0);
ObjectSet(orderComment, OBJPROP_WIDTH, 1);
ObjectSet(orderComment, OBJPROP_BACK, 1);
}
}
...
}
```
### Passaggio 3: Implementazione di Chiusura Ordini
Le funzioni di chiusura chiamate:
```mql
void closeBuyOrders() {
bool orderClose;
int openOrderIndex;
orderClose = false;
openOrderIndex = OrdersTotal() - 1;
if (openOrderIndex >= 0) {
do {
if (OrderSelect(openOrderIndex, 0, 0) && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicID && OrderType() == OP_BUY) {
orderClose = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), slippage, 16776960);
}
openOrderIndex = openOrderIndex - 1;
} while (openOrderIndex >= 0);
}
}
void closeSellOrders() {
bool orderClose;
int openOrderIndex;
orderClose = false;
openOrderIndex = OrdersTotal() - 1;
if (openOrderIndex >= 0) {
do {
if (OrderSelect(openOrderIndex, 0, 0) && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicID && OrderType() == OP_SELL) {
orderClose = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), slippage, 16776960);
}
openOrderIndex = openOrderIndex - 1;
} while (openOrderIndex >= 0);
}
}
```
### Fondo di Decisione: Solida Implementazione
Baseline potrebbero essere:
**1. Contare e controllare Ordini**
```mql
int countTotalOrders() {
int totalOrdersCount = 0;
int ordersCounter = OrdersTotal() - 1;
while (ordersCounter >= 0) {
if (OrderSelect(ordersCounter, SELECT_BY_POS)) {
if (OrderMagicNumber() == MagicID && OrderSymbol() == _Symbol) {
totalOrdersCount++;
}
}
ordersCounter--;
}
return totalOrdersCount;
}
```
**2. La principale Dispatch `OnTick()`**
```mql
void OnTick() {
string orderComment;
int currentHistoryIndex;
double williamPRange;
double stopLoss;
double takeProfit;
double historyPriceLevel;
int totalOrders = countTotalOrders();
ManageTrailingStop();
if (lastTickTime == Time[0])
return;
lastTickTime = Time[0];
// Determine historyPriceLevel only when there are no active orders
if (totalOrders == 0) {
currentHistoryIndex = -1;
int historyIndex = 0;
int lastValidIndex = recordsCount - 1;
while (historyIndex < recordsCount && priceHistory[historyIndex][0] < Time[0]) {
currentHistoryIndex = historyIndex;
historyIndex++;
}
if (currentHistoryIndex == -1) {
currentHistoryIndex = recordsCount - 1;
}
historyPriceLevel = NormalizeDouble(priceHistory[currentHistoryIndex, 1], _Digits);
ObjectDelete("level");
orderComment = "level";
if (ObjectFind(orderComment) != -1) {
ObjectMove(orderComment, 0, iTime(_Symbol, 1, 0), historyPriceLevel);
} else {
ObjectCreate(0, orderComment, OBJ_HLINE, 0, Time[1], historyPriceLevel);
ObjectSet(orderComment, OBJPROP_COLOR, 16776960);
ObjectSet(orderComment, OBJPROP_STYLE, 0);
ObjectSet(orderComment, OBJPROP_WIDTH, 1);
ObjectSet(orderComment, OBJPROP_BACK, 1);
}
}
if ((priceHistory[currentHistoryIndex, 1] > Open[0])) {
double spread = MarketInfo(_Symbol, MODE_SPREAD);
if (_Digits == 5 || _Digits == 3) {
spread /= 10.0;
}
string message = "\nSpread: " + DoubleToStr(spread, 2) + " pips";
message += "\nWPR Filter: " + (WPR_Filter ? "On" : "Off");
message += "\n\nBUY Signals - " + DoubleToStr(priceHistory[currentHistoryIndex, 1], _Digits);
message += "\nOrders Open: " + IntegerToString(totalOrders);
Comment(message);
}
if ((priceHistory[currentHistoryIndex, 1] < Open[0])) {
double spread = MarketInfo(_Symbol, MODE_SPREAD);
if (_Digits == 5 || _Digits == 3) {
spread /= 10.0;
}
string message = "\nSpread: " + DoubleToStr(spread, 2) + " pips";
message += "\nWPR Filter: " + (WPR_Filter ? "On" : "Off");
message += "\n\nSELL Signals - " + DoubleToStr(priceHistory[currentHistoryIndex, 1], _Digits);
message += "\nOrders Open: " + IntegerToString(totalOrders);
Comment(message);
}
}
```
### Conclusioni
Implementando la verifica di ordini aperti prima del caricamento `priceHistory`, risulta in linea con la strategia di `closeBuyOrders` e `closeSellOrders`. Questo logicamente mantiene costanza in trading evitando interferenze su ordini aperti.