Determining bull or bear trend - page 2

 
Sorry, that was the wrong one here is the correct one.
Files:
 

A couple of comments...

Print() https://docs.mql4.com/common/Print and GetLastError() https://docs.mql4.com/check/GetLastError are your friends, make plenty of use of them.

You can use print to check that each variable is calculating the way you expect it to.

Personally, I think it is easier to read your code if you calculate your signals seperate from your trading logic, then print the variables as a check and then run your trading logic. I would calculate the two MA's as seperate variables...when you are happy everything is calculating properly, you can then delete or // the print statements.

double   d30minMA,d5minMA;

d30minMA=iMA(NULL,30,20,0,MODE_SMA,PRICE_MEDIAN,0);
d5minMA=iMA(NULL,MAFast,20,0,MODE_SMA,PRICE_MEDIAN,0);

Print("30minMA "+d30minMA+" 5minMA "+d5minMA);

if (Bid > d30minMA+7*Point)
   {
   Posture = 1;
   }
if (Bid < d30minMA-7*Point)
   {
   Posture = 2;
   }

On your two MA's, you are calculating the MA's on different timeframes. That is fine of course, but I just wanted to check that is what you mean to do. The convention imho, would be that variables called MAfast and MAslow would refer to the number of bars to be used for the average (ie period) rather than timeframe. It is perhaps just the name that is throwing me.

So on your trading criteria.... you should use GetLastError() after your ordersend. You will see then that the Sell is failing with probably error code 129-Invalid Price. You are trying to Sell at the Ask. You sell at the Bid. Also as a comment, you switch from testing with Bid in the first if's (to set posture) to using Ask. All indicators are calculated using bid and particularly with variable spread brokers you may get undesirable results by switching your analysis between Bid and Ask.

To give you the idea... buy would be like this...

if (Posture == 1 && Bid>d5minMA+(BreakoutPips * Point))
   {
   OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,0,16384,0,Green);
   err=GetLastError();
   if (err!=0)
      {
      Print("OrderSend Failed. Error Code- ",err);
      }
   else
      Print("Ordersend OK");
   }

follow the same approach for sell (and remeber you sell on Bid not Ask).

Lots more stuff to do, like only sending 1 order rather than an order every tick (as will happen here) ....https://docs.mql4.com/trading/OrdersTotal

and closing orders...read this https://www.mql5.com/en/forum/126647

Anyway, hope the above helps

V

 
Sounds great to be able to print things as I go. That is what I need to see exactly what is hape4ning. The only problem is where can I find the file that has the output of the print command?. I am unable to find it.
 
deantracy:
Sounds great to be able to print things as I go. That is what I need to see exactly what is hape4ning. The only problem is where can I find the file that has the output of the print command?. I am unable to find it.
Not on file. Press Ctrl+T for Terminal. See it on the 'Experts' tab. See also 'Journal' tab. I prefer the Alert() function. You won't miss it if you use that.
 

Or even Comment() to get it right up there on the chart


int      err;
double   d30minMA,d5minMA;

d30minMA=iMA(NULL,30,20,0,MODE_SMA,PRICE_MEDIAN,0);
d5minMA=iMA(NULL,MAFast,20,0,MODE_SMA,PRICE_MEDIAN,0);


string comm =  "30minMA " + d30minMA + "\n" +
               "5minMA " + d5minMA +" \n" ;           
       Comment(comm);


V

 
I thougt for sure that I would not be writing for a while be it looks like I have got to the heart of the problem. Whenever we assign a moving average to a variable and then print it the result is 0 out to about 6 decimal points. Why is that happening?
 
deantracy:
I thougt for sure that I would not be writing for a while be it looks like I have got to the heart of the problem. Whenever we assign a moving average to a variable and then print it the result is 0 out to about 6 decimal points. Why is that happening?

Hmm, well something is up... not sure what would cause this... are you running it on a realtime chart or back testing... if backtesting do you have enough history loaded (ie have you got 20 past bars to look at) Would probably need to see the latest code to comment further.

Anyone else any ideas?

V

 
Can you, or anyone else for that matter, confirm that they can get a moving average and print something other than 0?. I am sure that the data is loaded because I have done it in visual mode several times.I will try a different time frame just in case.
 
Ok in a different time frame the 30min MA works but the 5min does not. I'l keep changing things and see if I can get it to work.
 

Well, I just applied this script and it worked fine. I then opened an exotic symbol that I hadn't opened before and replicated the 0.000. I then changed to 1min timeframe allowed it to update history applied the script and it worked fine.

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
double   d30minMA,d5minMA;

d30minMA=iMA(NULL,30,20,0,MODE_SMA,PRICE_MEDIAN,0);
d5minMA=iMA(NULL,5,20,0,MODE_SMA,PRICE_MEDIAN,0);


Alert(  "30minMA " + d30minMA);
Alert(  "5minMA " + d5minMA);         
    
//----
   return(0);
  }
Reason: