stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Need to help with "tradehour"
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=3161
Page 1 of 2
Author:  ROOI [ Sat Sep 21, 2013 10:52 am ]
Post subject:  Need to help with "tradehour"

Hi

Im trying to add to this code a set number to the "tradehour" and "trademinutes" and then create multiple blocks of these code to have different time for the EA to open trades.

Here is the code

Code: Select all

//+------------------------------------------------------------------+
//|                                                  time trader.mq4 |
//|                            Copyright © 2012, http://www.FxAutomated.com |
//|                                       http://www.FxAutomated.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, http://www.FxAutomated.com"
#property link      "http://www.FxAutomated.com"
//---- input parameters
extern string   time_trader_v2="visit http://www.FxAutomated.com for more!!!";
extern double   Lots=0.1;
extern int      TakeProfit=20;
extern int      StopLoss=20;
extern int      Slip=5;
extern string   TradeSettings="Empty4 time(min-max): hours 0-23, minutes 0-59, seconds 0-59";
extern bool     AllowBuy=true;
extern bool     AllowSell=true;
extern int      TradeHour= 0;
extern int      TradeMinutes= 0;
extern string   MagicNumbers="To be changed in case of conflict with other EAs";
extern int      BuyMagicNumber =10001;
extern int      SellMagicNumber =10002;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   Alert("Visit http://www.FxAutomated.com for more goodies!");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert starts                                  |
//+------------------------------------------------------------------+

int start()
  {
//----
int StopMultd,Sleeper=1;



int digits=MarketInfo("EURUSD",MODE_DIGITS);
StopMultd=10;
double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double SL=NormalizeDouble(StopLoss*StopMultd,Digits);
int Slippage=Slip*StopMultd;

// Calculate stop loss
double slb=NormalizeDouble(Ask-SL*Point,Digits);
double sls=NormalizeDouble(Bid+SL*Point,Digits);

// Calculate take profit
double tpb=NormalizeDouble(Ask+TP*Point,Digits);
double tps=NormalizeDouble(Bid-TP*Point,Digits);

//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
  for(int i=100; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {
          if(OrderMagicNumber()==BuyMagicNumber) {int halt1=1;}
          if(OrderMagicNumber()==SellMagicNumber) {int halt2=1;}
        }
     }
}
//-------------------------------------------------------------------+


if((halt1!=1)&&(AllowBuy==true)){// halt1

// Buy criteria
if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Buy
 {
   int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"time trader buy order ",BuyMagicNumber,0,Blue);
   if(openbuy<1){int buyfail=1;}
 }
 
}// halt1
 
if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
 // Sell criteria
 if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Sell
 {
   int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"time trader sell order ",SellMagicNumber,0,Green);
   if(opensell<1){int sellfail=1;}
 }
 
}// halt2

//-----------------------------------------------------------------------------------------------------
if(OrdersTotal()>0){
  for(i=1; i<=OrdersTotal(); i++){          // Cycle searching in orders
  
      if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
        
          if((OrderMagicNumber()==BuyMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,slb,tpb,0,CLR_NONE); }
          if((OrderMagicNumber()==SellMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,sls,tps,0,CLR_NONE); }

        }
     }
}
//-------------------------------------------------------------------+
// Error processing
//-------------------------------------------------------------------+
if(buyfail==1||sellfail==1){
int Error=GetLastError();
  if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
  if(Error==133){Alert("Trading prohibited.");}
  if(Error==2){Alert("Common error.");}
  if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}

}


//-------------------------------------------------------------------
   return(0);
  }
//+-----------------------------------
Author:  ROOI [ Sat Sep 21, 2013 11:02 am ]
Post subject:  Re: Need to help with "tradehour"

At the moment the "tradehour" and trademin" can only equal one specific time.

extern int TradeHour= 0;
extern int TradeMinutes= 0;



and


// Buy criteria
if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Buy
{
int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"time trader buy order ",BuyMagicNumber,0,Blue);
if(openbuy<1){int buyfail=1;}
}

}// halt1

if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
// Sell criteria
if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Sell
{
int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"time trader sell order ",SellMagicNumber,0,Green);
if(opensell<1){int sellfail=1;}
}

}// halt2



So what I want to know is if I will be able to set multiple times in the code.

For example how can I add to the code "tradehours" and "trademinutes" in the buy and sell criteria
and not have the error that the tradehour has been defined.There must be a simple way to be able for the EA to check the multiple buy/sell criteria with different values.


Can anyone help me please.... :|
Author:  ROOI [ Sat Sep 21, 2013 11:06 am ]
Post subject:  Re: Need to help with "tradehour"

am I at least trying to change the correct bit of code in the EA?
Author:  dietcoke [ Sun Sep 22, 2013 9:57 am ]
Post subject:  Re: Need to help with "tradehour"

ROOI wrote: So what I want to know is if I will be able to set multiple times in the code.

For example how can I add to the code "tradehours" and "trademinutes" in the buy and sell criteria
and not have the error that the tradehour has been defined.There must be a simple way to be able for the EA to check the multiple buy/sell criteria with different values.


