Daily and weekly profit/loss target?

 

Hi all,

I'm pretty new to MQL4 and I'm starting to build a new EA that I want to stop trading if I hit a certain number of pips (either profit or loss) for the day and the week (ie. 20/day and 50/week). The way I wanted to go about it was to look at the account balance on Monday and just do the math but haven't been able to find a way to do it. Is that possible?

If not, how would you recommend going about it?

Thanks!
Scott

 

Is that possible? Yes.

Your question is too broad. You'll have to learn to code is the simple answer. Start with the book.

 

I know how to code, I've taken a few college level C++ courses and have been using PHP/HTML/CSS for years... I'm just new to MQL4.

To simplify my question, here's what I'm looking for:

Once my EA reaches either a 20 pip profit or loss in any given day, it will not open any more trades... Once the EA reaches a 50 pip profit or loss for the week, it will not open any more trades.

So, let's say on Monday it successfully gets 20 pips - it will stop trading. Total for the week +20.

Tuesday, it loses 20 pips - it will stop trading. Total for the week 0.

Wednesday, it gains 20 pips - stops trading. Total for the week +20.

Thursday, it gains 20 pips - stops trading. Total for the week +40.

Friday, it only gains 10 because the weekly target of +50 is reached before the daily target of +20.

----------------------

That link looks incredibly helpful, I'll be sure to pick through there to expand my knowledge. Thanks!

 
Ok thats fine. I'll provide a sample code in a moment....
 

scottyPIPin:

I'm pretty new to MQL4 and I'm starting to build a new EA that I want to stop trading if I hit a certain number of pips (either profit or loss) for the day and the week (ie. 20/day and 50/week). The way I wanted to go about it was to look at the account balance on Monday and just do the math

Why would you look at account balance? Just do everything in pips. loop through all history records if it closeds today sum OrderClosePrice()-OrderOpenPrice in one var if it closed within 5 days sum in another.
 

Well here you go. I never went to coder's school but that's the best I can come up with for now ;)

Anyways, its not tested. Maybe it'll give you some ideas.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool is_Daily_Trading_Allowed=true;
bool is_Weekly_Trading_Allowed=true;
datetime Week_Open_Time;
datetime Day_Open_Time;
int Pip2Real;
int My_Expert_Unique_Number=777;
int Track_Last_Checked;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){//***************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    double Ans;
    //~~~~~~~~~~~~~~~
    if(Week_Open_Time!=iTime(Symbol(),PERIOD_W1,0)){
        Week_Open_Time=iTime(Symbol(),PERIOD_W1,0);
        is_Weekly_Trading_Allowed=true;
    }
    //~~~~~~~~~~~~~~~
    if(Day_Open_Time!=iTime(Symbol(),PERIOD_D1,0)){
        Day_Open_Time =iTime(Symbol(),PERIOD_D1,0);
        is_Daily_Trading_Allowed=true;
    }
    //~~~~~~~~~~~~~~~
    if(OrdersHistoryTotal()>Track_Last_Checked){
        Track_Last_Checked=OrdersHistoryTotal();
        for(int i=OrdersHistoryTotal()-1;i>=0;i--){
            if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
            && OrderMagicNumber()==My_Expert_Unique_Number
            ){
                if(OrderOpenTime()<Week_Open_Time){break;}
                if(StringSubstr(OrderSymbol(),0,6)=="USDJPY"){Pip2Real=100;}else{Pip2Real=10000;}
                if(OrderProfit()>0){Ans+=MathAbs(OrderOpenPrice()-OrderClosePrice())*Pip2Real;}
                if(OrderProfit()<0){Ans-=MathAbs(OrderOpenPrice()-OrderClosePrice())*Pip2Real;}
                if(MathAbs(Ans)>=20 && OrderOpenTime()>Day_Open_Time){is_Daily_Trading_Allowed=false;}
                if(MathAbs(Ans)>=50 && OrderOpenTime()>Week_Open_Time){is_Weekly_Trading_Allowed=false;break;}
            }
        }
    }
    //~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}//***************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
ubzen:

Well here you go. I never went to coder's school but that's the best I can come up with for now ;)

Anyways, its not tested. Maybe it'll give you some ideas.


Thank you so much, that will definitely give me something to work with.

Reason: