why is ChartIndicatorName not working?

 

hey so I'm having a problem with ChartIndicatorName, im trying to get the macd Signal name and handle. Here is my code, when i compile it it's good, but when i place it in a chart i get the invalid handle issue. Much appreciation to anyone that helps :) 


   //set the Macd handle

   nameMacd = ChartIndicatorName(0,3,0);

   nameMacdSignal = ChartIndicatorName(0,3,1);

   handleMacd = ChartIndicatorGet(0,3,nameMacd);

   handleMacdSignal = ChartIndicatorGet(0,3,nameMacdSignal);

   

   

   if(handleMacdSignal == INVALID_HANDLE) Print(__FUNCTION__," Macd Signal handle is invalid.");

      return(INIT_PARAMETERS_INCORRECT);

   

   

   

 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. nameMacd       = ChartIndicatorName(0,3,0);
    nameMacdSignal = ChartIndicatorName(0,3,1);

    Print your value or use the debugger and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

  3. Do you really have two MACDs on the same subwindow?

  4. Where did you get the subwindow ID from?
 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. Print your value or use the debugger and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

  3. Do you really have two MACDs on the same subwindow?

  4. Where did you get the subwindow ID from?

no i have 1 macd on one window, but ther macd has the macd value and the signal value, i need them both for when the macd crosses the signal value. im trying to grab the signal but it wont. 

Im following this tutorial: https://youtu.be/DMCYVVNKC4M?t=1224 and the subwindow is the window where the macd is on, so for me its 4 windows down making it 3.

Trade ANY MT5 Indicator using this Simple Expert Advisor | mql5 Coding Tutorial
Trade ANY MT5 Indicator using this Simple Expert Advisor | mql5 Coding Tutorial
  • 2022.12.10
  • www.youtube.com
*Complete MT5 Programming Course: https://en.bmtrading.de/mt5-masterclass/*Recommended Broker: https://en.bmtrading.de/broker/*Free Trading Journal: https://...
 

You are probably confusing indicator handles with indicator buffers.

 
Fabio Cavalloni #:

You are probably confusing indicator handles with indicator buffers.

Here is a larger portion of the code, it doesn't make sense because i have basically copied the buffer and double code from the stochastic rsi area and it works but the macd doesn't.


// ============
//variables
bool IsDemo = true; 
CTrade trade;
int TradesCurrentlyActive = 0;


//stoch variables
int handleStochasticMain;
int handleStochasticSignal;
string nameStochMain;
string nameStochSignal;

//williams variables
int handleWilliams;
string nameWilliams;

//macd variables
int handleMacd;
int handleMacdSignal;
string nameMacd;
string nameMacdSignal;




//==================================
int OnInit(){
   
   //set the starting handle value for stoch 
   nameStochMain = ChartIndicatorName(0,1,0);
   nameStochSignal = ChartIndicatorName(0,1,1);
   handleStochasticMain = ChartIndicatorGet(0,1,nameStochMain);
   handleStochasticSignal = ChartIndicatorGet(0,1,nameStochSignal);
   
   
   
   //set the willams percentage  handle
   nameWilliams = ChartIndicatorName(0,2,0);
   handleWilliams = ChartIndicatorGet(0,2,nameWilliams);
   
   
   //set the Macd handle
   nameMacd = ChartIndicatorName(0,3,0);
   nameMacdSignal = ChartIndicatorName(0,3,1);
   handleMacd = ChartIndicatorGet(0,3,nameMacd);
   handleMacdSignal = ChartIndicatorGet(0,3,nameMacdSignal);
   
   
   if(handleMacdSignal == INVALID_HANDLE) Print(__FUNCTION__," Macd Signal handle is invalid.");
      return(INIT_PARAMETERS_INCORRECT);
   
   
   
   //this code is to make sure the program is not active on a real account just to be safe
   if (!IsDemo) return (INIT_FAILED);
   int result = ValidateInputs();
   if (result != INIT_SUCCEEDED){
   return (result);};
  
  
   return(INIT_SUCCEEDED);
  }

//========================================
void OnTick()
  {
   
   //arrays to hold stochastic data
   double stochasticMain[]; 
   double stochasticSignal[];
   //stochastic rsi data fetch
   CopyBuffer(handleStochasticMain,0,0,StochLookBackPeriodsUnder20,stochasticMain);
   CopyBuffer(handleStochasticSignal,1,0,StochLookBackPeriodsUnder20,stochasticSignal);
   Comment("Stochastic Main: ",stochasticMain[0]);
   Comment("Stochastic Signal: ",stochasticSignal[0]);
   
   //array to hold williams data
   double williamsMain[];
   //williams data fetch
   CopyBuffer(handleWilliams,0,0,WilliamsLookBackPeriodForUnder20,williamsMain);
   Comment("Williams: ",williamsMain[0]);
   
   //array to hold Macd data
   double macdMain[];
   double macdSignalList[];
   //macd data fetch
   CopyBuffer(handleMacd,0,0,MacdLookBackPeriod,macdMain);
   CopyBuffer(handleMacdSignal,1,0,MacdLookBackPeriod,macdSignalList);
   Comment("Macd: ",macdMain[0]);
   Comment("Macd signal: ",macdSignalList[0]);
 
thecerealslayer #:
Wait, when i run the code i displayed the values for the stoch names as i wasnt having any issues with stoch so i figured its working and i get this: 
Name stoch main: Stoch(5,3,3), Name stoch Signal: , Handle Stoch main: 10, Handle stoch signal: -1

It looks like the stoch signal doesn't have a name also but it has a value of -1, I tried it again and got the same results over and over, does that mean that im doing something wrong, because i can tell that the stoch values are much different. whats going on here

 
Assign handles with iStochastic or iMACD commands, then copy buffers. 

You will find a lot of example into the code base. I don't understand where you found informations to assign handles using ChartIndicatorGet. I'm not sure if it's a wrong approach or not, for sure I never used this approach and I never had problems like yours 

Anyway, even if an indicator has 10 buffers, the handle is only 1. So for stochastic you need only 1 handle, same for macd, same for all indicators.
 
thecerealslayer #:
Wait, when i run the code i displayed the values for the stoch names as i wasnt having any issues with stoch so i figured its working and i get this: 

It looks like the stoch signal doesn't have a name also but it has a value of -1, I tried it again and got the same results over and over, does that mean that im doing something wrong, because i can tell that the stoch values are much different. whats going on here

Why are you using such approach instead of the classic usage of iCustom (or iMACD, iStoch...) ? If you want to see the indicators started with iCustom, use ChartIndicatorAdd().

Even if you have a solid rationale to use this approach, you should apply it correctly and understand how indicator's shortname, handle, window and buffers are working. As already noticed by Fabio you are mixing all.

Also, you should not hardcode any window index.

 
Alain Verleyen #:

Why are you using such approach instead of the classic usage of iCustom (or iMACD, iStoch...) ? If you want to see the indicators started with iCustom, use ChartIndicatorAdd().

Even if you have a solid rationale to use this approach, you should apply it correctly and understand how indicator's shortname, handle, window and buffers are working. As already noticed by Fabio you are mixing all.

Also, you should not hardcode any window

ok thanks for the advice, im quite new to metatrader so i didnt know imacd was a thing. cheers