
//+------------------------------------------------------------------+
//|                                         Heiken Ashi Smoothed.mq4 |
//+------------------------------------------------------------------+
//|                                                      mod by Raff |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                   mod by Paradox |
//|         Added Visual Backtesting for Offline Baskets.            |
//|         http://www.forexfactory.com/showthread.php?t=391229      |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 RoyalBlue
#property indicator_color3 Red
#property indicator_color4 RoyalBlue
//---- parameters
extern int MaMetod  = 2;
extern int MaPeriod = 15;
extern int MaMetod2  = 2;
extern int MaPeriod2 = 15;
extern double PipSize = 0.01;
extern double Spread = 50;
extern double BE = 150;
extern bool BEUseHighLow = false;
extern int ATRPeriod = 100;

extern int StatsFontSize = 10;
extern int StatsCorner = 1;
extern color StatsColor = LightSeaGreen;
extern color StatsNegativeColor = Red;
extern color StatsBgColor = DarkSlateGray;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
//----
int ExtCountedBars=0;
string PREFIX = "BASKET_CHART";
int ObjectId;

double TotalPips = 0;
double TotalLosingPips = 0;
double TotalWinningPips = 0;
double TradeCount = 0;
double BETradeCount = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   IndicatorBuffers(8);

   SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, Red);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, RoyalBlue);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, Red);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, RoyalBlue);
   SetIndexBuffer(3, ExtMapBuffer4);
//----
   SetIndexDrawBegin(0,5);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexBuffer(7,ExtMapBuffer8);
   start();
   
   DrawTrades();
   ShowStats();
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   DeleteObjectsByPrefix(PREFIX);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double maOpen, maClose, maLow, maHigh;
   double haOpen, haHigh, haLow, haClose;
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-ExtCountedBars-1;
   int pos2=pos;
   while(pos>=0)
     {
      maOpen=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_OPEN,pos);
      maClose=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_CLOSE,pos);
      maLow=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_LOW,pos);
      maHigh=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_HIGH,pos);

      haOpen=(ExtMapBuffer5[pos+1]+ExtMapBuffer6[pos+1])/2;
      haClose=(maOpen+maHigh+maLow+maClose)/4;
      haHigh=MathMax(maHigh, MathMax(haOpen, haClose));
      haLow=MathMin(maLow, MathMin(haOpen, haClose));

      ExtMapBuffer5[pos]=haOpen;
      ExtMapBuffer6[pos]=haClose;

 	   pos--;
     }
     
   int i;
   for(i=0; i<pos2; i++) ExtMapBuffer1[i]=iMAOnArray(ExtMapBuffer7,Bars,MaPeriod2,0,MaMetod2,i);
   for(i=0; i<pos2; i++) ExtMapBuffer2[i]=iMAOnArray(ExtMapBuffer8,Bars,MaPeriod2,0,MaMetod2,i);
   for(i=0; i<pos2; i++) ExtMapBuffer3[i]=iMAOnArray(ExtMapBuffer5,Bars,MaPeriod2,0,MaMetod2,i);
   for(i=0; i<pos2; i++) ExtMapBuffer4[i]=iMAOnArray(ExtMapBuffer6,Bars,MaPeriod2,0,MaMetod2,i);

   //DrawTrades();
   //ShowStats();
//----
   return(0);
  }
//+------------------------------------------------------------------+



void DrawTrades()
{

   TotalPips = 0;
   TotalLosingPips = 0;
   TotalWinningPips = 0;
   TradeCount = 0;
   BETradeCount = 0;

   DeleteObjectsByPrefix(PREFIX + "_TRADE");
   int lastSwitchBar;
   int direction = 0;
   double pips = 0;
   
   for (int i = 0; i < Bars - 1;i++)
   {     
      if (ExtMapBuffer4[i+1] == 0) break;
      pips = 0;
      direction = 0;
      
      bool atBE = false;
      if(ExtMapBuffer3[i+1] >= ExtMapBuffer4[i+1]  && ExtMapBuffer3[i] <= ExtMapBuffer4[i])
      {
         //Check for BE
         double entryPrice = Close[i] + Spread*PipSize;
         double exitPrice = Close[lastSwitchBar];
         if(BE > 0)
         {
            atBE = false;
            for (int j = i; j > lastSwitchBar; j--)
            {
               if(!atBE)
               {
                  if (BEUseHighLow)
                  {
                     double price = High[j];
                  }
                  else
                  {
                     price = Close[j];
                  }
                  double profit = (price - entryPrice)/PipSize;
                     
                  if (profit >= BE)
                  {  
                     //Print ("Profit ", profit, " BE BUY ", j, " ", TimeToStr(Time[j]), " price ", price, " entryPrice ", entryPrice );
                     atBE = true;
                  }
               }
               else
               {
                  if (((Low[j] - entryPrice)/PipSize) <= 0)
                  {
                     //Print ("Hit BE BUY ", j, " ", TimeToStr(Time[j]));
                     exitPrice = entryPrice;
                     lastSwitchBar = j;
                     break;
                  }
               }            
            }
         }
         pips =  (exitPrice - entryPrice)/PipSize;
         
         
         DrawOrderLine(GetUniqueName(PREFIX + "_TRADE"), Time[i], entryPrice, Time[lastSwitchBar], exitPrice, Green, DoubleToStr(pips,1), STYLE_DOT);
       
         direction = 1;
         Print (" i ", i , " Buy ", TimeToStr(Time[i]), " pips ", pips, " lastSwitchBar ", lastSwitchBar, " Last Time ", TimeToStr(Time[lastSwitchBar]) );
         
      }
      else if(ExtMapBuffer3[i+1] <= ExtMapBuffer4[i+1] && ExtMapBuffer3[i] >= ExtMapBuffer4[i])
      //else if(false)
      {
         pips = 0;
         //Check for BE
         entryPrice = Close[i] ;
         exitPrice = Close[lastSwitchBar]+ Spread*PipSize;
         
         if(BE > 0)
         {
            atBE = false;
            for (j = i; j > lastSwitchBar; j--)
            {
               if(!atBE)
               {
                  if (BEUseHighLow)
                  {
                     price = High[j];
                  }
                  else
                  {
                     price = Close[j];
                  }
                  profit = (entryPrice - (price+ Spread*PipSize))/PipSize;
                  if (profit >= BE)
                  {
                     //Print ("Profit ", profit, " BE Sell ", j, " ", TimeToStr(Time[j]), " price ", price, " entryPrice ", entryPrice );
                     atBE = true;
                  }
               }
               else
               {
                  if (((entryPrice - (High[j]+ Spread*PipSize))/PipSize) <= 0)
                  {
                     //Print (" BE SELL ", j, " ", TimeToStr(Time[j]), " High[j] ", High[j], " entryPrice ", entryPrice );
                     exitPrice = entryPrice;
                     lastSwitchBar = j;
                     break;
                  }
               }            
            }
         }
         
         pips = (entryPrice - exitPrice)/PipSize;
         DrawOrderLine(GetUniqueName(PREFIX + "_TRADE"), Time[i], entryPrice, Time[lastSwitchBar], exitPrice, Red , DoubleToStr(pips,1), STYLE_DOT);
         
         direction = -1;
         //Print (" i ", i , " Sell ", TimeToStr(Time[i]), " pips ", pips, " lastSwitchBar ", lastSwitchBar, " Last Time ", TimeToStr(Time[lastSwitchBar]) );
         
      }
      TotalPips+=pips;
      if (pips < 0)
      {  
         TradeCount++;
         TotalLosingPips+=MathAbs(pips);
         DrawLabel(GetUniqueName(PREFIX + "_TRADE"), DoubleToStr(pips,0) + "p", Time[lastSwitchBar], Close[lastSwitchBar] + ( -2.0 * (Spread*PipSize) *direction) ,StatsFontSize,"Calibri", Red,3);                  
      }
      else if (pips > 0)
      {
         TradeCount++;
         TotalWinningPips+=pips;
         DrawLabel(GetUniqueName(PREFIX + "_TRADE"), DoubleToStr(pips,0) + "p", Time[lastSwitchBar], Close[lastSwitchBar] + (-2.0 * (Spread*PipSize) *  direction),StatsFontSize,"Calibri", Green,3);                  
      }
      else if (pips == 0 && direction != 0)
      {
         TradeCount++;
         BETradeCount++;
         DrawLabel(GetUniqueName(PREFIX + "_TRADE"), "BE", Time[lastSwitchBar], Close[lastSwitchBar] + (-3.0 * (Spread*PipSize) *  direction),StatsFontSize,"Calibri", Green,3);                  
      }
      
      if (direction != 0)
      {
         direction = 0;
         lastSwitchBar = i;
      }
      
   }
}


void ShowStats()
{

   
   double sum;
   for (int i = 0; i < ATRPeriod; i++)                                
   {        
      sum = sum + (High[i] - Low[i])/PipSize;
   }
   
   DeleteObjectsByPrefix( PREFIX+"_STATSLABEL");
    
   string bg = "ggggggg";
   int y=0,dy=StatsFontSize+2,x=10;
   
   
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "Spread="+DoubleToStr(Spread,1) , StatsCorner, x, y, StatsFontSize, "Arial", StatsColor, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
   
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "BE="+DoubleToStr(BE,1) , StatsCorner, x, y, StatsFontSize, "Arial", StatsColor, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
       
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "ATR(" + ATRPeriod + ")="+DoubleToStr(sum/ATRPeriod,1) , StatsCorner, x, y, StatsFontSize, "Arial", StatsColor, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
       
       
   color c= StatsColor;
   if (ExtMapBuffer3[0] > ExtMapBuffer4[0]) c = StatsNegativeColor;
      
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "HA="+DoubleToStr((ExtMapBuffer4[0] - ExtMapBuffer3[0])/PipSize,1) , StatsCorner, x, y, StatsFontSize, "Arial", c, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
   
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "Total Trades="+DoubleToStr(TradeCount,0) , StatsCorner, x, y, StatsFontSize, "Arial", StatsColor, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
   
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "BE Trades="+DoubleToStr(BETradeCount,0) , StatsCorner, x, y, StatsFontSize, "Arial", StatsColor, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
   
   c= StatsColor;
   if (TotalPips < 0) c = StatsNegativeColor;
      
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "Net Pips="+DoubleToStr(TotalPips,1) , StatsCorner, x, y, StatsFontSize, "Arial", c, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
   
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "Win Pips="+DoubleToStr(TotalWinningPips,1) , StatsCorner, x, y, StatsFontSize, "Arial", StatsColor, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLBELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
   
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "Loss Pips="+DoubleToStr(TotalLosingPips,1) , StatsCorner, x, y, StatsFontSize, "Arial", StatsColor, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
   if (TotalLosingPips != 0)
   {
   y+=dy;DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABEL"), "PF="+DoubleToStr(TotalWinningPips/TotalLosingPips,2) , StatsCorner, x, y, StatsFontSize, "Arial", c, false);
   DrawFixedLabel(GetUniqueName(PREFIX+"_STATSLABELBG"), bg, StatsCorner, x-5, y-5, 16, "Webdings", StatsBgColor, true);
   }
       
}

 
 

void DeleteObjectsByPrefix(string Prefix)
{
   int L = StringLen(Prefix);
   int i = 0; 
   while(i < ObjectsTotal())
   {
    string ObjName = ObjectName(i);
    if(StringSubstr(ObjName, 0, L) != Prefix) 
      { 
        i++; 
        continue;
      }
    ObjectDelete(ObjName);
   }
}

string GetUniqueName(string Prefix)
{
   ObjectId++;
   return (Prefix+" "+DoubleToStr(ObjectId, 0));
}

void DrawOrderLine(string name, datetime start, double startPrice, datetime end, double endPrice, color c, string label, int style)
{
   //if (!DrawTradeLines) return;
   ObjectCreate(name, OBJ_TREND, 0, start, startPrice , end, endPrice);
   ObjectSet(name , OBJPROP_RAY, 0);
   ObjectSet(name , OBJPROP_COLOR, c);
   ObjectSet(name , OBJPROP_STYLE, style);
   ObjectSetText(name , label, 0);      
}

//+------------------------------------------------------------------
void DrawOrderArrow(string name, datetime t, double price, int arrowcode, color c, int width=1)
//+------------------------------------------------------------------
{
  if (ObjectFind(name) < 0) ObjectCreate(name, OBJ_ARROW, 0, t, price);
  else ObjectMove(name, 0, t, price);
  ObjectSet(name, OBJPROP_ARROWCODE,  arrowcode);
  ObjectSet(name, OBJPROP_COLOR, c);
  ObjectSet(name, OBJPROP_WIDTH, width);
}



//--------------------------------------------------------------------------------------
void DrawLabel(string objname, string s, int LTime, double LPrice, int FSize, string Font, color c, int width)
//--------------------------------------------------------------------------------------
{
//LTime-=15*61 * 2;
  if (ObjectFind(objname) < 0) {
    ObjectCreate(objname, OBJ_TEXT, 0, LTime, LPrice);
  } else {
    if (ObjectType(objname) == OBJ_TEXT) {
      ObjectSet(objname, OBJPROP_TIME1, LTime);
      ObjectSet(objname, OBJPROP_PRICE1, LPrice);
    }
  }

  ObjectSet(objname, OBJPROP_FONTSIZE, FSize);
  ObjectSetText(objname, s, FSize, Font, c);
} /* DrawLabel*/    



void DrawFixedLabel(string objname, string s, int Corner, int DX, int DY, int FSize, string Font, color c, bool bg)
{
   ObjectCreate(objname, OBJ_LABEL, 0, 0, 0);
   
   ObjectSet(objname, OBJPROP_CORNER, Corner);
   ObjectSet(objname, OBJPROP_XDISTANCE, DX);
   ObjectSet(objname, OBJPROP_YDISTANCE, DY);
   ObjectSet(objname,OBJPROP_BACK, bg);      
   ObjectSetText(objname, s, FSize, Font, c);
   
} 