change in coding.

 

Hello Everybody;

I have a robot program below. I want to make changes but couldn't succeed.

Working system of the robot; trading in daily candles. When the daily candle is closed, the next day when the candle is opened, it opens both buy and sell. Puts 10 pips of profit. I do not want it that way. When the daily candle closes, when the next candle is opened, if the candle is closing down, let the system sell, if it is closed up, it does buy. I could not change it. can you help me? thanks.


//+------------------------------------------------------------------+

//|                                                     day open_baris.mq4 |

//|                        Copyright 2014, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2014, MetaQuotes Software Corp."

#property link      "http://www.mql5.com"

#property version   "2.00"

#property strict

input double LOT=0.01;

extern int Profit=100;

extern int Stoploss=150;

int bars;

bool trade;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

trade=false;

bars=iBars(Symbol(),PERIOD_D1);

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

if (bars!=iBars(Symbol(),PERIOD_D1)){bars=iBars(Symbol(),PERIOD_D1);trade=true;}



if (trade){

            OrderSend(Symbol(),OP_BUY,LOT,Ask,2,Bid-Stoploss*Point,0,NULL,0123456,0,clrRed);

            OrderSend(Symbol(),OP_SELL,LOT,Bid,2,Ask+Stoploss*Point,0,NULL,0123456,0,clrRed);

            trade=false;

          }

   

  int tot=OrdersTotal();

  

  for(int i=0; i<=tot;i++)

  {OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

   if (OrderSymbol()==Symbol() &&OrderMagicNumber()==0123456)

      {

       if(OrderType()==OP_BUY){if (Bid>= OrderOpenPrice()+Profit*Point)OrderClose(OrderTicket(),LOT,Bid,2,clrAqua);}

             

       if(OrderType()==OP_SELL){if (Ask<= OrderOpenPrice()-Profit*Point)OrderClose(OrderTicket(),LOT,Ask,2,clrAqua);}

      }

  

  }

  

  }

//+------------------------------------------------------------------+


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2021.03.02
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Files:
Screenshot_2.jpg  133 kb
baris_.mq4  3 kb
 

You should add trading condition for both buy and sell orders.

//+------------------------------------------------------------------+

//|                                                     day open_baris.mq4 |

//|                        Copyright 2014, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2014, MetaQuotes Software Corp."

#property link      "http://www.mql5.com"

#property version   "2.00"

#property strict

input double LOT=0.01;

extern int Profit=100;

extern int Stoploss=150;

int bars;

bool trade;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

trade=false;

bars=iBars(Symbol(),PERIOD_D1);

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

if (bars!=iBars(Symbol(),PERIOD_D1)){bars=iBars(Symbol(),PERIOD_D1);trade=true;}



if (trade){
            if(iClose(NULL, PERIOD_D1, 1) > iClose(NULL, PERIOD_D1, 2)){     // Condition for buy order
              OrderSend(Symbol(),OP_BUY,LOT,Ask,2,Bid-Stoploss*Point,0,NULL,0123456,0,clrRed);
            }
            else if(iClose(NULL, PERIOD_D1, 1) < iClose(NULL, PERIOD_D1, 2)){ // Condition for sell order
              OrderSend(Symbol(),OP_SELL,LOT,Bid,2,Ask+Stoploss*Point,0,NULL,0123456,0,clrRed); 
            }
            trade=false;
          }

   

  int tot=OrdersTotal();

  

  for(int i=0; i<=tot;i++)

  {OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

   if (OrderSymbol()==Symbol() &&OrderMagicNumber()==0123456)

      {

       if(OrderType()==OP_BUY){if (Bid>= OrderOpenPrice()+Profit*Point)OrderClose(OrderTicket(),LOT,Bid,2,clrAqua);}

             

       if(OrderType()==OP_SELL){if (Ask<= OrderOpenPrice()-Profit*Point)OrderClose(OrderTicket(),LOT,Ask,2,clrAqua);}

      }

  

  }

  

  }

//+------------------------------------------------------------------+
 

Thank you for your help but it only sells. does not do buy operations. Likewise, if the candle has closed up, I want buy operations to do that too. thanks.

now there is only sell. thanks

there is a example in the picture in blow

Files:
 

I have backtesting the same code above and it's work both buy and sell order.

Files:
Capture.PNG  27 kb
2.PNG  7 kb
 

ok. i have tryed again. it is working now. At Dax it is working perfect but with stoploss=1500 Tp:100. if you want, i will send demo. now you know this working the best. i have a questions.

in normaly i am trying 100 dollar in back test. i want to this thing.

on 100 dollar = 0.01 lot

on 200 dollar = 0.02lot

0n 400 dollar = 0.04 lot

on 800 dollar = 0.08 lot 

and then go go go...

When the account is doubled, I want the lot amount to be doubled. now it opens 0.01 at $ 100, 0.01 at $ 200, 0.01 at $ 400.

thanks...

 

This should work.

//+------------------------------------------------------------------+

//|                                                     day open_baris.mq4 |

//|                        Copyright 2014, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2014, MetaQuotes Software Corp."

#property link      "http://www.mql5.com"

#property version   "2.00"

#property strict

extern double LOT=0.01;

extern int Profit=100;

extern int Stoploss=150;

int bars;

bool trade;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

trade=false;

bars=iBars(Symbol(),PERIOD_D1);

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {
    // auto lot
    int count = 1;
    static double autoLot = LOT;
    while(AccountBalance() > 100.0*count){
      LOT = autoLot*count;
      count++;
    }

if (bars!=iBars(Symbol(),PERIOD_D1)){bars=iBars(Symbol(),PERIOD_D1);trade=true;}



if (trade){
            if(iClose(NULL, PERIOD_D1, 1) > iClose(NULL, PERIOD_D1, 2)){     // Condition for buy order
              OrderSend(Symbol(),OP_BUY,LOT,Ask,2,Bid-Stoploss*Point,0,NULL,0123456,0,clrRed);
            }
            else if(iClose(NULL, PERIOD_D1, 1) < iClose(NULL, PERIOD_D1, 2)){ // Condition for sell order
              OrderSend(Symbol(),OP_SELL,LOT,Bid,2,Ask+Stoploss*Point,0,NULL,0123456,0,clrRed); 
            }
            trade=false;
          }

   

  int tot=OrdersTotal();

  

  for(int i=0; i<=tot;i++)

  {OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

   if (OrderSymbol()==Symbol() &&OrderMagicNumber()==0123456)

      {

       if(OrderType()==OP_BUY){if (Bid>= OrderOpenPrice()+Profit*Point)OrderClose(OrderTicket(),LOT,Bid,2,clrAqua);}

             

       if(OrderType()==OP_SELL){if (Ask<= OrderOpenPrice()-Profit*Point)OrderClose(OrderTicket(),LOT,Ask,2,clrAqua);}

      }

  

  }

  

  }

//+------------------------------------------------------------------+

Please change LOT external variable from input to extern so the robot can modify the lot size.

 
Thank you very much for your help. Nobody would have done this help to me. Finally, how can I add trade time to this?
For example: When doing a back test, let it run between 03:00 and 19:00. or to process between 10:00 and 22:00. Is it possible for you to code this too?
 

Put this function below OnTick function and call it before you place a trade.

bool tradingHour(){
  bool hour = false;
  if(Hour() >= 10 && Hour() < 22){
    hour = true;
  }
  return hour;
}
 

I did it but it gave 6 errors.


//+------------------------------------------------------------------+


//|                                                     baris.mq4 |


//|                        Copyright 2014, MetaQuotes Software Corp. |


//|                                              http://www.mql5.com |


//+------------------------------------------------------------------+


#property copyright "Copyright 2014, MetaQuotes Software Corp."


#property link      "http://www.mql5.com"


#property version   "2.00"


#property strict


extern double LOT=0.01;


extern int Profit=100;


extern int Stoploss=150;


int bars;


bool trade;


//+------------------------------------------------------------------+


//| Expert initialization function                                   |


//+------------------------------------------------------------------+


int OnInit()


  {


trade=false;


bars=iBars(Symbol(),PERIOD_D1);


   return(INIT_SUCCEEDED);


  }


//+------------------------------------------------------------------+


//| Expert deinitialization function                                 |


//+------------------------------------------------------------------+


void OnDeinit(const int reason)


  {


//---


   


  }


//+------------------------------------------------------------------+


//| Expert tick function                                             |


//+------------------------------------------------------------------+


void OnTick()


bool tradingHour(){

  bool hour = false;

  if(Hour() >= 10 && Hour() < 22){

    hour = true;

  }

  return hour;

}


  {

    // auto lot

    int count = 1;

    static double autoLot = LOT;

    while(AccountBalance() > 100.0*count){

      LOT = autoLot*count;

      count++;

    }


if (bars!=iBars(Symbol(),PERIOD_D1)){bars=iBars(Symbol(),PERIOD_D1);trade=true;}




if (trade){

            if(iClose(NULL, PERIOD_D1, 1) > iClose(NULL, PERIOD_D1, 2)){     // Condition for buy order

              OrderSend(Symbol(),OP_BUY,LOT,Ask,2,Bid-Stoploss*Point,0,NULL,0123456,0,clrRed);

            }

            else if(iClose(NULL, PERIOD_D1, 1) < iClose(NULL, PERIOD_D1, 2)){ // Condition for sell order

              OrderSend(Symbol(),OP_SELL,LOT,Bid,2,Ask+Stoploss*Point,0,NULL,0123456,0,clrRed); 

            }

            trade=false;

          }


   


  int tot=OrdersTotal();


  


  for(int i=0; i<=tot;i++)


  {OrderSelect(i,SELECT_BY_POS,MODE_TRADES);


   if (OrderSymbol()==Symbol() &&OrderMagicNumber()==0123456)


      {


       if(OrderType()==OP_BUY){if (Bid>= OrderOpenPrice()+Profit*Point)OrderClose(OrderTicket(),LOT,Bid,2,clrAqua);}


             


       if(OrderType()==OP_SELL){if (Ask<= OrderOpenPrice()-Profit*Point)OrderClose(OrderTicket(),LOT,Ask,2,clrAqua);}


      }


  


  }


  


  }


//+------------------------------------------------------------------+

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2021.03.06
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

Please remove all the unnecessary empty lines to make it easier for others to read.

 
I mean, place the function below OnTick function block, not inside it. And then just call it in your buy and sell condition.
Reason: