Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, ozgur), 1,415 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19055 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
MT4 Indicator Conversion #437779
02/26/14 02:58
02/26/14 02:58
Joined: Feb 2014
Posts: 11
M
Mandark Offline OP
Newbie
Mandark  Offline OP
Newbie
M

Joined: Feb 2014
Posts: 11
Greetings,

I've been playing around with converting an indicator popular on Steve Hopwoods and other sites, TMA_CG. Essentially using TMA bands + and - some Band deviation as a building block for the EA. Here is the link to the strategy and indicator: http://www.stevehopwoodforex.com/phpBB3/viewtopic.php?f=5&t=3372

I'm running into some confusing times converting arrays from MT4 into series in Zorro. Here is the offending code:
Code:
void calculateTma(int limit)
{
   int i,j,k;
   double FullLength = 2.0*HalfLength+1.0;
     
   for (i=limit; i>=0; i--)
   {
      double sum  = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i);
      double sumw = (HalfLength+1);
      for(j=1, k=HalfLength; j<=HalfLength; j++, k--)
      {
         sum  += k*iMA(NULL,0,1,0,MODE_SMA,Price,i+j);
         sumw += k;

         if (j<=i)
         {
            sum  += k*iMA(NULL,0,1,0,MODE_SMA,Price,i-j);
            sumw += k;
         }
      }
      tmBuffer[i] = sum/sumw;
  
      double diff = iMA(NULL,0,1,0,MODE_SMA,Price,i)-tmBuffer[i];
      if (i> (Bars-HalfLength-1)) continue;
      if (i==(Bars-HalfLength-1))
      {
         upBuffer[i] = tmBuffer[i];
         dnBuffer[i] = tmBuffer[i];
         if (diff>=0)
            {
               wuBuffer[i] = MathPow(diff,2);
               wdBuffer[i] = 0;
            }
         else
            {               
               wdBuffer[i] = MathPow(diff,2);
               wuBuffer[i] = 0;
            }                  
         continue;
      }
      if(diff>=0)
      {
          wuBuffer[i] = (wuBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength;
          wdBuffer[i] =  wdBuffer[i+1]*(FullLength-1)/FullLength;
      }
      else
      {
           wdBuffer[i] = (wdBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength;
           wuBuffer[i] =  wuBuffer[i+1]*(FullLength-1)/FullLength;
      }
      upBuffer[i] = tmBuffer[i] + BandsDeviations*MathSqrt(wuBuffer[i]);
      dnBuffer[i] = tmBuffer[i] - BandsDeviations*MathSqrt(wdBuffer[i]);
   }
}



I've attempted to convert this code and I get a nice crash on Zorro crazy My problem is that I get confused on when to use a Zorro series as a whole, just use the last bar of it with [0], or when I should actually be performing operations in a for loop...

Also, how in the world do I represent the i+j portion of the above code:
Code:
iMA(NULL,0,1,0,MODE_SMA,Price,i+j);



Print statements help but haven't been sure-fire. Appreciate any thoughts.

Last edited by Mandark; 02/27/14 01:10. Reason: Added full function code for comparison
Re: MT4 Indicator Conversion [Re: Mandark] #437787
02/26/14 07:47
02/26/14 07:47
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
As far as I see, the code above can be greatly simplified, but you can also just copy it - it's plain C and works exactly the same way in Zorro.

You only need to emulate the iMA. AFAIK it's equivalent to the SMA. A shift like "i+j" can just be added to the series, f.i. SMA(Price+i+j,...). Make sure that the lookback is long enough for covering the time period plus maximum i+j. Otherwise you'll get that crash that you mentioned. You can see under "Tips & Tricks" how to emulate a MT4 indicator.

Re: MT4 Indicator Conversion [Re: jcl] #437791
02/26/14 08:40
02/26/14 08:40
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
By the way, I've just seen that the TMA is just the Trima, which is included in the standard indicator library.

Re: MT4 Indicator Conversion [Re: jcl] #437797
02/26/14 11:07
02/26/14 11:07
Joined: Feb 2014
Posts: 11
M
Mandark Offline OP
Newbie
Mandark  Offline OP
Newbie
M

Joined: Feb 2014
Posts: 11
I'll give it a try after work, thanks the reply jcl

Re: MT4 Indicator Conversion [Re: Mandark] #437827
02/27/14 01:07
02/27/14 01:07
Joined: Feb 2014
Posts: 11
M
Mandark Offline OP
Newbie
Mandark  Offline OP
Newbie
M

Joined: Feb 2014
Posts: 11
Had some time to digest... Is this close?

Code:
BarPeriod = 240;
int    HalfLength      = 56;
double BandsDeviations = 2.5;
double FullLength = 2.0*HalfLength+1.0;

vars Price = series(price());
vars Trimavar = series(Trima(Price,5));
var upper = Trimavar[0]+BandsDeviations*sqrt(((FullLength-1)/FullLength)*pow(SMA(Price,1)-Trimavar[0],2));
var lower = Trimavar[0]-BandsDeviations*sqrt(((FullLength-1)/FullLength)*pow(SMA(Price,1)-Trimavar[0],2));

plot("Trimavar1",Trimavar[0],0,GREEN);
plot("upper1",upper,BAND1,RED);
plot("lower1",lower,BAND2,RED);



Key questions:
-How to calculate the upper and lower bands from the Trima. Right now I am not so sure I have it.
-What to use in the Trima Period variable, the MT4 doesn't look at the period in the calculation.
-While i believe that the previous C code could compile with a few changes, I think it doesn't translate well due to the array handling differences between the platforms. Prefer to stick to series.
-Does the Trima function actually emulate the MT4 code. Looking at the source code, i'm not so sure.

Asking these questions has been a learning process, sometimes asking the right question is the hard part!

Re: MT4 Indicator Conversion [Re: Mandark] #437828
02/27/14 03:25
02/27/14 03:25
Joined: Feb 2014
Posts: 11
M
Mandark Offline OP
Newbie
Mandark  Offline OP
Newbie
M

Joined: Feb 2014
Posts: 11
Here's an alternate, keeping things in the previous form. Get a nasty crash that I cant figure out:
Code:
double FullLength = 2.0*HalfLength+1.0;

	vars Trimavar = series(Trima(Price,5));
	var upper;
	var lower;
	vars wuBuffer;
	vars wdBuffer;
	double diff = 0;//SMA(Price,1)-Trimavar[0];
	
        upper = Trimavar[0];
        lower = Trimavar[0];
        if (diff>=0)
           {
              wuBuffer[0] = pow(diff,2);
              wdBuffer[0] = 0;
           }
        else
           {               
              wdBuffer[0] = pow(diff,2);
              wuBuffer[0] = 0;
           }                  
        if(diff>=0)
        {
           wuBuffer[0] = (wuBuffer[1]*(FullLength-1)+pow(diff,2))/FullLength;
           wdBuffer[0] =  wdBuffer[1]*(FullLength-1)/FullLength;
        }
        else
        {
           wdBuffer[0] = (wdBuffer[1]*(FullLength-1)+pow(diff,2))/FullLength;
           wuBuffer[0] =  wuBuffer[1]*(FullLength-1)/FullLength;
         }
         upper = Trimavar[0] + BandsDeviations*sqrt(wuBuffer[0]);
         lower = Trimavar[0] - BandsDeviations*sqrt(wdBuffer[0]);


Re: MT4 Indicator Conversion [Re: Mandark] #437837
02/27/14 09:44
02/27/14 09:44
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Yes, this will crash as you've defined no arrays, but just empty series pointers. Can you post the formula for the upper and lower TMA band? I'll then translate it in C.

Re: MT4 Indicator Conversion [Re: jcl] #437874
02/27/14 20:04
02/27/14 20:04
Joined: Feb 2014
Posts: 11
M
Mandark Offline OP
Newbie
Mandark  Offline OP
Newbie
M

Joined: Feb 2014
Posts: 11
JCL,

I updated the top post with the entirety of the function which includes the upper and lower bound calculations.

Re: MT4 Indicator Conversion [Re: Mandark] #437896
02/28/14 08:55
02/28/14 08:55
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
I meant the formula - the algorithm -, not the C code. You can not convert an indicator without knowing what it does and what it's supposed to calculate. Well, theoretically you can, but it's no fun wink.

Re: MT4 Indicator Conversion [Re: jcl] #437909
03/01/14 02:38
03/01/14 02:38
Joined: Feb 2014
Posts: 11
M
Mandark Offline OP
Newbie
Mandark  Offline OP
Newbie
M

Joined: Feb 2014
Posts: 11
JCL,

Nearest as I can tell, the bands are a form of Keltner channel with Halflength 56 and Band Deviation of 2.5. Surely you have heard of this indicator. Is this indicator supportable?

More info: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:keltner_channels

Page 1 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1