Not logged in

Forums

Viewing 14 posts - 1 through 14 (of 14 total)

Forums>EA Wizard>Feature Requests>Tell EA to stop trading for that Day if a Profit or Loss in Pips is reached?

  • #112314 |
    Participant
    25 Posts

    Hello Mark,

     

    In wich strategy block I could type the rule in, when I wanted to stop trading for that day, if the daily profit or loss is reached +/- 100 Pips for example?

     

    Thanks for your feedback,

    Birdy70

    #125178
    Administrator
    3055 Posts

    Hello,

     

    EA Wizard doesn’t have function that returns PL for a current day, but you can add it.

     

    1. Open {EA Wizard}/code/CustomFunctions.mq4 and add the following code there:

     
    double sqGetPLInPipsToday() {
       string todayTime = TimeToStr( TimeCurrent(), TIME_DATE);
       double plToday = 0;
     
       for(int i=0;i<OrdersHistoryTotal();i++) {
          if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true && OrderSymbol() == Symbol()) {
     
             if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
                // skip pending orders
                continue;
             }
     
             if(TimeToStr( OrderOpenTime(), TIME_DATE) == todayTime) {
                if(OrderType() == OP_BUY) {
                   plToday += sqGetBid(OrderSymbol()) – OrderOpenPrice();
                } else {
                   plToday += OrderOpenPrice() – sqGetAsk(OrderSymbol());
                }         
             }
          }
       }
     
       for (int cc = OrdersTotal() – 1; cc >= 0; cc–) {
          if (OrderSelect(cc, SELECT_BY_POS) && OrderSymbol() == Symbol()) {
     
             if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
                // skip pending orders
                continue;
             }
     
             if(TimeToStr( OrderOpenTime(), TIME_DATE) == todayTime) {
                if(OrderType() == OP_BUY) {
                   plToday += sqGetBid(OrderSymbol()) – OrderOpenPrice();
                } else {
                   plToday += OrderOpenPrice() – sqGetAsk(OrderSymbol());
                }         
             }
          }
       }
       
       return(plToday*gPointPow);
    }
     
     
    2. then in your rule you can call this new function, the rule could be :
    IF(your other rules)
    and (Custom Function: sqGetPLInPipsToday() < 100)     <– this means that PL today is smaller than 100 pips
     
    THEN

    Mark
    StrategyQuant architect

    #125205
    Participant
    25 Posts

    Hello Mark,

     

    where I have to type this code in? If I open on my PC the folder C:StrategyQuant/SQEAWizard/Code, I find a file called CustomFunctions.mql4. If I open that file, the Editor of MT4 opens with the window CustomFunctions. When I copy your code in that window, I get 6 error messages after compiling this code!

     

    Please help me.

     

    Birdy70

    #125244
    Administrator
    3055 Posts

    Hello,

     

    you have to copy this function to this file, but don’t compile it, it is not a complete EA.

    The contents of this file is appended to every EA generated by EA Wizard, so it is a place where we can put custom functions.

     

    So just open EA Wizard, load your strategy, add the custom function there and export to EA. Then it should be compiled without problems.

    Mark
    StrategyQuant architect

    #126130
    Participant
    25 Posts

    Hello Marc,

     

    I am sorry, but I do not understand! In my EAW Folder I find the File CustomFunctions.mql4. I could open this file with MT4 Editor (in MT4) and could paste your Trading Conditions you told me at first. If I open my EAW Strategy details, I could see in the Dialog Box under “Functions” the File “Custom Function”! But how to type your Trading Rule in this Window. 

     

    Maybe you could tell me step by step, how I could use this Rule in EAW or tell me another Trading Rule, that tells the EA “stop Trading for that Day, after a profit or Loss of 100 Pips or 100 Euro is reached”!

     

    This would be very great.

     

    Under the Button “Strategy Options” I see some Rules like:

    Sum of closed P/L (Magic Number / Orders Count)

    TotalProfits 

    Total Losses

    Closed P/L in Pips (Magic Number and trades ago)

    Closed P/L in money (Magic Number and trades ago)

     

    Is it not possible, to create with these Options a Rule like:

     

    IF

    Sum of closed BUY/SELL Orders is > 100 Pips or 100 Euro (for example)

     

    Then

    Stop Trading for that Day

     

    Thanks for your help! I hope it is possible, to create a Trading Rule like this.

     

    Best Regards

    Birdy70

    #126137
    Administrator
    3055 Posts

    Hello,

     

    when you copied this function to CustomFunctions.mq4 file it will be attached to every new EA, so you can use it in your conditions.

     

    How to do it – normally open your strategy in EA Wizard. Click to add new trading rule, then go to Functions -> Custom Function and in the Function field write: sqGetPLInPipsToday()

    Add < 100 comparison and click OK to close the dialog.

     

    That’s all, now save this strategy as an EA and it should work.

    Mark
    StrategyQuant architect

    #129459
    Participant
    11 Posts

    Hello Marc,

     

    I just recently bought EA Wizard after I had compared it with other EA builders, and I am very pleased with it as includes so many pre-built functions to use – I think this sets it apart from your competitors, so please keep up the good work.

     

    Unfortunately, I cannot get the sqGetPLInPipsToday() custom function here to work having spent all day on it. The problem is that trading still continues after I have reached my daily profit target and I don’t know why after I have followed your instructions very carefully. If you look at the attached chart you can clearly see the first trade hits the daily target of 20 pips, but the EA still continues to trade for that day. Could you kindly take a look at the attached files to determine the problem please.

     

    Thanks a lot.

    Regards,

     

    Jamie

    Attachments in this forum are visible only for registered users.
    Attachments in this forum are visible only for registered users.
    Attachments in this forum are visible only for registered users.
    #129552
    Administrator
    3055 Posts

    ok, I found the problem, there was a mistake in my function, below is the correct one.

     

     

    double sqGetPLInPipsToday() {
       string todayTime = TimeToStr( TimeCurrent(), TIME_DATE);
       double plToday = 0;
     
       for(int i=0;i<OrdersHistoryTotal();i++) {
          if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true && OrderSymbol() == Symbol()) {
     
             if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
                // skip pending orders
                continue;
             }
     
             if(TimeToStr( OrderOpenTime(), TIME_DATE) == todayTime) {
             Log(“Comparing “, TimeToStr( OrderOpenTime(), TIME_DATE), ” = “, todayTime);
                if(OrderType() == OP_BUY) {
                   plToday += OrderClosePrice() – OrderOpenPrice();
                } else {
                   plToday += OrderOpenPrice() – OrderOpenPrice();
                }
             }
          }
       }
     
       for (int cc = OrdersTotal() – 1; cc >= 0; cc–) {
          if (OrderSelect(cc, SELECT_BY_POS) && OrderSymbol() == Symbol()) {
     
             if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
                // skip pending orders
                continue;
             }
     
             if(TimeToStr( OrderOpenTime(), TIME_DATE) == todayTime) {
                if(OrderType() == OP_BUY) {
                   plToday += sqGetBid(OrderSymbol()) – OrderOpenPrice();
                } else {
                   plToday += OrderOpenPrice() – sqGetAsk(OrderSymbol());
                }
             }
          }
       }
     
       return(plToday*gPointPow);
    }

    Mark
    StrategyQuant architect

    #129560
    Participant
    11 Posts

    Hi Mark,

     

    Thanks very much.

    I will try the new code and I’ll let you know the results. I hope some back testing will show over trading is the problem and the script will fix this.

     

    Jamie

    #129888
    Participant
    11 Posts

    Hi Mark,

     

    Sorry for the delay in getting back here. 

     

    It is working and doing exactly what I want. Good job!

     

    May I suggest you put useful code like this and others in a forum sticky thread, so they can be easily found.

     

    Thank you,

    Jamie

    #130754
    Participant
    89 Posts

    I have tried your string above and did just as you explained but it keeps trading after number of pips reached. What am i doing wrong with this simple ea creation?

    Attachments in this forum are visible only for registered users.
    Attachments in this forum are visible only for registered users.
    #130768
    Participant
    89 Posts

    Guys,

      I have literally worked through the night as you can see by my few time posts and just cannot get this.. Can someone please just explain in detail to me what i am doing wrong.

     

    All i am looking for is a simple strategy to learn from. Say go long when EMA 10> EMA 20 AND SHORT WHEN EMA10 < EMA 20.. All i want to do is stop profit at $10 and start the next trading day.. I am exhausted from working through the night and would appreciate if this can get done asap..

    #135674
    Participant
    14 Posts

    hello, 

    first of all, thank you for all this great support on EA Wizard,

     

    this function is great and I desperately needed it. 

    But a small correction is needed for this code. 

     

            if(OrderType() == OP_BUY) {
                   plToday += OrderClosePrice() – OrderOpenPrice();
                } else {
                   plToday += OrderOpenPrice() – OrderOpenPrice();
     
    this code must be:
     
            if(OrderType() == OP_BUY) {
                   plToday += OrderClosePrice() – OrderOpenPrice();
                } else {
                   plToday += OrderOpenPrice() – OrderClosePrice();
     
    Please correct me if I am wrong since I am not a coder and I found it by trying (some couple of hours) :/
     
    thank you again for the support. 
    best 
    #135684
    Moderator
    2349 Posts

    Yes, that is correct. That one OrderOpenPrice() needs to be replaced by OrderClosePrice()

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.