//+------------------------------------------------------------------+
//|             Copyright © 2010, Konstantin http://www.fxaktiv.com  |
//|                        panelektrik@yandex.ru skype pan--elektrik |
//|                                ISQ 561090193 Моб.+380 50 7115118 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Konstantin http://www.fxaktiv.com  panelektrik@yandex.ru"
#property link      "http://www.fxaktiv.com  panelektrik@yandex.ru  skype pan--elektrik ISQ 561090193"
//+------------------------------------------------------------------+

extern double Lot = 0.1;
extern int TP = 500;
extern int SL = 120;
extern int Trall = 30;
extern int MaxTrades = 5;

extern int diapazon = 100;
extern int timerange = 5;

extern bool Use_ECN = TRUE;
extern double maxspread = 40.0;

extern int Magic = 67776;

int stoplevel;
double buf1[50];
int buf2[50];
//+------------------------------------------------------------------+
int orders(){int j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
 if((OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))j++;}return(j);}
//+------------------------------------------------------------------+
void Send_buy(double L){int t;
 t=OrderSend(Symbol(),OP_BUY,L,Ask,10,0,0,WindowExpertName()+" fxaktiv.com",Magic,0,Blue);
 if(t<0){Sleep(5000);t=OrderSend(Symbol(),OP_BUY,L,Ask,10,0,0,WindowExpertName()+" fxaktiv.com",Magic,0,Blue);}
 if(t<0){Sleep(5000);t=OrderSend(Symbol(),OP_BUY,L,Ask,10,0,0,WindowExpertName()+" fxaktiv.com",Magic,0,Blue);}
 if(t>0&&OrderSelect(t,SELECT_BY_TICKET,MODE_TRADES)==true){
 if(OrderModify(t,OrderOpenPrice(),OrderOpenPrice()-SL*Point,OrderOpenPrice()+TP*Point,0)==true){return(0);}
 Sleep(5000);
 if(OrderModify(t,OrderOpenPrice(),OrderOpenPrice()-SL*Point,OrderOpenPrice()+TP*Point,0)==true){return(0);}
 Sleep(5000);  
 if(OrderModify(t,OrderOpenPrice(),OrderOpenPrice()-SL*Point,OrderOpenPrice()+TP*Point,0)==true){return(0);}}}
//+------------------------------------------------------------------+
void Send_sell(double L){int t;
 t=OrderSend(Symbol(),OP_SELL,L,Bid,10,0,0,WindowExpertName()+" fxaktiv.com",Magic,0,Red);
 if(t<0){Sleep(5000);t=OrderSend(Symbol(),OP_SELL,L,Bid,10,0,0,WindowExpertName()+" fxaktiv.com",Magic,0,Red);}
 if(t<0){Sleep(5000);t=OrderSend(Symbol(),OP_SELL,L,Bid,10,0,0,WindowExpertName()+" fxaktiv.com",Magic,0,Red);}
 if(t>0&&OrderSelect(t,SELECT_BY_TICKET,MODE_TRADES)==true){
 if(OrderModify(t,OrderOpenPrice(),OrderOpenPrice()+SL*Point,OrderOpenPrice()-TP*Point,0)==true){return(0);}
 Sleep(5000);
 if(OrderModify(t,OrderOpenPrice(),OrderOpenPrice()+SL*Point,OrderOpenPrice()-TP*Point,0)==true){return(0);}
 Sleep(5000); 
 if(OrderModify(t,OrderOpenPrice(),OrderOpenPrice()+SL*Point,OrderOpenPrice()-TP*Point,0)==true){return(0);}}}
//+------------------------------------------------------------------+
void modify()
 {
 double tr=Trall*Point+5*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)&&(Bid-OrderStopLoss()>tr)&&(Bid-OrderOpenPrice()>tr)){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+5*Point,OrderTakeProfit(),0);}
 if((OrderType()==OP_SELL)&&(OrderStopLoss()-Ask>tr)&&(OrderOpenPrice()-Ask>tr)){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-5*Point,OrderTakeProfit(),0);}
 }}}
//+------------------------------------------------------------------+
int start() 
 {
 stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
//+--------------- 
 SmeshTime(buf2);buf2[0]=TimeCurrent();
//+---------------   
 timefilter(buf2);//если между тиками больше 5 сек то фильтруем;
//+--------------- 
 SmeshBid(buf1);buf1[0]=Bid;
//+--------------- 
 if(orders()<MaxTrades)Signal();
//+---------------  
 modify();
 }


void timefilter(int &bf2[50]){
 int i=TimeCurrent()-(bf2[index(bf2)-1]);
 while(i>timerange){bf2[index(bf2)-1]=0;
 i=TimeCurrent()-(bf2[index(bf2)-1]);
 if(index(bf2)<2)break;}}

int index(int bf2[50]){for(int i=0;i<ArraySize(bf2);i++)if(bf2[i]==0)return(i);return(ArraySize(bf2)-1);}//количество нужных элементов массива

void SmeshBid(double &bf1[50]){for(int i=ArraySize(bf1);i>0;i--) bf1[i]=bf1[i-1];bf1[0]=0;}//смещение на 1
void SmeshTime(int &bf2[50]){for(int i=ArraySize(bf2);i>0;i--)bf2[i]=bf2[i-1];bf2[0]=0;}//смещение на 1

void Signal(){
 if(buf1[ArrayMaximum(buf1,index(buf2))]-buf1[ArrayMinimum(buf1,index(buf2))]>diapazon*Point){
 if(Bid==buf1[ArrayMaximum(buf1,index(buf2))])Send_buy(Lot);
 if(Bid==buf1[ArrayMinimum(buf1,index(buf2))])Send_sell(Lot);}}








