ProRealtime code to MQL4 help needed

Post Reply
Travelfrog
Posts: 8
Joined: Sun Apr 13, 2014 6:47 pm

ProRealtime code to MQL4 help needed

Post by Travelfrog »

I am new to Empty4 and am trying to convert my code from ProRealTime into Empty4.

I get compile warning "possible loss of data due to type conversion". Can't seem to find how to correct the code.

The ProRealTime is in the blanked out rows and my MQL4 code below

Code: Select all

// c1 =abs( high - low)
   double c1=MathAbs(High[0]-Low[0]);
   // c2 = abs( close[1]-high )
   double c2=MathAbs(Close[1]-High[0]);
   // c3 = abs ( close[1] - low )
   double c3=MathAbs (Close[1]-Low[0]);
   // c4 = max(c1,c2)
   double c4=MathMax(c1,c2);
   // c5 = max(c4,c3)
   double c5=MathMax(c4,c3);
   //atr =Average[P](c5);
   atr =iMA(NULL, 0, Nbr_Periods, 0, MODE_SMA, c5, 0);
MrLong

ProRealtime code to MQL4 help needed

Post by MrLong »

Hello Travelfrog,

The code posted won't cause any problems.

Andy
Travelfrog
Posts: 8
Joined: Sun Apr 13, 2014 6:47 pm

ProRealtime code to MQL4 help needed

Post by Travelfrog »

I am not getting the indicator displayed though and only four of these warnings. Here is the for code:

Debug gives the following: Critical error "Array out of range"

Code: Select all

   for (i = Bars; i >= 0; i--) {
      TrendUp[i] = EMPTY_VALUE;
      TrendDown[i] = EMPTY_VALUE; 
   //////
   // c1 =abs( high - low)
   double c1=MathAbs(High[0]-Low[0]);
   // c2 = abs( close[1]-high )
   double c2=MathAbs(Close[1]-High[0]);
   // c3 = abs ( close[1] - low )
   double c3=MathAbs (Close[1]-Low[0]);
   // c4 = max(c1,c2)
   double c4=MathMax(c1,c2);
   // c5 = max(c4,c3)
   double c5=MathMax(c4,c3);
   //atr =Average[P](c5);
   atr =iMA(NULL, 0, Nbr_Periods, 0, MODE_SMA, c5, 0);
   //atr = iATR(NULL, 0, Nbr_Periods, i); 
   //////////////////////////////////
   // demiP = round(period/2)
   int demiP=MathRound(Nbr_Periods/2);
   // temp = 2*ExponentialAverage[demiP](close) - ExponentialAverage[period](close)
   double temp = iMA(NULL,0,demiP,0,MODE_EMA,PRICE_CLOSE,i) * 2 - iMA(NULL,0,Nbr_Periods,0,MODE_EMA,PRICE_CLOSE,i);
   // SqrtP = round(SQRT(period))
   int SqrtP = MathRound(MathSqrt(Nbr_Periods));  
   //medianPrice = ExponentialAverage[SqrtP](temp)
   medianPrice = iMA(NULL,0,SqrtP,0,1,temp,i);
   //////////////////////////////////////////////
   // up=medianPrice+aa*atr;
   // dn=medianPrice-aa*atr;
   //////////////////////////////////////////////
      up[i]=medianPrice+(Multiplier*atr);
      //Print("up: "+up[i]);
      dn[i]=medianPrice-(Multiplier*atr);
      //Print("dn: "+dn[i]);
      trend[i]=1;
   
      
      if (High[i]>up[i+1]) {
         trend[i]=1;
         if (trend[i+1] == -1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
         
      }
      else if (Low[i]<dn[i+1]) {
         trend[i]=-1;
         if (trend[i+1] == 1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
      }
      else if (trend[i+1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i+1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i+1]>0) {
         flag=1;
         //Print("flag: "+flag);
      }
      else {
         flag=0;
         //Print("flagh: "+flag);
      }
      
      if (trend[i]>0 && trend[i+1]<0) {
         flagh=1;
         //Print("flagh: "+flagh);
      }
      else {
         flagh=0;
         //Print("flagh: "+flagh);
      }
      
      if (trend[i]>0 && dn[i]<dn[i+1])
         dn[i]=dn[i+1];
      
      if (trend[i]<0 && up[i]>up[i+1])
         up[i]=up[i+1];
      
      if (flag==1)
         up[i]=medianPrice+(Multiplier*atr);
         
      if (flagh==1)
         dn[i]=medianPrice-(Multiplier*atr);
         
      //-- Draw the indicator
      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            changeOfTrend = 0;
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            changeOfTrend = 0;
         }
      }
   }
   WindowRedraw();     
