Adding a breakeven option to an EA

 

Hi all! 

 

Certain people were good enough to me on my first request for help in this forum that I've come back to ask for one last bit of help!

 

Attached is an EA I built using a free EA builder I found online. It only goes long on certain indicator signals, has a stoploss of 50 and no exit function (I exit manually).

I need to ask if somebody could please add the function (ideally on the menu too with a "true/false" setting if possible) to "move stop to breakeven" after a certain number of pips have passed (default 20 pips).

 

I know a second EA could maybe do it, but I really would like the function to be in this EA as an all in one package.

 

Any chance somebody could please help with this? I have seen other EAs do this but no idea how to incorporate it.

 

Thanks a billion

 

Pinky 

EDIT: ok I understand why nobody has replied - after looking around at other requests for help I realise that people don't generally "code for free" or do your work for you. Instead, the answer seems to be to have a genuine attempt at coding it yourself then, if it doesn't work, post the code on here and ask for guidance to solve it. Fair enough, so I'm going to spend some time today looking for code off the internet to solve this need, slip it into my code and make a genuine effort to get it working. Of course, I can't test it until tonight/tomorrow as it is Sunday morning now and so no incoming data.

Files:
 
pinky:

Hi all! 

Certain people were good enough to me on my first request for help in this forum that I've come back to ask for one last bit of help!

Attached is an EA I built using a free EA builder I found online. It only goes long on certain indicator signals, has a stoploss of 50 and no exit function (I exit manually).

I need to ask if somebody could please add the function (ideally on the menu too with a "true/false" setting if possible) to "move stop to breakeven" after a certain number of pips have passed (default 20 pips).

I know a second EA could maybe do it, but I really would like the function to be in this EA as an all in one package.

Any chance somebody could please help with this? I have seen other EAs do this but no idea how to incorporate it.

Thanks a billion

Pinky 

EDIT: ok I understand why nobody has replied - after looking around at other requests for help I realise that people don't generally "code for free" or do your work for you. Instead, the answer seems to be to have a genuine attempt at coding it yourself then, if it doesn't work, post the code on here and ask for guidance to solve it. Fair enough, so I'm going to spend some time today looking for code off the internet to solve this need, slip it into my code and make a genuine effort to get it working. Of course, I can't test it until tonight/tomorrow as it is Sunday morning now and so no incoming data.

And also this is a weekend and so everyone mind his/her own business or maybe they do read your topic and plainly lazy to reply, nevertheless anyway thanks a trillion for understanding that.

Speaking about free EA Bulder, maybe you should also read this https://www.mql5.com/en/forum/139865 and this https://www.mql5.com/en/forum/139608

 
pinky: EDIT: ok I understand why nobody has replied - after looking around at other requests for help I realise that people don't generally "code for free" or do your work for you. Instead, the answer seems to be to have a genuine attempt at coding it yourself then, if it doesn't work, post the code on here and ask for guidance to solve it. Fair enough, so I'm going to spend some time today looking for code off the internet to solve this need, slip it into my code and make a genuine effort to get it working. Of course, I can't test it until tonight/tomorrow as it is Sunday morning now and so no incoming data.

Thanks for understanding. Sounds like you're good at searching for stuff. My recommendation: if you're going to use EA-Builders [nothing wrong with that], then you need to be good at searching for free tools on-line. Me being in your position, I would search for Order-Management Experts [and there are some really good ones out there] which would manage my trailing-stop.

To use something like that, you would put your Expert on one-chart and the manager on-another. Best wishes.

Edit* with strikeouts, failed to remember u needed a function and aware of managers.

 
pinky:

Hi all! 

 Of course, I can't test it until tonight/tomorrow as it is Sunday morning now and so no incoming data.

You should learn how to use the Strategy Tester then you don't need to wait for the Markets to open . . .
 
Thanks for the replies guys and gals! I didn't realise about the strategy tester, I'll try to have a look at this today too but to be honest it may take the same time to just wait for markets to open now and test my butchered code tonight/tomorrow. I have been playing around with the code in my utterly newbie way (!) by slicing and dicing other code I found online. So far maybe so good, I'll find out soon enough!! It's actually quite exciting all this coding stuff!! :) Anyway, I'll post my success or lack of it and any help I might need over the next couple of days! Thanks for all your help Pinky
 

Here you can add the following functions to your Expert. Above and Below the Start() as shown below. Also Example of usage. I strongly recommend using a Magic#. If you want additional reliability when communicating with broker server then look for a program called Order Reliable.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extern bool BreakEven=false;    /* Default To False*/
extern int  BE_Profit=20;       /* In_Pips Not_Points*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    BreakEven(0);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void BreakEven(int Magic){
    if(!BreakEven){return;}
    for(int i=OrdersTotal()-1; i>=0; i--){
        if(!OrderSelect(i,SELECT_BY_POS)){continue;}
        if(OrderMagicNumber()!=Magic){continue;}
        if(OrderProfit()<=0){continue;}
        string Osy=OrderSymbol();
        double Osl=OrderStopLoss();
        double Onp=OrderOpenPrice();
        int iOsl=p2i(Osy,Osl);
        int iOnp=p2i(Osy,Onp);
        if(iOsl==iOnp){continue;}
        double Ocp=OrderClosePrice();
        int iOcp=p2i(Osy,Ocp);
        int p2p=p2points(Osy,BE_Profit);
        if(MathAbs(iOcp-iOnp)<=p2p){continue;}
        if(p2p<=Symb_sLevel(Osy)){continue;}
        int Ot=OrderTicket();
        double Otp=OrderTakeProfit();
        if(!IsTesting()){while(IsTradeContextBusy()){Sleep(500);}}
        bool Res=OrderModify(Ot,Onp,Onp,Otp,0,0);
        if(Res){Print("Order_"+Ot+" StopLoss Set To BreakEven");}
        else{Print("Order_"+Ot+" SL_Failed_Err#"+GetLastError());}
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int p2i(string Symb, double X){
    /*Converts Price_2_Integer*/ int SymDigit=Symb_Digit(Symb);
    if(SymDigit%2==0){if(SymDigit==2){int Y=100;}else{Y=10000;}
    }else{if(SymDigit==3){Y=1000;}else{Y=100000;}} return(X*Y);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double p2points(string Symb, double X){//Pips2Points
    int D=Symb_Digit(Symb); if(D%2==1){X*=10;} return(X);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double Symb_Digit(string Symb){
    return(MarketInfo(Symb,MODE_DIGITS));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double Symb_sLevel(string Symb){
    return(MarketInfo(Symb,MODE_STOPLEVEL));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Thanks ubzen, I think I have cracked it or closer to the point, I think I got lucky. Either way, I looked at that strategy tester thing and realised it was easy to use, so I have been testing today afterall without a live data feed! Brilliant that I can use my weekends for this now. I'll do a more thorough test in the week, but the breakeven function I threw into the EA seems to work now. So that's all cool!

 

I have come across another problem though with another part of my EA (nothing to do with moving to breakeven) so instead of asking about it in this thread I'll start another one in the week. I have tried to solve it in the code but have no idea why it won't do what I need it to (probably because I'm only good at chopping up code at this stage LOL!!)

 

Thanks for everything so far!!

 

Pinky 

Reason: