Just for reference:
Please find the code of the strategies which can be used as "pattern" how to change order and which demonstrates that the current production release (1.09.101210) and the current servers works properly:
Create entry order 100 pips far from the market and then change it 20 times by 10 pips
- Code: Select all
function Init()
strategy:name("TEST EDIT 1");
strategy:description("");
strategy.parameters:addString("ACCOUNT", "Account", "", "");
strategy.parameters:setFlag("ACCOUNT", core.FLAG_ACCOUNT);
end
local offer, amount;
local step = 0;
local tradid;
local rate = nil;
local orderid = nil;
local requestid = nil;
local ti;
function Prepare(onlyName)
instance:name(profile:id());
offer = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID;
amount = core.host:execute("getTradingProperty", "baseUnitSize", instance.bid:instrument(), instance.parameters.ACCOUNT);
canclose = core.host:execute("getTradingProperty", "canCreateMarketClose", instance.bid:instrument(), instance.parameters.ACCOUNT);
ti = core.host:execute("setTimer", 1, 1);
end
function Update()
local valuemap, success, msg;
if step == 0 then
-- create order initially
valuemap = core.valuemap();
valuemap.Command = "CreateOrder";
valuemap.OrderType = "LE";
valuemap.OfferID = offer;
valuemap.AcctID = instance.parameters.ACCOUNT;
valuemap.Quantity = amount;
valuemap.BuySell = "S";
valuemap.Rate = instance.bid[NOW] + 100 * instance.bid:pipSize();
rate = instance.bid[NOW] + 100 * instance.bid:pipSize();
valuemap.CustomID = "MYID";
success, msg = terminal:execute(100, valuemap);
assert(success, msg);
step = 1;
core.host:trace("order request id " .. msg);
return ;
end
if step > 0 and step <= 20 then
local enum, row, found;
enum = core.host:findTable("orders"):enumerator();
found = false;
while not found do
row = enum:next();
if row == nil then
break;
end
if row.QTXT == "MYID" then
if orderid == nil then
orderid = row.OrderID;
core.host:trace("order id " .. orderid);
end
found = true;
if math.abs(row.Rate - rate) < 0.00001 then
rate = rate + 10 * instance.bid:pipSize();
valuemap = core.valuemap();
valuemap.Command = "EditOrder";
valuemap.OfferID = offer;
valuemap.AcctID = instance.parameters.ACCOUNT;
valuemap.OrderID = row.OrderID;
valuemap.Rate = rate;
success, msg = terminal:execute(100, valuemap);
assert(success, msg);
core.host:trace("change rate to " .. rate);
step = step + 1;
end
end
end
if not found and orderid ~= nil then
enum:reset();
while not found do
row = enum:next();
if row == nil then
break;
end
if row.OrderID == orderid then
if row.QTXT ~= nil then
core.host:trace("order custom id is " .. row.QTXT);
else
core.host:trace("order custom id is nil");
end
step = 21;
end
end
end
return ;
end
if step > 20 then
if ti ~= nil then
core.host:execute("killTimer", ti);
ti = nil;
end
end
end
function AsyncOperationFinished(cookie, successful, message)
if (cookie == 1) then
Update();
elseif cookie == 100 then
if not successful then
assert(successful, message);
return;
end
end
end
Create entry order 100 pips far from the market with +/-50 pips stop/limit and then change all three orders 20 times by 10 pips
- Code: Select all
function Init()
strategy:name("TEST EDIT 1");
strategy:description("");
strategy.parameters:addString("ACCOUNT", "Account", "", "");
strategy.parameters:setFlag("ACCOUNT", core.FLAG_ACCOUNT);
end
local offer, amount;
local step = 0;
local tradid;
local rate = nil;
local orderid = nil;
local stopid = nil;
local limitid = nil;
local requestid = nil;
local ti;
function Prepare(onlyName)
instance:name(profile:id());
offer = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID;
amount = core.host:execute("getTradingProperty", "baseUnitSize", instance.bid:instrument(), instance.parameters.ACCOUNT);
canclose = core.host:execute("getTradingProperty", "canCreateMarketClose", instance.bid:instrument(), instance.parameters.ACCOUNT);
ti = core.host:execute("setTimer", 1, 1);
end
function Update()
local valuemap, success, msg;
if step == 0 then
-- create order initially
valuemap = core.valuemap();
valuemap.Command = "CreateOrder";
valuemap.OrderType = "LE";
valuemap.OfferID = offer;
valuemap.AcctID = instance.parameters.ACCOUNT;
valuemap.Quantity = amount;
valuemap.BuySell = "S";
valuemap.Rate = instance.bid[NOW] + 100 * instance.bid:pipSize();
rate = instance.bid[NOW] + 100 * instance.bid:pipSize();
valuemap.RateStop = rate + 50 * instance.bid:pipSize();
valuemap.RateLimit = rate - 50 * instance.bid:pipSize();
valuemap.CustomID = "MYID";
success, msg = terminal:execute(100, valuemap);
assert(success, msg);
step = 1;
core.host:trace("order request id " .. msg);
return ;
end
if step > 0 and step <= 20 then
local enum, row, found;
enum = core.host:findTable("orders"):enumerator();
found = false;
while true do
row = enum:next();
if row == nil then
break;
end
if row.QTXT == "MYID" then
if orderid == nil then
orderid = row.OrderID;
stopid = row.StopOrderID;
limitid = row.LimitOrderID;
core.host:trace("order id " .. orderid);
end
found = true;
if math.abs(row.Rate - rate) < 0.00001 then
rate = rate + 10 * instance.bid:pipSize();
valuemap = core.valuemap();
valuemap.Command = "EditOrder";
valuemap.OfferID = offer;
valuemap.AcctID = instance.parameters.ACCOUNT;
valuemap.OrderID = row.OrderID;
valuemap.Rate = rate;
success, msg = terminal:execute(100, valuemap);
assert(success, msg);
core.host:trace("change rate to " .. rate);
valuemap = core.valuemap();
valuemap.Command = "EditOrder";
valuemap.OfferID = offer;
valuemap.AcctID = instance.parameters.ACCOUNT;
valuemap.OrderID = row.StopOrderID;
valuemap.Rate = rate + 50 * instance.bid:pipSize();
success, msg = terminal:execute(100, valuemap);
assert(success, msg);
valuemap = core.valuemap();
valuemap.Command = "EditOrder";
valuemap.OfferID = offer;
valuemap.AcctID = instance.parameters.ACCOUNT;
valuemap.OrderID = row.LimitOrderID;
valuemap.Rate = rate - 50 * instance.bid:pipSize();
success, msg = terminal:execute(100, valuemap);
assert(success, msg);
step = step + 1;
end
end
if row.OrderID == stopid and row.QTXT ~= "MYID" then
core.host:trace("Lost stop custom id");
end
if row.OrderID == limitid and row.QTXT ~= "MYID" then
core.host:trace("Lost limit custom id");
end
end
if not found and orderid ~= nil then
enum:reset();
while not found do
row = enum:next();
if row == nil then
break;
end
if row.OrderID == orderid then
if row.QTXT ~= nil then
core.host:trace("order custom id is " .. row.QTXT);
else
core.host:trace("order custom id is nil");
end
step = 21;
end
end
end
return ;
end
if step > 20 then
if ti ~= nil then
core.host:execute("killTimer", ti);
ti = nil;
end
end
end
function AsyncOperationFinished(cookie, successful, message)
if (cookie == 1) then
Update();
elseif cookie == 100 then
if not successful then
assert(successful, message);
return;
end
end
end