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

 extern int Magic = 8907;
 extern double lot = 0.1;
 extern int tp = 10000;
 extern int otstupSL = 0;  //отступ для выставления стоп лосса от края свечи
 extern int N = 50;        //если свеча больше на N то открываем слелку
 extern int trallsl= 1000; //стартовое значение трейлинга
 extern double k = 1;      //при убыточной сделке коэффициент добавления лота, если 0 то функция отключена
 extern double ktral=0.4;  //коэффициент добавления трала? если 0 то функция плавающего трала выключена
 extern int mL = 2;  //если минимальный лот у брокера 0.01 то mL = 2, если 0.1 то mL = 1, если 1 то mL = 0
  
 int TrallSL,trade,tiket,l,min;
 double Lot;
//+------------------------------------------------------------------+ 
void init(){if((IsTesting()==false)&&(IsOptimization()==false)&&(IsVisualMode()==false))
 {trade=GlobalVariableGet("trade"+Magic);
 tiket=GlobalVariableGet("tiket"+Magic);
 l=GlobalVariableGet("l"+Magic);
 Lot=GlobalVariableGet("Lot"+Magic);
 TrallSL=GlobalVariableGet("TrallSL"+Magic);}}
//+------------------------------------------------------------------+
void Send_buy(){RefreshRates();if(orders_buy()==0){tiket=OrderSend(Symbol(),OP_BUY,Lot,Ask,10,Low[1]-otstupSL*Point,Ask+tp*Point,"BUY",Magic,0,Blue);}}
//+------------------------------------------------------------------+  
void Send_sell(){RefreshRates();if(orders_sell()==0){tiket=OrderSend(Symbol(),OP_SELL,Lot,Bid,10,High[1]+otstupSL*Point,Bid-tp*Point,"SELL",Magic,0,Red);}}
//+------------------------------------------------------------------+
int orders_buy(){int j=0;for (int i=0;i<OrdersTotal();i++)
 {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderType()==OP_BUY)&&(OrderMagicNumber()==Magic))j++;}return(j);}
//+------------------------------------------------------------------+
int orders_sell(){int j=0;for (int i=0;i<OrdersTotal();i++)
 {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderType()==OP_SELL)&&(OrderMagicNumber()==Magic))j++;}return(j);}
//+------------------------------------------------------------------+
 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);}
//+------------------------------------------------------------------+ 
 void lots()
 {if(tiket==0)Lot=lot;if(OrderSelect(tiket,SELECT_BY_TICKET,MODE_HISTORY)==true)
 {if((OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))
 {if(OrderProfit()>=0)Lot=lot;if(OrderProfit()<0)Lot=NormalizeDouble(Lot+lot*k,mL);}}}
//+------------------------------------------------------------------+ 
void Trall()
 {
 if(orders_buy()+orders_sell()==0)TrallSL=trallsl;
//+----------  
 if(orders_buy()>0){if(OrderStopLosS("BUY")>=OrderOpenPricE("BUY"))
 {TrallSL=0;TrallSL=trallsl+trallsl*(((Bid-OrderOpenPricE("BUY"))/(trallsl*Point))*ktral);}}
//+----------  
 if(orders_sell()>0){if(OrderStopLosS("SELL")<=OrderOpenPricE("SELL"))
 {TrallSL=0;TrallSL=trallsl+trallsl*(((OrderOpenPricE("SELL")-Ask)/(trallsl*Point))*ktral);}}
 }
//+------------------------------------------------------------------+
 int start()
 {if(orders_buy()+orders_sell()>0)trade=Time[0];
 int stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);Trall();
 if(orders_buy()>0)if(Bid-OrderOpenPricE("BUY")>TrallSL*Point)if(Bid-OrderStopLosS("BUY")>TrallSL*Point){modify_sl_buy("BUY");}
 if(orders_sell()>0)if(OrderOpenPricE("SELL")-Ask>TrallSL*Point)if(OrderStopLosS("SELL")-Ask>TrallSL*Point){modify_sl_sell("SELL");}
//+---------- 
 if((Time[0]!=trade)&&(orders_buy()+orders_sell()==0)&&(MathAbs(Open[1]-Close[1])>MathAbs(Open[2]-Close[2])+N*Point))
 {
 if(l==0){lots();l=1;}
 if(Close[1]-Open[1]>0)Send_buy();
 if(Open[1]-Close[1]>0)Send_sell();
 if(orders_buy()+orders_sell()>0){trade=Time[0];l=0;}
 }
//+----------  
 if((IsTesting()==false)&&(IsOptimization()==false)&&(IsVisualMode()==false)&&(Minute()!=min))
 {GlobalVariableSet("trade"+Magic,trade);
 GlobalVariableSet("tiket"+Magic,tiket);
 GlobalVariableSet("l"+Magic,l);
 GlobalVariableSet("Lot"+Magic,Lot);
 GlobalVariableSet("TrallSL"+Magic,TrallSL);
 min=Minute();}
 Comment("Trall = "+TrallSL);
 }
//+------------------------------------------------------------------+ 