#property  copyright "fxdaytrader"
#property  link      "http://ForexBaron.net"
#property  indicator_chart_window

extern int maperiod1  = 21;
extern int mashift1   = 0;
extern int mamethod1 = MODE_EMA;
extern int maprice1   = PRICE_CLOSE;

extern int maperiod2  = 55;
extern int mashift2   = 0;
extern int mamethod2 = MODE_EMA;
extern int maprice2   = PRICE_CLOSE;

extern int candleshift = 1;//0:current candle, 1:previous candle, etc.

extern double TPpips       = 100.0;
extern double SLbufferPips = 0.0;

int Multiplier;
double pips2dbl;

int init() {
 BrokerDigitAdjust(Symbol());
 return(0);
}

int deinit() {
 Comment("");
 return(0);
}


int start() {
 int limit,i,counted_bars = IndicatorCounted();

 if(counted_bars < 0) return(-1);
 if(counted_bars > 0)  counted_bars--;
 limit = Bars - counted_bars;

 for(i = limit - 1; i >= 0; i--) {

  double ma1 = iMA(Symbol(),0,maperiod1,mashift1,mamethod1,maprice1,candleshift);
  double ma2 = iMA(Symbol(),0,maperiod2,mashift2,mamethod2,maprice2,candleshift);
  double madist = MathAbs(ma1-ma2);
  double bid = MarketInfo(Symbol(),MODE_BID);
  double ask = MarketInfo(Symbol(),MODE_ASK);
  double slbuy = ask-madist-(SLbufferPips*pips2dbl);
  double slsell = bid+madist+(SLbufferPips*pips2dbl);
  double tpbuy = ask+(TPpips*pips2dbl);
  double tpsell = bid-(TPpips*pips2dbl);
  
  Comment("ma1: "+DoubleToStr(ma1,Digits)+", ma2: "+DoubleToStr(ma2,Digits)+", distance: "+DoubleToStr((madist/pips2dbl),2)+" pips"+"\n"+
          "SL for buy: "+DoubleToStr(slbuy,Digits)+", SL for sell: "+DoubleToStr(slsell,Digits)+"\n"+
          "TP for buy: "+DoubleToStr(tpbuy,Digits)+", TP for sell: "+DoubleToStr(tpsell,Digits));


 
 }//for(i = limit - 1; i >= 0; i--) {
 return(0);
}


void BrokerDigitAdjust(string symbol) {
 Multiplier = 1;
 if (MarketInfo(symbol,MODE_DIGITS) == 3 || MarketInfo(symbol,MODE_DIGITS) == 5) Multiplier = 10;
 if (MarketInfo(symbol,MODE_DIGITS) == 6) Multiplier = 100;   
 if (MarketInfo(symbol,MODE_DIGITS) == 7) Multiplier = 1000;
 pips2dbl = Multiplier*MarketInfo(symbol,MODE_POINT);
}


