I am getting order close error 4051

 

I am getting order close error 4051

invalid ticket for order close function.

Below is the code that I am using --- can anyone please provide a solution?

Thx

#include <stdlib.mqh>


// External Variables
extern double LotSize = 0.01;
extern double StopLoss = 500;
extern double TakeProfit = 1000;

extern int Slippage = 5;
extern int MagicNumber = 123;

// Global Variables
int BuyTicket;
int SellTicket;

double UsePoint;
int UseSlippage;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
UsePoint = PipPoint(Symbol());
UseSlippage = GetSlippage(Symbol(),Slippage);
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double FastMA = iMA(NULL,0,5,0,0,0,0);
double SlowMA = iMA(NULL,0,200,0,0,0,0);

//To Buy

if(Close[0] > SlowMA && High[1] < High[2] && High[2] < High[3] && High[3] < High[4]
&& Low[1] < Low[2] && Low[2] < Low[3] && Low[3] < Low[4] && BuyTicket == 0)

{
OrderSelect(SellTicket,SELECT_BY_TICKET);

// Close Order
if(OrderCloseTime() == 0 && SellTicket > 0)
{
double CloseLots = OrderLots();
double ClosePrice = Ask;

bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
}

double OpenPrice = Ask;

// Calculate stop loss and take profit
if(StopLoss > 0) double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);

// Open buy Order
BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,
BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);

SellTicket = 0;
}

// Sell Order
if(Close[0] < SlowMA && High[1] > High[2] && High[2] > High[3] && High[3] > High[4]
&& Low[1] > Low[2] && Low[2] > Low[3] && Low[3] > Low[4] && SellTicket == 0)
{
OrderSelect(BuyTicket,SELECT_BY_TICKET);
if(OrderCloseTime() == 0 && BuyTicket > 0)

{
CloseLots = OrderLots();
ClosePrice = Bid;

Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
}

OpenPrice = Bid;

if(StopLoss > 0) double SellStopLoss = OpenPrice + (StopLoss * UsePoint);
if(TakeProfit > 0) double SellTakeProfit = OpenPrice - (TakeProfit * UsePoint);

SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice,UseSlippage,
SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);

BuyTicket = 0;

}
return(0);
}

//Pip Point Function
double PipPoint(string Currency)
{
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
return(CalcPoint);
}

// Get Slippage Function
int GetSlippage(string Currency, int SlippagePips)
{
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
return(CalcSlippage);
}

 

Try Normalising your doubles :-

if(StopLoss > 0) double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);

should read like this :-

if(StopLoss > 0) double BuyStopLoss = NormalizeDouble(OpenPrice - (StopLoss * UsePoint),Digits);
 

OrderSelect(SellTicket,SELECT_BY_TICKET);

// Close Order
if(OrderCloseTime() == 0 && SellTicket > 0)
{
  double CloseLots = OrderLots();
  double ClosePrice = Ask;

What is SellTicket initially? It's zero, so the orderselect fails and everything after that is bogus

You don't test the orderSelect return value.

Any ticket orderSelect correctly selects will by definition be open and orderclosetime will be zero.

Once you close an order, set the ticket value to zero.

 
WHRoeder wrote >>

What is SellTicket initially? It's zero, so the orderselect fails and everything after that is bogus

You don't test the orderSelect return value.

Any ticket orderSelect correctly selects will by definition be open and orderclosetime will be zero.

Once you close an order, set the ticket value to zero.

I am a newbie. Could you please elaborate as to where and how the code needs to be altered so as to fix this issue?

This is my 1st try at this.

thx and regards,

Reason: