Help me code this macd ea

 

Hi 


I am giving the ea code below 

  double macd[2];   

   double signal[2];
   int i;
   
   for(i=0;i<2;i++)
  {
  macd[i]= iMACD(Symbol(), 0, FAST_EMA_PERIOD, SLOW_EMA_PERIOD, MACD_SIGNAL_PERIOD, PRICE_CLOSE, MODE_MAIN, i+1);
   signal[i]= iMACD(Symbol(), 0, FAST_EMA_PERIOD, SLOW_EMA_PERIOD, MACD_SIGNAL_PERIOD, PRICE_CLOSE, MODE_SIGNAL, i+1);
   }
   
   if(UseHedge)
   {
      if(macd[0]>signal[0] && macd[1]<=signal[1] )
      {
         CloseOrders(OP_SELL);

         if(state!=1)
         if(-1!=OrderSend(Symbol(), OP_BUY, Lots, Ask, MarketInfo(Symbol(), MODE_SPREAD), 0,
            0, WindowExpertName(), MagicNumber, 0, Green))                           
               state= 1;
            
      }
   
      if(macd[0]<signal[0] && macd[1]>=signal[1] )
      {
         CloseOrders(OP_BUY);

         if(state!=2)
         if(-1!=OrderSend(Symbol(), OP_SELL, Lots, Bid, MarketInfo(Symbol(), MODE_SPREAD), 0,
            0, WindowExpertName(), MagicNumber, 0, Red))
            state= 2;                        
      }   
   }   
   
//----
   return(0);
  }

All i want is we put a tp and sl of 30 pips along with the order and the order should be opened at 2 pips from the open ask of the next candle post the signal and in case of buy 2 pip less than the open bid value of the next candle post the signal candle. If any order is present then when a new order is opened when the signal changes the old running order should be closed. 


can somebody chip in 

 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
So instead using macd you want to use open price of new candle and place orders:
open+2 pips sell
open -2 pips buy?

I think 2 pips is very close. 
What about if you have already order opened from prior candle and it is at loss at open of next candle where new signal comes. Should we close prior order at loss on new signal?
 

i need someone who will teach me how to code EA

 
Bartlomiej Gorski:
So instead using macd you want to use open price of new candle and place orders:
open+2 pips sell
open -2 pips buy?

I think 2 pips is very close. 
What about if you have already order opened from prior candle and it is at loss at open of next candle where new signal comes. Should we close prior order at loss on new signal?

macd will give the signal on a candle once the candle closes , now for sell the close value of the signal candle minus 2 pips is taken to arrive at the exact price to sell . so we minus 2 pips from bid in case of sell . 

 

Should we close prior order at loss on new signal?

 

Yes ,  

Reason: