Mi scuso per la confusione precedentemente generata. Ecco un esempio di come il codice potrebbe essere scritto in MQ4 per lavorare con MetaTrader 4:
// Dichiarazione delle variabili globali
int HigherHigh = 0;
int LowerLow = 0;
// Funzione per trovare l'HH e l'LL giornaliero
void FindHHLLDaily(string symbol, int timeframe)
{
double rates_high[];
double rates_low[];
int copied = CopyHigh(symbol, timeframe, 0, 1, rates_high);
copied = CopyLow(symbol, timeframe, 0, 1, rates_low);
double higher_high_temp = ArrayMaximum(rates_high, copied);
double lower_low_temp = ArrayMinimum(rates_low, copied);
HigherHigh = (int)MathRound(higher_high_temp);
LowerLow = (int)MathRound(lower_low_temp);
}
// Funzione per aprire posizione short
void OpenShortPosition(string symbol, int size, int stoploss, int takeprofit)
{
double stop_loss = (double)stoploss / MathPow(10, Digits);
double take_profit = (double)takeprofit / MathPow(10, Digits);
int ticket = OrderSend(symbol, OP_SELL, size, Ask, slippage, 0, 0, "", magicnumber, 0, clrRed);
if (ticket > 0)
{
if (stoploss > 0)
OrderModify(ticket, OrderOpenPrice(), Ask + stop_loss, 0, 0, Red);
if (takeprofit > 0)
OrderModify(ticket, OrderOpenPrice(), 0, Ask - take_profit, 0, Red);
}
}
// Funzione per aprire posizione long
void OpenLongPosition(string symbol, int size, int stoploss, int takeprofit)
{
double stop_loss = (double)stoploss / MathPow(10, Digits);
double take_profit = (double)takeprofit / MathPow(10, Digits);
int ticket = OrderSend(symbol, OP_BUY, size, Bid, slippage, 0, 0, "", magicnumber, 0, clrGreen);
if (ticket > 0)
{
if (stoploss > 0)
OrderModify(ticket, OrderOpenPrice(), Bid - stop_loss, 0, 0, Green);
if (takeprofit > 0)
OrderModify(ticket, OrderOpenPrice(), 0, Bid + take_profit, 0, Green);
}
}
// Funzione principale
void OnTick()
{
datetime now = TimeLocal();
// Imposta l'orario di inizio e fine delle operazioni
datetime start_time = StrToTime("00:00:00") + GMTOffset();
datetime end_time = StrToTime("19:00:00") + GMTOffset();
// Verifica se l'orario attuale è all'interno dell'intervallo operativo
if (now >= start_time && now <= end_time)
{
// Verifica se è il momento di trovare HH e LL giornaliero
if (TimeHour(now) < 15 && TimeMinute(now) < 20)
{
FindHHLLDaily(Symbol(), PERIOD_D1);
}
// Verifica se è il momento di aprire una posizione
else if (TimeHour(now) >= 15 && TimeMinute(now) >= 25 && TimeHour(now) < 19)
{
if (HigherHigh > 0)
{
// Verifica se chiude sopra l'HH in un candle a 5 min
double close_price = iClose(Symbol(), PERIOD_M5, 0);
if (close_price > HigherHigh)
{
OpenShortPosition(Symbol(), Size, StopLoss, TakeProfit);
// Azzera l'HH e LL a fine giornata
HigherHigh = 0;
LowerLow = 0;
}
}
else if (LowerLow > 0)
{
// Verifica se chiude sotto l'LL in un candle a 5 min
double close_price = iClose(Symbol(), PERIOD_M5, 0);
if (close_price < LowerLow)
{
OpenLongPosition(Symbol(), Size, StopLoss, TakeProfit);
// Azzera l'HH e LL a fine giornata
HigherHigh = 0;
LowerLow = 0;
}
}
}
}
}
Si prega di notare che il codice sopra è solo un esempio di come potrebbe essere scritto in MQ4 per MetaTrader 4. È possibile che sia necessario apportare modifiche al codice in base alle proprie esigenze specifiche, compreso l'adattamento delle variabili globali, l'aggiunta di funzioni di gestione del periodo, ecc.