Open orders once a day or more...

 

Hello all I have been having some challenges with what I want to accomplish with my EA. I would like to add an extern option which asks the user if he wants the EA to open one trade per day or multiple times. If user chooses true to open once a day then EA will open only once a day. If not, every time there is a set up the EA would open a trade. To give you an idea this is what it would look like:

int signal() {

if this happens then buy return (10)

if this happens then sell return (20)

if none happen then return (30)

}

int start (){

signal = signal();

if (signal !=30){

if (CountOpenOrdersHistoryPerDay(OP_BUY) + CountOpenOrdersHistoryPerDay(OP_SELL) + CountOpenOrdersPerDay(OP_BUY) + CountOpenOrdersPerDay(OP_SELL) == 0) {

if (signal == 10) {buy}

if (signal == 20) {sell}

}

}

return (0);

}


I would like to add an extern bool variable asking the user if it wants to do this operation once a day or more often but I'm having a hard time figuring it out. Where in the code can I add this true/false bool variable to ignore the

if (CountOpenOrdersHistoryPerDay(OP_BUY) + CountOpenOrdersHistoryPerDay(OP_SELL) + CountOpenOrdersPerDay(OP_BUY) + CountOpenOrdersPerDay(OP_SELL) == 0)

Basically if opentradeonceaday is false then skip code above.

I would really appreciate anyone's help. Thank you!

 
The bool is usually added as an {extern bool variable} declared globally. This is usually one of the first things anyone learns from mql4 if you have any more questions about this then check out the mql4.com book or the code base for some examples.
 
ubzen:
The bool is usually added as an {extern bool variable} declared globally. This is usually one of the first things anyone learns from mql4 if you have any more questions about this then check out the mql4.com book or the code base for some examples.

I don't think you understood my question or my problem. Maybe I didn't specified correctly, but I thought I was clear. Anyways, here I'll try again. I understand that if a bool variable is equal to true or 1, then it will execute the rest of the command between the { } but if it's false it will not execute it. What I'm trying to accomplish is

extern bool OpenOneTradeADay = True;

if (OpenOneTradeADay = true) then only open one order per day, which would check for total orders opened on that day and compare with the history.

else open as many orders as the signal gives.

What I'm trying to avoid is to recopy the whole command between the { } twice. My code is like this right now:

int signal() {
            if (conditions for a buy happens) return(10);
            if (conditions for a sell happens) return(20);
            return(30);
}
int start() {
         if (signal != 30) {//Open signal != 30
            if (CountOpenOrdersHistoryPerDay(OP_BUY) + CountOpenOrdersHistoryPerDay(OP_SELL) + CountOpenOrdersPerDay(OP_BUY) + CountOpenOrdersPerDay(OP_SELL) == 0) { //Open one trade per day
               if (signal == 10) { //Open BUY Signal
                                 }//Close Buy Signal
               if (signal == 20) {//Open Sell Signal
                                 }//Close Sell Signal
            }//Close one trade per day
         }//Close signal != 30
return(0)
}


Now If I remove the Close one trade per day parameters I know it will open as many trades as the signal gives. But I would like to add an extern bool variable that if it's true it will open only once a day, but if it's false, it will open as many as the signal gives, but I'm not sure where to put that bool and how to do it, cause if that new bool variable is false it will not execute the signal sent.

Anyone?

 

Give me a sec ... I'm working on it. In the main-time, the answer is to make it return(0) with a if statement before the order is placed. example if(Trade_OncePerDay==true && isOrderToday()==true){return(0);}

Updated: Here's what I was working on. It's a modified version of something I did earlier to suit your case. Again place the check b4 the order if your ea needs to do other stuff like modify stop-loss for example. Otherwise in my example below it just alerts and exits from the start.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extern string Setting1="true   =1_Trade_Per_Day";
extern string Setting2="false =Take_Every_Signal";
extern bool Trade_OncePerDay=true;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Ans, i, Signal=30, StopLoss=20, TakeProfit=40, 
    Magic=777, iMinute=60, TimeStamp, TradeFrequency=15;
double Avg_Current, Avg_Previus;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int start(){
    //~~~~~~~~~~
    if(Trade_OncePerDay==true && isOrderToday()==true){
        Alert("***Already Traded Today***");return(0);
    }
    //~~~~~~~~~~
    Avg_Current= iMA(NULL,0,30,0,MODE_SMMA,PRICE_CLOSE,1);
    Avg_Previus= iMA(NULL,0,30,0,MODE_SMMA,PRICE_CLOSE,2);
    //~~~~~~~~~~
    if(Signal==30){Signal=Signal();}
    //~~~~~~~~~~
    if(Signal!=30 && OrdersTotal()==0){
        if(TimeCurrent()>TimeStamp+TradeFrequency*iMinute){
            if(Signal==10 && Ask<Avg_Current){
                OrderSend(Symbol(),OP_BUY,1,Ask,3,Bid-StopLoss*Point,
                Bid+TakeProfit*Point,"BUY",Magic,0,Blue);
                Signal=30; TimeStamp=TimeCurrent();
            }
        }
        //~~~~~~~~~~
        if(TimeCurrent()>TimeStamp+TradeFrequency*iMinute){
            if(Signal==20 && Bid>Avg_Current){
                OrderSend(Symbol(),OP_SELL,1,Bid,3,Ask+StopLoss*Point,
                Ask-TakeProfit*Point,"SELL",Magic,0,Red);
                Signal=30; TimeStamp=TimeCurrent();
            }
        }
    }
    //~~~~~~~~~~
    Comment("Signal=",Signal,"\n");
    //~~~~~~~~~~
return(0);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~Signal-Function
double Signal(){Ans=30;
    //~~~~~~~~~~
    if(iOpen(NULL,PERIOD_M1,1)<Avg_Current
        && iClose(NULL,PERIOD_M1,1)>Avg_Current){
        Ans=10;
    }
    //~~~~~~~~~~
    if(iOpen(NULL,PERIOD_M1,1)>Avg_Current 
        && iClose(NULL,PERIOD_M1,1)<Avg_Current){
        Ans=20;
    }
    //~~~~~~~~~~
return(Ans);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~isOrderToday-Function
bool isOrderToday(){Ans=false;
    datetime Today=iTime(NULL,PERIOD_D1,0);
    //~~~~~~~~~~
    for(i=OrdersTotal()-1;i>=0;i--){
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true
            && OrderSymbol()==Symbol()){
            if(OrderOpenTime()>=Today){
                Ans=true;return(Ans);
            }
        }
    }
    //~~~~~~~~~~
    for(i=OrdersHistoryTotal()-1;i>=0;i--){
        if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true
            && OrderSymbol()==Symbol()){
            if(OrderOpenTime()>=Today){
                Ans=true;return(Ans);
            }
            if(OrderOpenTime() <Today){
                return(Ans);
            }
        }
    }
    //~~~~~~~~~~
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
ubzen:

Give me a sec ... I'm working on it. In the main-time, the answer is to make it return(0) with a if statement before the order is placed. example if(Trade_OncePerDay==true && isOrderToday()==true){return(0);}

Updated: Here's what I was working on. It's a modified version of something I did earlier to suit your case. Again place the check b4 the order if your ea needs to do other stuff like modify stop-loss for example. Otherwise in my example below it just alerts and exits from the start.


I understand your code and I thank you for the effort you are putting. I fixed the problem. I should have thought of that before but for some reason my mind was clogged. Been working a lot lately and I might be a bit tired. So I added the return(0) like you mentioned and closed the {} before the signal could have been received, therefore, now if I want one trade a day or more I can change on input. I didn't use your code but I really appreciate your efforts!

Thanks one more time!

Cheers!

Reason: