Indicator Help

 

Hey guys,

This is my first stab at trying to program an indicator - could someone please verify for me that it is doing what I think it should be doing?

It is supposed to calculate the slope of price over X amount of periods(rise: difference in last close and close X periods ago divided by run: amount of periods) and then display it as a line in a seperate window.

Is the code correct? Thanks

//+------------------------------------------------------------------+
//|                                                        Slope.mq4 |
//|                                         Copyright © 2012,Gulzaar |
//|                                       mailto:gulzaar88@gmail.com |
//+------------------------------------------------------------------+


#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue


//---- input parameters
extern int       Periods=9;

//---- buffers
double SlopeBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   //---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,SlopeBuffer);
   SetIndexDrawBegin(0,Periods);
   //---- name for DataWindow and indicator subwindow label
   SetIndexLabel(0,"Slope");
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   //---- 
   
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int limit;
   int counted_bars = IndicatorCounted();
   //---- 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;

   for(int i=limit; i>=0; i--)
   {
   int shift1 = i,
       shift2 = i+Periods;
   double price1 = Close[shift1],
          price2 = Close[shift2];
      

      SlopeBuffer[i] = /*rise*/ (price1 - price2) * 10000 / /*run*/ (shift2-shift1); 
   }
   
   //----
   return(0);
}
//+------------------------------------------------------------------+
 
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars;

   for(int i=limit; i>=0; i--)
   {
   int shift1 = i,
       shift2 = i+Periods;
   double price1 = Close[shift1],
          price2 = Close[shift2];
  1. Contradictory information on IndicatorCounted() - MQL4 forum No need for the decrement. Limit = Bars - 1 - counted_bars.
  2. You're look back is Periods.Thus you need:
    SetIndexDrawBegin(0,Periods);
    When counted_bars=0, i=Bars-1 so Close[shift2] = Close[Bars-1+Periods] which is beyond the limit of the arrays.
   // if (counted_bars>0) counted_bars--;
   // look back == Periods
   if (counted_bars < Periods) counted_bars = Periods;
   limit = Bars -1 - counted_bars;
   :

      SlopeBuffer[i] = /*rise*/ (price1 - price2) * 10000 / /*run*/ (shift2-shift1); 
Don't hard code numbers. 10000 will not work on JPY or metals.
      SlopeBuffer[i] = /*rise*/ (price1 - price2) /pips2dbl / /*run*/ (shift2-shift1); 
///////
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){                                             OptInitialization();
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    if (ticket < 0)
       Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET))
       Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
       Alert("OrderModify failed: ", GetLastError());
     */
 
WHRoeder:
  1. Contradictory information on IndicatorCounted() - MQL4 forum No need for the decrement. Limit = Bars - 1 - counted_bars.
  2. You're look back is Periods.Thus you need:
    When counted_bars=0, i=Bars-1 so Close[shift2] = Close[Bars-1+Periods] which is beyond the limit of the arrays.

Don't hard code numbers. 10000 will not work on JPY or metals.


Thanks!
 

dabbler:

WHRoeder:

You're look back is Periods.Thus you need:


ERROR MESSAGE 6:


"You are" <you're> found when possessive "your" expected.

Compilation aborted.

:-)

LOL.

WHRoeder coding too much, good thing he's sharing 'em.

:)

 
dabbler:

ERROR MESSAGE 6:


"You are" <you're> found when possessive "your" expected.

Compilation aborted.

:-)

 
WHRoeder:


What kind Black or Irish....
 

This is spam : https://www.mql5.com/en/forum/139579

COFFEE TO WAKE UP OWL NIGHT LONG

You can have 'em from java.com