Chart operations in an Expert Advisor

 

Hi all,

I have a simple question! I just want to know whether it is possible to use chart index as chart ID in an expert advisor when I call the function ChartSetInteger to change chart's properties such as color? I have tried to use index assuming 0 is the current chart which is not closed yet and starting from 1 to Bars - 1 would be first, second, etc. This indexing was valid in an indicator that I wrote but apparently doesn't work in an Expert Advisor since the charts are not changed when I use index. Also, when I use the function ChartID() to return the ID of the current chart, a long number is returned. Can you please let me know how can I access each chart I want, to modify the properties?

Another question is, does High[] and Low[] indexing in an Expert Advisor work like indicator so I can use indexes there without problem?

Thanks for your help in advance,

D.

 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. You are confusing a bar index [0 … Bars-1] with chart ID. There is no such thing as a chart index. Chart IDs refer to different charts open in the terminal. Bar indexes refer to bars of a specific chart. Do you really want the EA running on one chart to be making modifications to other charts? High[] and Low[] only apply to the current chart. Your question makes no sense.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3. What do you mean High[] and Low[] "like indicator"? Makes no sense.

    1. Arrays must be manually sized. They have no direction. You must move elements if you want a stack/as-series (set non-series, enlarge the array, set as-series).

    2. Buffers are automatically size, are as-series, and elements are moved for you, new elements are set to EMPTY_VALUE (or your designated. They can also draw on the chart automatically.

    3. In MT4, buffers and the predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.

      To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.
                Event Handling Functions - Functions - Language Basics - MQL4 Reference


 

Thanks William!

My question is not mql4 specific so I think this is the correct place to post. I was actually misunderstood the charts and bars. My intention is to select a chart (current chart, so I guess calling ChartID() returns handle to the current chart) and then access bars using the indexes. As far as I know, I need to create an object over a bar and then modify its graphics including color but I have not found any proper code snippet. The most solutions are pointing indicators and not EAs, but the method might be same. Can you please advice? Lets assume that I want to change the color of the last closed bar which is Bars[1]. Then is there any way to create an object for that *bar* and set its color to some color?


Regards,

D.

 
  1. Danesh Daroui #: My question is not mql4 specific so I think this is the correct place to post.

    MT5 has no High[] or Low[]. Think again.

  2. Danesh Daroui #: My intention is to select a chart (current chart, so I guess calling ChartID() returns handle to the current chart) and then access bars using the indexes. 

    So where is your loop where you find the correct chart ID? You need that for all access calls. r

  3. Now you are implying that you are changing the current chart. Chart ID is now irrevalent; use ChartID(), zero, or the calls that don't have it. You haven't stated a problem.

  4. Danesh Daroui #: As far as I know, I need to create an object over a bar and then modify its graphics including color but I have not found any proper code snippet. T

    Only use know what you need.

  5. Perhaps you should read the manual. ObjectCreate - Object Functions - MQL4 Reference includes examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

State your problem!

 
Danesh Daroui #:

Thanks William!

My question is not mql4 specific so I think this is the correct place to post. 

Danesh Daroui:

Another question is, does High[] and Low[] indexing in an Expert Advisor work like indicator so I can use indexes there without problem?

MQL5 does not have High[] or Low[] so that makes your post MQL4 specific.

You can use  High[] and Low[] in indicators and EAs.

I have moved your topic.

 

I am newbie! Sorry for not being clear. I have implemented an indicator to find "inside" bars and change their color to blue. Very simple. This was done by looping over all closed bars 1..Bars-1 and check high and low values for two consecutive bars.

I want to do same but in an EA to be able to send orders when a condition is satisfied. The problem is that I am not able to paint a bar when an "inside" is detected. I know that an object should be created over a bar but I don't know how to indicate a bar to create an object for it. I think Time[i] should be passed to CreateObject where i is the index of the bar of interest, but it didn't work. Actually the CreateObject always returns false whenever I call it as below:

bool obj = ObjectCreate(ChartID(), "barObj", OBJ_VLINE, 0, Time[1], High[1]);

So the problem is to create an object for a bar and then paint is with an arbitrary color. The rest should be straightforward.

I hope I was clear enough.

Regards,

D.

 
Danesh Daroui #: bool obj = ObjectCreate(ChartID(), "barObj", OBJ_VLINE, 0, Time[1], High[1]);

That will be very inefficient for the terminal. See how the Heiken Ashi indicator uses two histogram buffers to draw its candles.

Reason: