developping a new EA based on a custom indicator: signal always gives the same value

 

Hi all,

I'm new here, and also I'm a newbie in coding EA with MQL5. In advance I want to thank who wants to help me, even if maybe I'm trying to solve some too simple issue.

Basically I'm doing this: I downloaded this indicator (supertrend.mq5 ) and I want to use it in my EA. I put the downloaded file in the /Indicators folder. Then I created an EA from the wizard and I added my code, that is:

//+------------------------------------------------------------------+

//|                                                  My_second_EA.mq5 |

//|                        Copyright 2010, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2010, MetaQuotes Software Corp."

#property link      "http://www.mql5.com"

#property version   "1.00"


//--- input parameters


input int      EA_Magic=12345;   // EA Magic Number

input int      PeriodoSuperTr_1=21; //peri SuperTrend1



input int      MoltiplSupTrend_1=2; //moltipl SuperTrend1

//--- Other parameters

int SupTrend_1; // handle for SuperTrend_1 indicator



double STvalue1[];

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+



int OnInit()

  {

//--- Get handle

   SupTrend_1=iCustom(NULL,0,"supertrend",10,3,false);

//--- What if handle returns Invalid Handle

   if(SupTrend_1<=0)

     {

      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");

      return(-1);

     }
   return(0);  
  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {
// 
   MqlTradeRequest request;
   MqlTradeResult result;
   MqlTradeCheckResult resultCheck;
    
   bool buy_sign=false;
   bool sell_sign=false;
   double take_profit=0;
   double stop_loss=0;
   double loss_risk=0;
   double profit_risk=0;
  
   if(CopyBuffer(SupTrend_1,0,0,5,STvalue1)==false)
     {
      Print("Can't copy indicator buffer!");
      return;
     }
  
   if(STvalue1[0]<SymbolInfoDouble(_Symbol,SYMBOL_LAST) && STvalue1[1]<SymbolInfoDouble(_Symbol,SYMBOL_BID))
     {
      buy_sign=true;
      take_profit=0.6*SymbolInfoDouble(_Symbol,SYMBOL_LAST)+SymbolInfoDouble(_Symbol,SYMBOL_LAST);
      stop_loss=SymbolInfoDouble(_Symbol,SYMBOL_LAST);
      loss_risk=0.03*AccountInfoDouble(ACCOUNT_MARGIN_FREE)*(SymbolInfoDouble(_Symbol,SYMBOL_LAST)-stop_loss);
      profit_risk=0.03*AccountInfoDouble(ACCOUNT_MARGIN_FREE)*(take_profit-SymbolInfoDouble(_Symbol,SYMBOL_LAST));
      request.type=ORDER_TYPE_BUY;
      
     }
        
   request.action=TRADE_ACTION_DEAL;
   request.symbol=Symbol();
   request.volume=0.03*AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   request.type_filling=ORDER_FILLING_FOK;
   request.sl=NormalizeDouble(stop_loss,Digits());
   request.tp=NormalizeDouble(take_profit,Digits());
  
   if(buy_sign!=sell_sign && PositionSelect(Symbol())==0 && OrderCheck(request,resultCheck))
      {
       if(OrderSend(request,result))
         {
          Print("problem in sending an order!!");
         }
      }
}

//+------------------------------------------------------------------+

I checked in debug my code and the STvalue1[0] or STvalue1[0] returns always the same incorrect value, that is 1.797693134862316e+308. Obviously I think there is an error, but I've not understand where is it?

Is it correct how to get the handle of the custom indicator (

SupTrend_1=iCustom(NULL,0,"supertrend",10,3,false);

)?

Practically I'm not able to get custom indicators

Sorry if the code is not well formatted, and if I copied a lot from other tutorials on these pages.


Thanks again to all who want to help me!!

 
   if(CopyBuffer(SupTrend_1,0,0,5,STvalue1)==false)

The second parameter is the buffer index. Why are you using buffer 0 ? looking at Supertrend indicator you probably want buffer 2.

CopyBuffer() doesn't return a boolean, it returns an int, why are you comparing to "false" ? Please check the documentation.

FYI: this value 1.797693134862316e+308 is EMPTY_VALUE used in indicators to say "nothing to draw".
 
Alain Verleyen:
FYI: this value 1.797693134862316e+308 is EMPTY_VALUE used in indicators to say "nothing to draw".

Is this a difference from MQL4? EMPTY_VALUE is normally 2147483647 (INT_MAX) whereas DBL_MAX is 1.797693134862316e+308

Edit - the autolink answered my question! Yes it is different :-) 

 
Alain Verleyen:
   if(CopyBuffer(SupTrend_1,0,0,5,STvalue1)==false)

The second parameter is the buffer index. Why are you using buffer 0 ? looking at Supertrend indicator you probably want buffer 2.

CopyBuffer() doesn't return a boolean, it returns an int, why are you comparing to "false" ? Please check the documentation.

FYI: this value 1.797693134862316e+308 is EMPTY_VALUE used in indicators to say "nothing to draw".

Thanks for your fast reply, and it works!

Sorry for my stupid question, but I've not understood what is the buffer index.

Reading the documentation, I got:

int  CopyBuffer(
   int       indicator_handle,     // indicator handle
   int       buffer_num,           // indicator buffer number
   int       start_pos,            // start position
   int       count,                // amount to copy
   double    buffer[]              // target array to copy
   );

so in my case I have only one handler and so one dynamic buffer, should I put 1? Why 2? What does this number set?

As start position I chose 0, because I want the last one (STvalue[0]), and the previous 5 (that is STvalue[1], STvalue[2], STvalue[3], STvalue[4], STvalue[5] ). Right? In my case it seems it returns STvalue[1]. Why?


Thanks for a complete explanation

 

Don't confuse buffers with indices.

Your indicator has 9 buffers. Some are used for the final display, some just for calculation. 

   SetIndexBuffer(0,Filled_a,INDICATOR_DATA);
   SetIndexBuffer(1,Filled_b,INDICATOR_DATA);
   SetIndexBuffer(2,SuperTrend,INDICATOR_DATA);
   SetIndexBuffer(3,ColorBuffer,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(4,Atr,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,Up,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,Down,INDICATOR_CALCULATIONS);
   SetIndexBuffer(7,Middle,INDICATOR_CALCULATIONS);
   SetIndexBuffer(8,trend,INDICATOR_CALCULATIONS);

So you need to choose which one you want to access. Alain suggested Buffer 2.

STvalue[5] doesn't mean buffer 5. It is index 5 of the buffer you have chosen. 

HTH 

 
honest_knave:

Don't confuse buffers with indices.

Your indicator has 8 buffers. Some are used for the final display, some just for calculation. 

   SetIndexBuffer(0,Filled_a,INDICATOR_DATA);
   SetIndexBuffer(1,Filled_b,INDICATOR_DATA);
   SetIndexBuffer(2,SuperTrend,INDICATOR_DATA);
   SetIndexBuffer(3,ColorBuffer,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(4,Atr,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,Up,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,Down,INDICATOR_CALCULATIONS);
   SetIndexBuffer(7,Middle,INDICATOR_CALCULATIONS);
   SetIndexBuffer(8,trend,INDICATOR_CALCULATIONS);

So you need to choose which one you want to access. Alain suggested Buffer 2.

STvalue[5] doesn't mean buffer 5. It is index 5 of the buffer you have chosen. 

HTH 

Thanks, I start to understand now.

I solved as you suggested, but now I get this in the test:"2016.12.20 20:21:27.831    2016.02.04 01:26:03   failed instant buy 300.00 EURUSD at 0.00000 sl: 1.08964 tp: 1.08164 [Invalid request]

"

Is the sending order all ok? Why did the buy operation fail?

Thanks again!!

 
managertop40:

Thanks, I start to understand now.

I solved as you suggested, but now I get this in the test:"2016.12.20 20:21:27.831    2016.02.04 01:26:03   failed instant buy 300.00 EURUSD at 0.00000 sl: 1.08964 tp: 1.08164 [Invalid request]

"

Is the sending order all ok? Why did the buy operation fail?

Thanks again!!

Because there is a bug :-)
 
Alain Verleyen:
Because there is a bug :-)

Where is bug? I've not understood.

When it returns 'failed instant buy 300.00 EURUSD at 0.00000', which value should it return at the place of 0.00000? The error in the code is in setting the trade request, or in sending order?

Thanks again!

 
managertop40:

Where is bug? I've not understood.

When it returns 'failed instant buy 300.00 EURUSD at 0.00000', which value should it return at the place of 0.00000? The error in the code is in setting the trade request, or in sending order?

Thanks again!

At which price do you want to BUY ? Are you thinking 0.0 is a good price ?

Also 300 lots is a lot, don't you think ?

 
Alain Verleyen:

At which price do you want to BUY ? Are you thinking 0.0 is a good price ?

Also 300 lots is a lot, don't you think ?

I understood that, and I fixed adding this line
request.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
request.volume=0.1;

But now I get that:

2016.12.21 18:16:24.918    2016.01.04 01:26:09   failed instant sell 0.10 EURUSD at 1.08463 sl: 1.08957 tp: 1.08155 [Invalid request]


Why is the request invalid? Where is the problem?


Thanks again for all support

Thanks

 
managertop40:
I understood that, and I fixed adding this line
request.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
request.volume=0.1;

But now I get that:

2016.12.21 18:16:24.918    2016.01.04 01:26:09   failed instant sell 0.10 EURUSD at 1.08463 sl: 1.08957 tp: 1.08155 [Invalid request]


Why is the request invalid? Where is the problem?


Thanks again for all support

Thanks

Show your last code.
Reason: