Orders Execution
0
Views
0
Downloads
0
Favorites
OpenOrderMarketExecution
//+------------------------------------------------------------------+
//| OpenOrderMarketExecution.mq4 |
//| Copyright © 2010, Khlystov Vladimir |
//| cmillion@narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Khlystov Vladimir"
#property link "cmillion@narod.ru"
#property show_inputs
//--------------------------------------------------------------------
extern int stoploss = 50, //óðîâåíü âûñòàâëåíèÿ SL, åñëè 0, òî SL íå âûñòàâëÿåòñÿ
takeprofit = 50, //óðîâåíü âûñòàâëåíèÿ TP, åñëè 0, òî TP íå âûñòàâëÿåòñÿ
Magic = 777; //óíèêàëüíûé íîìåð îðäåðà
extern bool SELL = true, //îòêðûòü îðäåð SELL
BUY = true; //îòêðûòü îðäåð BUY
extern double Lot = 0.0; //îáúåì îðäåðà
extern double Risk = 1; //% êîòîðûì ìû ãîòîâû ðèñêíóòü, ïðèìåíÿåòñÿ ïðè Lot=0
extern int slippage = 5; //Ìàêñèìàëüíî äîïóñòèìîå îòêëîíåíèå öåíû äëÿ ðûíî÷íûõ îðäåðîâ
extern bool MarketExecution = true; //Âûñòàâëåíèå ñòîïîâ íà ñëåäóþùåì òèêå
//--------------------------------------------------------------------
int start()
{
string txt =StringConcatenate("OpenOrderMarketExecution Copyright © 2010, cmillion@narod.ru time ",
TimeToStr(TimeCurrent(),TIME_MINUTES),"\n","-------------------------------------","\n");
if (BUY ) txt =StringConcatenate(txt,"Open BUY SL = ",DoubleToStr(Bid - stoploss*Point,Digits),
" TP = ",DoubleToStr(Ask + takeprofit*Point,Digits),"\n");
if (SELL) txt =StringConcatenate(txt,"Open SELL SL = ",DoubleToStr(Ask + stoploss*Point,Digits),
" TP = ",DoubleToStr(Bid - takeprofit*Point,Digits),"\n");
double MinLot = MarketInfo(Symbol(),MODE_MINLOT);
double Maxlot = MarketInfo(Symbol(),MODE_MAXLOT);
int OkrLOT;
if (MinLot==0.01) OkrLOT = 2;
if (MinLot >0.01) OkrLOT = 1;
if (MinLot >0.1 ) OkrLOT = 0;
if (Lot==0)
if (stoploss==0) {Comment("stoploss íå ìîæåò áûòü = 0 ïðè îïðåäåëåíèè % ðèñêà");return;}
else Lot = NormalizeDouble(AccountFreeMargin()*Risk/100/MarketInfo(Symbol(),MODE_TICKVALUE)/stoploss,OkrLOT);
if (Lot<MinLot) {Lot=MinLot;txt =StringConcatenate(txt,"Âûñòàâëåí ìèíèìàëüíûé ");}
if (Lot>Maxlot) {Lot=Maxlot;txt =StringConcatenate(txt,"Âûñòàâëåí ìàêñèìàëüíûé ");}
txt =StringConcatenate(txt,"Lot = ",Lot);
Comment(txt);
//---
double SL,TP;
if (BUY)
{
if (takeprofit!=0) TP = NormalizeDouble(Ask + takeprofit*Point,Digits); else TP=0;
if (stoploss!=0) SL = NormalizeDouble(Bid - stoploss*Point,Digits); else SL=0;
OPENORDER (1,SL,TP);
}
if (SELL)
{
if (takeprofit!=0) TP = NormalizeDouble(Bid - takeprofit*Point,Digits); else TP=0;
if (stoploss!=0) SL = NormalizeDouble(Ask + stoploss*Point,Digits); else SL=0;
OPENORDER (-1,SL,TP);
}
while(MarketExecution)
{
if (stoploss==0 && takeprofit==0) break;
else
{
if (SetStop()==0) break;
}
}
return(0);
}
//--------------------------------------------------------------------
void OPENORDER(int ord,double SL,double TP)
{
int error,err;
if (MarketExecution) {SL=0;TP=0;}
while (true)
{ error=true;
if (ord== 1) error=OrderSend(Symbol(),OP_BUY, Lot,NormalizeDouble(Ask,Digits),
slippage,SL,TP,"BUY",Magic,0,Blue);
if (ord==-1) error=OrderSend(Symbol(),OP_SELL,Lot,NormalizeDouble(Bid,Digits),
slippage,SL,TP,"SELL",Magic,0,Red);
if (error==-1)
{
Print(Symbol()," ",ShowERROR());
err++;Sleep(2000);RefreshRates();
}
if (error || err >10) return;
}
return;
}
//--------------------------------------------------------------------
string ShowERROR()
{
int err=GetLastError();
string com;
switch ( err )
{
case 0: return;
case 1: return;
case 2: com=StringConcatenate(com," Íåò ñâÿçè ñ òîðãîâûì ñåðâåðîì ");break;
case 3: com=StringConcatenate(com," Íåïðàâèëüíûå ïàðàìåòðû ");break;
case 130: com=StringConcatenate(com," Áëèçêèå ñòîïû ");break;
case 134: com=StringConcatenate(com," Íåäîñòàòî÷íî äåíåã ");break;
case 136: com=StringConcatenate(com," Íåò öåí ");break;
case 146: com=StringConcatenate(com," Ïîäñèñòåìà òîðãîâëè çàíÿòà ");break;
case 129: com=StringConcatenate(com," Íåïðàâèëüíàÿ öåíà ");break;
case 131: com=StringConcatenate(com," Íåïðàâèëüíûé îáúåì ");break;
case 137: com=StringConcatenate(com," Áðîêåð çàíÿò ");break;
case 138: com=StringConcatenate(com," Íîâûå öåíû ");break;
default: com=StringConcatenate(com," Error " ,err);break;
}
return(com);
}
//--------------------------------------------------------------------
int SetStop()
{ int tip,Ticket,n=0;
double SL,TP;
double OOP;bool error;
int SPREAD = MarketInfo(Symbol(),MODE_SPREAD);
for (int i=0; i<OrdersTotal(); i++)
{ if (OrderSelect(i, SELECT_BY_POS)==true)
{ tip = OrderType();
if (tip<2 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if ((OrderStopLoss()==0&&stoploss!=0)||(OrderTakeProfit()==0&&takeprofit!=0))
{
OOP = OrderOpenPrice();
Ticket = OrderTicket();
if (tip==OP_BUY)
{
if (takeprofit!=0) TP = NormalizeDouble(OOP + takeprofit*Point,Digits); else TP=0;
if (stoploss!=0) SL = NormalizeDouble(OOP - (stoploss+SPREAD)* Point,Digits); else SL=0;
error=OrderModify(Ticket,OOP,SL,TP,0,White);
if (error) Print("SetStop ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));
else {Print(Symbol()," ",ShowERROR());n++;}
}
if (tip==OP_SELL)
{
if (takeprofit!=0) TP = NormalizeDouble(OOP - takeprofit*Point,Digits); else TP=0;
if (stoploss!=0) SL = NormalizeDouble(OOP + (stoploss+SPREAD)* Point,Digits); else SL=0;
error=OrderModify(Ticket,OOP,SL,TP,0,White);
if (error) Print("SetStop ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));
else {Print(Symbol()," ",ShowERROR());n++;}
}
}
}
}
}
return(n);
}
//--------------------------------------------------------------------
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
---