Need help w/ News Breakout EA (closing opposite pending order when one pending order is filled)

 

 Hello Smart MQL coders,

I need some help with this News Breakout EA. Just started learning mql days ago. My idea for this EA is to create both a pending sell and pending buy order at a distance from the price at a certain news release time.

I am having problem coding to close out other opposite pending order when one pending order is filled. (SEE THE RED WORDS)

I tried to count from the pool, to see if there is an OP_SELL or OP_BUY order with the same magic number as the EA. If there is, close out the opposite pending order. 

There is also something wrong with the counting function, can anyone help me to clean that up? 

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

//|                                                News Breakout.mq4 |

//|                                                         Ryan Lee |

//|                                                                  |

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

#property copyright "Ryan Lee"

#property link      ""

#include <stdlib.mqh>



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

//| expert initialization function                                   |

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

//external parameters (add extern in front so that it can be adjusted by user in EA dialog box)



extern bool UseLocalTime = true;

extern int NewsHour = 0;

extern int NewsMinute = 0;

extern int Expiration = 1;

extern double LotSize = 0.10;

extern int PendingPips = 20;

extern int StopLoss = 20;

extern int TakeProfit = 20;

extern int Slippage = 5;

extern int MagicNumber = 123;



// Global variables



int BuyTicket;

int SellTicket;

int CloseTicket;

double UsePoint;

int UseSlippage;

int ErrorCode;











//init Function



int init()

{

UsePoint = PipPoint(Symbol());

UseSlippage = GetSlippage(Symbol(),Slippage);

}



// 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);

}





// Start Function

int start()

{







//if(UseLocalTime == true) datetime CurrentTime = TimeLocal(); // Local time, also my computer's time

//else CurrentTime = TimeCurrent(); // Server(broker platform) time



if(UseLocalTime == true) datetime CurrentTime = TimeLocal(); // Local time

else CurrentTime = TimeCurrent(); // Server time



int GetHour = TimeHour(CurrentTime);

int GetMinute = TimeMinute(CurrentTime);





if(GetHour == NewsHour &&  GetMinute== NewsMinute && BuyTicket ==0 && SellTicket==0)



{

// Buy Stop

double BuyPendingPrice = Ask + (PendingPips * UsePoint);

// Calculate stop loss and take profit

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

if(TakeProfit > 0) double BuyTakeProfit = BuyPendingPrice + (TakeProfit * UsePoint);

// Open buy stop order

BuyTicket = OrderSend(Symbol(),OP_BUYSTOP,LotSize,BuyPendingPrice,UseSlippage,

BuyStopLoss,BuyTakeProfit,"Buy Stop Order",MagicNumber,Expiration,Green);



// Sell Stop

double SellPendingPrice = Bid - (PendingPips * UsePoint);

if(StopLoss > 0) double SellStopLoss = SellPendingPrice + (StopLoss * UsePoint);

if(TakeProfit > 0) double SellTakeProfit = SellPendingPrice - (TakeProfit * UsePoint);

// Open Sell Stop order

SellTicket = OrderSend(Symbol(),OP_SELLSTOP,LotSize,SellPendingPrice,UseSlippage,

SellStopLoss,SellTakeProfit,"Sell Stop Order",MagicNumber,Expiration,Red);

}



{

int Counter;

int OrderCount;

for(Counter = 0; Counter <= OrdersTotal()-1; Counter++)

{

OrderSelect(Counter,SELECT_BY_POS);



if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())

{

OrderCount++;

}

}

return(OrderCount);

}





if(Counter > 0)

{

OrderSelect(SellTicket,SELECT_BY_TICKET);



if(OrderCloseTime() == 0 && SellTicket > 0 && OrderType() == OP_SELLSTOP)

{

bool DeletedSellStop = OrderDelete(SellTicket,Red);

if(DeletedSellStop == true) SellTicket = 0;

}

}



if(Counter > 0)

{

OrderSelect(BuyTicket,SELECT_BY_TICKET);



if(OrderCloseTime() == 0 && BuyTicket > 0 && OrderType() == OP_BUYSTOP)

{

bool DeletedBuyStop = OrderDelete(BuyTicket,Red);

if( DeletedBuyStop== true) BuyTicket = 0;

}

}

}
 
icylord:

 Hello Smart MQL coders,

I need some help with this News Breakout EA. Just started learning mql days ago. My idea for this EA is to create both a pending sell and pending buy order at a distance from the price at a certain news release time.

I am having problem coding to close out other opposite pending order when one pending order is filled. (SEE THE RED WORDS)

I tried to count from the pool, to see if there is an OP_SELL or OP_BUY order with the same magic number as the EA. If there is, close out the opposite pending order. 

There is also something wrong with the counting function, can anyone help me to clean that up? 

<CODE DELETED>

Please read some other posts before posting . . .

Please     edit    your post . . .    please use the   SRC   button to post code: How to use the   SRC   button. 

 
  1. Use SRC
  2. What are Function return values ? How do I use them ? - MQL4 forum
  3. OrderSelect(BuyTicket,SELECT_BY_TICKET);
    if(OrderCloseTime() == 0 
    You are selecting out of the order pool, not history. OCT all ALWAYS be zero
  4. return(OrderCount);
    }
    if(Counter > 0) <== This will never be done because you have already returned (above)


.

Reason: