Problem with breakeven fuction - page 2

 

It works. There's an issue elsewhere in your code. A big part of programming is debugging and unit testing. If you don't know how to do those then I'd recommend you spend some time learning them. For example you could have made a unit test to check the break even function as a process of elimination... 

//+------------------------------------------------------------------+
//|                                              test_break_even.mq4 |
//|                                                      nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict
#include  <stdlib.mqh>
//+------------------------------------------------------------------+
int OnInit()
{
   if(!IsDemo())
      return INIT_FAILED;
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnTick()
{
   static bool once = false;
   if (!once && OrderSend(_Symbol, OP_BUY, 0.1, Ask, 0, 0, 0) >= 0)
      once = true;
   if (!break_even(0.0)) {
      Alert(ErrorDescription(GetLastError()));
   }
}
//+------------------------------------------------------------------+
bool break_even(double break_even_trigger, int magic_number= 0 , string symbol= NULL)
{
   if (symbol == NULL)
      symbol = Symbol();
   int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
   double pip = SymbolInfoDouble(symbol, SYMBOL_POINT);
   if (digits == 3 || digits == 5) 
      pip *= 10 ;
   RefreshRates();
   for (int i= OrdersTotal ()- 1 ; i>= 0 ; i--) {
      if (position_select(i, magic_number, symbol)) {
         double pos_pips = (OrderClosePrice () - OrderOpenPrice ()) / pip;
         if (OrderType () == OP_SELL) {
            pos_pips = -pos_pips;
         }
         if (pos_pips >= break_even_trigger && OrderOpenPrice() != OrderStopLoss()) {
            bool is_mod = OrderModify(
               OrderTicket(),       //ticket  
               OrderOpenPrice(),     //price 
               OrderOpenPrice(),     //stop 
               OrderTakeProfit(),   //take 
               0 
            ); 
            if (!is_mod) {
               Print (ErrorDescription(GetLastError ()));
               return false;
            } else {
               return break_even(break_even_trigger, magic_number, symbol);
            }
         }
      }
   }
   return true ;
}

bool position_select(int index, int magic=0, string symbol=NULL) 
{
   bool is_position = (
         OrderSelect(index, SELECT_BY_POS)
      && OrderType() < 2 // OP_BUY or OP_SELL
      && (symbol == NULL || symbol == OrderSymbol())
      && (magic == 0 ||  magic == OrderMagicNumber())
   );
   return is_position;
}
 
nicholi shen:

There is no such thing as clean-code with non-const global variables, especially when they aren't labeled using the "g_variable" convention. Abusing global variables is a mortal sin

I'm actually refactoring 1,000's of lines of code because I used a global variable called: g_chartSymbol.

Granted, I used the "g_" naming convention, but even so.

When I initially began the EA, I never thought it would handle multiple currencies. So my variable of convenience (and/or expedience) has nabbed me.

nicholi shen:

A big part of programming is debugging and unit testing.

I <3 Unit Tests.

This was a great thread. Listen to @nicholi shen -- he is speaking the truth.

 
Anthony Garot:

I'm actually refactoring 1,000's of lines of code because I used a global variable called: g_chartSymbol.

Granted, I used the "g_" naming convention, but even so.

When I initially began the EA, I never thought it would handle multiple currencies. So my variable of convenience (and/or expedience) has nabbed me.

I <3 Unit Tests.

This was a great thread. Listen to @nicholi shen -- he is speaking the truth.

Hi Anthony....

I've been scratching my head for days now wondering why Nicholi said that "Global" variables are a mortal sin to use.... but, I get it now and maybe this will help others to understand:

"Most" of us (well, me...) refer to a variable created in the "Global Scope" as a "Global Variable".... I did'nt even know that we have access to "Client Terminal Global Variables".... but I'm getting off-point here...

Could you please be more specific w.r.t "This was a great thread" - I <3 Unit Tests...? I'd be interested to read that... thanx

 
Mike Tanton:

Hi Anthony.... Could you be more specific w.r.t "This was a great thread" - I <3 Unit Tests...?

Sure.

(1) This was a great thread ==> @nicholi shen gave some great advice for programming/programmers in this thread.

(2) Unit tests are beneficial for all sorts of reasons. Writing unit tests, while programming, highlights external dependencies and gives you a better perspective on how to structure your code. If a piece of code isn't testable, it could/should probably be broken down into components that are. If your unit tests check edge cases, you will find bugs quicker. The overall quality of your code is enhanced with unit tests.

While I am not an advocate of TDD, I always wanted the programmers working with/for me to use unit tests. Usually they're too lazy, and that always comes back to haunt the project later.

That said, I've fallen into the trap myself. If I have an idea I am chasing down, I may get a little sloppy. But in general, for core code, I want the quality assurance that unit tests provide.

 
Anthony Garot:

Sure.

(1) This was a great thread ==> @nicholi shen 

I had a look there but couldn't find the thread.... must have missed it... but thanx... looks like something I need to know more about...

 
Mike Tanton:

I had a look there but couldn't find the thread.... must have missed it... but thanx... looks like something I need to know more about...

Oh, sorry for the confusion. I mean this thread—the one we're chatting in.
 

Oh... sorry... but it prompted me to look into this further and found this link... so thanx (sorry Roberto)

https://www.mql5.com/en/articles/1579

Enhancing the Quality of the Code with the Help of Unit Test
Enhancing the Quality of the Code with the Help of Unit Test
  • www.mql5.com
While programming in MQL4, I had a wide experience dealing with some finished MQL4 programs and creating several dozens of my own ones. As a result, I came to the conclusion that MQL4 is a very favourable environment for creating low-quality programs. The reasons are listed below: MetaTrader 4 has no built-in debugger. Searching for errors can...
 
Mike Tanton:

Oh... sorry... but it prompted me to look into this further and found this link... so thanx (sorry Roberto)

https://www.mql5.com/en/articles/1579

In the comments of the article you mention, femto gives a link to his github site. That's the same code I use for my unit tests, although I ported it to MQL5.

Be warned:

You need to rewrite the code for assertEquals() for floats and doubles. He uses a simple equality operation, which doesn't work for float and double comparisons.

 
Thanx...
 

My recommendation would be to look to see if there are any errors in the expert log. If not then I would put some print lines in my code at various places.. is the breakeven function running when it should? put a print line to find out.. if it is running put some error checking code on the ordermodify result.

Are you sure you only want the breakeven opportunity to be checked once on each new candle or would you like it to run on each tick? It may move into an area that warrants moving to breakeven AFTER the candle has been building a while.. only to retreat by candle close and stopping you out. Just something to think about..

As regards the Order_Magic and Breakeven variables. You can use input instead of extern if they are not going to be changed by the program. If however, your program changes breakeven amount based on timeframes or currency pairs.. then leave it a variable.. or perhaps you have the EA build a unique magic number based on each currency pair. 

As long as your expert template uses the same names in your EA's I see no problem.

Obviously you have something else going on. A few print lines will tell the story. When you see what the EA is seeing you will have an Aha! moment.. be sure to come back and tell us what simple thing you forgot to do..

Pip Pip.. Jimdandy

Reason: