excelmatlabtime-seriesddealgorithmic-trading

DDE: Time series in Excel analysis


Summary: I need to store/analyze a live time-series that comes into 1 cell in Excel using DDE.

Problem: Since it is 1 cell that is constantly changing, I don't know how to grab each instance of the updated value so I can use it in other formulas, plots, etc. So its 1 cell in an Excel spreadsheet which changes every millisecond, and I want to get the actual time-series (t,t-1, t-2, t-3, etc). I don't know how to store as a time series.

Detail: I am using MetaTrader 4 (MT4) to develop some analysis. The codes to import live prices look like this:

=MT4|BID!EURUSD
=MT4|ASK!EURUSD
=MT4|HIGH!EURUSD
=MT4|LOW!EURUSD
=MT4|TIME!EURUSD

I want to be able to use the time series in various formulas to calculate and update plots in real-time. If I could send the live data to MATLAB, that would be helpful also. But it all has to be live data in real-time analysis.


Solution

  • If you are open to a VBA solution, you can use the Workbook.SetLinkOnData method to call a Sub when the incoming data changes.

    I would suggest only responding to changes on the TIME topic, and copying all relavent data in one go

    Based on this data layout

    Based on this data layout

    Set up the monitor on the Open event (place this in the ThisWorkbook module)

    Sub Workbook_Open()
        Dim wb As Workbook
        Dim Links As Variant
        Dim i As Long
    
        Set wb = ThisWorkbook
        Links = wb.LinkSources(xlOLELinks)
    
        For i = LBound(Links) To UBound(Links)
            If Left$(Links(i), 8) = "MT4|TIME" Then
                wb.SetLinkOnData Links(i), "MT4_OnUpdate"
            End If
        Next
    End Sub
    

    And code your data handler in a normal module

    Sub MT4_OnUpdate()
        ' DDE Updated TIME, copy data
        Dim ws As Worksheet
        Dim Source As Range
        Dim Dest As Range
    
        Set ws = Worksheets("Your DDE Data Sheet")
    
        With ws
            Set Source = ws.Range("A2:E2")
            Set Dest = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, Source.Columns.Count)
        End With
    
        Dest.Value = Source.Value
    End Sub
    

    This will copy cells A2:E2 to the bottom of the Historical data list, each time A2 (time stamp from MT4) changes.

    Note: you say in your OP that you want to update every millsecond. This can't happen, because MT4|TIME is returning a DateTime serial which has a resolution of 1 second. And even if it could happen, that would be way too much data for Excel to handle