Can anyone help me please.... :|
ROOI wrote:am I at least trying to change the correct bit of code in the EA?
The easiest, but least elegant way to do this would be to have a separate extern variable for each hour and minute you want to trade at

eg

Code: Select all

extern int tradehour1 = 6;
extern int trademinute1 =45; 
etc...

you then need to include the new variables in you trade open test eg:

Code: Select all

if ((TradeHour==Hour()&&TradeMinutes==Minute()) || (TradeHour1==Hour()&&TradeMinutes1==Minute()) ) //Signal Buy

Obviously, this becomes an issue if you want to use lots of different possible entry times. In that case you will have to significantly rewrite your code to hold the trade times in an array which you loop through and test for each time in turn
Author:  fxdaytrader [ Sun Sep 22, 2013 10:43 am ]
Post subject:  Re: Need to help with "tradehour"

In another thread you asked the same question, I suggested to use Steves tradinghour-code, I think it should work well, if you set the difference between start and end-hour to one minute (in the external variable, for example:
+07:00,-07:01

I have added Steves/Baludas tradingtime-checking stuff to the EA.

I spotted further that there are two variables (halt1,halt2) in the ea, but they never get resetted if once set to 1. I think this is a bloop, because if there are no open trades the halt1,halt2 variables should be set to 0 again ...

See attached ...
Author:  ROOI [ Mon Sep 23, 2013 7:28 am ]
Post subject:  Re: Need to help with "tradehour"

Thanks fort he help guys.Will have a look at.You have been a great help already
Author:  ROOI [ Mon Sep 23, 2013 4:07 pm ]
Post subject:  Re: Need to help with "tradehour"

fxdaytrader wrote:In another thread you asked the same question, I suggested to use Steves tradinghour-code, I think it should work well, if you set the difference between start and end-hour to one minute (in the external variable, for example:
+07:00,-07:01

I have added Steves/Baludas tradingtime-checking stuff to the EA.

I spotted further that there are two variables (halt1,halt2) in the ea, but they never get resetted if once set to 1. I think this is a bloop, because if there are no open trades the halt1,halt2 variables should be set to 0 again ...

See attached ...

Hi

Thanks again for your help.You have been generous.I have added in the times like you said but nothing happens at the times specified.(I want it to open a hedged order)

Please let me now if there is something I else I need to do or can try to do.

Once again.Thanks for your effort.
Author:  ROOI [ Mon Sep 23, 2013 4:17 pm ]
Post subject:  Re: Need to help with "tradehour"

dietcoke wrote:
ROOI wrote: So what I want to know is if I will be able to set multiple times in the code.

For example how can I add to the code "tradehours" and "trademinutes" in the buy and sell criteria
and not have the error that the tradehour has been defined.There must be a simple way to be able for the EA to check the multiple buy/sell criteria with different values.


Can anyone help me please.... :|
ROOI wrote:am I at least trying to change the correct bit of code in the EA?
The easiest, but least elegant way to do this would be to have a separate extern variable for each hour and minute you want to trade at

eg

Code: Select all

extern int tradehour1 = 6;
extern int trademinute1 =45; 
etc...

you then need to include the new variables in you trade open test eg:

Code: Select all

if ((TradeHour==Hour()&&TradeMinutes==Minute()) || (TradeHour1==Hour()&&TradeMinutes1==Minute()) ) //Signal Buy

Obviously, this becomes an issue if you want to use lots of different possible entry times. In that case you will have to significantly rewrite your code to hold the trade times in an array which you loop through and test for each time in turn

Hi so I have added the code as follows :

Code: Select all

//+------------------------------------------------------------------+
//|                                                  time trader.mq4 |
//|                            Copyright © 2012, www.FxAutomated.com |
//|                                       http://www.FxAutomated.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, www.FxAutomated.com"
#property link      "http://www.FxAutomated.com"
//---- input parameters
extern string   time_trader_v2="visit www.FxAutomated.com for more!!!";
extern double   Lots=0.1;
extern int      TakeProfit=20;
extern int      StopLoss=20;
extern int      Slip=5;
extern string   TradeSettings="Empty4 time(min-max): hours 0-23, minutes 0-59, seconds 0-59";
extern bool     AllowBuy=true;
extern bool     AllowSell=true;
extern int      TradeHour= 0;                                
extern int      TradeMinutes= 0;
extern int      Tradehour1 = 0; //added here  PLEASE GO DOWN TO WHERE i ADDED AGAIN                           
extern int      Trademinute1 =0; //added here PLEASE GO DOWN TO WHERE i ADDED AGAIN
extern string   MagicNumbers="To be changed in case of conflict with other EAs";
extern int      BuyMagicNumber =10001;
extern int      SellMagicNumber =10002;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   Alert("Visit www.FxAutomated.com for more goodies!");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert starts                                  |
//+------------------------------------------------------------------+

int start()
  {
//----
int StopMultd,Sleeper=1;



int digits=MarketInfo("EURUSD",MODE_DIGITS);
StopMultd=10;
double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double SL=NormalizeDouble(StopLoss*StopMultd,Digits);
int Slippage=Slip*StopMultd;

// Calculate stop loss
double slb=NormalizeDouble(Ask-SL*Point,Digits);
double sls=NormalizeDouble(Bid+SL*Point,Digits);

// Calculate take profit
double tpb=NormalizeDouble(Ask+TP*Point,Digits);
double tps=NormalizeDouble(Bid-TP*Point,Digits);

//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
  for(int i=100; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {
          if(OrderMagicNumber()==BuyMagicNumber) {int halt1=1;}
          if(OrderMagicNumber()==SellMagicNumber) {int halt2=1;}
        }
     }
}
//-------------------------------------------------------------------+


if((halt1!=1)&&(AllowBuy==true)){// halt1

// Buy criteria
if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Buy


//THIS IS WHERE ADDED IT IN !!!!!  Signal Buy    THIS IS WHERE ADDED IT IN !!!!!

if ((TradeHour==Hour() &&(TradeMinutes==Minute()) || (TradeHour1==Hour()&&TradeMinutes1==Minute()) ) //Signal Buy   
 {
   int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"time trader buy order ",BuyMagicNumber,0,Blue);
   if(openbuy<1){int buyfail=1;}
 }
 
}// halt1
 
if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
 // Sell criteria
 if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Sell
//THIS IS WHERE ADDED IT IN !!!!!  Signal Sell THIS IS WHERE ADDED IT IN !!!!!

 if ((TradeHour==Hour() &&(TradeMinutes==Minute()) || (TradeHour1==Hour()&&TradeMinutes1==Minute()) ) //Signal sell
 {
   int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"time trader sell order ",SellMagicNumber,0,Green);
   if(opensell<1){int sellfail=1;}
 }

}// halt2

//-----------------------------------------------------------------------------------------------------
if(OrdersTotal()>0){
  for(i=1; i<=OrdersTotal(); i++){          // Cycle searching in orders
  
      if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
        
          if((OrderMagicNumber()==BuyMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,slb,tpb,0,CLR_NONE); }
          if((OrderMagicNumber()==SellMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,sls,tps,0,CLR_NONE); }

        }
     }
}
//-------------------------------------------------------------------+
// Error processing
//-------------------------------------------------------------------+
if(buyfail==1||sellfail==1){
int Error=GetLastError();
  if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
  if(Error==133){Alert("Trading prohibited.");}
  if(Error==2){Alert("Common error.");}
  if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}

}


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

When I compile it gives 4 errors :
'TradeHour1' - variable not defined D:\alpri demo indice\experts\time trader.mq4 (84, 55)
'TradeMinutes1' - variable not defined D:\alpri demo indice\experts\time trader.mq4 (84, 75)
'TradeHour1' - variable not defined D:\alpri demo indice\experts\time trader.mq4 (98, 56)
'TradeMinutes1' - variable not defined D:\alpri demo indice\experts\time trader.mq4 (98, 76)

Please help as to where I can put the lines of code.

Thanks very much for your time and energy in replying to noob like me
Author:  fxdaytrader [ Mon Sep 23, 2013 4:28 pm ]
Post subject:  Re: Need to help with "tradehour"

ROOI wrote:I have added in the times like you said but nothing happens at the times specified.(I want it to open a hedged order)
I do not know what you are doing, but it works.
Maybe you did some errors when inserting the tradingtimes, I added an example. And that works, tested some minutes ago with trading +19.19,-19.20 (insert your local time) ....
What maybe not works is the tp/sl setting (during my test I commentet that out and replaced by a print-call) ...

So, you asked for help regarding the tradingtimes-stuff. That is done because it is working. :idea:
For your other problems (and there are some, I am sure) you should open a new thread or ask new questions ;)

See attached
Author:  ROOI [ Mon Sep 23, 2013 4:44 pm ]
Post subject:  Re: Need to help with "tradehour"

fxdaytrader wrote:
ROOI wrote:I have added in the times like you said but nothing happens at the times specified.(I want it to open a hedged order)
I do not know what you are doing, but it works.
Maybe you did some errors when inserting the tradingtimes, I added an example. And that works, tested some minutes ago with trading +19.19,-19.20 (insert your local time) ....
What maybe not works is the tp/sl setting (during my test I commentet that out and replaced by a print-call) ...

So, you asked for help regarding the tradingtimes-stuff. That is done because it is working. :idea:
For your other problems (and there are some, I am sure) you should open a new thread or ask new questions ;)

See attached

HI

Yip.Got it to open trades.Works well.The problem is it open a hedge trade every tic for that minute that is "trading hours" are on.So its like a little to many stacked orders.

IS there anyway to change that it executes once per trading interval ?

Once again thanks for the help.You guy are really helping me understand a scratch of the surface of you guys do.
All times are UTC Page 1 of 2