breakeven function

 

Hi all,

since the search function seems to be down on this site I'll just ask away. I implemented a breakeven function into my EA from someone else's EA.

There were no errors when compiling and back-testing yet when I watched the back-test report there were also no trades / order placements within the testing period (1 year). I back-tested again, this time without the "breakeven function" part of the code and now there were trades, which means the problem originates from the breakeven function. I'd like to know where the problem lies.

Please have a look at the attachment below. Thank you very much in advance.

Files:
 
Ah, auto-generated codes. The classic man vs machine. Sorry, we cannot support auto-generated codes here. We recommend you contact the inventor of the generator.
 
ubzen:
Ah, auto-generated codes.
  1. if (EachTickMode && Bars != BarCount)
    Bars is unreliable, once you reach max bars on chart Bars doesn't change. Volume is unreliable. Always use Time.
  2. Total = OrdersTotal();
       for (int i = 0; i < Total; i ++) {
          OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
          if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) {
    
    This makes the EA incompatable with any other EA including itself on other charts. Also you MUST count down when closing orders. Always test return codes.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
    

  3. if(Bid - OrderOpenPrice() > Point * TrailingStop) {
    EA must adjust for 4 vs 5 digit brokers
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  4. extern bool UseTrailingStop = False;
    extern int TrailingStop = 0;
    
    Did you actually turn trailing stops on?
 
Yojimbo:

Hi all,

since the search function seems to be down on this site I'll just ask away. I implemented a breakeven function into my EA from someone else's EA.

There were no errors when compiling and back-testing yet when I watched the back-test report there were also no trades / order placements within the testing period (1 year). I back-tested again, this time without the "breakeven function" part of the code and now there were trades, which means the problem originates from the breakeven function. I'd like to know where the problem lies.

Please have a look at the attachment below. Thank you very much in advance.

Hi Yojimbo,

I could not find any Breakeven function in your attached EA to see how it was working.

You are using the EA Builder Template - Your Breakeven routines need to be placed in the Trailing Stop code loops, so the Breakeven conditions are checked along with the Trailing Stop...

Hope this helps,

Robert

 

... This happens if you leave your desktop unorganized... The file attachment from my initial post was the unmodified machine generated code which works.

The following attachment is the modified version with the breakeven function that causes the EA not to place orders in backtests.

@ubzen: If the unmodified machine generated code had caused any errors I wouldn't have posted any support request in this forum. But the machine code works. It is my man written modification that creates the undesired effects, which is why I decided to let other ppl have a look at it.

@WHRoeder: Thx, for your advice. I will have to read through all the articles covering these topics again to actually understand what you are talking about. xD Except for the 4/5 digit thing maybe. I have read a lot about that (actually mostly written by you xD ) Interestingly enough while backtesting there was no Ordersend Error(130) so maybe the 4/5 digit thing is not always necessary...

@Robert: Thx for making me realize that my attachment didn't have the breakeven function xD Below is the modified one.

 
Yojimbo:

@Robert: Thx for making me realize that my attachment didn't have the breakeven function xD Below is the modified one.


Hi Yojimbo,

I ran the updated EA attachment with Breakeven and as it is, it does not trade at all ...and there are no error messages in the logs.

For this particular EA template model that doesn't actually use functions like "void TrailingStop()"...there may a simpler solution.

If this template does use void functions()...it may be placed in the wrong section...not under init() but in start()?

For the simpler solution, I would suggest completely taking out the "void TrailingStop()" under the init() and working your Breakeven code directly as a separate routine right below the existing Trailing Stop routines...in the same loops...in both Buy and Sell sections.

To make it easier, copy and use the existing Trailing Stop routine for your Breakeven code. Place the copy right under the Trailing Stop routine so the Breakeven code gets read in the same loop, then replace the names Trailing Stop with your Breakeven code names and try it out.

Hope this helps,

Robert

 

Thx again for your reply.

Now I did what you suggested. With interesting results:

The code with the breakeven routine produces exactly the same backtest results as the one without the breakeven routine (no error messages during compiling and backtesting). Maybe I messed up with the editing?

 
Yojimbo:

Thx again for your reply.

Now I did what you suggested. With interesting results:

The code with the breakeven routine produces exactly the same backtest results as the one without the breakeven routine (no error messages during compiling and backtesting). Maybe I messed up with the editing?

Hi Yojimbo,

The good news is your EA code compiles without errors and makes trades...so it looks like your breakeven code was placed properly, but I'm not sure of the breakeven logic itself.

Since it now makes trades ok, next add PRINT statements in each of the Trailing Stop and Breakeven routines.

Print the values to your logs to see exactly what both the Trailing Stop and Breakeven values are and which one is actually closing the trades.

That will give you info...and confidence it's now working fine...or give you more info to tweak your breakeven logic.

Secondly, run your Strategy Tester to find a good trade, then re-run that trade in Slow-Motion and watch the Trailing Stop and Breakeven lines...BOTH are activated (the TS line appears...then disappears, replaced by the Breakeven line, and then the TS line appears again...)

So it looks like BOTH are working BUT...there is some conflict as to which one is controlling the trade.

Good effort so far. Keep on it like a bloodhound on the trail...lol!

Hope this helps,

Robert

 

just before your break even logic, you have

                  if ( mode==OP_BUY )

First, the variable mode will only have the last value of OrderType() from the first loop at the top.

Second, you can remove this test in both places - it is already in the BUY logic and SELL logic.

sn

 

Thank you both for your replies.

After removing the entire trailing stop function (thx @ Robert) and tweaking the breakeven function (thx @ serpentsnoir) the latter finally seems to work.

Now I moved on to trying to make a similar function that adjusts the SL-lvl a second time once price has reached a new high (the same OrderModify routine obviously). Without success. No error message when compiling or backtesting but the second SL adjuster never seems to start (i.e. no difference in the backtest results between EA with said function and EA without it.

Files:
works.mq4  12 kb
 
Yojimbo:

Thank you both for your replies.

After removing the entire trailing stop function (thx @ Robert) and tweaking the breakeven function (thx @ serpentsnoir) the latter finally seems to work.

Now I moved on to trying to make a similar function that adjusts the SL-lvl a second time once price has reached a new high (the same OrderModify routine obviously). Without success. No error message when compiling or backtesting but the second SL adjuster never seems to start (i.e. no difference in the backtest results between EA with said function and EA without it.

Hi Yojimbo,

Did you have any progress with your latest breakeven version called Works?

I could not get that version to trade so I went back to your previous mod - YourExpertAdvisormodifiedagain. You said this one finally seemed to work...?

I played with the BK and TS...since they both appeared to be working, but... they kept flashing on and off re-setting each other...

It looked like BK needed to be turned off when the TS is triggered.

I add a BKeven==True flag to turn off the BK (BKeven==False) and I repositioned the code of BK and TS by placing BK code first (not sure if that actually did anything, but it now works).

Howeven, I'm not sure how the BKeven = True is getting turned back on...but again...BK seems to work fine on the next trades...

I also removed the extra Mode's as suggested by Serpentsnoir.

Play with it and let us know what you find out. Will be interesting to see if this Breakeven actually works...

Maybe some advanced coders can take a look and help also. Feels like we are real close.

Hope this helps,
Robert

Here are my notes I've added to the Breakeven test. Renamed - YourExpertAdvisormodifiedagain - Breakeven Test.mq4 - Attached

// ----------For TESTING BREAKEVEN----------------------------------------------------
** In the previous mod the BK and TS code appeared to be working...but they kept re-setting each other...
** Added a bool BKeven = True flag to use BreakEven only 1x. Then the BreakEven is replaced by TrailingStop when triggered.
** Positioned BreakEven code before TrailingStop. TS turns BKeven flag = false and controls trade from there.

TakeProfit = 300; // Large enough to allow both BK and TS space to trigger
TrailingStop = 100; // Large enough for BK to trigger...small enough to not wait to see TS trigger
BreakEven = 50; // Set high enough to not trigger BK from quick price flickers. Different TF's and strategies will need different settings.

** Find 2 good Buy and Sell trades with a large pip profit (if TS <=10 then pips profit > 12 to see it work)
** Run those trades in the Visual tester in slow motion to watch the BK and TS trigger.
** BreakEven will trigger first, then TrailingStop will take over and begin trailing
** BreakEven now appears to work...but no guarantees...still just a test! Please share your results.
** Renamed this version - YourExpertAdvisormodifiedagain - Breakeven Test.mq4 6/7/2011 - For Testing Only

//-------------------------------------------------------------------------------------

Reason: