//+------------------------------------------------------------------+
//|                                                      KOROBKI.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, STARIGIN KONSTANTIN panelektrik@yandex.ru"
#property link      "http://pan-elektrik.inmarket.biz"
 extern string _="общие настройки";
 extern int Magic = 7645;
 extern double lot = 0.1;
 extern int otstupsl = 10;
 extern int tp = 700;
 extern int TrallSL = 50;
 extern int period = 100; 
 extern int otstup= 100; 
//+------------------------------------------------------------------+
 double high;
 double low;
 double A,B,C;
 double lotbay,lotsell;
//+------------------------------------------------------------------+ 
 int orders(int type){int j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderType()==type)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))j++;}return(j);}
//+------------------------------------------------------------------+ 
 void Send_buystop(double Lot,double cena,string comm,double SL){if(orders(OP_BUYSTOP)==0)OrderSend(Symbol(),OP_BUYSTOP,lotbay,(cena+(Ask-Bid)),10,SL,(cena+(Ask-Bid))+tp*Point,comm,Magic,0,Blue);}
//+------------------------------------------------------------------+  
 void Send_sellstop(double Lot,double cena,string comm,double SL){if(orders(OP_SELLSTOP)==0)OrderSend(Symbol(),OP_SELLSTOP,Lot,cena,10,SL,cena-tp*Point,comm,Magic,0,Red);}
//+------------------------------------------------------------------+
 void close(){for(int i=0;i<OrdersTotal();i++){if(orders(OP_BUYSTOP)+orders(OP_SELLSTOP)>0)if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))OrderDelete(OrderTicket());}}
//+------------------------------------------------------------------+
 void modify_sl_buy(string comm){for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true){if((OrderComment()==comm)&&(OrderType()==OP_BUY)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))
 {OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+(Bid-OrderStopLoss()-TrallSL*Point),OrderTakeProfit(),0);}}}}
//+------------------------------------------------------------------+
 void modify_sl_sell(string comm){for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true){if((OrderComment()==comm)&&(OrderType()==OP_SELL)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol())) 
 {OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-(OrderStopLoss()-Ask-TrallSL*Point),OrderTakeProfit(),0);}}}}
//+------------------------------------------------------------------+
 double OrderOpenPricE(string comm){double j=0;for (int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true){if((OrderComment()==comm)&&(OrderType()==OP_BUY||OrderType()==OP_SELL)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))j=OrderOpenPrice();}}return(j);}
//+------------------------------------------------------------------+
 double OrderStopLosS(string comm){double j=0;for (int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true){if((OrderComment()==comm)&&(OrderType()==OP_BUY||OrderType()==OP_SELL)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))j=OrderStopLoss();}}return(j);}
//+------------------------------------------------------------------+
 int start()
 {
 int stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
//+----------------------
 double Ma1 = iMA(NULL,0,period,0,MODE_SMA,PRICE_CLOSE,0);
 double Ma2 = iMA(NULL,0,period,0,MODE_SMA,PRICE_CLOSE,5);
//+----------------------
 lotbay=lot;lotsell=lot;
 //if(Ma1>Ma2)lotbay=lot*2;
 //if(Ma1<Ma2)lotsell=lot*2;
//+---------------------- 
 double a=0;double b=0;double c=0;
 for(int i=0;i<200;i++)
 {
 double L1=iFractals(Symbol(),0,MODE_UPPER,i);
 double L2=iFractals(Symbol(),0,MODE_LOWER,i);
 double L=MathMax(L1,L2); 
 if((L>0)&&(b>0)&&(c==0)){c=L;}
 if((L>0)&&(b==0)){b=L;}  
 }
//+---------------------- 
 if((B!=b)&&(C!=c))
 {
 if(b>c){close();if(b-Ask>=stoplevel*Point)Send_buystop(lot,b+otstup*Point,"buy",c-otstupsl*Point);if(Bid-c>=stoplevel*Point)Send_sellstop(lot,c-otstup*Point,"sell",b+otstupsl*Point);}
 if(b<c){close();if(c-Ask>=stoplevel*Point)Send_buystop(lot,c+otstup*Point,"buy",b-otstupsl*Point);if(Bid-b>=stoplevel*Point)Send_sellstop(lot,b-otstup*Point,"sell",c+otstupsl*Point);}
 B=b;C=c;
 }
 if(orders(OP_BUY)>0)if(Bid-OrderOpenPricE("buy")>TrallSL*Point)if(Bid-OrderStopLosS("buy")>MathMax(TrallSL*Point,stoplevel*Point)){modify_sl_buy("buy");}
 if(orders(OP_SELL)>0)if(OrderOpenPricE("sell")-Ask>TrallSL*Point)if(OrderStopLosS("sell")-Ask>MathMax(TrallSL*Point,stoplevel*Point)){modify_sl_sell("sell");}
 }
//+------------------------------------------------------------------+