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
I have backtesting the same code above and it's work both buy and sell order.
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.
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);}
}
}
}
//+------------------------------------------------------------------+
- 2021.03.06
- www.mql5.com
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.