Download MetaTrader 5

Rendering text at the computed SMA value, specific to the chart EA is on

To add comments, please log in or register
Terrence Brannon
204
Terrence Brannon  

For some reason, the labels "H4 SMA" and "H1 SMA" are not being rendered on the y-axis where the H1 SMA has been calculated. 

 Instead, they are always rendered at the exact same place on every chart that I put this EA on.

How can I get the labels to render at the computed value of the SMA?

Source code follows:

// Three Ducks

// A technique developed by Captain Currency
// Discussion thread
// http://www.trade2win.com/boards/forex/123288-3-ducks-trading-system-413.html

// Author
// Terrence Brannon
// http://iwantyoutoprosper.com/income/transient/forex-transient/three-ducks-ea

#property copyright ""
#property link      "http://iwantyoutoprosper.com/income/transient/forex-transient/three-ducks-ea"


string TradeCode="THREE_DUCKS_1.0";
double H4_SMA,H1_SMA,M5_SMA;
string H4_direction;
bool H1_confirms_H4;
double high;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {

   ObjectsDeleteAll();

   H1_confirms_H4=false;
   calculate_moving_averages();
   determine_and_confirm_direction();
   Print("H4 direction",H4_direction);
   Print("H1 confirms",H1_confirms_H4);

   return(0);

  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+-------------------------General Functions------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

int calculate_moving_averages()
  {

   int averaging_period=60;
   int ma_shift=0;
   int shift= 0;
   int mode = MODE_SMA;
   int applied_price=PRICE_CLOSE;

   H4_SMA = iMA(Symbol(), PERIOD_H4, averaging_period, ma_shift, mode, applied_price, shift);
   H1_SMA = iMA(Symbol(), PERIOD_H1, averaging_period, ma_shift, mode, applied_price, shift);
   M5_SMA = iMA(Symbol(), PERIOD_M5, averaging_period, ma_shift, mode, applied_price, shift);

   ObjectCreate("H4_SMA",OBJ_LABEL,0,Time[1],H4_SMA);
   ObjectSetText("H4_SMA","H4 SMA",14,"Tahoma",Gold);

   ObjectCreate("H1_SMA",OBJ_LABEL,0,Time[1],H1_SMA);
   ObjectSetText("H1_SMA","H1 SMA",14,"Tahoma",Blue);

   return(0);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int determine_and_confirm_direction()
  {

   if(H4_SMA>Bid)
     {
      H4_direction="sell";
      if(H1_SMA>Bid)
        {
         H1_confirms_H4=true;
         draw_low();
        }
     }

   if(H4_SMA<Bid)
     {
      H4_direction="buy";
      if(H1_SMA<Bid)
        {
         H1_confirms_H4=true;
         draw_high();
        }
     }

   if(H4_SMA==Bid)
      H4_direction="neither";

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int draw_high()
  {

   for(int i=2; i<999;++i)
     {
      double bar_max=MathMax(Open[i],Close[i]);
      if(bar_max>M5_SMA)
        {
         high=bar_max;
         int window_number=0;
         ObjectCreate("high_line",OBJ_HLINE,window_number,Time[i],high);
         break;
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int draw_low()
  {

   for(int i=2; i<999;++i)
     {
      double bar_max=MathMin(Open[i],Close[i]);
      if(bar_max<M5_SMA)
        {
         Print("The bar index that is below the SMA =",i);
         high=bar_max;
         int window_number=0;
         ObjectCreate("high_line",OBJ_HLINE,window_number,Time[i],high);
         break;
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
10561
Keith Watford  
 ObjectCreate("H4_SMA",OBJ_LABEL,0,Time[1],H4_SMA);
   ObjectSetText("H4_SMA","H4 SMA",14,"Tahoma",Gold);

OBJ_LABEL is not positioned according to time and price, it is by pixels on the x-y axis

Use OBJ_TEXT if you want to use time and price

Drazen Penic
2668
Drazen Penic  

From ObjectCreate() help:

 

Note

Objects of the OBJ_LABEL type ignore the coordinates. Use the ObjectSet() function to set up the OBJPROP_XDISTANCE and OBJPROP_YDISTANCE properties. 

To add comments, please log in or register