//+------------------------------------------------------------------+
//| Закрытие части позиции по условию взятии части профита.mq4 |
//| А также виртуальные SL и ТР |
//| Copyright 2013, MetaQuotes Software Corp. |
//| http://www.mункцql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link "http://www.mункцql5.com"
#property version "1.00"
#property strict
input double Lots = 0.02; // Лот
static input string ClosePart = " - = Настройка частичного закрытия ордеров = -";
input int StopLoss = 300; // Стоп лосс
input int TakeProfit = 600; // Тейк профит
input double PartCloseLots = 0.5; // Сколько закрывать
input int Slip = 10; // Проскальзование
static input int Magic = 1961; // Magic
//---
int posType = -1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
CloseOrder();
}
//+------------------------------------------------------------------+
//| Проверка на условие закрытия PartTakeProfit и само закрытие поз |
//+------------------------------------------------------------------+
void CloseOrder()
{
int ticket;
double lot = 0;
for(int i=0; i<OrdersTotal(); i++)
{
ticket = OrderTicket();
if(!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
if(OrderSymbol() != Symbol()) continue;
if(OrderMagicNumber()!=Magic) continue;
if(OrderType() == OP_BUY)
{
if(Bid - OrderOpenPrice() >= StopLoss*Point) //часть ордера при TP=SL
{
lot = NormalizeDouble(MathCeil(OrderLots()*100*PartCloseLots)/100,2);
ClosePosition(ticket,lot);
}
if(Bid - OrderOpenPrice() >= TakeProfit*Point) // весь ордер по ТР
ClosePosition(ticket,OrderLots());
if(OrderOpenPrice() - Bid >= StopLoss*Point) // весь ордер по SL
ClosePosition(ticket,OrderLots());
}
if(OrderType() == OP_SELL)
{
if(OrderOpenPrice() - Ask >= StopLoss*Point) //часть ордера при TP=SL
{
lot = NormalizeDouble(MathCeil(OrderLots()*100*PartCloseLots)/100,2);
ClosePosition(ticket,lot);
}
if(OrderOpenPrice() - Ask >= TakeProfit*Point) // весь ордер по ТР
ClosePosition(ticket,OrderLots());
if( Ask - OrderOpenPrice() >= StopLoss*Point) // весь ордер по SL
ClosePosition(ticket,OrderLots());
}
}
}
//+------------------------------------------------------------------+
//| Функция закрытия позиций |
//+------------------------------------------------------------------+
void ClosePosition(int tic, double lot)
{
bool closed;
int lastError=0;
for(int i=0; i<5; i++)
{
double price=MarketInfo(Symbol(),posType==OP_BUY ? MODE_BID : MODE_ASK);
closed=OrderClose(tic,lot,price,Slip,clrYellow);
if(!closed)
lastError=GetLastError();
if(closed) // Позиция закрыта
break; // Выходим из цикла
if(lastError==4108) // Не верный номер тикета
break; // Выходим из цикла
Sleep(100);
Print("Close Position retry no: "+IntegerToString(i+2));
}
}
//+------------------------------------------------------------------+
kvashnin007