//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, STARIGIN KONSTANTIN panelektrik@yandex.ru"
#property link      "http://pan-elektrik.inmarket.biz"

 extern int Magic = 984534;
 extern double Lot = 0.01;
 extern int bars = 1;  
 extern double period = 0.0052; 
 
 extern string __="يàًٌٍîéêè ىîنèôèêàِèè"; 
 extern int SL = 1500;                 //رٍîï ëîٌٌ
 extern int TP = 2000;                 //زهéê ïًîôèٍ
 extern int Bezub = 2000;               //ؤèٌٍàيِèے ًٌàلîٍêè لهçَلûٍêà
 extern int BezubPlus = 20;            //سًîâهيü ïًîôèٍà ïًè لهçَلûٍêه
 extern int Trall = 2400;              //ؤèٌٍàيِèے ًٍàëà
 extern int ShagTrall = 50;            //طàم ًٍàëà 
 
 extern bool LOTS = true; 
//+------------------------------------------------------------------+
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_buy(){OrderSend(Symbol(),OP_BUY,Lots(),Ask,10,Bid-SL*Point,Ask+TP*Point,WindowExpertName(),Magic,0,Blue);}
//+------------------------------------------------------------------+  
void Send_sell(){OrderSend(Symbol(),OP_SELL,Lots(),Bid,10,Ask+SL*Point,Bid-TP*Point,WindowExpertName(),Magic,0,Red);}
//+------------------------------------------------------------------+
void ALLCloseBUY(){RefreshRates();for(int i=OrdersTotal(); i+1>=0; i--){
 if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderType()==OP_BUY)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))
 OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(Symbol(),MODE_SPREAD));}}
//+------------------------------------------------------------------+
void ALLCloseSELL(){RefreshRates();for(int i=OrdersTotal(); i+1>=0; i--){
 if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderType()==OP_SELL)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))
 OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(Symbol(),MODE_SPREAD));}}
//+------------------------------------------------------------------+
double Sdelka(){double j=0;int t=0;RefreshRates();for(int i=OrdersHistoryTotal(); i+1>=0; i--){
 if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol())){
 if(OrderOpenTime()>t){t=OrderOpenTime();j=OrderProfit();}}}return(j);}
//+------------------------------------------------------------------+
double Lots(){double lot=Lot;if((Sdelka()<0)&&(LOTS==true)){lot=lot*2;return(lot);}return(lot);}
//+------------------------------------------------------------------+
void modify()
 {
 double bz=Bezub*Point+BezubPlus*Point+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; 
 double tr=Trall*Point+ShagTrall*Point+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
 for(int i=0;i<OrdersTotal();i++)
 {
 if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))
 {
 if((OrderType()==OP_BUY)&&(OrderStopLoss()<OrderOpenPrice())&&(Bid-OrderOpenPrice()>bz)){OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+BezubPlus*Point,OrderTakeProfit(),0);}
 if((OrderType()==OP_SELL)&&(OrderStopLoss()>OrderOpenPrice())&&(OrderOpenPrice()-Ask>bz)){OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-BezubPlus*Point,OrderTakeProfit(),0);}
 if((OrderType()==OP_BUY)&&(Bid-OrderStopLoss()>tr)&&(Bid-OrderOpenPrice()>tr)){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+ShagTrall*Point,OrderTakeProfit(),0);}
 if((OrderType()==OP_SELL)&&(OrderStopLoss()-Ask>tr)&&(OrderOpenPrice()-Ask>tr)){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-ShagTrall*Point,OrderTakeProfit(),0);}
 }}}
//+------------------------------------------------------------------+
 int start()
 {
//ووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووو+
 static int time; if(Time[0]==time)return(0);time=Time[0];   
//ووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووووو+  
 double H=iCustom(NULL,0,"supertrend",0,1);
 double L=iCustom(NULL,0,"supertrend",1,1); 
//+---------
 if((H<1000)){ALLCloseSELL();if(orders(OP_BUY)+orders(OP_SELL)==0)Send_buy();}
 if((L<1000)){ALLCloseBUY();if(orders(OP_BUY)+orders(OP_SELL)==0)Send_sell();}
//+--------- 
 modify();
 }
//+------------------------------------------------------------------+