I need to know what is the code for ORDER CLOSE Error Handling with MAGIC NUMBER.
I am able to do that with Order OPEN error handling.
But I need to confirm that my code is right with Order CLOSE error handling.
This is my Order OPEN error handling code for Buy Order which works fine:
//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
}
else //*** ADDED ELSE to if (Ticket >0)
{
// We had an error opening the trade so deal with it
Print("Error opening BUY order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
This is my Order CLOSE Error Handling Code for Buy Order:
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
nomoretrades = true;
InTrade = false;
Ticket = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("SELL order closed : ", OrderClosePrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
} else {
Print("Error closing BUY order : ", GetLastError());
nomoretrades = false;
InTrade = true;
}
}
else //*** ADDED ELSE to if (Ticket >0)
{
// We had an error closing the trade so deal with it
Print("Error closing BUY order : ", GetLastError());
nomoretrades = false;
InTrade = true;
}
}
}
}
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
} else {
Can Anyone tell me whether my ORDER CLOSE Error Handling Code is correct?
Also, I seem to have a problem printing the OrderClosePrice() because the price printed is wrong. I have no problems with printing the OrderOpenPrice() for the Order Opening Code.
I am able to do that with Order OPEN error handling.
But I need to confirm that my code is right with Order CLOSE error handling.
This is my Order OPEN error handling code for Buy Order which works fine:
//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
}
else //*** ADDED ELSE to if (Ticket >0)
{
// We had an error opening the trade so deal with it
Print("Error opening BUY order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
This is my Order CLOSE Error Handling Code for Buy Order:
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
nomoretrades = true;
InTrade = false;
Ticket = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("SELL order closed : ", OrderClosePrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
} else {
Print("Error closing BUY order : ", GetLastError());
nomoretrades = false;
InTrade = true;
}
}
else //*** ADDED ELSE to if (Ticket >0)
{
// We had an error closing the trade so deal with it
Print("Error closing BUY order : ", GetLastError());
nomoretrades = false;
InTrade = true;
}
}
}
}
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
} else {
Can Anyone tell me whether my ORDER CLOSE Error Handling Code is correct?
Also, I seem to have a problem printing the OrderClosePrice() because the price printed is wrong. I have no problems with printing the OrderOpenPrice() for the Order Opening Code.