EA which opens and closes positions at same TimeFrame

Back to topics list To post a new topic, please log in or register
avatar
11
marosvrtak 2015.06.14 10:41
 

Hi, Im new in mql coding. Couple days ago i tried do make a simple EA. I wanted open and closes postions at choosen timeframe. For Example: timeframe H4 pair EURUSD: EA will start placing orders at 00:00. So my first trade is: buy at 00:00 and closes at 03:59. Second one: sell 04:00 closes at 07:59 then buy then sell etc. always change buy and sell. There cannot be situations when two trades are in same mode in row.

i wrote a simple code:

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)==true && OrderMagicNumber()==mn && (h==3 || h==7 || h==11 || h==15 || h==19 || h==23) && m==59)

      {

         if(OrderType()==OP_BUY) {close=OrderClose(OrderTicket(), lot, Bid, 2, 0);}

         if(OrderType()==OP_SELL) {close=OrderClose(OrderTicket(), lot, Ask, 2, 0);}

      }

   }

     

     for(counter=0;counter<OrdersTotal();counter++)

   {

      if(OrderSelect(counter, SELECT_BY_POS, MODE_TRADES)&& OrderMagicNumber()==mn)

      {return(0);}

   }

         

  if(d<7 && (h==0 || h==8 || h==16) && m<5 )

    {ticket = OrderSend( Symbol(), OP_SELL, lot, Bid, 0, Bid+SL, Bid-TP, NULL, mn, 0, 0);}

  if(d<7 && (h==4 || h==12 || h==20) && m<5 )

    {ticket = OrderSend( Symbol(), OP_BUY, lot, Ask, 0, Ask-SL, Ask+TP, NULL, mn, 0, 0);} 

   

   return(0);

  }

 But there is a problem: it doesnt matter which TF is set in strategy tester, it always testing for H4. I can manually write code for H1 or for M30 or for any others, but i searching for universal solution, it menas, i will set TF in strategy tester no in code. 

Please somebody help me 

avatar
Moderator
3206
angevoyageur 2015.06.14 12:20 #
 

Hello,

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


This time, I edited it for you.

avatar
11
marosvrtak 2015.06.14 13:28 #
 
angevoyageur:

Hello,

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


This time, I edited it for you.

thanks, i will remember it
avatar
13012
WHRoeder 2015.06.14 14:53 #
 
marosvrtak:

So my first trade is: buy at 00:00 and closes at 03:59. Second one: sell 04:00 closes at 07:59

But there is a problem: it doesnt matter which TF is set in strategy tester, it always testing for H4.

I can manually write code for H1 or for M30 or for any others, but i searching for universal solution, it menas, i will set TF in strategy tester no in code.

Don't write manual code, don't assume a TF.

Just code it to do exactly what you just said, open at the start of a new bar, close one minute before the end of the current bar.

avatar
11
marosvrtak 2015.06.14 17:20 #
 

I have already soleved the problem how to open orders based on choosen TF in strategy tester:

oprice = iOpen(Symbol(), PERIOD_CURRENT, 0);
ticket = OrderSend( Symbol(), OP_SELL, lot, oprice, 0, 0, 0, NULL, mn, 0, 0);

 now when i set in strategy tester any TF it always opens orders at open price of choosen TF, thats works correct.

but now i dont know how to close this order. For example: TF: M30 start at 00:00 and it should closes this order at 00:30 or couple seconds before closing. I tried to use

cprice = iClose(Symbol(), PERIOD_CURRENT, 0);
if(OrderType()==OP_SELL) {close=OrderClose(OrderTicket(), lot, cprice, 2, 0);

 but it doesnt works. Any solutions?

avatar
13012
WHRoeder 2015.06.14 20:34 #
 
cprice = iClose(Symbol(), PERIOD_CURRENT, 0);

if(OrderType()==OP_SELL) {close=OrderClose(OrderTicket(), lot, cprice, 2, 0);
These two lines have nothing to do with when. Post your code where you decide it's a minute till end of the bar.
avatar
11
marosvrtak 2015.06.14 23:56 #
 
WHRoeder:
These two lines have nothing to do with when. Post your code where you decide it's a minute till end of the bar.
//+------------------------------------------------------------------+
//|                                                   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;
   double oprice, cprice;
   
   oprice = iOpen(Symbol(), PERIOD_CURRENT, 0);
   cprice = iClose(Symbol(), PERIOD_CURRENT, 1);
   
  
   for(counter=0;counter<OrdersTotal();counter++)
   {
      if(OrderSelect(counter, SELECT_BY_POS, MODE_TRADES)==true && OrderMagicNumber()==mn)
      {
         if(OrderType()==OP_BUY) {close=OrderClose(OrderTicket(), lot, cprice, 2, 0);}
         if(OrderType()==OP_SELL) {close=OrderClose(OrderTicket(), lot, cprice, 2, 0);}
      }
   }
   
  
   for(counter=0;counter<OrdersTotal();counter++)
   {
      if(OrderSelect(counter, SELECT_BY_POS, MODE_TRADES)&& OrderMagicNumber()==mn)
      {return(0);}
   }
 
   ticket = OrderSend( Symbol(), OP_SELL, lot, oprice, 0, 0, 0, NULL, mn, 0, 0);
 
     return(0);
  }
  
//+------------------------------------------------------------------+
i dont know how to close order. It doesnt matter which TF is set I want close order at close price( or couple second before closing) of choosen TF.
Back to topics list  

To add comments, please log in or register