Orders Execution
Miscellaneous
0
Views
0
Downloads
0
Favorites
cm-CloseLock
//+------------------------------------------------------------------+
//| Copyright © 2014, Vladimir Hlystov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, http://cmillion.narod.ru"
#property link "cmillion@narod.ru"
//-------------------------------------------------------------------
extern int CloseProfit = 0; //çàêðûâàòü ïî ñóììàðíîìó ïðîôèòó
extern int Magic = 0; //-1 âñå îðäåðà
//-------------------------------------------------------------------
string AC;
int font_size=10;
color text_color=Aqua;
//-------------------------------------------------------------------
int start()
{
if (!IsTradeAllowed())
{
DrawLABEL("Òîðãîâëÿ",0,0,0,Red,"Òîðãîâëÿ çàïðåùåíà");
return(0);
}
else DrawLABEL("Òîðãîâëÿ",0,0,0,Lime,"Òîðãîâëÿ ðàçðåøåíà");
//---
double MaxOrderBuy,MinOrderSell;
double OOP,Profit,ProfitPlus,LossBuy,LossSell;
int TicketB,TicketS,tip;
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol()==Symbol() && (Magic==OrderMagicNumber() || Magic==-1))
{
tip = OrderType();
OOP = NormalizeDouble(OrderOpenPrice(),Digits);
Profit=OrderProfit()+OrderCommission()+OrderSwap();
if (Profit>0) ProfitPlus+=Profit;
if (tip==OP_BUY)
{
if (MaxOrderBuy < OOP || MaxOrderBuy==0) {MaxOrderBuy = OOP; TicketB=OrderTicket(); LossBuy=Profit;}
}
if (tip==OP_SELL)
{
if (MinOrderSell > OOP || MinOrderSell==0) {MinOrderSell = OOP; TicketS=OrderTicket(); LossSell=Profit;}
}
}
}
}
//---
if (ProfitPlus+LossBuy>=CloseProfit && LossBuy<0)
{
if (OrderSelect(TicketB, SELECT_BY_TICKET))
{
if (OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),30,Blue))
{
CloseAll();
Text(Lime,High[0],StringConcatenate(DoubleToStr(ProfitPlus,2),DoubleToStr(LossBuy,2)),0);
}
}
}
//---
if (ProfitPlus+LossSell>=CloseProfit && LossSell<0)
{
if (OrderSelect(TicketS, SELECT_BY_TICKET))
{
if (OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),30,Red))
{
CloseAll();
Text(Lime,Low[0],StringConcatenate(DoubleToStr(ProfitPlus,2),DoubleToStr(LossSell,2)),0);
}
}
}
//---
double AB = AccountBalance();
//---
DrawLABEL("Balance" ,1,5,0,Aqua,StringConcatenate("Balance ",DoubleToStr(AB,2),AC));
DrawLABEL("Equity" ,1,5,0,Aqua,StringConcatenate("Equity ",DoubleToStr(AccountEquity(),2),AC));
DrawLABEL("FreeMargin" ,1,5,0,Aqua,StringConcatenate("FreeMargin ",DoubleToStr(AccountFreeMargin(),2),AC));
DrawLABEL("Take" ,1,5,0,Color(ProfitPlus>0,Lime,Red),StringConcatenate("Profit ",DoubleToStr(ProfitPlus,2),AC));
DrawLABEL("Óáûòî÷íûé Buy" ,1,5,0,Color(LossBuy>0,Lime,Red),StringConcatenate("Buy " ,TicketB," | ",DoubleToStr(LossBuy,2),AC));
DrawLABEL("Óáûòî÷íûé Sell" ,1,5,0,Color(LossSell>0,Lime,Red),StringConcatenate("Sell ",TicketS," | ",DoubleToStr(LossSell,2),AC));
return(0);
}
//------------------------------------------------------------------
int deinit()
{
if (!IsTesting())
{
ObjectsDeleteAll(0);
}
return(0);
}
//-------------------------------------------------------------------
void DrawLABEL(string name, int CORNER, int X, int Y, color clr, string Name)
{
if (ObjectFind(name)==-1)
{
ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
ObjectSet(name, OBJPROP_CORNER, CORNER);
ObjectSet(name, OBJPROP_XDISTANCE, X);
ObjectSet(name, OBJPROP_YDISTANCE, Y);
}
ObjectSetText(name,Name,font_size,"Arial",clr);
}
//+------------------------------------------------------------------+
int init()
{
AC = StringConcatenate(" ", AccountCurrency());
int Y=15;
DrawLABEL("Òîðãîâëÿ" ,1,5,Y,Red,"Òîðãîâëÿ ");Y += font_size*2;
DrawLABEL("Balance" ,1,5,Y,text_color,StringConcatenate("Balance ",DoubleToStr(AccountBalance(),2),AC));Y += font_size*1.5;
DrawLABEL("Equity" ,1,5,Y,text_color,StringConcatenate("Equity ",DoubleToStr(AccountEquity(),2),AC));Y += font_size*1.5;
DrawLABEL("FreeMargin",1,5,Y,text_color,StringConcatenate("FreeMargin ",DoubleToStr(AccountFreeMargin(),2),AC));Y += font_size*3;
DrawLABEL("Óáûòî÷íûé Sell" ,1,5,Y,text_color,"Buy " );Y += font_size*1.5;
DrawLABEL("Óáûòî÷íûé Buy" ,1,5,Y,text_color,"Sell ");Y += font_size*3;
DrawLABEL("Take" ,1,5,Y,Lime,"Profit ");Y += font_size*2;
return(0);
}
//+------------------------------------------------------------------+
void Text(color COLOR,double Price,string Name,int a)
{
string name = StringConcatenate(TimeToStr(TimeCurrent(),TIME_SECONDS)," ",Name);
ObjectDelete(name);
ObjectCreate(name, OBJ_TEXT,0,Time[0],Price,0,0,0,0);
ObjectSet(name, OBJPROP_ANGLE, a);
ObjectSetText(name, Name,font_size, "Times New Roman", COLOR);
}
//+------------------------------------------------------------------+
color Color(bool P,color a,color b)
{
if (P) return(a);
else return(b);
}
//------------------------------------------------------------------
bool CloseAll()
{
bool error=true;
int err,nn,OT;
double Profit;
while(true)
{
for (int j = OrdersTotal()-1; j >= 0; j--)
{
if (OrderSelect(j, SELECT_BY_POS))
{
if (OrderSymbol() == Symbol() && (Magic==OrderMagicNumber() || Magic==-1))
{
Profit=OrderProfit()+OrderCommission()+OrderSwap();
if (Profit<0) continue;
OT = OrderType();
if (OT>1) error=OrderDelete(OrderTicket());
if (OT==OP_BUY)
{
error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),30,Blue);
}
if (OT==OP_SELL)
{
error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),30,Red);
}
if (!error)
{
err = GetLastError();
if (err<2) continue;
if (err==129)
{
RefreshRates();
continue;
}
if (err==146)
{
if (IsTradeContextBusy()) Sleep(2000);
continue;
}
Print("Îøèáêà ",err," çàêðûòèÿ îðäåðà N ",OrderTicket()," ",TimeToStr(TimeCurrent(),TIME_SECONDS));
}
}
}
}
int n=0;
for (j = 0; j < OrdersTotal(); j++)
{
if (OrderSelect(j, SELECT_BY_POS))
{
if (OrderSymbol() == Symbol() && (Magic==OrderMagicNumber() || Magic==-1))
{
Profit=OrderProfit()+OrderCommission()+OrderSwap();
if (Profit<0) continue;
n++;
}
}
}
if (n==0) break;
nn++;
if (nn>10)
{
Alert(Symbol()," Íå óäàëîñü çàêðûòü âñå ñäåëêè, îñòàëîñü åùå ",n);
return(0);
}
Sleep(1000);
RefreshRates();
}
return(1);
}
//--------------------------------------------------------------------
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
---