#property copyright "Copyright © 2006, maloma"
#property  link     "ICQ 16-292-050"

extern double Lots       = 0.1;
extern int    StopLoss   = 50;
extern int    TakeProfit = 50;

extern int magic=965656;

int    TTFbars=15; //15=default number of bars for computation.
int    t3_period=5;
double b=0.7;
int    ppor=50;
int    mpor=-50;
int    barBegin=1000; //<1000 recommended for faster speed, 0=All bars computed & plotted.

bool   BSig=false, SSig=false;

void CheckSig()
{
 int T=iCustom(Symbol(),0,"BtTrendTrigger-T",TTFbars,t3_period,b,ppor,mpor,barBegin,1,0);
 double S0=iCustom(Symbol(),0,"BtTrendTrigger-T",TTFbars,t3_period,b,ppor,mpor,barBegin,0,0);
 double S1=iCustom(Symbol(),0,"BtTrendTrigger-T",TTFbars,t3_period,b,ppor,mpor,barBegin,0,1);
 if (T==50 && S0>50 && S1<50) {BSig=true;} else {BSig=false;}
 if (T==-50 && S0<-50 && S1>-50) {SSig=true;} else {SSig=false;}
 return(0);
}

void OpenOrders()
{
 if (BSig) OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"e-BTT",magic,0,Green);
 if (SSig) OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"e-BTT",magic,0,Yellow);
 return(0);
}

void CloseOrders()
{
 OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
 if (OrderType()==OP_BUY && SSig) OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
 if (OrderType()==OP_SELL && BSig) OrderClose(OrderTicket(),OrderLots(),Ask,3,Yellow);
 return(0);
}

int start()
{
 CheckSig();
 if (OrdersTotal()==1) CloseOrders();
 if (OrdersTotal()==0) OpenOrders();
 return(0);
}