//+------------------------------------------------------------------+
//|                           Trigger Bar.mq4                         |
//|                         Copyright © 2009, TradeForexFx            |
//|                                                                   |
//| Where the arrows appear,
//|  I need a "DASH" X pips above the high of the up bar
//|  or X pips below the low of the down bar.
//|  Immediately at the open of the next bar,
//|  I need a comment to appear stating the price
//|  at which the "DASH" was placed,
//|  "Buy at" for a bar with up arrow and
//|  "Sell at" for a bar with a down arrow.
//|  Also these values be saved into global variables 
//|  for "Buy" and "Sell" with latest values
//+------------------------------------------------------------------+

#property copyright "Copyright © 2009, TradeForexFx"
#property link      "http://"

//#property indicator_separate_window

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red

//---- input parameters

extern int     MA_Period =60;
extern string  MA_MODE_ = "0 = SMA, 1 = EMA, 2 = SMMA, 3 = LWMA";
extern int     MA_Mode =1;

extern bool    ShowComments = True;

extern int     PriceGapX = 3;
extern color   BuyColor = Aqua;
extern color   SellColor = Yellow;

datetime LineShiftTime;
string strTradeType;
string ObjectID = "TBMAInd_";
double myPoint;
bool Buy=false, Sell=false;
double Buy1, Sell1;

string      gBuyAtName;
string      gSellAtName;

