Determining bull or bear trend

 

Attached is a script that I am trying to set a variable determining whether the trend is bullish or bearish. It is supposed to set a variable to determine bull or bear. It is not setting the variable. can anyone determine why not by looking at it? I have commented out the rest of the script (still not working either) so that Bull or Bear conditions execute the trade.

Files:
 

You are missing the timescale parameter on your iMA.

V

 
Viffer:

You are missing the timescale parameter on your iMA.

V


I have the timescale parameter set as a variable, MAFaast or MAslow. I tried it with the constant such as PERIOD_M5 and with the value 5. the documentation is confusing on which to use. either way it does not work. I get all bullish trades whether I preset the POSTURE variable to bull or bear. That would tell me the the fist statement is working but the second is not. The only difference is changing < to >. Confusing.
 
deantracy:

I have the timescale parameter set as a variable, MAFaast or MAslow. I tried it with the constant such as PERIOD_M5 and with the value 5. the documentation is confusing on which to use. either way it does not work. I get all bullish trades whether I preset the POSTURE variable to bull or bear. That would tell me the the fist statement is working but the second is not. The only difference is changing < to >. Confusing.

Is it your intention to compare the last 5 minute bar to the last 20minute bar? That is what this would try to do, but 20 isn't a valid timeframe enumeration and I wouldn't use iMA to get a value from a single bar.


I think what you mean is you want to compare a 5 period MA with a 20 period MA for the same time frame... in which case, you need the time frame parameter (0 for current chart) and use your MAfast / Maslow for the period. You originally had 1 for the period parameter.

if (
      iMA(NULL,0,MAFast,0,MODE_SMA,PRICE_MEDIAN,0)
       > 
      iMA(NULL,0,MASlow,0,MODE_SMA,PRICE_MEDIAN,0)
    ) 
    Posture = 1;
if (
     iMA(NULL,0,MAFast,0,MODE_SMA,PRICE_MEDIAN,0)
       < 
     iMA(NULL,0,MASlow,0,MODE_SMA,PRICE_MEDIAN,0)
   )
    Posture = 2;

If it is just the last bar median value, let me know as there are better ways of getting that info over iMA

hth

V

 
Viffer:

Is it your intention to compare the last 5 minute bar to the last 20minute bar? That is what this would try to do, but 20 isn't a valid timeframe enumeration and I wouldn't use iMA to get a value from a single bar.


I think what you mean is you want to compare a 5 period MA with a 20 period MA for the same time frame... in which case, you need the time frame parameter (0 for current chart) and use your MAfast / Maslow for the period. You originally had 1 for the period parameter.

If it is just the last bar median value, let me know as there are better ways of getting that info over iMA

hth


I Just need to establish bull or bear trend. I re-did it so I think it makes more sense.

if (

Ask > iMA(NULL,PERIOD_M30,1,0,MODE_SMA,PRICE_MEDIAN,0)+ 5*Point

)

Posture = 1;

if (

Bid < iMA(NULL,PERIOD_M30,1,0,MODE_SMA,PRICE_MEDIAN,0)-5*Point

)

Posture = 2;

The problem is still that the first statement works but the second does not. There is another part of the code that executes a trade based on this. I have proved that this part of the script works.

 

Well, your use of iMA is unorthodox as you are trying to get the average of 1 bar... and the currently forming bar at that. I would use

Ask > ((iHigh(NULL,30,0)+iLow(NULL,30,0))/2)+5*Point 

to see where Ask is relative to the mid price of the current bar +5points on a 30 min TF. That is,however, the same as what you are doing with iMA so other than being clearer, is not going to solve anything. I think it now has to do with your logic.

I'm not so sure about your definition of Bull and Bear. Do you really mean anytime the bid is below the currently forming bars mid point, that constitutes bear? The midpoint will vary dramatically from the start to the completion of the current bar. On your current logic, a tick that is currently bear could in the next instant be bull. And given spread, the ASK will have a bias to above the mid point which is calculated on BID. Is that really what you are aiming to achieve?


Perhaps you could explain what you define as Bull and Bear and I could better help you code it. Presumably you have discounted the Elder Ray definition https://docs.mql4.com/indicators/iBullsPower and https://docs.mql4.com/indicators/iBearsPower.

V

 
OK, it looks like what I thought was simple is in fact not. Which may explain why it is not working. All that I want to do is establish trend direction so I can determine if I am in a buying or selling mode. I my stock training they said that a simple way to indicate this if the price is currently above or below moving average. Is there another way I can establish trend?
 
deantracy:
OK, it looks like what I thought was simple is in fact not. Which may explain why it is not working. All that I want to do is establish trend direction so I can determine if I am in a buying or selling mode. I my stock training they said that a simple way to indicate this if the price is currently above or below moving average. Is there another way I can establish trend?

That's simple enough with iMA... the point is how many bars are used to calculate the moving average. Currently, you are setting the averaging period for the calculation to 1, which is basically the latest bar. You should look at the average price of say the last 20 bars. You use the timescale parameter to detirmine the timescale of each bar. if you use 0 for timescale, it just uses the timescale of the chart you loaded the EA onto, but if you want to use a specific hard coded timeframe, then you would put in the desired value.


For your example if you want to determin if the price is above or below a 20 period MA, then this code would do it. Null means current symbol, the 30 means on 30 minute timeframe (0 is default) and 20 means average of 20 bars.

if (Bid > iMA(NULL,30,20,0,MODE_SMA,PRICE_MEDIAN,0))
   {
   Posture = 1;
   }
if (Bid < iMA(NULL,30,20,0,MODE_SMA,PRICE_MEDIAN,0))
   {
   Posture = 2;
   } 

I think you are just getting confused with each parameter in iMA https://docs.mql4.com/indicators/iMA

V

 
dOk I have made the suggested changes. However I still only get Long trades. I have code that reset the postaure variable to zero each time. the Posture variable appears to be set for "1" each time and long trade is trigggered, This is regardless of the current trend direction. Attached is the full code.
Files:
breakout.ex4  3 kb
 
deantracy:
dOk I have made the suggested changes. However I still only get Long trades. I have code that reset the postaure variable to zero each time. the Posture variable appears to be set for "1" each time and long trade is trigggered, This is regardless of the current trend direction. Attached is the full code.

Can you attach the MQ4 file rather than ex4 and I will take a look.

V

 
sorry, here it is.
Files: