moving average cross over + stochastic signal

 

hi all

i'm trying to code the following condition (as below)

I want to place an arrow the first time stoch crosses upwards the 50value, after sma20 crosses above sma50


What i've managed to code so far is showing up only when it happens all together presumably because the condition is all set in a single && ( )

I have tried making 2 if's statement (first for the sma x over and then another if statement for the stoch) but it still gives the same result.

  {
   static int x = 1;
   double f1 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 1);
   double f2 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 2);
   double s1 = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 1);
   double s2 = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 2);
   double stoch_1 = iStochastic(NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
   double stoch_2 = iStochastic(NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2);

   if(f1 > s1 && s2 > f2 && stoch_1 > 50 && stoch_2 <= 50)
     {
      ObjectCreate(x, OBJ_ARROW_UP, 0, Time[0], Low[0]);
      x = x + 1;
     }
   return(rates_total);
  }

is it possible to keep 1st condition waiting (sma x over) until second condition occurs (rsi x 50)?

thanks for any help

 
zelda_lee:

hi all

i'm trying to code the following condition (as below)

I want to place an arrow the first time stoch crosses upwards the 50value, after sma20 crosses above sma50


What i've managed to code so far is showing up only when it happens all together presumably because the condition is all set in a single && ( )

I have tried making 2 if's statement (first for the sma x over and then another if statement for the stoch) but it still gives the same result.

is it possible to keep 1st condition waiting (sma x over) until second condition occurs (rsi x 50)?

thanks for any help

You have to use a stuff like that on MT4 for the arrow :
          string name = "arrow up"+(string)Time[0];


      ObjectCreate(name ,OBJ_ARROW,0,Time[0],Low[0]);
         ObjectSet(name , OBJPROP_ARROWCODE, 233);

And then add width of the arrow, + the color of the arrow, +  WindowRedraw();

This condition is never met, check this

 if( f1 > s1 && s2 > f2 
 
zelda_lee: is it possible to keep 1st condition waiting (sma x over) until second condition occurs (rsi x 50)?

Would you ever want to trade fast MA below slow but RSI above 50? No, then you have one condition. Stop thinking crosses, just levels.

 
ffoorr:
You have to use a stuff like that on MT4 for the arrow :

And then add width of the arrow, + the color of the arrow, +  WindowRedraw();

This condition is never met, check this

thanks for the tip about drawing arrows

as for the other condition it does happen but not very often, the way you've edited it, will only detected moving average cross overs without taking stochastic in consideration.



William Roeder:

Would you ever want to trade fast MA below slow but RSI above 50? No, then you have one condition. Stop thinking crosses, just levels.

I never said this is a strategy or anything that i'm going to trade. I'm just learning to code on my own and trying to practice what i've learnt. The scope of this for me is to see if i'm able to make a trigger where i want to (hence the arrow to confirm that).


ps. also the trigger is fast above slow not vice versa

 
zelda_lee:

 the way you've edited it, will only detected moving average cross overs without taking stochastic in consideration.


Yes you have to modify your entry condition,

the "&&" is ok   if( ma20 > ma50 && sto > 50)   for up arrow

   if(f1 > s1 && stoch_2 < 50 && stoch_1 >= 50)

The  easiest way to check this is to put the code directly into an EA.

And use the arrow up and down to check if each of the conditions is  met.

 
I want to place an arrow the first time stoch crosses upwards the 50value, after sma20 crosses above sma50


This will complicate your EA, because you have to detect first the crossing of ma20 and ma50, then put a bool to true, then wait for the sto > 50.

Then when sto > 50, do what you have to do, and, thereafter, put the bool to false again to wait for the next entry condition.

And in the case sto > 50 never come, put the bool to false again when ma20 < ma50; to wait for the next entry condition.

 
ffoorr:

Yes you have to modify your entry condition,

the "&&" is ok   if( ma20 > ma50 && sto > 50)   for up arrow

The  easiest way to check this is to put the code directly into an EA.

And use the arrow up and down to check if each of the conditions is  met.

Hi ffoorr thanks once again for your input.

but with this edit, wouldn't the code be triggered everytime stoch crosses 50 upwards? I wish to limit it to once per cross over.


as for the boolean I did try something similar. I dont have the code in hand since right now, but if i remember correctly it was something like

while crossover = true

  if stoch crosses over >50

    draw arrow and set crossover to false (so that it stops the loop)

else crossover still true


but somehow it kept crashing everytime i backtested it, so i presume it got locked into an infinte loop or something :/

I tried adding also a break/continue if statement too to get out of the loop but it still kept crashing


i can't find anything related to "holding onto a condition" or similar topics on google or help either

 
zelda_lee:

Hi ffoorr thanks once again for your input.

but with this edit, wouldn't the code be triggered everytime stoch crosses 50 upwards? I wish to limit it to once per cross over.


as for the boolean I did try something similar. I dont have the code in hand since right now, but if i remember correctly it was something like

while crossover = true

  if stoch crosses over >50

    draw arrow and set crossover to false (so that it stops the loop)

else crossover still true


but somehow it kept crashing everytime i backtested it, so i presume it got locked into an infinte loop or something :/

I tried adding also a break/continue if statement too to get out of the loop but it still kept crashing


i can't find anything related to "holding onto a condition" or similar topics on google or help either

The code has to be inside an EA, I have made functions for arrow_up and arrow_down, and used the arrow_down to show the crossing up of the MAs

 static int x = 1;
 static bool  cross_up = false;
 
  double ma20_1 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 1);
   double ma20_2 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 2);
   double ma50_1 = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 1);
   double ma50_2 = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 2);
   
   double stoch_1 = iStochastic(NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
   double stoch_2 = iStochastic(NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2);
  
  
  
     if( ma20_2 < ma50_2 && ma20_1 > ma50_1 ) // ma cross up
       {
        arrow_down(); //
        cross_up = true;
        }
    
    // mas have crossed up and  sto > 50
      if( cross_up == true && stoch_2 < 50 && stoch_1 >= 50 ) 
          {
           arrow_up();
           cross_up = false;
           }
      // if ma cross down, it is canceled
if( cross_up == true && ma20_1 < ma50_1 ) cross_up = false;             
        
  
  
  
good luck
 
ffoorr:

The code has to be inside an EA, I have made functions for arrow_up and arrow_down, and used the arrow_down to show the crossing up of the MAs

good luck

hey thanks a lot for your hard work much appreciated!

will try out your new code after work and will let you know how it goes, running through it visually looks like it might do the trick!

thanks again

 
zelda_lee:

hey thanks a lot for your hard work much appreciated!

will try out your new code after work and will let you know how it goes, running through it visually looks like it might do the trick!

thanks again

testing it out

i'm creating functions for the arrow and after a quick run looks like this does the trick

thanks for pointing out what i was doing wrong

 
zelda_lee:

testing it out

i'm creating functions for the arrow and after a quick run looks like this does the trick

thanks for pointing out what i was doing wrong

Nice to read that.
Reason: