Not logged in
Viewing 10 posts - 1 through 10 (of 10 total)

Forums>AlgoWizard / EA Wizard>General Discussion>Possible to breakeven/activate stop loss/exit a trade after…

  • #112105 |
    Customer
    484 Posts

    Possible to breakeven/activate stop loss/exit a trade after 24 Hours(bars)?
    My strategy is a Daily bar strategy.
    I want to breakeven after exactly 24 hours or exit if the trade is not winning after exactly 24 hours.
    I see there is a “BarsSinceOrderOpen” option but this option has to timeframe option. Can you add it? Or are there other possibilities?

    #124337
    Customer
    197 Posts

    Is this what you are looking for?

     

    -Stearno

    Attachments in this forum are visible only for registered users.
    #124363
    Mark Fric
    Administrator
    1182 Posts

    you can do this by making a rule for this.

     

    Something like:

     

    IF(BarsSinceOrderOpen >= 1)   // trade older than day

    and OpenPL() > 0

    THEN 

    move SL to BE

     

     

    and another rule for closing the trade:

     

     

    IF(BarsSinceOrderOpen >= 1)   // trade older than day

    and OpenPL() < 0

    THEN 

    ClosePosition

    Mark
    StrategyQuant architect

    #124383
    Customer
    484 Posts

    You guys did not read the question fully or maybe I was unclear. My apologies.
    BarsSinceOrderOpen>=1 is exactly what I said wont work because it has no timeframe option. What if the order was entered @ 23:00 yesterday? Then it breaks-even after only 1 hour. BarsSinceOrderOpen does not mean 24 hours. It simple means a new bar. This could be 1 hour 2 hour 3 hours 4 hours 5 hours….
    I’m looking for break even after exactly 24 hours (or 24 H1 bars) for  D1 strategy.
    I need to do:

     

    BarsSinceOrderOpen(Timeframe: PERIOD_H1) > 24

    But there is no timeframe option.

     

     

    My strategy is a Daily bar strategy.
    I want to breakeven after exactly 24 hours or exit if the trade is not winning after exactly 24 hours.
    I see there is a “BarsSinceOrderOpen” option but this option has *no timeframe option*. Can you add it? Or are there other possibilities?

    #124435
    Mark Fric
    Administrator
    1182 Posts

    ok, I got it. Unfortunately, this is not easy to solve right now, without adding timeframe option to Bars Since Order Open.

     

    But you can do it using a custom function. Add the following functions in EA Wizard/code/CustomFunctions.mq4 file:

     

    int sqGetBarsSinceEntryH1(int orderMagicNumber) {
       for (int cc = OrdersTotal() – 1; cc >= 0; cc–) {
          if (OrderSelect(cc, SELECT_BY_POS) ) {
     
             if((orderMagicNumber == 0 || OrderMagicNumber() == orderMagicNumber) && OrderSymbol() == Symbol()) {
                return (sqGetBarsFromOrderOpenH1());
             }
          }
       }
     
       return(-1);
    }
     
    int sqGetBarsFromOrderOpenH1() {
       datetime opTime = OrderOpenTime();
       int barsTOCheck = 30;
       int numberOfBars = 0;
       
       for(int i=0; i<=barsTOCheck; i++) {
          if(opTime < iTime(NULL, PERIOD_H1,i)) {
             numberOfBars++;
          }
       }
     
       return(numberOfBars);
    }

     

    and then in EA Wizard in your condition you can call Custom Function: sqGetBarsSinceEntryH1(your magic number) >= 24

    Mark
    StrategyQuant architect

    #124471
    Customer
    484 Posts

    Awesome! Is there a way to apply this to TS activation?

     

    I know how to with BE now.

    This is my attempt to apply it to TS Activation.

     

    TS Activation (variable) = TSAktivate

     

    Attachments in this forum are visible only for registered users.

    #124479
    Customer
    484 Posts

    The EA wont load successfully after I changed the CustomFunctions.mq4 file.
    I’ll upload it. Perhaps my editing was incorrect.
    I copy and pasted all the code you provided and added to the end of the file.

     

    Here is my work aswell:

    Attachments in this forum are visible only for registered users.

    Attachments in this forum are visible only for registered users.
    #124484
    Participant
    16 Posts

    I would try this:

     

    1. make a variable : ‘opentime’

    2. Rule1#: IF order opened this minute is true THEN assign variable opentime = hour(0 bars ago)

    3. Rule 2# IF bars since order open = 1 and Timecurrent = GetTime(opentime, 0, 0)

    THEN

    whatever

     

    Since you want to close after 24 hours it means that it will happen at the same time as it was opened just one day later.

    #124492
    Customer
    484 Posts

    Very simple approach, I do like it.
    Tested it, but doesnt work.

    Attachments in this forum are visible only for registered users.

    #124520
    Mark Fric
    Administrator
    1182 Posts

    Hi,

     

    is it still not working? Can you attach your strategy here?

    As I checked your CustomFunctions file you attached I didn’t see my functions added there, and it compiled correctly.

    Mark
    StrategyQuant architect

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

You must be logged in to reply to this topic.