//----
   return(0);
  }
//+------------------------------------------------------------------+
MrLong

ProRealtime code to MQL4 help needed

Post by MrLong »

Hi,

You will need to post your code if you would like some help.


Andy
Travelfrog
Posts: 8
Joined: Sun Apr 13, 2014 6:47 pm

ProRealtime code to MQL4 help needed

Post by Travelfrog »

Code: Select all

#property strict
#property indicator_chart_window
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_buffers 2

double TrendUp[], TrendDown[];
int changeOfTrend;
extern int Nbr_Periods=12; // MOVING AVERAGE PERIODS
extern double Multiplier = 2; //ATR MULTIPLIER
//+------------------------------------------------------------------+
//| START Custom indicator initialization function                   |
//+------------------------------------------------------------------+
int init()
  {
//--- indicator buffers mapping (indicators)
   SetIndexBuffer(0, TrendUp);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 3);
   SetIndexLabel(0, "Trend Up");
   SetIndexBuffer(1, TrendDown);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 3);
   SetIndexLabel(1, "Trend Down");
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| END Custom indicator initialization function                     |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| START Custom indicator deinitialization function                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| END Custom indicator deinitialization function                   |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| START Custom indicator iteration function                        |
//+------------------------------------------------------------------+
int start()
  {int limit, i, flag, flagh, trend[5000];
   double up[5000], dn[5000], medianPrice, atr;
   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;
   //Print(limit);
   
//----
   for (i = Bars; i >= 0; i--) {
      TrendUp[i] = EMPTY_VALUE;
      TrendDown[i] = EMPTY_VALUE; 
   //////
   // c1 =abs( high - low)
   double c1=MathAbs(High[0]-Low[0]);
   // c2 = abs( close[1]-high )
   double c2=MathAbs(Close[1]-High[0]);
   // c3 = abs ( close[1] - low )
   double c3=MathAbs (Close[1]-Low[0]);
   // c4 = max(c1,c2)
   double c4=MathMax(c1,c2);
   // c5 = max(c4,c3)
   double c5=MathMax(c4,c3);
   //atr =Average[P](c5);
   atr =iMA(NULL, 0, Nbr_Periods, 0, MODE_SMA, c5, 0);
   //////////////////////////////////
   // demiP = round(period/2)
   int demiP=MathRound(Nbr_Periods/2);
   // temp = 2*ExponentialAverage[demiP](close) - ExponentialAverage[period](close)
   double temp = iMA(NULL,0,demiP,0,MODE_EMA,PRICE_CLOSE,i) * 2 - iMA(NULL,0,Nbr_Periods,0,MODE_EMA,PRICE_CLOSE,i);
   // SqrtP = round(SQRT(period))
   int SqrtP = MathRound(MathSqrt(Nbr_Periods));  
   //medianPrice = ExponentialAverage[SqrtP](temp)
   medianPrice = iMA(NULL,0,SqrtP,0,1,temp,i);
   //////////////////////////////////////////////
   // up=avg+aa*atr;
   // dn=avg-aa*atr;
   //////////////////////////////////////////////
      up[i]=medianPrice+Multiplier*atr;
      //Print("up: "+up[i]);
      dn[i]=medianPrice-Multiplier*atr;
      //Print("dn: "+dn[i]);
      trend[i]=1;
   
      
      if (High[i]>up[i+1]) {
         trend[i]=1;
         if (trend[i+1] == -1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
         
      }
      else if (Low[i]<dn[i+1]) {
         trend[i]=-1;
         if (trend[i+1] == 1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
      }
      else if (trend[i+1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i+1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i+1]>0) {
         flag=1;
         //Print("flag: "+flag);
      }
      else {
         flag=0;
         //Print("flagh: "+flag);
      }
      
      if (trend[i]>0 && trend[i+1]<0) {
         flagh=1;
         //Print("flagh: "+flagh);
      }
      else {
         flagh=0;
         //Print("flagh: "+flagh);
      }
      
      if (trend[i]>0 && dn[i]<dn[i+1])
         dn[i]=dn[i+1];
      
      if (trend[i]<0 && up[i]>up[i+1])
         up[i]=up[i+1];
      
      if (flag==1)
         up[i]=medianPrice+(Multiplier*atr);
         
      if (flagh==1)
         dn[i]=medianPrice-(Multiplier*atr);
         
      //-- Draw the indicator
      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            changeOfTrend = 0;
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            changeOfTrend = 0;
         }
      }
   }
   WindowRedraw();
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
MrLong

ProRealtime code to MQL4 help needed

Post by MrLong »

Try removing Strict.

//#property strict
Travelfrog
Posts: 8
Joined: Sun Apr 13, 2014 6:47 pm

ProRealtime code to MQL4 help needed

Post by Travelfrog »

Thanks, I tried that already.
All that does is take away the warning error from compiler, but it does not make the code work.
User avatar
milanese
TechAdmin
Posts: 3293
Joined: Wed Jan 09, 2013 9:02 am
Location: btr rdx, r8 +

ProRealtime code to MQL4 help needed

Post by milanese »

Travelfrog » Sat Apr 19, 2014 10:53 am wrote:Thanks, I tried that already.
All that does is take away the warning error from compiler, but it does not make the code work.
Try this code it works ..

Code: Select all




#property indicator_chart_window
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_buffers 2
double TrendUp[], TrendDown[];
int changeOfTrend;
extern int Nbr_Periods = 12;
extern double Multiplier = 2.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0, TrendUp);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(0, "Trend Up");
   SetIndexBuffer(1, TrendDown);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(1, "Trend Down");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit, i, flag, flagh, trend[5000];
   double up[5000], dn[5000], medianPrice, atr;
   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;
   //Print(limit);
   
//----
   for (i = Bars; i >= 0; i--) {
      TrendUp[i] = EMPTY_VALUE;
      TrendDown[i] = EMPTY_VALUE;
      atr = iATR(NULL, 0, Nbr_Periods, i);
      //Print("atr: "+atr[i]);
      medianPrice = (High[i]+Low[i])/2;
      //Print("medianPrice: "+medianPrice[i]);
      up[i]=medianPrice+(Multiplier*atr);
      //Print("up: "+up[i]);
      dn[i]=medianPrice-(Multiplier*atr);
      //Print("dn: "+dn[i]);
      trend[i]=1;
   
      
      if (Close[i]>up[i+1]) {
         trend[i]=1;
         if (trend[i+1] == -1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
         
      }
      else if (Close[i]<dn[i+1]) {
         trend[i]=-1;
         if (trend[i+1] == 1) changeOfTrend = 1;
         //Print("trend: "+trend[i]);
      }
      else if (trend[i+1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i+1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i+1]>0) {
         flag=1;
         //Print("flag: "+flag);
      }
      else {
         flag=0;
         //Print("flagh: "+flag);
      }
      
      if (trend[i]>0 && trend[i+1]<0) {
         flagh=1;
         //Print("flagh: "+flagh);
      }
      else {
         flagh=0;
         //Print("flagh: "+flagh);
      }
      
      if (trend[i]>0 && dn[i]<dn[i+1])
         dn[i]=dn[i+1];
      
      if (trend[i]<0 && up[i]>up[i+1])
         up[i]=up[i+1];
      
      if (flag==1)
         up[i]=medianPrice+(Multiplier*atr);
         
      if (flagh==1)
         dn[i]=medianPrice-(Multiplier*atr);
         
      //-- Draw the indicator
      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            changeOfTrend = 0;
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            changeOfTrend = 0;
         }
      }
   }
   WindowRedraw();
      
//----
   return(0);
  }
Cheers :)

Tommaso
Global Prime is the official SHF broker :yahoo:
Searching for Servers and Workstations with individual configuration?
Just PM
:smile: Click here to go to the BoardKnowledgeBase
NOTE: Cookies and JavaScript are required for the using the board, with full functionality
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

Array out of range error

Post by Radar »

Hey gang,

I was just playing with the T3MA indicator, from Slowkey's Gann Ribbon System, here, http://www.stevehopwoodforex.com/phpBB3 ... =16&t=3524 and came across the "Array out of range" problem... The fix I used is...

Code: Select all

int start()
{
   int i,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;

//---- main loop
for(i=(limit-1); i>=0; i--) // Changed from "for(i=limit; i>=0; i--)"
   {
   	// Do what ya gotta do...
   }
I tried replacing limit with Bars in the original, but got the same error.

Hope this helps.

Have fun!

Radar =8^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
Post Reply

Return to “Coders Hangout”