Download MetaTrader 5
To add comments, please log in or register
Do you need private account monitoring? The Signals will provide you one!
Håkan Magnusson
102
Håkan Magnusson 2015.11.25 15:26 

Hi!

I were testing an EA and in i have a hardcoded rule that if the PSAR value is below the price there should be a sell. Now at more than one point this does not happen.


As far as i can see my EA misses some candles. Forexample i have printed every tick and the values that are of intrest.

It looks like this:


2015.04.10 04:29  EA EURUSD,H1: PSAR_VALUE: 1.068813,           PRICE: 1.067400, DIFF: 0.001413,                  PROFIT: 0.200000

2015.04.14 13:00  EA EURUSD,H1: PSAR_VALUE: 1.060007,           PRICE: 1.055790, DIFF: 0.004217,                  PROFIT: 0.200000

Look at the date and times at the left. I looks like a lot of candles are not accounted for. On my chart there is also candles all the day on the 10th of april and also on the 13th. Why does my EA not include this?


Thoughts?


Thanks!

Carl Schreiber
5953
Carl Schreiber 2015.11.25 15:43  
It can be that the history of you broker - probably a demo account? - has some holes?
Håkan Magnusson
102
Håkan Magnusson 2015.11.25 16:08  
Okay, thats a good input. It could be that, but then i think the candles should not appear on the chart....*confused*
Keith Watford
Moderator
7947
Keith Watford 2015.11.25 21:17  
If you are using the strategy tester and printing every tick, all prints will not be logged in the journal tab. Open the actual log file.
Håkan Magnusson
102
Håkan Magnusson 2015.11.25 22:04  

GumRai: Yes, id did that and i typed out every tick and every time someting got printed to the console. There is a clear break when it fails. The two lines


2015.04.10 04:29  EA EURUSD,H1: PSAR_VALUE: 1.068813,           PRICE: 1.067400, DIFF: 0.001413,                  PROFIT: 0.200000

2015.04.14 13:00  EA EURUSD,H1: PSAR_VALUE: 1.060007,           PRICE: 1.055790, DIFF: 0.004217,                  PROFIT: 0.200000


Are next to each other in this log.
Keith Watford
Moderator
7947
Keith Watford 2015.11.25 22:13  
Show the code and conditions responsible for the prints
Håkan Magnusson
102
Håkan Magnusson 2015.11.25 22:47  
void OnTick()
  {
//---
       //printf("LastBar: %f CurrentBar: %f",lastBar, iBars(Symbol(),PERIOD_H1));
       RealtimeAnalyze();
  }

And the realtime function that looks for when to get our of a position looks like this (in part)

void RealtimeAna()
{
      if( OrdersTotal() > 0 )
         CheckForExit(); // kolla om vi skall gå ur postition
 }

In this analyze function the "CheckForExit" is the first thing that happens.


The last function looks like this:

void CheckForExit()
{
   double close1       = NormalizeDouble(iClose(Symbol(), global_PERIOD, 1), Digits);

   double psar = iSAR(NULL,global_PERIOD,0.02,0.2,1);
   double adxDminus = iADX(NULL,global_PERIOD,14,PRICE_HIGH,MODE_MINUSDI,1);
   double adxDplus = iADX(NULL,global_PERIOD,14,PRICE_HIGH,MODE_PLUSDI,1);
   
   double adxDminus_2 = iADX(NULL,global_PERIOD,14,PRICE_HIGH,MODE_MINUSDI,2);
   double adxDplus_2 = iADX(NULL,global_PERIOD,14,PRICE_HIGH,MODE_PLUSDI,2);
   
  double adxMain = iADX(NULL,global_PERIOD,14,PRICE_HIGH,MODE_PLUSDI,1); 
  double adxMain_2 = iADX(NULL,global_PERIOD,14,PRICE_HIGH,MODE_MAIN,2);

     if( OrdersTotal() > 0  )
     {
         if( triggerType == OP_SELL)
         {
               double diff = MathAbs(psar - close1);
               OrderSelect(orderTicket,triggerType);
               
               if( diff/close1 <=0.001 )
               { // 
                  printf("Close deal, Psar to close to price, profit: %f",OrderProfit());
                  cleanUp();
                  OrderStatus = false;
                  bestProfit = 0;
                  OrderSent = false;   
                  orderTicket = -1;          
                  triggerType = -1;                 
               }
             
               if( psar < close1 ) 
               {
                  printf("psar < than close price, profit: %f", OrderProfit());
                  cleanUp();
                  OrderStatus = false;
                  bestProfit = 0;
                  OrderSent = false;   
                  triggerType = -1;           
                  orderTicket = -1;          
              }
               
               
               if( OrderProfit()<0 == OP_SELL ) 
               {
                 
                  if( MathAbs(OrderProfit()) > AccountEquity()*0.01)
                  {
                     printf("-------------------Closing deal! Loss: %f, Asets: %f-------------------",OrderProfit(),AccountEquity() );
                     cleanUp();
                     OrderStatus = false;
                     bestProfit = 0;
                     OrderSent = false;   
                     triggerType = -1;           
                     orderTicket = -1;          
                  }
               }
               
               // If adxMinus cuts main from under
               if(adxDminus>adxMain && adxDminus_2<adxMain_2)
               {
                  printf("Adx - cuts main from under, profit: %f", OrderProfit());
                  cleanUp();
                  OrderStatus = false;
                  bestProfit = 0;
                  OrderSent = false;   
                  triggerType = -1;               
                  orderTicket = -1;          
               }
         }

         
      }
}

This condition:

if( psar < close1 ) 

Should be impossible to get by under normal circumstances. So i really dont understand why it happens. Could there be an error in

     if( OrdersTotal() > 0  )
     

Because that would cause the function to fail.

I have attached a screen shot of the fail in progress. As you can see it sells at the 17th of august and there are trading days after the 17. 18, 19, 20 & 21. In my log this is the output.

2    23:17:57    2015.08.17 01:00  EURUSD,H1: open #134 sell 0.10 EURUSD at 1.11119 ok
0    23:18:03    2015.08.21 11:00  EURUSD,H1: -------------------Closing Position! Loss: -141.600000, Assets: 10399.577000-------------------
2    23:18:03    2015.08.21 11:00  EURUSD,H1: close #134 sell 0.10 EURUSD at 1.11119 at price 1.12535

Messed up!

Check the dates. There is 3 trading days between the opening and closing of the position as seen in the file. And the condition

if( psar < close1 ) 

In the "CheckForExit function should be impossible to slip by.

Any ideas?

Im sorry for the sloppy code, this is just an test EA that im making now. I need to sleep now so i will be off a few hours.


Thank you for all your inputs.

Keith Watford
Moderator
7947
Keith Watford 2015.11.26 00:07  
               if( OrderProfit()<0 == OP_SELL ) 

That doesn't make sense, you are comparing a condition with an enum int. I would think that OP_SELL is the same as True.

 

I really don't know what your query is now. In your OP you gave the impression that you were expecting a print every tick

     if( OrdersTotal() > 0  )
     {
         if( triggerType == OP_SELL)
         {

 This will only be executed if there is an order open. The first time it prints something triggerType is set to -1.

So the code cannot be executed again until triggerType is re-set to OP_SELL, which must be elsewhere in your code. 

Håkan Magnusson
102
Håkan Magnusson 2015.11.26 18:05  

Hi GumRai!


Wow that if statment...omg what a melt down. I cant believe i did not see it! I have corrected it now but i have the same issue im afriaid. 


Yes, it is true what you say, triggertype will be -1 if the functions prints. The CleanUp function is closing all positions. So when CheckForExit() returns to its caller there will be no positions open.

                  if( MathAbs(OrderProfit()) > AccountEquity()*0.01)
                  {
                     printf("-------------------Closing deal! Loss: %f, Asets: %f-------------------",OrderProfit(),AccountEquity() );
                     cleanUp(); // <--- exits all open positions. 
                     OrderStatus = false;
                     bestProfit = 0;
                     OrderSent = false;   
                     triggerType = -1;           
                     orderTicket = -1;          
                  }
/
To add comments, please log in or register