Orders Execution
Miscellaneous
0
Views
0
Downloads
0
Favorites
VirtualClose
//+------------------------------------------------------------------+
//| VirtualClose.mq4 |
//| https://login.mql5.com/ru/users/TheXpert |
//+------------------------------------------------------------------+
#property copyright "TheXpert"
#property link "https://login.mql5.com/ru/users/TheXpert"
#property show_inputs
extern color SelectionColor = Red;
extern color TextColor = White;
string StringIf(bool c, string ifTrue, string ifFalse)
{
if (c) return (ifTrue);
return (ifFalse);
}
///=========================================================================
/// GUI
///=========================================================================
void SetLabel(string name, int x, int y, string text, int size = 10, color clr = White, int corner = 1, string font = "")
{
ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
ObjectSet(name, OBJPROP_CORNER, corner);
ObjectSet(name, OBJPROP_XDISTANCE, x);
ObjectSet(name, OBJPROP_YDISTANCE, y);
ObjectSetText(name, text, size, font, clr);
}
void SetHLine(string name, double price, color clr = Red, int style = 0, int width = 2)
{
ObjectCreate(name, OBJ_HLINE, 0, 0, price);
ObjectSet(name, OBJPROP_PRICE1, price);
ObjectSet(name, OBJPROP_STYLE, style);
ObjectSet(name, OBJPROP_COLOR, clr);
ObjectSet(name, OBJPROP_WIDTH, width);
}
#define IDOK 1
#define IDCANCEL 2
bool Confirmed(string action)
{
int result = MessageBox("Do you really want to\n" + action + "?", "Please confirm", IDOK);
return (result == IDOK);
}
///=========================================================================
/// Misc
///=========================================================================
string ID()
{
static string result;
if (StringLen(result) == 0) result = "ZZCEA_close";
result = StringSetChar(result, 0, 255);
return (result);
}
int IDLen()
{
static int size = -1;
if (size == -1) size = StringLen(ID());
return (size);
}
int x = 50;
int y = 50;
int d = 20;
string items[];
int tickets[];
///=========================================================================
/// DND Implementation
///=========================================================================
bool CheckDrag(string name)
{
double x = ObjectGet(name, OBJPROP_PRICE1);
if (GetLastError() != 0) return (false);
if (ObjectSet(name, OBJPROP_PRICE1, x*1.1))
{
ObjectSet(name, OBJPROP_PRICE1, x);
return (false);
}
return (true);
}
bool CheckDrop(string name)
{
double x = ObjectGet(name, OBJPROP_PRICE1);
if (GetLastError() != 0) return (true);
if (ObjectSet(name, OBJPROP_PRICE1, x*1.1))
{
ObjectSet(name, OBJPROP_PRICE1, x);
return (true);
}
return (false);
}
bool IsDragging = false;
string DragName = "";
double DropPrice;
bool CanFinish = false;
int SelectedTicket = -1;
void OnDropped(string name)
{
IsDragging = false;
if (name == ID() + " select") OnDroppedSelect(name);
if (name == ID() + "price") OnDroppedPrice(name);
}
void OnDroppedSelect(string name)
{
if (name != ID() + " select") return;
int y = ObjectGet(name, OBJPROP_YDISTANCE);
int item = GetItem(y);
int ticket = tickets[item];
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
if (Confirmed(" close order #" + ticket))
{
CanFinish = true;
SelectedTicket = ticket;
}
}
}
void OnDroppedPrice(string name)
{
if (name != ID() + "price") return;
double p = ObjectGet(name, OBJPROP_PRICE1);
if (p > 0)
{
if (Confirmed(" close #" + SelectedTicket + " at " + DoubleToStr(p, Digits)))
{
GlobalVariableSet("_VIRTUAL_C_close_" + SelectedTicket, NormalizeDouble(p, Digits));
}
}
CanFinish = true;
}
void WhileDragging(string name)
{
if (name == ID() + " select") WhileDraggingSelect(name);
}
void WhileDraggingSelect(string name)
{
if (name != ID() + " select") return;
int y = ObjectGet(name, OBJPROP_YDISTANCE);
int item = GetItem(y);
DrawList();
if (item >= 0 && item < ArraySize(items))
{
ObjectSet(ID() + item, OBJPROP_COLOR, SelectionColor);
}
}
///=========================================================================
/// Implementation
///=========================================================================
void DrawList()
{
string label = StringSetChar(" ", 0, 235);
SetLabel(ID() + " select", x - 10, y - 10, label, 20, SelectionColor, 1, "Wingdings");
int size = ArraySize(items);
for (int i = 0; i < size; i++)
{
SetLabel(ID() + i, x + d, y + d*(i + 1), items[i], 10, TextColor);
}
int total = ObjectsTotal();
for (i = total - 1; i >= 0; i--)
{
string s = ObjectName(i);
if (s == ID() + " select") continue;
if (StringFind(s, ID()) == 0)
{
int idx = StrToInteger(StringSubstr(s, IDLen()));
if (idx >= size)
{
ObjectDelete(s);
}
}
}
}
void MakeList()
{
ArrayResize(items, 0);
ArrayResize(tickets, 0);
int total = OrdersTotal();
for (int i = 0; i < total; i++)
{
if (!OrderSelect(i, SELECT_BY_POS)) continue;
if (OrderSymbol() == Symbol())
{
int type = OrderType();
if (type == OP_BUY || type == OP_SELL)
{
string s = "#" + OrderTicket() + " ( ";
s = s + DoubleToStr(OrderLots(), 2) + StringIf(type == OP_BUY, " Buy at ", " Sell at ");
s = s + DoubleToStr(OrderOpenPrice(), Digits) + " )";
int size = ArraySize(items);
ArrayResize(items, size + 1);
ArrayResize(tickets, size + 1);
items[size] = s;
tickets[size] = OrderTicket();
}
}
}
}
int GetItem(int yPos)
{
return ((yPos - y)/d - 1);
}
int init()
{
IsDragging = false;
DragName = "";
}
int start()
{
if (IsTesting()) return (0);
while (!IsStopped() && !CanFinish)
{
if (IsDragging)
{
if (CheckDrop(DragName))
{
OnDropped(DragName);
}
else
{
WhileDragging(DragName);
}
}
else
{
MakeList();
DrawList();
if (CheckDrag(ID() + " select"))
{
DragName = ID() + " select";
IsDragging = true;
}
}
WindowRedraw();
Sleep(50);
}
int count = ObjectsTotal();
for (int i = count - 1; i >= 0; i--)
{
string name = ObjectName(i);
if (StringFind(name, ID()) != -1)
{
ObjectDelete(name);
}
}
if (SelectedTicket == -1) return (0);
CanFinish = false;
while (!IsStopped() && !CanFinish)
{
if (IsDragging)
{
if (CheckDrop(DragName))
{
OnDropped(DragName);
}
}
else
{
double p = (WindowPriceMax() + WindowPriceMin())/2.0;
SetHLine(ID() + "price", p, SelectionColor);
if (CheckDrag(ID() + "price"))
{
DragName = ID() + "price";
IsDragging = true;
}
}
WindowRedraw();
Sleep(50);
}
count = ObjectsTotal();
for (i = count - 1; i >= 0; i--)
{
name = ObjectName(i);
if (StringFind(name, ID()) != -1)
{
ObjectDelete(name);
}
}
return(0);
}
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
---