- Trade modification error I can't solve
- Need help for code
- why this EA can t buy and sell automatically when I backtest it , is there anyone know why?
The first problem is that it doesn't open any trades.
I tried these conditions:
low1 < low2 && close0 > low1 && close0 < high1 && !SellOnly
and
high1 > high2 && close0 < high1 && close0 > low1 && !BuyOnly
It opens trades at almost every candle, but the EA fails to close them. Why use an array to store the tickets? It seems so complicated...
The first problem is that it doesn't open any trades.
I tried these conditions:
and
It opens trades at almost every candle, but the EA fails to close them. Why use an array to store the tickets? It seems so complicated...
yes, its buy and sell conditions are as follow
Buy Logic:The EA will open a buy position if the low of the current bar breaks the low of the previous bar and then closes inside the previous bar (i.e., between the high and low of the previous bar).
Sell Logic:The EA will open a sell position if the high of the current bar breaks the high of the previous bar and then closes inside the previous bar (i.e., between the high and low of the previous bar).
The first problem is that it doesn't open any trades.
I tried these conditions:
and
It opens trades at almost every candle, but the EA fails to close them. Why use an array to store the tickets? It seems so complicated...
i added the logic, and it is still not opening any position in the tester
#include <Trade/Trade.mqh> // Include the trade library for trading functions #property version "1.1" input double Lots = 0.1; // Lot size for trades input bool TslActive = true; // Activate trailing stop loss input bool BuyOnly = false; // Activate buy only positions input bool SellOnly = false; // Activate sell only positions input double CloseProfitDistance = 5.0; // Distance in dollars to close position when in profit input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT; // Define the timeframe for the EA input double ATRMultiplier = 1.5; // Multiplier for ATR to set trailing stop int atrHandle; // Handle for the ATR indicator int totalBars; // Variable to store the total number of bars CTrade trade; // Create a trade object to handle trading operations ulong tickets[]; // Array to store trade tickets double initialPrice; // Store the initial price of the position double referenceClose = 0.0; // Store the close price of the bar before the trigger bar int OnInit() { totalBars = iBars(_Symbol, Timeframe); // Get total number of bars atrHandle = iATR(_Symbol, Timeframe, 14); // Initialize the 14-period ATR indicator return (INIT_SUCCEEDED); // Return initialization success code } void OnDeinit(const int reason) {} void OnTick() { int bars = iBars(_Symbol, Timeframe); // Get current number of bars on the timeframe if (totalBars != bars) { // Check if a new bar has appeared totalBars = bars; // Update the total number of bars double atr[]; // Array to store ATR values CopyBuffer(atrHandle, 0, 0, 1, atr); // Copy the latest ATR value double close1 = iClose(_Symbol, Timeframe, 1); // Close price of the previous bar (trigger bar) double close2 = iClose(_Symbol, Timeframe, 2); // Close price of two bars ago (reference bar) double low1 = iLow(_Symbol, Timeframe, 1); // Low of the previous bar double low2 = iLow(_Symbol, Timeframe, 2); // Low of the bar two bars ago double high1 = iHigh(_Symbol, Timeframe, 1); // High of the previous bar double high2 = iHigh(_Symbol, Timeframe, 2); // High of the bar two bars ago double open0 = iOpen(_Symbol, Timeframe, 0); // Open of the current bar double close0 = iClose(_Symbol, Timeframe, 0); // Close of the current bar double low0 = iLow(_Symbol, Timeframe, 0); // Low of the current bar double high0 = iHigh(_Symbol, Timeframe, 0); // High of the current bar double currentATR = atr[0]; // Get the current ATR value // --- Position Management: Close if subsequent bar closes against the reference bar --- if (ArraySize(tickets) > 0) { for (int i = 0; i < ArraySize(tickets); i++) { if (PositionSelectByTicket(tickets[i])) { double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN); double currentProfit = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ? (SymbolInfoDouble(_Symbol, SYMBOL_BID) - entryPrice) * PositionGetDouble(POSITION_VOLUME) : (entryPrice - SymbolInfoDouble(_Symbol, SYMBOL_ASK)) * PositionGetDouble(POSITION_VOLUME); // Condition to close position when in profit if (currentProfit >= CloseProfitDistance) { trade.PositionClose(tickets[i]); // Close position Print(__FUNCTION__, " > Position #", tickets[i], " closed due to reaching profit distance."); ArrayRemove(tickets, i); // Remove closed ticket from the array continue; // Skip to the next position } } } } // --- Signal Generation: Open Buy or Sell based on the new logic --- // Buy condition: The current bar's low breaks the previous bar's low, but closes inside the previous bar if (low0 < low1 && close0 > low1 && close0 < high1 && !SellOnly) { Print(__FUNCTION__, " > Buy Signal..."); // Close existing Sell positions before opening a Buy if (ArraySize(tickets) > 0) { if (PositionSelectByTicket(tickets[0]) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { trade.PositionClose(tickets[0]); // Close the Sell position Print(__FUNCTION__, " > Closed Sell position before opening Buy."); } } // Open Buy position double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double sl = low1 - ATRMultiplier * currentATR; // Set stop loss below the previous bar's low if (trade.Buy(Lots, _Symbol, ask, sl)) { if (trade.ResultRetcode() == TRADE_RETCODE_DONE) { ArrayResize(tickets, 1); // Store the ticket number of the new Buy position tickets[0] = trade.ResultOrder(); initialPrice = ask; referenceClose = close1; // Store the close price of the bar before the trigger bar Print(__FUNCTION__, " > Buy position opened with ticket #", tickets[0]); } } } // Sell condition: The current bar's high breaks the previous bar's high, but closes inside the previous bar else if (high0 > high1 && close0 < high1 && close0 > low1 && !BuyOnly) { Print(__FUNCTION__, " > Sell Signal..."); // Close existing Buy positions before opening a Sell if (ArraySize(tickets) > 0) { if (PositionSelectByTicket(tickets[0]) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { trade.PositionClose(tickets[0]); // Close the Buy position Print(__FUNCTION__, " > Closed Buy position before opening Sell."); } } // Open Sell position double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); double sl = high1 + ATRMultiplier * currentATR; // Set stop loss above the previous bar's high if (trade.Sell(Lots, _Symbol, bid, sl)) { if (trade.ResultRetcode() == TRADE_RETCODE_DONE) { ArrayResize(tickets, 1); // Store the ticket number of the new Sell position tickets[0] = trade.ResultOrder(); initialPrice = bid; referenceClose = close1; // Store the close price of the bar before the trigger bar Print(__FUNCTION__, " > Sell position opened with ticket #", tickets[0]); } } } } }
Since you're trading by bar opening, when you check your conditions new just created bar has the following state: close0 == low0 == high0 == open0. So "low0 < low1 && close0 > low1" or "high0 > high1 && close0 < high1" gives false always.
When you run a trading system based on finished bars, it's usually supposed to check signals on bars with indices 1+, not using 0-th bar, because it's just a dash.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use