Orders Execution
Miscellaneous
0
Views
0
Downloads
0
Favorites
DeGrid
//+------------------------------------------------------------------+
//| DeGrid.mq4 |
//| Copyright 2020, Maxim Kuznetsov |
//| https://www.luxtrade.tk |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Maxim Kuznetsov"
#property link "https://www.luxtrade.tk"
#property description "Simple helper for close grid"
#property version "1.00"
#property strict
//--- input parameters
input bool CLOSE_GRID=false;
const int TimerSeconds=10;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
datetime LastTimerTime=0;
void OnTick()
{
if (LastTimerTime==0 || LastTimerTime+TimerSeconds<=TimeLocal()) {
OnTickTimer();
}
}
void OnTickTimer()
{
LastTimerTime=TimeLocal();
int preventToCloseBuy=-1;
int preventToCloseSell=-1;
if (CheckGridForClose(_Symbol,preventToCloseBuy,preventToCloseSell)) {
if (CLOSE_GRID) {
DeGrid(_Symbol,preventToCloseBuy,preventToCloseSell);
} else {
PrintFormat("Grid %s should be close!!!",_Symbol);
PlaySound("alert.wav");
}
}
}
bool CheckGridForClose(string symbol,int &bestTicketBuy,int &bestTicketSell)
{
// int bestTicket=-1;
double bestProfit[2]={0};
double bestPrice[2]={0};
int bestTicket[2]={-1,-1};
int count[2]={0};
double sumProfit[2]={0};
bestPrice[OP_BUY]=DBL_MAX;
bestPrice[OP_SELL]=DBL_MIN;
bestTicketBuy=-1;
bestTicketSell=-1;
for(int pos=OrdersTotal()-1;pos>=0;pos--) {
if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;
if (OrderSymbol()!=symbol) continue;
if (OrderCloseTime()!=0) continue;
int type=OrderType();
if (type!=OP_BUY && type!=OP_SELL) continue;
double profit=OrderProfit()+OrderCommission()+OrderSwap();
if ( (type==OP_BUY && OrderOpenPrice()<bestPrice[type]) ||
(type==OP_SELL && OrderOpenPrice()>bestPrice[type])) {
bestTicket[type]=OrderTicket();
bestPrice[type]=OrderOpenPrice();
bestProfit[type]=profit;
}
sumProfit[type]+=profit;
count[type]++;
}
bool ret=false;
if (count[OP_BUY]>1 && sumProfit[OP_BUY]-bestProfit[OP_BUY]>=0) {
ret=true;
bestTicketBuy=bestTicket[OP_BUY];
}
if (count[OP_SELL]>1 && sumProfit[OP_SELL]-bestProfit[OP_SELL]>=0) {
ret=true;
bestTicketBuy=bestTicket[OP_SELL];
}
return ret;
}
void DeGrid(string symbol,int preventToCloseBuy,int preventToCloseSell)
{
for(int pos=OrdersTotal()-1;pos>=0;pos--) {
if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;
if (OrderSymbol()!=symbol) continue;
if (OrderCloseTime()!=0) continue;
int type=OrderType();
int ticket=OrderTicket();
double lots=OrderLots();
if (type!=OP_BUY && type!=OP_SELL) continue;
if (type==OP_BUY && (preventToCloseBuy==-1 || ticket==preventToCloseBuy)) continue;
if (type==OP_SELL && (preventToCloseSell==-1 || ticket==preventToCloseSell)) continue;
for(int attempt=0;attempt<3;attempt++) {
if (!OrderClose(ticket,lots,(type==OP_BUY?Bid:Ask),5)) {
Sleep(50);
RefreshRates();
}
}
}
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---