Nov 29, 2011 17:38
//+------------------------------------------------------------------+
//| StMartin.mq4 |
//| Sergey Kodolov |
//| 84232676421@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Sergey Kodolov"
#property link "84232676421@mail.ru"
extern int TP = 1000;
extern int TS = 1000;
extern int gap = 0;
extern double lots = 0.1;
double volumz;
int ticket,total;
int slip = 3;
int Magic = 4;
static bool HaveLines = false;
static bool warning = false;
static int CriticalTicket = 0;
int TicketArray[4];
void FormTicket(int number) //объявил шаблон под функцию запоминания тикетов
{
for(int i=3;i>0;i--)
{
TicketArray[0] = ticket;
if(TicketArray[0]>0 && TicketArray[0] == TicketArray[1])break;
TicketArray[i] = TicketArray[i-1];
}
}
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
if(OrdersHistoryTotal()==0)
{
ticket = OrderSend(Symbol(),OP_BUY,lots,Ask,slip,Bid-TS*Point,Bid+TP*Point,"First order",Magic,0,Yellow); //открываем первый ордер
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
CriticalTicketFunction();
int OrderTimeCheck = check3(); //проверяем, закрыт ли ордер
bool OrderCloseCheck = check2(OrderTimeCheck,warning); //проверяем, закрылись ли ордера в минус
DrawLines(OrderCloseCheck); //рисуем линии
FormTicket(ticket); //вызываем функцию, чтоб запоминала тикеты
OrderOpenFunction(OrderCloseCheck); //тут открываем ордера
//----
return(0);
}
//+------------------------------------------------------------------+
void CriticalTicketFunction()
{
OrderSelect(CriticalTicket,SELECT_BY_TICKET);
if(OrderStopLoss() == OrderClosePrice())
{
total = OrdersTotal();
if(CriticalTicket > 0)
{
if(OrderCloseTime()>0)
{
if(OrderClosePrice()==OrderStopLoss())
{
if(OrderType() == OP_BUY)
{
ObjectCreate("BuyLine1",OBJ_HLINE,0,0,OrderOpenPrice());
ObjectCreate("SellLine1",OBJ_HLINE,0,0,OrderClosePrice());
}
if(OrderType() == OP_SELL)
{
ObjectCreate("BuyLine1",OBJ_HLINE,0,0,OrderClosePrice());
ObjectCreate("SellLine1",OBJ_HLINE,0,0,OrderOpenPrice());
}
HaveLines = true;
}
}
}
}
}
int check3() //проверяем, закрыт ли ордер
{
OrderSelect(TicketArray[1],SELECT_BY_TICKET);
if(OrderClosePrice()==OrderTakeProfit())
{
CriticalTicket=0;
ObjectDelete("warning");
warning = false;
ObjectDelete("BuyLine1");
ObjectDelete("SellLine1");
}
if(OrderCloseTime()>0)return(100);
}
bool check2(int OrderTimeCheck, bool warning) //проверяем, если ордер закрыт, то последние 3 закрытых ордера были ли убыточными?
{
if(warning == false)
{
if(OrderTimeCheck == 100)
{
bool flag = true;
for(int j=1;j<4;j++)
{
OrderSelect(TicketArray[j],SELECT_BY_TICKET);
if(OrdersHistoryTotal()>2)
{
if(OrderClosePrice()!=OrderStopLoss())
{
flag = false;
}
}
if(OrdersHistoryTotal()<=2)
{
flag = false;
}
//Print(OrderClosePrice(),",,,,,",OrderStopLoss(),",,,,,",TicketArray[j]);
}
//Print(flag);
return(flag);
}
}
if(warning == true)return(false);
}
void DrawLines(bool OrderCloseCheck)
{
if(CriticalTicket == 0)
{
if(HaveLines == false)
{
if(OrderCloseCheck == true)
{
OrderSelect(TicketArray[3],SELECT_BY_TICKET);
if(OrderType() == OP_BUY)
{
ObjectCreate("BuyLine1",OBJ_HLINE,0,0,OrderOpenPrice());
OrderSelect(TicketArray[1],SELECT_BY_TICKET);
ObjectCreate("SellLine1",OBJ_HLINE,0,0,OrderClosePrice());
}
if(OrderType() == OP_SELL)
{
ObjectCreate("BuyLine1",OBJ_HLINE,0,0,OrderClosePrice());
OrderSelect(TicketArray[1],SELECT_BY_TICKET);
ObjectCreate("SellLine1",OBJ_HLINE,0,0,OrderOpenPrice());
}
ObjectCreate("dont torg",OBJ_LABEL,0,0,0);
ObjectSetText("dont torg","FLAT",14, "Arial", Aqua);
ObjectSet("dont torg", OBJPROP_CORNER,1);
ObjectSet("dont torg", OBJPROP_XDISTANCE,40);
ObjectSet("dont torg", OBJPROP_YDISTANCE,40);
ObjectCreate("warning",OBJ_LABEL,0,0,0);
ObjectSetText("warning","Warning",14, "Arial", Aqua);
ObjectSet("warning", OBJPROP_CORNER,1);
ObjectSet("warning", OBJPROP_XDISTANCE,40);
ObjectSet("warning", OBJPROP_YDISTANCE,20);
HaveLines = true;
warning = true;
}
}
}
}
void OrderOpenFunction(bool OrderCloseCheck)
{
total = OrdersTotal();
if(HaveLines == true)
{
if(total < 1)
{
double BuyLine, SellLine;
BuyLine = ObjectGet("BuyLine1",OBJPROP_PRICE1);
SellLine = ObjectGet("SellLine1",OBJPROP_PRICE1);
if(Ask>BuyLine+gap*Point)
{
OrderSelect(TicketArray[1],SELECT_BY_TICKET);
volumz = OrderLots();
double lot3 = volumz*2;
ticket = OrderSend(Symbol(),OP_BUY,lot3,Ask,slip,Bid-TS*Point,Bid+TP*Point,0,Magic,0,Green);
if(ticket>0)
{
CriticalTicket = ticket;
ObjectDelete("dont torg");
ObjectDelete("BuyLine1");
ObjectDelete("SellLine1");
HaveLines = false;
}
}
if(Bid0)
{
CriticalTicket = ticket;
ObjectDelete("dont torg");
ObjectDelete("BuyLine1");
ObjectDelete("SellLine1");
HaveLines = false;
}
}
}
}
else
{
if(total < 1)
{
OrderSelect(TicketArray[1],SELECT_BY_TICKET);
volumz = OrderLots();
if(OrderClosePrice() == OrderStopLoss())
{
if(OrderType() == OP_BUY)
{
double lot1 = volumz*2;
ticket = OrderSend(Symbol(),OP_SELL,lot1,Bid,slip,Ask+TS*Point,Ask-TP*Point,0,Magic,0,Red);
}
if(OrderType() == OP_SELL)
{
double lot2 = volumz*2;
ticket = OrderSend(Symbol(),OP_BUY,lot2,Ask,slip,Bid-TS*Point,Bid+TP*Point,0,Magic,0,Green);
}
}
if(OrderClosePrice() == OrderTakeProfit())
{
if(OrderType() == OP_BUY)
{
ticket = OrderSend(Symbol(),OP_BUY,lots,Ask,slip,Bid-TS*Point,Bid+TP*Point,0,Magic,0,Green);
}
if(OrderType() == OP_SELL)
{
ticket = OrderSend(Symbol(),OP_SELL,lots,Bid,slip,Ask+TS*Point,Ask-TP*Point,0,Magic,0,Red);
}
}
}
}
}