To put data via DDE in excel, all you need is to enter the following formula into the excel cell:
=A|B!C
where
A is a service name
B is a topic
and
C is a value.
So, all you need is a service which publish the current offer data via DDE.
The Trading Station or Lua themselves do not support DDE, so you need an extension module for Lua which provides DDE services.
I developed such extension, you must download and install it before using the DDE offers publishing strategy:
To install the extension module:
1) Save the ddeserver_lua.dll file to your computer.
2) Copy the file into:
*
"C:\Program Files\Candleworks\FXTS2\"
folder if you use a 32 bit operating systemor
*
"C:\Program Files (x86)\Candleworks\FXTS2\"
folder if you use a 64 operating systemThat's all, the extension is ready to be used in this or other strategies.
Now download and install (as described here on wiki: http://fxcodebase.com/wiki/index.php/Cu ... arketscope) the DDE_Offers.lua strategy:
Now you can launch DDE_Offers strategy and just watch the data in Excel (click on the image to see it in full size):
Note 1: The strategy has just one parameter - the name of the service. Each DDE publishing strategy you are executing must have its unique name. However, you can run as many DDE publishing strategies as you need, just put the new service name every time when you start another one strategy.
Note 2: The strategy populates the following data:
1) =TS2OFFERS|OFFERS!LIST
(if you changed the name of the service, use it instead of TS2OFFERS in the formulas!, for example is the service name you entered in the strategy parameters is MYOFFERS, the formula must be =MYOFFERS|OFFERS!LIST).
The semicolon-separated list of the topics. A topic is created for each instrument you are subscribed. The non-character symbols (such as / (slash)) are replaced with an underscore. So, for example EUR/USD offer will be translated as EUR_USD topic.
2) Each offer's topic has the following values:
BID - current bid price
ASK - current ask price
TIME - date and time of the last price change.
DIGITS - precision of the offer's prices.
For example to get a bid price of the EUR/USD, you must enter:
=TS2OFFERS|EUR_USD!BID
3) Please note, that the date and time is populated as a number. But, Trading Station and Excel uses exactly the same date/time format, so all you need is to change the cell format to date/time (for example to m/d/yy hh:mm:ss), and the date/time will be displayed properly.
4) The date/time is always in EST/EDT (New York) time zone
5) The prices are updated every second.
Additional Material:
1) Source code of the strategy:
- Code: Select all
function Init()
strategy:name("DDE Offer")
strategy:description("Publishes Offers via DDE")
strategy.parameters:addString("SRV", "Service Name", "The service name must be unique amoung all running instances of the strategy", "TS2OFFERS");
end
require("ddeserver_lua");
local dde_server;
local ids = {};
local timeid;
function Prepare(onlyName)
instance:name(profile:id() .. "(" .. instance.parameters.SRV .. ")");
if onlyName then
return ;
end
-- start dde server
dde_server = ddeserver_lua.new(instance.parameters.SRV);
local enum = core.host:findTable("offers"):enumerator();
local row, topic;
local offers, offer;
-- create topics
offers = "";
local ofr, val;
while true do
row = enum:next();
if row == nil then
break ;
end
topic = {};
offer = string.gsub(row.Instrument, "([^A-Za-z0-9])", "_");
offers = offers .. offer .. ";";
topic.id = dde_server:addTopic(offer);
topic.bid = dde_server:addValue(topic.id, "Bid");
topic.ask = dde_server:addValue(topic.id, "Ask");
topic.time = dde_server:addValue(topic.id, "Time");
val = dde_server:addValue(topic.id, "Digits");
dde_server:set(topic.id, val, row.Digits);
ids[row.Instrument] = topic;
end
ofr = dde_server:addTopic("OFFERS");
val = dde_server:addValue(ofr, "LIST");
dde_server:set(ofr, val, offers);
timerid = core.host:execute("setTimer", 1, 1);
end
function Update()
end
function AsyncOperationFinished(cookie, success, msg)
if cookie == 1 then
local enum = core.host:findTable("offers"):enumerator();
local row, topic;
-- create topics
while true do
row = enum:next();
if row == nil then
return ;
end
topic = ids[row.Instrument];
if topic ~= nil then
dde_server:set(topic.id, topic.bid, row.Bid);
dde_server:set(topic.id, topic.ask, row.Ask);
dde_server:set(topic.id, topic.time, row.Time);
end
end
end
end
function ReleaseInstance()
core.host:execute("killTimer", timerid);
dde_server:close();
end
2) the source code of ddeserver_lua extension (Visual C++). Please note that you will need Indicore Integration SDK (see here: http://fxcodebase.com/wiki/index.php/Ca ... grationSDK) to compile that code.
3) How to use ddeserver_lua in your own strategy (or indicator)
Step 1) Deploy ddeserver_lua.dll into the same folder where the application is deployed (for TS see instruction above, for Indicore SDK - deploy to
C:\Gehtsoft\IndicoreSDK\
, for your own application - put into the same folder where lua5.1.dll is located).Step 2) Add
require("ddeserver_lua");
before Prepare()
function.Step 3) Declare a global variable for the server, e.g. local
dde_server
before Prepare()
function.Step 4) In the
Prepare()
function, when onlyName
parameter is false
:a) create an instance of the dde server and give the service an unique name. This value shall be used as first part of the DDE reference, e.g. for the code below, all DDE references will be started with
MYSERVICENAME
.dde_server = ddeserver_lua.new("MYSERVICE");
b) Register topic and value inside the topic, keep the identifiers of the topic and value for further usage:
topic = dde_server:addTopic("MYTOPIC");
value = dde_server:addValue(topic, "MYVALUE");
where topic and value are global variables.
Now, your value is available from Excel as =MYSERVICE|MYTOPIC!MYVALUE.
Step 5). Set the value whereever you need.
dde_server:set(topic, value, 12345);
The value can be either a number or a string.
Step 6) Implement
ReleaseInstance()
function and call dde_server:close()
in this function.