Download MetaTrader 5

EA with Custom Indicator: no values

To add comments, please log in or register
leo1987
43
leo1987  

Hi,

I am trying to convert a my custom indicator, perfectly working, into an EA. I have set the iCustom function as follows:

double vwp_up=iCustom(_Symbol,0,"VWP - Price",1,0);
double vwp_down=iCustom(_Symbol,0,"VWP - Price",0,0);

 Commenting the those variables, to see their value, I get only random allocation numbers: 2147483647. Furthermore those values are constants and do not change over time (while back testing).

So it is impossible to trade! If I stop the backtesting, strange thing, in the indicator window I get and see "vwp_up" and "vwp_down" values and lines. Here is a pic:

EA iCustom 

How could I handle this?

Thanks! 

Alain Verleyen
Moderator
33504
Alain Verleyen  
leo1987:

Hi,

I am trying to convert a my custom indicator, perfectly working, into an EA. I have set the iCustom function as follows:

 Commenting the those variables, to see their value, I get only random allocation numbers: 2147483647. Furthermore those values are constants and do not change over time (while back testing).

So it is impossible to trade! If I stop the backtesting, strange thing, in the indicator window I get and see "vwp_up" and "vwp_down" values and lines. Here is a pic:

 

How could I handle this?

Thanks! 

2147483647 is not a random value, it's EMPTY_VALUE.

You got this as you are using a shift of 0 in iCustom, but your indicator doesn't have a value at current open bar.

Other constants - MQL4 Documentation
  • docs.mql4.com
Other constants - MQL4 Documentation
leo1987
43
leo1987  
How could I fix it? I have tried to set the EA only on new bars opening but I still got this issue!
Alain Verleyen
Moderator
33504
Alain Verleyen  
leo1987:
How could I fix it? I have tried to set the EA only on new bars opening but I still got this issue!

Fix what ? I don't know what you want to do, but your indicator doesn't have a value on open candle so don't try to use it.

Your indicator provides values starting at shift 1 :

double vwp_up=iCustom(_Symbol,0,"VWP - Price",1,1);
leo1987
43
leo1987  
Alain Verleyen:

Fix what ? I don't know what you want to do, but your indicator doesn't have a value on open candle so don't try to use it.

Your indicator provides values starting at shift 1 :

You're right... I have forget a part of the question, sorry! lol

I changed that, it should not be my question, I mean that changing shift from 0 to 1 results in a continuous order opening by the EA! I have already used the template in which I attached the indicator and it never behave this way

 

void OnTick()
  {
//---
   if(Volume[0]==1)
     {
      double vwp_down=iCustom(_Symbol,0,"VWP - Price",0,1);
      double vwp_up=iCustom(_Symbol,0,"VWP - Price",1,1);

      if(vwp_up>vwp_down)
        {
         // Open buy order
         gBuyTicket=OrderSend(_Symbol,OP_BUY,LOTS,Ask,slippage,0,0,"Market Buy Order due to MA_HIGH violation",MAGIC,0,clrGreen);
         gSellTicket=0;
        }

      // Sell Order Condition
      if(vwp_up<vwp_down)
        {
         // Open sell order
         gSellTicket=OrderSend(_Symbol,OP_SELL,LOTS,Bid,slippage,0,0,"Market Sell Order due to MA_LOW violation",MAGIC,0,clrRed);
         gBuyTicket=0;
        }

      // Close Sell Order if condition is violated
      if(gSellTicket!=0)
        {
         if(vwp_up>vwp_down)
           {
            bool select=OrderSelect(gSellTicket,SELECT_BY_TICKET);
            // Close Order
            bool closed=OrderClose(gSellTicket,OrderLots(),Ask,slippage,clrGreen);
            gSellTicket= 0;
           }
        }
      // Close Buy Order if condition is violated
      if(gBuyTicket!=0)
        {
         if(vwp_up<vwp_down)
           {
            bool select=OrderSelect(gBuyTicket,SELECT_BY_TICKET);
            // Close Order
            bool closed=OrderClose(gBuyTicket,OrderLots(),Bid,slippage,clrRed);
            gBuyTicket = 0;
           }
        }
     }
  }
Alain Verleyen
Moderator
33504
Alain Verleyen  
leo1987:

You're right... I have forget a part of the question, sorry! lol

I changed that, it should not be my question, I mean that changing shift from 0 to 1 results in a continuous order opening by the EA! I have already used the template in which I attached the indicator and it never behave this way

 

Ok. Can you explain what you mean ? Please show the trades opened and/or the log.
leo1987
43
leo1987  
Alain Verleyen:
Ok. Can you explain what you mean ? Please show the trades opened and/or the log.

I mean that instead of just control the position, EA closes and reopens new one, like this:

EA 

When VWP DOWN is still above the UP one it should not close or open anything! :| 

 

EDIT: 

I missed those two conditions: (vwp_up>vwp_down && gBuyTicket == 0 && gSellTicket == 0) .. That's ok now! :D Thanks Alain!

Alain Verleyen
Moderator
33504
Alain Verleyen  
leo1987:

I mean that instead of just control the position, EA closes and reopens new one, like this:

 

When VWP DOWN is still above the UP one it should not close or open anything! :| 

Why not ? Your code doesn't check if there is an existing position before opening a new one. On each new bar, it will open a buy or a sell, except if up=down.

Aside that please note that Volume==1 is not a reliable way to detect a new bar (on a live chart you can miss ticks and so a new bar).

Marco vd Heijden
Moderator
5679
Marco vd Heijden  

you forgot

if(OrdersTotal==0)
 {
  //Do order logic
 }

else if(OrdersTotal>0)
 {
  //Do Control positions
 }
To add comments, please log in or register