I'm a newbie, Please help me, Error 4107

 

// parameters
extern int MagicNumber = 19102011;
extern int TP = 150;
extern int SL = 100;
extern double Lot = 0.01;
extern int Slip = 10; // do truot cho giá

extern double B = 0.00020; // Breakout points

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

// start here

int i = 0;

int err;

int ticket;

double MAShort;

double MALong;

string Com = "Hoa";

double P = NormalizeDouble( PRICE_CLOSE, 5);

double Spread = MarketInfo(Symbol(),MODE_SPREAD)*Point;

//check orders

for ( i= 0; i < OrdersTotal(); i++)

{

if ( OrderSelect( i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol())

{

// kiem tra cac vi tri, neu co 1 vai lenh da duoc mo, kiem tra chung voi cac indicator

if (OrderType() == OP_BUY)

{

return(0);

}

if (OrderType() == OP_SELL)

{

return(0);

}

}

}

// tính cac giá tri cua System

MAShort = iMA( Symbol(), 0, 5, 0, MODE_SMA, PRICE_CLOSE, 0);

MALong = iMA( Symbol(), 0, 20, 0, MODE_SMA, PRICE_CLOSE, 0);

// Condition for BUY


if(MAShort > MALong)

{

ticket = OrderSend( Symbol(), OP_BUY, Lot, P, 0, P - SL, P + TP, Com, MagicNumber, 0, Blue);

if (ticket == -1)

{

err = GetLastError();

Print("error(",err,")");

}

}

else

{

Comment("\n","Cannot set OP_BUY",

"\n","Khong du dieu kien de vao lenh Buy");

}

// dieu kien cho lenh SELL


if (MAShort < MALong)

{

ticket = OrderSend( Symbol(), OP_SELL, Lot, P, 0, P + SL, P - TP, Com, MagicNumber, 0, Red);

if (ticket == -1)

{

err = GetLastError();

Print("error(",err,")");

}

}

else

{

Comment("\n","Cannot set OP_SELL",

"\n","Khong du dieu kien de vao lenh SELL");

}

}

 

Please use this to post code . . . it makes it easier to read.

What is this supposed to do ?

double P = NormalizeDouble( PRICE_CLOSE, 5);
 

If you look here: https://docs.mql4.com/constants/prices you will see that PRICE_CLOSE has a value of 0 and it's an int

Using a value of 0 for P in this code is probably why you get error ERR_INVALID_PRICE_PARAM (4107)

ticket = OrderSend( Symbol(), OP_BUY, Lot, P, 0,  P - SL,  P + TP, Com, MagicNumber, 0, Blue);   //  0 - SL = -SL which is an invalid price

 

thanks your support, i'll try

Reason: