Help with E-mail and Close Orders Operations of EA

 

I would greatly appreciate any help anybody could give me on my EA. I am having a problem with the SendMail function, I receive the e-mail but for order open price I receive 0.0000.

The other problem I am having is the OrderClose function is not closing the order. I've tried several solutions with no results.

Thank You


bool openposition = false; // Used to limit the number of open positions
int start()
{
string text = "", text2 = "";
int index=OrderTicket(); // Order Ticket # of open position
double
openprice,
SMA, // Varable for 20hr Simple Moving Avg
LO,
LC,
SO,
SC;
openprice=OrderOpenPrice();
LO = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); // 10hr sma long position buy indicator
LC = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); // 10hr sma indicator used for closing long position
SO = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); // 10hr sma short position sell indicator
SC = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); // 10hr sma indicator used for closing short position
SMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0); // 20hr sma indicator

//
if (openposition == false && LC>SMA) // If the 10hr sma crosses over the 20hr sma = buy signal
{
OrderSend(Symbol(),OP_BUY,.01,Ask,8,0,0,NULL,17,0,Green); // Sends market order to open (1) long position
SendMail("Long Position Opened ","Bought Price @ "+DoubleToStr(openprice,4)); //Sends Email with open order price

openposition = true; // Changes bool varable to true - there is an open position
}
else
{
if (SO<SMA && openposition == false) // If 10hr sma crosses under the 20hr sma = sell signal
{ // No current market position
OrderSend(Symbol(),OP_SELL,.01,Bid,8,0,0,NULL,17,0,Orange); // Sends market order to open (1) short position
SendMail("Opened Short Position","Sold Price @" +DoubleToStr(openprice,4)); // Sends an email of opening short position
}
openposition = true; // Changes bool varable to true - there is an open position
}

//---------------------------------------------------------------------------------------------------------------------------------------------------
//Order Close
//---------------------------------------------------------------------------------------------------------------------------------------------------
string Symb=Symbol(); //
double Profit=OrderProfit(); //
int Ticket=OrderTicket(); // Order ticket
double Lot=OrderLots(); //
double Price=OrderOpenPrice();
double closeprice=OrderClosePrice();
int mn=OrderMagicNumber();
//------------------------------------------------------------------------------------------------------------------------------------------------
if (OrderTicket()>0 && LC==SMA && openposition==true) // If 10hr sma crosses back under 20hr sma = close long position signal

{
OrderClose(OrderTicket(),.01,Bid,8,Green); // Closes open long position
SendMail("Long Position Closed ","Close Price" +DoubleToStr(closeprice,4)+"For a profit of"+DoubleToStr(Profit,4)); // Sends email of closing long position w/ profit
}
else
{
if (OrderTicket()>0 && SC==SMA && openposition==true) // If 10hr sma crosses back over 20hr sma = close short position signal
{
OrderClose(OrderTicket(),.01,Ask,8,Orange); // Closes open short position
SendMail("Short Position Closed ","Close Price" +DoubleToStr(closeprice,4)+"For a profit of"+DoubleToStr(Profit,4)) ; // Sends email of closing short position
}

}


return; //
}

 
Read carefully about OrderSelect() command. Your code is incorrect at all.
You can't know OrderTicket(), OrderOpenPrice() etc. before selecting proper order.
 
Malgarty wrote >>

I would greatly appreciate any help anybody could give me on my EA. I am having a problem with the SendMail function, I receive the e-mail but for order open price I receive 0.0000.

The other problem I am having is the OrderClose function is not closing the order. I've tried several solutions with no results.

Thank You


bool openposition = false; // Used to limit the number of open positions
int start()
{
string text = "", text2 = "";
int index=OrderTicket(); // Order Ticket # of open position
double
openprice,
SMA, // Varable for 20hr Simple Moving Avg
LO,
LC,
SO,
SC;
openprice=OrderOpenPrice();
LO = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); // 10hr sma long position buy indicator
LC = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); // 10hr sma indicator used for closing long position
SO = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); // 10hr sma short position sell indicator
SC = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); // 10hr sma indicator used for closing short position
SMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0); // 20hr sma indicator

//
if (openposition == false && LC>SMA) // If the 10hr sma crosses over the 20hr sma = buy signal
{
OrderSend(Symbol(),OP_BUY,.01,Ask,8,0,0,NULL,17,0,Green); // Sends market order to open (1) long position
SendMail("Long Position Opened ","Bought Price @ "+DoubleToStr(openprice,4)); //Sends Email with open order price

openposition = true; // Changes bool varable to true - there is an open position
}
else
{
if (SO<SMA && openposition == false) // If 10hr sma crosses under the 20hr sma = sell signal
{ // No current market position
OrderSend(Symbol(),OP_SELL,.01,Bid,8,0,0,NULL,17,0,Orange); // Sends market order to open (1) short position
SendMail("Opened Short Position","Sold Price @" +DoubleToStr(openprice,4)); // Sends an email of opening short position
}
openposition = true; // Changes bool varable to true - there is an open position
}

//---------------------------------------------------------------------------------------------------------------------------------------------------
//Order Close
//---------------------------------------------------------------------------------------------------------------------------------------------------
string Symb=Symbol(); //
double Profit=OrderProfit(); //
int Ticket=OrderTicket(); // Order ticket
double Lot=OrderLots(); //
double Price=OrderOpenPrice();
double closeprice=OrderClosePrice();
int mn=OrderMagicNumber();
//------------------------------------------------------------------------------------------------------------------------------------------------
if (OrderTicket()>0 && LC==SMA && openposition==true) // If 10hr sma crosses back under 20hr sma = close long position signal

{
OrderClose(OrderTicket(),.01,Bid,8,Green); // Closes open long position
SendMail("Long Position Closed ","Close Price" +DoubleToStr(closeprice,4)+"For a profit of"+DoubleToStr(Profit,4)); // Sends email of closing long position w/ profit
}
else
{
if (OrderTicket()>0 && SC==SMA && openposition==true) // If 10hr sma crosses back over 20hr sma = close short position signal
{
OrderClose(OrderTicket(),.01,Ask,8,Orange); // Closes open short position
SendMail("Short Position Closed ","Close Price" +DoubleToStr(closeprice,4)+"For a profit of"+DoubleToStr(Profit,4)) ; // Sends email of closing short position
}

}


return; //
}

Hello,

In your sentence : SendMail("Long Position Opened ","Bought Price @ "+DoubleToStr(openprice,4)); //Sends Email with open order price
openprice is = 0 because you haven't read an order yet and you said openprice=orderopenprice() so as you haven't read an order orderopenprice()=0. In your sendMail sentence, you should use orderopenprice() instead of openprice and you will get the open price of your current order or before the sentence initialize openprice with orderopenprice().

As far as I understand your program, you are checking if it is time to close the order you just have opened...

p.s.: Roger did give you a good advice.

 
Roger wrote >>
Read carefully about OrderSelect() command. Your code is incorrect at all.
You can't know OrderTicket(), OrderOpenPrice() etc. before selecting proper order.

Roger Thank You for your help!

Is this correct?

if (OrderSelect(OrderTicket(),SELECT_BY_TICKET)==true && LC==SMA && openposition==true) // If 10hr sma crosses back under 20hr sma = close long position signal

{
OrderClose(OrderTicket(),.01,Bid,8,Green);

 
Jacques366 wrote >>

Hello,

In your sentence : SendMail("Long Position Opened ","Bought Price @ "+DoubleToStr(openprice,4)); //Sends Email with open order price
openprice is = 0 because you haven't read an order yet and you said openprice=orderopenprice() so as you haven't read an order orderopenprice()=0. In your sendMail sentence, you should use orderopenprice() instead of openprice and you will get the open price of your current order or before the sentence initialize openprice with orderopenprice().

As far as I understand your program, you are checking if it is time to close the order you just have opened...

p.s.: Roger did give you a good advice.

Thank You for your help!

Is this how my EMAIL should read?

SendMail("Long Position Opened ","Bought Price @ "+DoubleToStr(OrderOpenPrice(),4));

As I'm sure you can tell I'm very new to this but I love it!

 
I see you definitely didn't read documentation. You don't know OrderTicket() until you select the order.
Look at this EA for example: