EA simple question

 

Hi 

I m trying to create a very simple EA (thats my first one) that will use a simple indicator to enter and exit a trade.
This indicator has two lines (Buffers) a green and a red. So I want to enter the trade when green & red cross and exit when they cross again AND RE-ENTER the trade.

I ve created the code bellow based on an MT4 EA  code:

#define MAGICMA  20050610

extern double Lots               = 0.1;

string customIndName="XYZ-Indicator";

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() /*&& OrderMagicNumber()==MAGICMA*/)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma, red,green, red_1,green_1;
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 

        int history=10; // int

        green = iCustom(Symbol(),Period(),customIndName,history , 0 , 0);  
        green_1 = iCustom(Symbol(),Period(),customIndName,history , 0 , 1); 

        red   = iCustom(Symbol(),Period(),customIndName,history ,  1 , 0);   
        red_1   = iCustom(Symbol(),Period(),customIndName,history ,  1 , 1);    
        
          Print ("O T: green:"+green+" red:"+red)         ;


   //ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---- sell conditions
   if(red>green)  
     {
      res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//---- buy conditions
   if(green>red)  
     {
      res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",MAGICMA,0,Green);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma, red,green, red_1,green_1;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 

        int history=10; // int
        green = iCustom(Symbol(),Period(),customIndName, history , 0 , 0);  
        green_1 = iCustom(Symbol(),Period(),customIndName,history , 0 , 1); 

        red   = iCustom(Symbol(),Period(),customIndName,history ,  1 , 0);   
        red_1   = iCustom(Symbol(),Period(),customIndName,history ,  1 , 1);   
         
          Print ("C T: green:"+green+" red:"+red)         ;

//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      
      //---- check order type 
      if(OrderType()==OP_BUY) // if we are in a BUY order then SELL
        {
         if(red>green)   {OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
                          OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",MAGICMA,0,Red);}
         break;
        }
      if(OrderType()==OP_SELL) // if we are in a SELL order then BUY
        {
         if(green>red )  {OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
                          OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",MAGICMA,0,Green);}
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----

  }
//+------------------------------------------------------------------+

 Is this ok ? I m asking cause when i m also placing the indicator with the EA as well it doesn't show correct but the code looks ok to me.

Or should i say something like :

if(red>green && green_1>red_1)  
....
if(green>red && red_1>green_1)  
...

 Also during a backtest, is there a way to output anything to a file/log/Console/Print whatever?

 Thanks 

 
athanfx:

Hi 

I m trying to create a very simple EA (thats my first one) that will use a simple indicator to enter and exit a trade.
This indicator has two lines (Buffers) a green and a red. So I want to enter the trade when green & red cross and exit when they cross again AND RE-ENTER the trade.

I ve created the code bellow based on an MT4 EA  code:

 Is this ok ? I m asking cause when i m also placing the indicator with the EA as well it doesn't show correct but the code looks ok to me.

Or should i say something like :

 Also during a backtest, is there a way to output anything to a file/log/Console/Print whatever?

 Thanks 

The Indicator you are using,  it only has one extern variable ?

You can Print() from your EA while it is in the Strategy Tester,  the output will appear in the Strategy Tester journal tab and also in the log in  tester/logs 

 
RaptorUK:

The Indicator you are using,  it only has one extern variable ?

You can Print() from your EA while it is in the Strategy Tester,  the output will appear in the Strategy Tester journal tab and also in the log in  tester/logs 


Thanks for your answer Raptor.

Yes it has only one extern variable. I do not need any more cause i do not want any money management at this stage. I just want to test it with a standard lot size.

Any ideas if the code is ok so to enter the trade when green & red cross and exit when they cross again AND RE-ENTER the trade?
 
athanfx:

Thanks for your answer Raptor.

Yes it has only one extern variable. I do not need any more cause i do not want any money management at this stage. I just want to test it with a standard lot size.

Any ideas if the code is ok so to enter the trade when green & red cross and exit when they cross again AND RE-ENTER the trade?
There are some things you need to consider,  does your Indicator handle bar 0 ?  meaning does it repaint ?  if your EA uses bar 0 data from your Indicator it can be crossing multiple times during bar 0,  how do you propose to handle that ?
 
RaptorUK:
There are some things you need to consider,  does your Indicator handle bar 0 ?  meaning does it repaint ?  if your EA uses bar 0 data from your Indicator it can be crossing multiple times during bar 0,  how do you propose to handle that ?


Well my indicator is using bar 0 but i m not sure how an indicator can repaint. As far as i have checked, my indicator crosses do not change position as time passes.

i m only interested in enter/exit a trade as soon as a candle is closed and there is a cross.

 
athanfx:


Well my indicator is using bar 0 but i m not sure how an indicator can repaint. As far as i have checked, my indicator crosses do not change position as time passes.

i m only interested in enter/exit a trade as soon as a candle is closed and there is a cross.

You should not be using bar 0 then . . .  use bars 1 and 2 (and maybe bar 3)  and check for the cross as soon as a new bar 1 is created . . .

If you want to see an Indicator repaint put a SMA on a M1 chart applied to the close price, call up the  Data Windows  (Ctrl + D) an watch the Indicator value for bar 0 and you will see it changes, repaints.