Download MetaTrader 5
To add comments, please log in or register
Find out how to install MetaTrader on Mac OS
somecdnguy4
9
somecdnguy4 2014.11.03 22:01 

 Hey everyone,

 

I'm developing a simple MACD cross buy/sell script, but when I run the tester, it opens a buy order for some lots, and then nothing happens. Prior to the script completing, the single order is then closed. Does anyone know what's going on here? Thanks.

Here is the output journal: 

2014.11.03 16:56:25.019 BTCUSD,Daily: 2303909 tick events (1304 bars, 2304909 bar states) processed within 1937 ms (total time 19078 ms)
2014.11.03 16:56:25.019 2014.10.30 23:59  MACDcrossBTCUSD BTCUSD,Daily: End of MACD cross trading.
2014.11.03 16:56:25.019 2014.10.30 23:59  Tester: order #1 is closed
2014.11.03 16:56:23.091 2014.01.03 00:00  MACDcrossBTCUSD BTCUSD,Daily: ORDER: 1
2014.11.03 16:56:23.091 2014.01.03 00:00  MACDcrossBTCUSD BTCUSD,Daily: open #1 buy 12.00 BTCUSD at 792.637 ok
2014.11.03 16:56:23.087 2014.01.01 00:00  MACDcrossBTCUSD BTCUSD,Daily: Start of MACD cross trading.
2014.11.03 16:56:23.085 MACDcrossBTCUSD test started

 

The script is as follows: 

int lots          = 0;
int thisBarTrade  = 0;
bool haveBought   = false;

int init()
{
   Print("Start of MACD cross trading.");
   return(0);
}

int deinit()
{
   Print("End of MACD cross trading.");
   return(0);
}

int start()
{
   if (Bars != thisBarTrade && OrdersTotal() == 0) {
      thisBarTrade = Bars;
      
      int orderTicket;
       
      if (iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1) > 0 &&
          iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2) < 0 &&
          haveBought == false) {
         // Buy when MACD is crossing from sell zone to buy zone
         
         lots = AccountBalance() / Ask;
         orderTicket = OrderSend(_Symbol,OP_BUY,lots,Ask,3,0,0,"MACD cross Buy",12345,0,clrBlue);
         
         if (orderTicket < 0) {
            Print("BUY OrderSend failed with error #", GetLastError());
         } else {
            Print("ORDER: ", orderTicket);
            haveBought = true;
         }
         
      } else if (iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1) < 0 &&
                 iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2) > 0 &&
                 haveBought == true) {
         // Sell when MACD is crossing from buy zone to sell zone
         
         orderTicket = OrderSend(_Symbol,OP_SELL,lots,Bid,3,0,0,"MACD cross Sell",12345,0,clrRed);
         
         if (orderTicket < 0) {
            Print("SELL OrderSend failed with error #", GetLastError());
         } else {
            Print("ORDER: ", orderTicket);
            haveBought = false;
         }
         
      }
      
   }
   
   // return
   return(0);
}
whroeder1
13596
whroeder1 2014.11.04 00:30  
ChetManley: and then nothing happens.
   if (Bars != thisBarTrade && OrdersTotal() == 0) {
You open a buy. Nothing is supposed to happen until that trade closes..
somecdnguy4
9
somecdnguy4 2014.11.04 02:32  
Ahh, yes! I didn't realize you have to close the trades as well. Thanks!
/
To add comments, please log in or register