Code snippets - page 2

 
zefx:
There is something wrong with the last link, Smoothed momentum couldn't be downloaded. I've tested my browser with other links from this page, they are working fine.

I tested the download and all is OK

Just in case : here is the source of that attachment

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_color1 PaleVioletRed

#property indicator_width1 2

#property indicator_level1 0

//

//

//

//

//

extern int MomentumLength = 14;

extern int MomentumPrice = PRICE_CLOSE;

double momentum[];

//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//

//

//

//

int init()

{

SetIndexBuffer(0,momentum);

IndicatorShortName("Smoothed momentum ("+MomentumLength+")");

return(0);

}

int start()

{

int counted_bars=IndicatorCounted();

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

int limit = MathMin(Bars-counted_bars,Bars-1);

for(int i=limit; i>=0; i--) momentum = iMomentumS(iMA(NULL,0,1,0,MODE_SMA,MomentumPrice,i),MomentumLength,1,2,i);

return(0);

}

//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//

//

//

//

double workMom[];

double iMomentumS(double price, double length, double powSlow, double powFast, int i)

{

if (ArraySize(workMom)!=Bars) ArrayResize(workMom,Bars);

i = Bars-i-1; workMom = price;

//

//

//

//

//

double suma = 0.0, sumwa=0;

double sumb = 0.0, sumwb=0;

for(int k=0; k<length; k++)

{

double weight = length-k;

suma += workMom * MathPow(weight,powSlow);

sumb += workMom * MathPow(weight,powFast);

sumwa += MathPow(weight,powSlow);

sumwb += MathPow(weight,powFast);

}

return(sumb/sumwb-suma/sumwa);

}

The significant part is iMomentumS() function

All the rest is just a support for it

 

Mladen, thanks for the Code, it helped a lot, since the Gremlins are still doing their thing as far as I am concerned.

Great indicator, as is, I think also one of yours: Momentum - atr normalized - nl smoothed nmc. Have been trying to figure out which is better for shorter time periods and what are the right settings.

Pozdrav iz Beograda,

Zeljko

 
mladen:
For people using momentum : Momentum is a very useful indicator. But probably the biggest problem it has is that it can create a lot of false signals simply because it is not smooth. This is one way how it can be solved without adding any lag at all (on the contrary : in some cases it is faster than momentum). Here is a comparison : red is the soothed momentum, blue is the regular momentum (same period, same price)

Hi mladen. What is the best way to use this indicator in our EAs? For example, would you say anything above X value open BUY ticket. Or would you use the slope: if previous value was X and now it is <= Y open BUY ticket.

I'd like to use this smoothed version of the indicator in one of my EAs, but want to make sure I understand the best way to use it. Thanks

 
hermes:
Hi mladen. What is the best way to use this indicator in our EAs? For example, would you say anything above X value open BUY ticket. Or would you use the slope: if previous value was X and now it is <= Y open BUY ticket. I'd like to use this smoothed version of the indicator in one of my EAs, but want to make sure I understand the best way to use it. Thanks

Hermes

Depends.

If your EA is to scalp, then yes, use the slope (since the slope is smooth and is a almost with no lag at all, it could be used effectively for scalping)

If your EA is to follow the trend, then my opinion is that it is better to use the zero line cross.

Using it at crosses at some values is tricky : it depends on time frame you use, symbol you use (it is an unbound indicator so you can not have same levels for all time frames and symbols)

_______________________________

Mark Jurik (since that version is almost the same as Juriks velocity as far as values are concerned) recommends slope and divergence usage (see this : VEL ) I know that what he wrote there is mostly for commercial purposes, but that info might help too

 

Thank you, I'm going with slope. Might add a few variables like A<BB>C<D<E to indicate a direction change. Anyway I'll try out a few combos using slope and see the results.

 

Schaff trend cycle as a function :

//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//

//

//

//

double aSchaff[][5];

#define sPrice 0

#define stoch11 1

#define stoch12 2

#define stoch21 3

#define stoch22 4

double iSchaff(double price, int stcPeriod, int i, int instance=0)

{

if (ArrayRange(aSchaff,0)!=Bars) ArrayResize(aSchaff,Bars); i = Bars-i-1; instance *= 5;

aSchaff = price;

//

//

//

//

//

double minval = aSchaff;

double maxval = aSchaff;

for (int k=1;k=0; k++)

{

minval = MathMin(aSchaff,minval);

maxval = MathMax(aSchaff,maxval);

}

maxval -= minval;

if (maxval > 0)

aSchaff = 100*((aSchaff-minval)/maxval);

else aSchaff = aSchaff;

aSchaff = aSchaff+0.5*(aSchaff-aSchaff);

//

//

//

//

//

minval = aSchaff;

maxval = aSchaff;

for (k=1; k=0; k++)

{

minval = MathMin(aSchaff,minval);

maxval = MathMax(aSchaff,maxval);

}

maxval -= minval;

if (maxval > 0)

aSchaff = 100*((aSchaff-minval)/maxval);

else aSchaff = aSchaff;

aSchaff = aSchaff+0.5*(aSchaff-aSchaff);

return(aSchaff);

}

Use first parameter to calculate Schaff trend cycle of any value (it doe not have to be MACD as it is in the original Schaff trend cycle)

 
mladen:
Schaff trend cycle as a function :
//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//

//

//

//

double aSchaff[][5];

#define sPrice 0

#define stoch11 1

#define stoch12 2

#define stoch21 3

#define stoch22 4

double iSchaff(double price, int stcPeriod, int i, int instance=0)

{

if (ArrayRange(aSchaff,0)!=Bars) ArrayResize(aSchaff,Bars); i = Bars-i-1; instance *= 5;

aSchaff = price;

//

//

//

//

//

double minval = aSchaff;

double maxval = aSchaff;

for (int k=1;k=0; k++)

{

minval = MathMin(aSchaff,minval);

maxval = MathMax(aSchaff,maxval);

}

maxval -= minval;

if (maxval > 0)

aSchaff = 100*((aSchaff-minval)/maxval);

else aSchaff = aSchaff;

aSchaff = aSchaff+0.5*(aSchaff-aSchaff);

//

//

//

//

//

minval = aSchaff;

maxval = aSchaff;

for (k=1; k=0; k++)

{

minval = MathMin(aSchaff,minval);

maxval = MathMax(aSchaff,maxval);

}

maxval -= minval;

if (maxval > 0)

aSchaff = 100*((aSchaff-minval)/maxval);

else aSchaff = aSchaff;

aSchaff = aSchaff+0.5*(aSchaff-aSchaff);

return(aSchaff);

}

Use first parameter to calculate Schaff trend cycle of any value (it doe not have to be MACD as it is in the original Schaff trend cycle)

Is there such a function of tradestation, please?

 
sebastianK:
Is there such a function of tradestation, please?

Not in that form.Sorry

 
mladen:
Not in that form.Sorry

OK, thanks !

 

Dear Mladen,please tell as to register unique value in a code? and where it will need to be inserted?two indicators on a chart, but signals of one, disturb signals of another.

Thank you

Reason: