Help with 1 trade per candle

 

I need help in adding code to my EA only to execute once per Candle and not per tick.

Acn someone please assist with the code and where to add it please?

// ------------------------------------------------------------------------------------------------
// EXTERN VARS
// ------------------------------------------------------------------------------------------------
int magic = 99999;
// Configuration
string www = "";
string MoneyManagementSettings = "---------------------------------------------";
// Money Management
extern double min_lots = 0.01;
double min_lots_increment = 0.01;
double account_risk = 100.0;
// Indicator
string IndicatorSettings = "---------------------------------------------";
int wpr_period=14; 
int shift = 1;

// ------------------------------------------------------------------------------------------------
// GLOBAL VARS
// ------------------------------------------------------------------------------------------------
string key = "";
// Ticket
int buy_tickets[50];
int sell_tickets[50];
// Lots
double buy_lots[50];
double sell_lots[50];
// Current Profit
double buy_profit[50];
double sell_profit[50];
// Open Price
double buy_price[50];
double sell_price[50];
// Indicator
double wpr1=0, wpr2=0;
// Number of orders
int buys = 0;
int sells = 0;
double total_buy_profit=0,total_sell_profit=0;
double total_buy_lots=0, total_sell_lots=0;
double buy_max_profit=0, buy_close_profit=0;
double sell_max_profit=0, sell_close_profit=0;
// Cuenta
double balance, equity;
int slippage=0;


// ------------------------------------------------------------------------------------------------
// START
// ------------------------------------------------------------------------------------------------

int start()
{  

  double point = MarketInfo(Symbol(), MODE_POINT);
  double dd=0;
  int ticket, i, n;
  double price;
  
  if (MarketInfo(Symbol(),MODE_DIGITS)==4 || MarketInfo(Symbol(),MODE_DIGITS)==2)
  {
    slippage = user_slippage;
  }
  else if (MarketInfo(Symbol(),MODE_DIGITS)==5 || MarketInfo(Symbol(),MODE_DIGITS)==3)
  {
    slippage = 1*user_slippage;
  }
  
  if(IsTradeAllowed() == false) 
  {
    Comment("");
    return;  
  }
  
  // Updating current status
  InitVars();
  UpdateVars();
  SortByLots();
  ShowData();
  ShowLines();
  
  Robot();
  
  return(0);
}
 
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum
 

Please use OnInit() and OnTick() instead of clashing everything into start() which is deprecated.

Things that need to be run once on startup go into OnInit. Things that need to be updated regularly go into OnTick.

// ------------------------------------------------------------------------------------------------
// EXTERN VARS
// ------------------------------------------------------------------------------------------------
input int magic = 99999;
// Configuration
input string www = "";
sinput string MoneyManagementSettings = "---------------------------------------------";
// Money Management
input double min_lots = 0.01;
input min_lots_increment = 0.01;
input account_risk = 100.0;
// Indicator
sinput string IndicatorSettings = "---------------------------------------------";
input int wpr_period=14; 
input int shift = 1;

// ------------------------------------------------------------------------------------------------
// GLOBAL VARS
// ------------------------------------------------------------------------------------------------
...
double point;
double dd=0;
int ticket, i, n;
double price;

int OnInit() {
  point = MarketInfo(Symbol(), MODE_POINT);
  
  if (MarketInfo(Symbol(),MODE_DIGITS)==4 || MarketInfo(Symbol(),MODE_DIGITS)==2)
  {
    slippage = user_slippage;
  }
  else if (MarketInfo(Symbol(),MODE_DIGITS)==5 || MarketInfo(Symbol(),MODE_DIGITS)==3)
  {
    slippage = 10*user_slippage;
  }
  
  if(IsTradeAllowed() == false) 
  {
    Comment("");
    return INIT_FAILED;
  }
  InitVars();
  return INIT_SUCCEEDED;
}

void OnTick() {
  UpdateVars();
  SortByLots();
  ShowData();
  ShowLines();
  Robot();
}

If you need to perform tasks only once per candle, search for new bar check.

 
William Roeder:
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum

ok, so can you help me with what ever code I should add to where for a time factor of 60 minutes or a sleep time of 60 minutes?

 
lippmaje:

Please use OnInit() and OnTick() instead of clashing everything into start() which is deprecated.

Things that need to be run once on startup go into OnInit. Things that need to be updated regularly go into OnTick.

If you need to perform tasks only once per candle, search for new bar check.

I am a bit lost to what you did or said. I am not a programmer and very new to this. I just want a simple add on in the code to ensure the EA does not open multiple orders at the same time when the conditions are right. Can you assist please?

 
ECI Invest:ok, so can you help me with what ever code I should add to where for a time factor of 60 minutes or a sleep time of 60 minutes?

You asked for new bar test, not a time test. And I gave you the code.

 
William Roeder:

You asked for new bar test, not a time test. And I gave you the code.

ok, it would be nice if you can show me where to add it in the original code, its all greek to me. Can you please show me?

 
ECI Invest:

I need help in adding code to my EA only to execute once per Candle and not per tick.

Acn someone please assist with the code and where to add it please?

// ------------------------------------------------------------------------------------------------
// EXTERN VARS
// ------------------------------------------------------------------------------------------------
int magic = 99999;
// Configuration
string www = "";
string MoneyManagementSettings = "---------------------------------------------";
// Money Management
extern double min_lots = 0.01;
double min_lots_increment = 0.01;
double account_risk = 100.0;
// Indicator
string IndicatorSettings = "---------------------------------------------";
int wpr_period=14; 
int shift = 1;

// ------------------------------------------------------------------------------------------------
// GLOBAL VARS
// ------------------------------------------------------------------------------------------------
string key = "";
// Ticket
int buy_tickets[50];
int sell_tickets[50];
// Lots
double buy_lots[50];
double sell_lots[50];
// Current Profit
double buy_profit[50];
double sell_profit[50];
// Open Price
double buy_price[50];
double sell_price[50];
// Indicator
double wpr1=0, wpr2=0;
// Number of orders
int buys = 0;
int sells = 0;
double total_buy_profit=0,total_sell_profit=0;
double total_buy_lots=0, total_sell_lots=0;
double buy_max_profit=0, buy_close_profit=0;
double sell_max_profit=0, sell_close_profit=0;
// Cuenta
double balance, equity;
int slippage=0;


// ------------------------------------------------------------------------------------------------
// START
// ------------------------------------------------------------------------------------------------

int start()
{  
   if(iVolume(Symbol(),0,0)==1)
   {
  double point = MarketInfo(Symbol(), MODE_POINT);
  double dd=0;
  int ticket, i, n;
  double price;
  
  if (MarketInfo(Symbol(),MODE_DIGITS)==4 || MarketInfo(Symbol(),MODE_DIGITS)==2)
  {
    slippage = user_slippage;
  }
  else if (MarketInfo(Symbol(),MODE_DIGITS)==5 || MarketInfo(Symbol(),MODE_DIGITS)==3)
  {
    slippage = 1*user_slippage;
  }
  
  if(IsTradeAllowed() == false) 
  {
    Comment("");
    return;  
  }
  }
  // Updating current status
  InitVars();
  UpdateVars();
  SortByLots();
  ShowData();
  ShowLines();
  
  Robot();
  
  return(0);
}
 
Mehmet Bastem:

Hey Mehmet, after adjusting the code and running a backtest there is no change, do you know the reason why perhaps?

 
ECI Invest:

I am a bit lost to what you did or said. I am not a programmer and very new to this. I just want a simple add on in the code to ensure the EA does not open multiple orders at the same time when the conditions are right. Can you assist please?

 
andrew4789:
I just save the time for the next possible trade in an array or variable, after OrderSend().
long nextTrade=TimeCurrent()+Period()* 60 * n

Then check if TimeCurrent>nextTrade before next OrderSend.

Reason: