How to get Moving Average Cross price

Jackery
312
Jackery  

I am writing an EA that uses crossing moving averages. I tell the crossing of moving averages in the following way:


// if shift5MA > shift0MA 
   if (shift5MAArray[1] > shift0MAArray[1]) {
      
      //if shift5MA < shift0MA VALID SELL
      if (shift5MAArray[2] < shift0MAArray[2]) {
         signal = "sell";
      }
      }
   
   if (shift5MAArray[1] < shift0MAArray[1]) {
      
      //if shift5MA > shift0MA
      if (shift5MAArray[2] > shift0MAArray[2]) {
         signal = "buy";
      }
      }

How can I tell the exact price at the point that moving averages cross, preferably after a candle closes?

Jackery
312
Jackery  
Can someone please help me with this?
Faeze Bakhshayesh
5284
Faeze Bakhshayesh  
Jackery:

I am writing an EA that uses crossing moving averages. I tell the crossing of moving averages in the following way:


How can I tell the exact price at the point that moving averages cross, preferably after a candle closes?

double   MA30  =  iMA(NULL,TMF,MAPeriod1,0,MODE_LWMA,PRICE_MEDIAN,0);
double   MA15  =  iMA(NULL,TMF,MAPeriod4,0,MODE_LWMA,PRICE_MEDIAN,0);
      
if (OrdersTotal()==0)
 {
  if ( (MA15 > MA30 && OldSignal !="BUY") )
   {
    // buy here
    OldSignal = "BUY";
   }
  if ( (MA15 < MA30 && OldSignal !="SELL") )
   {
     // sell here
     OldSignal = "SELL";
   }
 }

and define "OldSignal" variable on top off your code, global variable

William Roeder
25339
William Roeder  
  1. Faeze Bakhshayesh:

    That is not checking for a cross.

  2. Jackery: How can I tell the exact price at the point that moving averages cross, preferably after a candle closes?

    Search new bar. Stop checking each tick.

  3. Exact price where they crossed? You can easily estimate that with algebra: f(t) = f[2] + (f[1]-f[2]) × t, s(t) = s[2] + (s[1]-s[2]) × t. At cross, they are equal and so is t.

  4. Or the price when they crossed? Either use 1 and 0 and note the Bid on first cross. Or much harder binary search on price.

  5. Why do you care? The market has moved away from either (№ 3 or 4) at candle close.
Faeze Bakhshayesh
5284
Faeze Bakhshayesh  
William Roeder:
  1. That is not checking for a cross.

  2. Search new bar. Stop checking each tick.

  3. Exact price where they crossed? You can easily estimate that with algebra: f(t) = f[2] + (f[1]-f[2]) × t, s(t) = s[2] + (s[1]-s[2]) × t. At cross, they are equal and so is t.

  4. Or the price when they crossed? Either use 1 and 0 and note the Bid on first cross. Or much harder binary search on price.

  5. Why do you care? The market has moved away from either (№ 3 or 4) at candle close.

You can find crossing 2 ma with hundred of ways :)

Jackery
312
Jackery  
William Roeder:
  1. That is not checking for a cross.

  2. Search new bar. Stop checking each tick.

  3. Exact price where they crossed? You can easily estimate that with algebra: f(t) = f[2] + (f[1]-f[2]) × t, s(t) = s[2] + (s[1]-s[2]) × t. At cross, they are equal and so is t.

  4. Or the price when they crossed? Either use 1 and 0 and note the Bid on first cross. Or much harder binary search on price.

  5. Why do you care? The market has moved away from either (№ 3 or 4) at candle close.

Sorry, I've not been here for a while. 

I am trying to figure out how to use the equation you've provided in my code. I don't really get the understand what s(t) and f(t) represent with respect to the code I provided.

From my observation and backtesting, based on some other additional conditions, price might return to fill in orders at this cross price before continuing in it's respective direction.

Jackery
312
Jackery  
Faeze Bakhshayesh:

You can find crossing 2 ma with hundred of ways :)

I would really appreciate you showing me one of those hundred ways :)

Jackery
312
Jackery  

I would really appreciate some help with getting this solved

Mehmet Bastem
2682
Mehmet Bastem  
Faeze Bakhshayesh:

and define "OldSignal" variable on top off your code, global variable

double   MA301  =  iMA(NULL,TMF,MAPeriod1,0,MODE_LWMA,PRICE_MEDIAN,0);
double   MA302  =  iMA(NULL,TMF,MAPeriod1,0,MODE_LWMA,PRICE_MEDIAN,1);

double   MA151  =  iMA(NULL,TMF,MAPeriod4,0,MODE_LWMA,PRICE_MEDIAN,0);
double   MA152  =  iMA(NULL,TMF,MAPeriod4,0,MODE_LWMA,PRICE_MEDIAN,1);

      
if (OrdersTotal()==0)
 {
  if ( (MA151 > MA301 && MA152 < MA302 && OldSignal !="BUY") )
   {
    // buy here
    OldSignal = "BUY";
   }
  if ( (MA151 < MA301 && MA152 > MA302 && OldSignal !="SELL") )
   {
     // sell here
     OldSignal = "SELL";
   }
 }