# EA Can not load moving average

#### CEO

I developed an EA based on a strategy and its revolves around a moving average. did it on mt4 but got stuck tho i finished the code . I created same code it mql5 and programmed the custom moving average similarly but when the EA runs s, the Journal prints cannot Load moving average
MQL5:
```//+------------------------------------------------------------------+
//|                                               Moving Average.mq5 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property description "The Hand of God expert advisor"
////--- Inputs
input double Lots          = 0.5;
input double MaximumRisk   = 1;
input double DecreaseFactor= 1;
input int    MovingPeriod  = 34;
input int    MovingShift   = 0;
int POSITION_SELL;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()
{
int buys = 0, sells = 0;
for (int i = 0; i < PositionsTotal(); i++)
{
if (PositionGetInteger(POSITION_TYPE) == POSITION_SELL)
sells++;
}
}

//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized(){
double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double calculatedLots; // New variable to store the calculated lot size

if (accountBalance < 100)
calculatedLots = 0.02;
else if (accountBalance < 500)
calculatedLots = 0.05;
else if (accountBalance < 1000)
calculatedLots = 0.1;
else if (accountBalance < 2500)
calculatedLots = 0.11;
else if (accountBalance < 5000)
calculatedLots = 0.12;
else if (accountBalance < 5500)
calculatedLots = 0.15;
else if (accountBalance < 10000)
calculatedLots = 0.2;
else if (accountBalance < 15000)
calculatedLots = 0.25;
else if (accountBalance < 25000)
calculatedLots = 0.5;
else if (accountBalance < 50000)
calculatedLots = 1;
else if (accountBalance < 100000)
calculatedLots = 2;
else
calculatedLots = 3;

return calculatedLots; // Return the calculated lot size
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double ma_value[];
ArrayResize(ma_value, 2); // Initialize ma with 2 elements

//--- Go trading only for the first tick of the new bar
if (iVolume(_Symbol, 0,0) < 2660)
return;

//--- Get Moving Average
double ma_valueCurrent = iMA(NULL, 0, 34,0,MODE_LWMA, 0);
double ma_valuePrevious = iMA(NULL, 0, 34,0,MODE_LWMA,1);
//int maHandle = iMA(Symbol(), 0, 14, 0, MODE_SMA, PRICE_CLOSE);
//ma[i] = iMA(_Symbol, _Period, MovingShift, PRICE_CLOSE, MODE_LWMA,i);

//--- Sell conditions
if (iClose(Symbol(), 0, 1) <= ma_valuePrevious && iClose(Symbol(), 0, 0) < ma_valueCurrent && iVolume(Symbol(), 0, 0) > 4000)
{
// Define a trade request structure

// Set the trade request parameters
request.type = ORDER_TYPE_SELL; // Use ORDER_TYPE_SELL for a market sell order
request.symbol = _Symbol;
request.volume = LotsOptimized(); // Specify your lot size here
request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
//request.slippage = 6;
request.deviation = 0;
request.comment = "";
request.magic = 0;
request.tp = 0; // Take profit (optional)
request.sl = 0; // Stop loss (optional)

// Send the order
if (OrderSend(request, result))
{
// Order was sent successfully
Print("Sell order sent. Order ticket: ", result.order);
}
else
{
// Error handling
Print("OrderSend error: ", result.retcode);
}

}

if (iClose(Symbol(), 0, 1) >= ma_valuePrevious && iClose(Symbol(), 0, 0) > ma_valueCurrent && iVolume(Symbol(), 0, 0) > 4000)
{
// Define a trade request structure

request.symbol = _Symbol;
request.volume = LotsOptimized(); // Specify your lot size here
// request.slippage = 6;
request.deviation = 0;
request.comment = "";
request.magic = 0;
request.tp = 0; // Take profit (optional)
request.sl = 0; // Stop loss (optional)

// Send the order
if (OrderSend(request, result))
{
// Order was sent successfully
Print("Buy order sent. Order ticket: ", result.order);
}
else
{
// Error handling
Print("OrderSend error: ", result.retcode);
}

};
};

//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
//  if (PositionSelectByIndex(i) == false)
//   break;

//  if (PositionGetSymbol() != Symbol())
//   continue;

// Check order type
{
// Check conditions on a 30-minute chart
double highestHigh = iHigh(Symbol(), 30, iHighest(Symbol(), 30, MODE_HIGH, 54, 0));
double lowestLow = iLow(Symbol(), 30, iLowest(Symbol(), 30, MODE_LOW, 26, 0));

// Check if either the highest high or lowest low conditions are met
if (SymbolInfoDouble(Symbol(), SYMBOL_BID) <= lowestLow || SymbolInfoDouble(Symbol(), SYMBOL_BID) >= highestHigh)
{
//    int res = OrderClose(PositionGetTicket(), PositionGetDouble(POSITION_VOLUME), SymbolInfoDouble(Symbol(), SYMBOL_BID), 3, clrWhite);
//     if (res == false)
Print("OrderClose error ", GetLastError());
}
}

if (PositionGetInteger(POSITION_TYPE) == POSITION_SELL)
{
// Check conditions on a 30-minute chart
double highestHigh = iHigh(Symbol(), 30, iHighest(Symbol(), 30, MODE_HIGH, 54, 0));
double lowestLow = iLow(Symbol(), 30, iLowest(Symbol(), 30, MODE_LOW, 26, 0));

// Check if either the highest high or lowest low conditions are met
{
//  int res = OrderClose(PositionGetTicket(), PositionGetDouble(POSITION_VOLUME), SymbolInfoDouble(Symbol(), SYMBOL_ASK), 3, clrWhite);
//    if (res == false)
Print("OrderClose error ", GetLastError());
}
}
}
}

//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
{
// Initialize the ma array
double ma_value[];
int bars = Bars(_Symbol, _Period); // Get the number of bars for the current symbol and timeframe
ArrayResize(ma_value, bars);

for (int i = 0; i < bars; i++)
{

ma_value[i] = iMA(NULL, 0, 0, 0, MODE_LWMA, i);

}

if (stopsLevel < 0)
return;
}```

#### Attachments

25.9 KB · Views: 3

#### Enivid

Staff member
Working with indicators is completely different in MT4.

First, you create indicator handles in the OnInit() handler. Then, you copy indicator buffers inside your OnTick(), checking whether the copying went alright (if it didn't, it's likely the data isn't ready yet and you have to wait).

By the way, this line tries to create a handle for an MA with the period = 0, which is probably causing your cannot load indicator 'Moving Average' [4002] error:
MQL5:
`ma_value[i] = iMA(NULL, 0, 0, 0, MODE_LWMA, i);`
However, even if you set a correct period there, it still won't work because you have to fix the entire code as I've described above.

#### CEO

Done and I'm so happy. but i havent tested it yet to see if it takes the trades. MA appears now

Replies
1
Views
474
Replies
1
Views
289
Replies
1
Views
418
Replies
2
Views
3K
Replies
12
Views
8K