sto scrivendo un ea per mt4. voglio inserire una condizione per chiudere i trade…
- User
sto scrivendo un ea per mt4. voglio inserire una condizione per chiudere i trade alternativa a quella presente: chiudi tutti i buy quando una candela chiude a ridosso della banda superiore di bollinger chiudi tutti i sell quando una candela chiude a ridosso della banda inferiore
ci deve essere un bool per decidere se attivare questa modalità oppure no. questo il codice da aggiornare (riscrivilo per intero con le modifiche) #property strict
// Input parameters input int bbPeriod = 80; // Bollinger Bands period input double bbDeviation = 2.0; // Bollinger Bands deviation input int rsiPeriod = 5; // RSI period input double rsiOverbought = 80; // RSI overbought level input double rsiOversold = 20; // RSI oversold level input bool aggressiveMode = false; // Aggressive mode boolean input double LotSize = 0.1; // Lot size for each trade input int Slippage = 3; // Maximum slippage input bool useStopLoss = false; // Use stop loss input double StopLossPips = 150; // Stop loss in pips input bool useTakeProfit = false; // Use take profit input double TakeProfitPips = 200; // Take profit in pips input bool useTrailingStop = false; // Use trailing stop input double TrailingStop = 10; // Trailing stop distance in pips input double TrailingStep = 5; // Trailing step distance in pips
// Expert initialization function int OnInit() { // Initialization code here return(INIT_SUCCEEDED); }
// Expert tick function void OnTick() { static datetime last_bar_time = 0;
// Checking new bar if (Time[0] != last_bar_time) { last_bar_time = Time[0]; double upper_band = iBands(NULL, 0, bbPeriod, bbDeviation, 0, PRICE_CLOSE, MODE_UPPER, 0); double lower_band = iBands(NULL, 0, bbPeriod, bbDeviation, 0, PRICE_CLOSE, MODE_LOWER, 0); double middle_band = iBands(NULL, 0, bbPeriod, bbDeviation, 0, PRICE_CLOSE, MODE_MAIN, 0); double rsi_current = iRSI(NULL, 0, rsiPeriod, PRICE_CLOSE, 0); // Check for Buy Condition if ((Close[1] < lower_band) || (aggressiveMode && Close[1] > middle_band && Close[2] <= middle_band)) { OpenBuyTrade(); } // Check for Sell Condition if ((Close[1] > upper_band) || (aggressiveMode && Close[1] < middle_band && Close[2] >= middle_band)) { OpenSellTrade(); } // Check for Close Buy Conditions if (rsi_current > rsiOverbought) { CloseBuyTrades(); } // Check for Close Sell Conditions if (rsi_current < rsiOversold) { CloseSellTrades(); } }}
void OpenBuyTrade() { double stop_loss = useStopLoss ? NormalizedStopLoss(StopLossPips, OP_BUY) : 0; double take_profit= useTakeProfit? NormalizedTakeProfit(TakeProfitPips, OP_BUY) : 0; if (!OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, stop_loss, take_profit, "Buy Order", 0, 0, Blue)) { Print("Buy Order Send Error: ", GetLastError()); } }
- Assistant
- User
devi aggiornare la parte per chiudere i trades e non l'hai neppure scritta!!!!!!!!!!!
- Assistant
Mi scuso per l'inconveniente. Aggiorno correttamente la parte per chiudere i trades aggiungendo il controllo basato sulle bande di Bollinger e il nuovo parametro
closeOnBollinger. Ecco come dovrebbero apparire le funzioniCloseBuyTrades()eCloseSellTrades()aggiornate:// ... // Close Buy Trades Function void CloseBuyTrades() { if (closeOnBollinger) { double upper_band = iBands(NULL, 0, bbPeriod, bbDeviation, 0, PRICE_CLOSE, MODE_UPPER, 0); if (Close[1] >= upper_band) { for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_BUY && OrderSymbol() == Symbol()) { if (!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrNONE)) { Print("Close Buy Trade Error: ", GetLastError()); } } } } } } // Close Sell Trades Function void CloseSellTrades() { if (closeOnBollinger) { double lower_band = iBands(NULL, 0, bbPeriod, bbDeviation, 0, PRICE_CLOSE, MODE_LOWER, 0); if (Close[1] <= lower_band) { for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELL && OrderSymbol() == Symbol()) { if (!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clrNONE)) { Print("Close Sell Trade Error: ", GetLastError()); } } } } } } // ... - User
- Assistant
- User
puoi riscrivere tutto il codice in modo completo? continuo ad avere l'errore
- Assistant
- User
no, manca metà codice
- Assistant
- User
no, non ci siamo. se è attivo il bool per la chiusura con le bb deve chiudere utilizzando quel tipo di metodo, altrimenti utilizza il metodo con rsi. sono alternative
- Assistant
- User
- Assistant
- User
scrivi tutto senza omettere parti
- Assistant
- User
non è completo. manca praticamente metà codice originale!!
- Assistant