Search code examples
algorithmic-tradingquantitative-financemql4metatrader4forex

How to draw a chart from a CSV-file in MQL4?


I'm new to MQL and MetaTrader 4,
but I want to read a .CSV-file and draw the values I've got into the chart of the Expert Advisor I'm working on.

Every .CSV file has the form of:

;EURUSD;1
DATE;TIME;HIGH;LOW;CLOSE;OPEN;VOLUME
2014.06.11;19:11:00;1.35272;1.35271;1.35271;1.35272;4
2014.06.11;19:14:00;1.35287;1.35282;1.35284;1.35283;30

Where the EURUSD part is the _Symbol, which another program generated, the 1 is the period, and all the other things are the data to draw.

Is there any form to do it inside an Expert Advisor, or do I need to use a Custom Indicator?

If that's the case, how can I do it in the simplest way?

P.S.: I read the data in a struct:

struct entry
{
  string date;
  string time;
  double high;
  double low;
  double close;
  double open;
  int volume;
};

Solution

  • There are three principally different approaches available in MT4

    First,
    one may
    reshuffle data-cells into a compatible format T,O,H,L,C,V and
    import records using F2 History Center [Import] facility of the MetaTrader Terminal. One may create one's own Symbol-name so as to avoid name-colliding cases in the History Center database.

    This way, one lets MT4 to create system-level illustrations of the TOHLCV-data, using the platform's underlying graphical engine.

    enter image description here


    Second,
    one may ignore the underlying graphical engine and
    work on a user-controlled GUI-overlay
    so as to

    implement an algorithm to read a CSV file and
    create a set of MQL4 GUI-objects algorithmically, based on the data contained in the said CSV file. An experience based decision whether to use an { ExpertAdvisor | CustomIndicator } would yield to use a Script for this purpose, due to it's one-shot processing.
    One shall realise, MT4 code-execution ecosystem does a specific context-binding between an MQL4-code ( which is being run ) and an MT4.Graph which does not allow a code launched on a GBPJPY MT4.Graph to process directly objects, related with FTSE.100 MT4.Graph. Yes, if asked to, one may implement a few add-ons and develop a sofisticated distributed processing model to make this work "accross" the said context-binding borders.


    Third,
    and for some cases the most interesting way is a file based approach, where
    one may
    pre-process CSV data in a similar way as in second option but not inside a live-MT4 process, but "beforehand" and
    generate one's own Profile file, keeping an MT4 convention of placing & content of
    - ~/profiles/<aProfileNAME>/chart01.chr
    - ~/profiles/<aProfileNAME>/order.wnd
    -~/profiles/lastprofile.ini, referring <aProfileNAME> on it's first row

    This way, once the MT4 session starts, the pre-fabricated files are pilot-tape auto-loaded and displayed as one wishes to, Q.E.D.

    A .chr file syntax sample:

    <chart>
    id=130394787628125000
    comment=msLIB.TERMINAL: _______________2013.04.15 08:00:00 |cpuClockTIXs = 448765484 |
    symbol=EURCHF
    period=60
    leftpos=6188
    digits=4
    scale=4
    graph=1
    fore=0
    grid=0
    volume=1
    scroll=0
    shift=1
    ohlc=1
    ...
    
    <window>
    height=100
    fixed_height=0
    
    <indicator>
    name=main
    
    <object>
    type=10
    object_name=Fibo 16762
    ...
    
    <object>
    type=16
    object_name=msLIB.RectangleOnEVENT
    period_flags=0
    create_time=1348596865
    color=25600
    style=0
    weight=1
    background=0
    filling=0
    selectable=1
    hidden=0
    zorder=0
    time_0=1348592400
    value_0=1.213992
    time_1=1348624800
    value_1=1.209486
    ray=0
    </object>
    ...
    
    <object>
    type=17
    object_name=msLIB.TriangleMarker
    period_flags=0
    create_time=1348064992
    color=17919
    style=2
    weight=1
    background=0
    filling=0
    selectable=1
    hidden=0
    zorder=0
    time_0=1348052400
    value_0=1.213026
    time_1=1348070400
    value_1=1.213026
    time_2=1348070400
    value_2=1.210476
    </object>