//---- buffers
double up[];
double dn[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexBuffer(0, up);
   SetIndexArrow(0, 233);

   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexBuffer(1, dn);
   SetIndexArrow(1, 234);
   
     myPoint = SetPoint();
     GetGlobalVars();

//----
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
   DeleteMyObjects();
   
//----
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

  
   
 int start() {

   int limit, i, counter;
   int counted_bars=IndicatorCounted();
   double spread;
   double band;
   double Range, AvgRange;
   double EMA60; 
   //= iMA(NULL,0,60,0,MODE_EMA,PRICE_CLOSE,1); 
   
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;
   
   if (!NewBar()) return(0); // wait until first tick after bar close to take any action; 
   
//----
//   for(int i = 0; i < limit; i++)
//   {
   
     for(i = limit; i >= 0; i--) {
   
      counter=i;
      Range=0;
      AvgRange=0;

      for (counter=i ;counter<=i+9;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;  



    EMA60 = iMA(NULL,0,MA_Period,0,MA_Mode,PRICE_CLOSE,i);  
   
      
      up[i] = 0;
      dn[i] = 0;
      
//    if(Low[i] < Low[i+1] )    
      if(Low[i] < Low[i+1] && Low[i+1] > EMA60)
      {
      //   dn[i] = Low[i];
         up[i] = Low[i] - Range*0.5;
         Buy = true;
         Sell = false;
      }
      else 
 //     if(High[i] > High[i+1])
      if(High[i] > High[i+1] && High[i+1] < EMA60 )
      {
    //     up[i] = Low[i];
         dn[i] = High[i] + Range*0.5;
         Sell = true;
         Buy = false;
      }
//---- Set line labels on chart window
      if (Buy)
      {
         Buy1 = High[i] + PriceGapX * myPoint;
         SaveBuyAt(Buy1);
         strTradeType = "BUY at " + DoubleToStr(Buy1,Digits);
         Comment (strTradeType);
         LineShiftTime = Time[0];
         DisplayLine("BuyAt", Buy1, BuyColor);
         RemoveLine("SellAt");
         Buy = false;
      }
      if (Sell)
      {
         Sell1 = Low[i] - PriceGapX * myPoint;
         SaveSellAt(Sell1);
         strTradeType = "SELL at " + DoubleToStr(Sell1,Digits);
         Comment (strTradeType);
         LineShiftTime = Time[0];
//---- Set line labels on chart window
         DisplayLine("SellAt", Sell1, SellColor);
         RemoveLine("BuyAt");
         Sell = false;
      }
   }
//----
   return(0);
}
//+------------------------------------------------------------------+

bool NewBar()
{
   static datetime dt = 0;
   
   if (Time[0] != dt)
   {
      dt = Time[0];
      return(true);
   }
   return(false);
}

double SetPoint()
{
   double mPoint;
   
   if (Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

//---  Draw  Pivot lines on chart
void DisplayLine(string LineName, double LinePos, color LineColor)
{
   string myLineName;
   
   myLineName = ObjectID + LineName;
   
   if(ObjectFind(myLineName) != 0)
   {
      ObjectCreate(myLineName, OBJ_HLINE, 0, LineShiftTime, LinePos);
      ObjectSet(myLineName, OBJPROP_STYLE, STYLE_DASHDOTDOT);
      ObjectSet(myLineName, OBJPROP_COLOR, LineColor);
      ObjectSet(myLineName, OBJPROP_WIDTH, 1);
   }
   else
   {
      ObjectMove(myLineName, 0, LineShiftTime, LinePos);
   }
}

void DeleteMyObjects()
{
   ObjectDelete(ObjectID + "BuyAt");
   ObjectDelete(ObjectID + "SellAt");
}

void RemoveLine(string LineName)
{
   ObjectDelete(ObjectID + LineName);
}

string tf2txt(int tf)
{
   switch(tf)
   {
      case PERIOD_M1: return("M1");
      case PERIOD_M5: return("M5");
      case PERIOD_M15: return("M15");
      case PERIOD_M30: return("M30");
      case PERIOD_H1: return("H1");
      case PERIOD_H4: return("H4");
      case PERIOD_D1: return("D1");
      case PERIOD_W1: return("W1");
      case PERIOD_MN1: return("MN");
   
   }
   return("??");
}


void GetGlobalVars()
{
   NameGlobalVars();
   InitGlobalVars();
   GetGlobalVarValues();
}
  
void GetGlobalVarValues()
{

// Booleans stored as double so convert to boolean
// > 0 true, < 0 false

    
    Buy1 = GlobalVariableGet(gBuyAtName);
    Sell1 = GlobalVariableGet(gSellAtName);
    

}

void InitGlobalVars()
{

// check variable before use

  if(!GlobalVariableCheck(gBuyAtName))
    GlobalVariableSet(gBuyAtName,0);
    
  if(!GlobalVariableCheck(gSellAtName))
    GlobalVariableSet(gSellAtName,0);
    
}

void NameGlobalVars()
{
   string  prefix;

   prefix = ObjectID + Symbol() +  tf2txt(Period());
   gBuyAtName = prefix + "_BuyAt";
   gSellAtName = prefix + "_SellAt";
}

void SaveBuyAt(double myVal)
{
    GlobalVariableSet(gBuyAtName,myVal);
}
    
void SaveSellAt(double myVal)
{
    GlobalVariableSet(gSellAtName,myVal);
}

//+------------------------------------------------------------------+
/*

void ShowComments()
{
   int Total;
   double QtyOrders = 0.00;
   double QtyOrderSells = 0.00;

   Total = OrdersTotal();
   
   //Check position
   bool IsTrade = False;

// Better to do this from highest to lowest
//   for (int i = 0; i < Total; i ++) {
   for (int i = Total - 1; i >= 0; i --) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
//*******************************************
// Important to have code check MagicNumber and Symbol

      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != MagicNumber) continue;





      
      if(OrderType() <= OP_SELL)
      {
         IsTrade = True;
         
         if(OrderType() == OP_BUY) QtyOrders = OrderLots();
       
         if(OrderType() == OP_SELL) QtyOrderSells = OrderLots();
       
Comment (
          "Quantity in trade - Buys ", QtyOrders, "\n",
          "Quantity in trade - Sells", QtyOrderSells
          //, "\n",
       
         );
       
    }}}        
 

*/

