MT4 EA not working in Live or Demo account.
-
Only works in strategy test. Please see codes below:
input double InitialLotSize = 0.01;
input int MaxOrders = 3;
input bool UseMartingale = true;
input double MartingaleMultiplier = 2.0;
input bool StartWithBuy = true;bool IsBuy = true;
int TradeCounter = 0;
double LastBarClose = 0.0;
double LotSize = InitialLotSize;void OnStart()
{
LastBarClose = iClose(Symbol(), 0, 1);
if (!StartWithBuy)
{
IsBuy = false;
}
}void OnTick()
{
// Check if the current bar is closed
double currentBarClose = iClose(Symbol(), 0, 0);
if (currentBarClose != LastBarClose)
{
LastBarClose = currentBarClose;// Close open orders CloseOrders(); // Check if we reached the maximum number of orders if (TradeCounter == MaxOrders) { // Switch direction IsBuy = !IsBuy; TradeCounter = 0; } // Place new order if no order is placed if (TradeCounter < MaxOrders) { PlaceOrder(); } }}
void CloseOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && (OrderType() == OP_BUY || OrderType() == OP_SELL))
{
bool closed = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), IsBuy ? MODE_BID : MODE_ASK), 0, clrNONE);
if (!closed)
{
Print("Order close failed: ", GetLastError());
}
else
{
if (closedInProfit(OrderTicket()))
{
// Reset the lot size to the initial value after closing in profit
LotSize = InitialLotSize;
}
}
}
}
}
}void PlaceOrder()
{
double price = IsBuy ? Ask : Bid;int ticket = OrderSend(Symbol(), IsBuy ? OP_BUY : OP_SELL, LotSize, price, 0, 0, 0, "", 0, clrNONE, 0); if (ticket > 0) { TradeCounter++; } else { Print("Order send failed: ", GetLastError()); } // Update the lot size for the next trade if using Martingale if (UseMartingale) { LotSize *= MartingaleMultiplier; }}
bool closedInProfit(int ticket)
{
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
double profit = OrderProfit();
return profit >= 0.0;
}
return false;
} -
HI, you should share your project by link. Who can analyse your code here?