
//+------------------------------------------------------------------+
//| CloseLot.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link "http://www.forexsystems.biz"
#property description "Simple expert advisor"
#include <stdlib.mqh>
//--- Inputs
extern int StopLoss = 500;
extern int TakeProfit = 500;
extern int TakeProfit1 = 100;
extern int TakeProfit2 = 200;
extern int TakeProfit3 = 300;
extern int OpenOrder=2; //0-off. 1-buy. 2-sell.
extern int Slip = 30;
extern double Lot = 1;
extern double Lot1 = 0.1;
extern double Lot2 = 0.2;
extern double Lot3 = 0.4;
extern int Magic = 111;
bool CloseOrd1,CloseOrd2,CloseOrd3;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void OpenPos()
{
int res=0,err=0;
double sl;
//--- sell
if(OpenOrder==2)
{
double bid=MarketInfo(NULL,MODE_BID);
if(StopLoss==0) sl=0; else sl=bid+StopLoss*Point;
res=OrderSend(Symbol(),OP_SELL,Lot,bid,Slip,sl,bid-TakeProfit*Point,"",Magic,0,Red);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
}
//--- buy
if(OpenOrder==1)
{
double ask=MarketInfo(NULL,MODE_ASK);
if(StopLoss==0) sl=0; else sl=ask-StopLoss*Point;
res=OrderSend(Symbol(),OP_BUY,Lot,ask,Slip,sl,ask+TakeProfit*Point,"",Magic,0,Blue);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
}
//---
}
//+------------------------------------------------------------------+
void CloseOrders()
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY)
{
if(Bid-OrderOpenPrice()>TakeProfit1*Point)
{
if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Bid,Slip,Blue);
CloseOrd1=false;
}
if(Bid-OrderOpenPrice()>TakeProfit2*Point)
{
if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Bid,Slip,Blue);
CloseOrd2=false;
}
if(Bid-OrderOpenPrice()>TakeProfit3*Point)
{
if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Bid,Slip,Blue);
CloseOrd3=false;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()-Ask>TakeProfit1*Point)
{
if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Ask,Slip,Red);
CloseOrd1=false;
}
if(OrderOpenPrice()-Ask>TakeProfit2*Point)
{
if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Ask,Slip,Red);
CloseOrd2=false;
}
if(OrderOpenPrice()-Ask>TakeProfit3*Point)
{
if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Ask,Slip,Red);
CloseOrd3=false;
}
}
}
}
}
}
return;
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
int p=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL) p++;
}
}
}
if(p<1)
{
CloseOrd1=true;
CloseOrd2=true;
CloseOrd3=true;
OpenPos();
}
if(p>0) CloseOrders();
//---
}
//+------------------------------------------------------------------+
AM2