Code snippets - page 3

 
Air92:
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

It depends on the indicator (if it is written in a way that allows multiple instances at all). There is no universal rule how it is done. I usually use UniqueID parameter for that but then the rest of the code must be changed in order to use that UniqueID

 
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)

_smoothed_momentum.mq4

and, as a plus, it can be applied to any value (not just price)

mladen- I'm trying learn how to incorporate your Smoothed Momentum indicator into an EA. If possible I don't want to have the EA call the indicator, I'd like it to be part of the EA.

To figure this out I took the default MACD EA that comes with MT4 and the indicator in lines 12-57. My question is, how do use it to make a trade? For example, lets say if the indicator is > 0 then I want it to open a trade. What do I put in line 95 to have the EA check the value of the indicator?

Really appreciate the help!

 
hermes:
mladen- I'm trying learn how to incorporate your Smoothed Momentum indicator into an EA. If possible I don't want to have the EA call the indicator, I'd like it to be part of the EA.

To figure this out I took the default MACD EA that comes with MT4 and the indicator in lines 12-57. My question is, how do use it to make a trade? For example, lets say if the indicator is > 0 then I want it to open a trade. What do I put in line 95 to have the EA check the value of the indicator?

Really appreciate the help!

Hermes

Here is this one. You will see that it is very easy to simply paste it into any other code. Simply declare a momentum array of desired size (I used 100) and check array value 0 (for current) or 1 (for first closed) and compare it to 0 for entries.

PS: you have to have an arrauy bigger than the period calculated, otherwise you are not going to get corrected values. For example, for period 14 it would be even enough to have a 15 element array, but better to use a bit biggers number (what's safe is safe )

_smoothed_momentum_limited.mq4

 

momentum[0] and momentum[1] don't seem to call a value. I added them to the code (lines 65 & 78) but no luck. I left the array value at 100.

could you try a quick backtest to see what is wrong? The momentum should pretty much always be either greater or less than zero, so this thing should be placing tons of orders...

 
hermes:
momentum[0] and momentum[1] don't seem to call a value. I added them to the code (lines 65 & 78) but no luck. I left the array value at 100.

could you try a quick backtest to see what is wrong? The momentum should pretty much always be either greater or less than zero, so this thing should be placing tons of orders...

Try this way : v14.mq4

Files:
v14.mq4  6 kb
 

I see, momentum is defined in the void data type. Now momentum[1] and momentum[0] return values.

Thank you!

 
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)

Mladen, sorry I don't mean to take a course here, but can you explain a bit more how to use this piece of code ? Thank you If I understand, I can call the function iSchaff(Price,Period,i,0), is that right ?

 
airquest:
Mladen, sorry I don't mean to take a course here, but can you explain a bit more how to use this piece of code ? Thank you If I understand, I can call the function iSchaff(Price,Period,i,0), is that right ?

Yes, that is right

Also, you can use any value instead of price. Like this, for example :

iSchaff(iAO(NULL,0,i),Period,i,0);

It calculates Schaff trend cycle of awesome oscillator. And so on. You can use any value you wish to test to see what would the results of making it a "schaff like" be

 
mladen:
Yes, that is right

Also, you can use any value instead of price. Like this, for example :

iSchaff(iAO(NULL,0,i),Period,i,0);

It calculates Schaff trend cycle of awesome oscillator. And so on. You can use any value you wish to test to see what would the results of making it a "schaff like" be

Amazing, thanks !

 
airquest:
Amazing, thanks !

I am a sucker for simplicity

Reason: