Download MetaTrader 5

open trades every new candle

To add comments, please log in or register
Maros Vrtak
741
Maros Vrtak  

Hi, i want to open an order of every new candle lets say on H1. i wrote this code, but it always wait for close on opened order. I mean i want open 24 orders per day (H1 TF).

 

//+------------------------------------------------------------------+
//|                                                   marospokus.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function 
extern double SLpips=200;                                  
extern double TPpips=1000;
extern double lot = 0.1;
//+------------------------------------------------------------------+
int init()
  {
//---
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
  {
//---
   int d = DayOfWeek();
   int h = TimeHour(TimeCurrent());
   int m = TimeMinute(TimeCurrent());
   int ticket, counter, close;
   double SL=SLpips*Point;
   double TP=TPpips*Point;
   int mn=1;
   
    for(counter=0;counter<OrdersTotal();counter++)
   {
      if(OrderSelect(counter, SELECT_BY_POS, MODE_TRADES)&& OrderMagicNumber()==mn)
      {return(0);}
   }
 
  if(d<7 && h<24 && m==15 )
    {ticket = OrderSend( Symbol(), OP_BUY, lot, Ask, 0, Ask-SL, Ask+TP, NULL, mn, 0, 0);} 

   
      return(0);
  }
  
//+------------------------------------------------------------------+

 How can I change it to open trade every hour?

Keith Watford
Moderator
10611
Keith Watford  
    for(counter=0;counter<OrdersTotal();counter++)
   {
      if(OrderSelect(counter, SELECT_BY_POS, MODE_TRADES)&& OrderMagicNumber()==mn)
      {return(0);}
   }
Delete this
Maros Vrtak
741
Maros Vrtak  

when i delete this part

for(counter=0;counter<OrdersTotal();counter++)
   {
      if(OrderSelect(counter, SELECT_BY_POS, MODE_TRADES)&& OrderMagicNumber()==mn)
      {return(0);}
   }

 EA places lot of orders in one minute. as you can see in attached picture. and its still wait for SL or TP. I want open order on every hour

Keith Watford
Moderator
10611
Keith Watford  

Oh yes, I forgot about that

   static int trade_hour=-1;
   int d = DayOfWeek();
   int h = TimeHour(TimeCurrent());
   int m = TimeMinute(TimeCurrent());
   int ticket, counter, close;
   double SL=SLpips*Point;
   double TP=TPpips*Point;
   int mn=1;
   
  if(trade_hour!=h && d<7 && h<24 && m==15 )
    {
    ticket = OrderSend( Symbol(), OP_BUY, lot, Ask, 0, Ask-SL, Ask+TP, NULL, mn, 0, 0);
    trade_hour=h;
    } 

 

 try this

Maros Vrtak
741
Maros Vrtak  
GumRai:

Oh yes, I forgot about that

 

 try this

perfect, thanks a lot, you helped me a lot
Maros Vrtak
741
Maros Vrtak  
If I may ask. How to make it universl for any timeframe. Because now it doesnt matter which TF I set in stretegy tester, it always H1. I mean when i set in stretegy tester for example M15 i want to place orders every 15min. 
whroeder1
16112
whroeder1  
marosvrtak: I set in stretegy tester, it always H1. I
Wrong, it is what you set it to.
Maros Vrtak
741
Maros Vrtak  
WHRoeder:
marosvrtak: I set in stretegy tester, it always H1. I
Wrong, it is what you set it to.

it works only for H1. Any others TF doesnt work.
whroeder1
16112
whroeder1  
Maros Vrtak
741
Maros Vrtak  

As I said. It doesnt matter which TF is set in strategy. This code 

if(trade_hour!=h && d<7 && h<24 && m==15 )
    {
    ticket = OrderSend( Symbol(), OP_BUY, lot, Ask, 0, Ask-SL, Ask+TP, NULL, mn, 0, 0);
    trade_hour=h;
    } 

 always test only on H1. I want to make it universal, it means, when I set in strategy tester H4, EA will places trade every 4 hour, so 00:00 04:00 08:00. when i set M5 it will be like 00:00 00:05 00:10 etc.

whroeder1
16112
whroeder1  
if(trade_hour!=h && d<7 && h<24 && m==15 )

Has nothing to do with the period of the chart. Add Print statements before and inside your if's including variable values and your alleged H1 period. I think you will find your if isn't being called at all.

As for the rest, Search "new bar." and drop your hard coded variables.

To add comments, please log in or register