What's wrong in my program?

 

Hi,


I wrote a small algo which use the indicator of KDJ. This could be download from https://www.mql5.com/de/code/9173.

My algo in the program is simple as follow:

if K crosses over D -> Long

if K crosses under D -> Close Long and go Short

And vise verse.


By backtesting I found some Buy and Sell point are not identical with my algo. I am not sure if there is a mistake in my program or in the KDJ indicator?


My program will be atteched. Here are some screenshots.




One can see for example from the screenshots above, the third order (sell) should not happen since the K > D.

KDJ
KDJ
  • 2014.04.21
  • Walter
  • www.mql5.com
KDJ indicator
Files:
TestKDJ.mq4  13 kb
 
thomas2004: the third order (sell) should not happen since the K > D.
  1. You are looking at the lines after the bar closed. Your code is looking at the forming candle.
  2. Simplify your code
    bool long1 = KCurrent > DCurrent && KPrevious < DPrevious;
    bool short1 = KCurrent < DCurrent && KPrevious > DPrevious;
                // Handelssignale ermitteln
       if(NeuePeriodeBegonnen == true){
          // Buy Signal
          if(long1) LongSignal = true;
          else LongSignal = false;
          // Short Signal      
          if(short1) ShortSignal = true;
          else ShortSignal = false;
         }
       else{
          LongSignal = false;
          ShortSignal = false;
         }
    bool long1 = KCurrent > DCurrent && KPrevious < DPrevious;
    bool short1 = KCurrent < DCurrent && KPrevious > DPrevious;
                // Handelssignale ermitteln
       if(NeuePeriodeBegonnen == true){
          // Buy Signal
          LongSignal = long1;
          // Short Signal      
          ShortSignal = short1;
         }
       else{
          LongSignal = false;
          ShortSignal = false;
         }
    bool long1 = KCurrent > DCurrent && KPrevious < DPrevious;
    bool short1 = KCurrent < DCurrent && KPrevious > DPrevious;

    LongSignal = long1 && NeuePeriodeBegonnen;
    ShortSignal = short1 && NeuePeriodeBegonnen;

    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  3. if(LongOrder>0){
       if(OrderSelect(LongOrder,SELECT_BY_TICKET)== true){
          bool LongOrderGeclosed = OrderClose(LongOrder,OrderLots(),Bid,10,Blue);
    
    EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
 

Hi,


Thanks for your reply.


I tried your optimation. But the problem is still existed. From the screenshot below one can see, the 6. trade should be a SELL since the K < D. But in fact it is a BUY. 

From the 6.trade all the trades are wrong (reversed).

I want to debug. But it's MT4.

P.S.: I use the 15 min timeframe. From 2017.05.01 to 2017.05.03.  





 
thomas2004:

I tried your optimation. But the problem is still existed.

From the screenshot below one can see, the 6. trade should be a SELL since the K < D. But in fact it is a BUY. From the 6.trade all the trades are wrong (reversed).

I want to debug. But it's MT4.

  1. Why would you think an optimization would change anything? Optimization or simplification, is not a correction.
  2. Previously answered #1 item 1
  3. Irrelevant. Use the built in debugger or print out your variables, including _LastError and find out why.
Reason: