//+------------------------------------------------------------------+
//|                                    Get trend with Atr script.mq4 |
//|                                  Copyright © 2010, Steve Hopwood |
//|                              http://www.hopwood3.freeserve.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Steve Hopwood"
#property link      "http://www.hopwood3.freeserve.co.uk"
#include <WinUser32.mqh>
#include <stdlib.mqh>
#define  NL    "\n"
#define  up "Trending Up"
#define  down "Trending Down"
#define  ranging "Ranging"

string   trend;
double   AtrVal;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----
   
   start();
//----
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----
   Comment("");
//----
   return(0);
}

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{
//----
   
   double PriceNow = Bid;
   double PriceThen = iOpen(NULL, PERIOD_D1, 10);
   AtrVal = iATR(NULL, PERIOD_D1, 20, 10);
   double x2AtrVal = AtrVal * 2;
   
   trend = ranging;
   double TrendLevel = NormalizeDouble(PriceThen - x2AtrVal, Digits);
   if (PriceNow > PriceThen) 
   {
      TrendLevel = NormalizeDouble(PriceThen + x2AtrVal, Digits);
      if (Ask > TrendLevel) trend = up;
   }//if (PriceNow > PriceThen) 
   
   if (PriceNow < PriceThen) 
   {
      TrendLevel = NormalizeDouble(PriceThen - x2AtrVal, Digits);
      if (Bid < TrendLevel) trend = down;
   }//if (PriceNow > PriceThen) 
   
   Comment("Price now:  " + DoubleToStr(Bid, Digits) + NL
              + "Open price 10 days ago:  " + DoubleToStr(PriceThen, Digits) + NL
              + "Atr: " + DoubleToStr(AtrVal, Digits) + NL
              + "x 2 Atr: " + DoubleToStr(x2AtrVal, Digits) + NL
              + "Trend level: " + DoubleToStr(TrendLevel, Digits) + NL
              + "Trend is: " + trend + NL
              );
   
//----
   return(0);
}
//+------------------------------------------------------------------+