Create "Adaptive Algo" Order - Interactive Broker API

12 views (last 30 days)
I'm trying to determine the matlab method of creating an "Adaptive Algo" order in the Interactive Broker api. The c# method is stated here: https://interactivebrokers.github.io/tws-api/ibalgos.html#adaptive however, I can't figure out its matlab equivalent
C#:
Order baseOrder = OrderSamples.LimitOrder("BUY", 1000, 1);
...
AvailableAlgoParams.FillAdaptiveParams(baseOrder, "Normal");
client.placeOrder(nextOrderId++, ContractSamples.USStockAtSmart(), baseOrder);
...
public static void FillAdaptiveParams(Order baseOrder, string priority)
{
baseOrder.AlgoStrategy = "Adaptive";
baseOrder.AlgoParams = new List<TagValue>();
baseOrder.AlgoParams.Add(new TagValue("adaptivePriority", priority));
}
What I've done so far in Matlab:
% Create TWS connection and Create Order
ib = ibtws('',7496);
ibOrder = ib.Handle.createOrder;
ibOrder.action = 'BUY';
ibOrder.totalQuantity = 1000;
ibOrder.orderType = 'LMT';
% Specifying Adaptive Algo Order
ibOrder.algoStrategy = "Adaptive";

Accepted Answer

Annie Leonhart
Annie Leonhart on 21 Dec 2019
Edited: Annie Leonhart on 23 Dec 2019
I have not tested this, but it should work. I'm about 99.75% sure. Let me know how it goes. If you want to change the algo, the same concept will apply. Create the algo properties, then set the value. Easy.
% Connect to IBTWS or GATEWAY
ib = ibtws('',4001,0);
% Create Contract
contract = ib.Handle.createContract;
contract.symbol = 'AAPL';
contract.secType = 'STK';
contract.exchange = 'SMART';
contract.primaryExchange = 'SMART';
contract.currency = 'USD';
% Create Order
order = ib.Handle.createOrder;
order.action = 'BUY';
order.totalQuantity = 1000;
order.orderType = 'LMT';
order.lmtPrice = '274.25';
% Add properties for Algo order
order.algoStrategy = 'Adaptive';
addproperty(order.algoParams,'adaptivePriority');
order.algoParams.adaptivePriority = 'Normal';
% Place the order
id = orderid(ib);
exec = createOrder(ib,contract,order,id);
% Check order status after 5 seconds
pause(5);
exec(1,1).STATUS
  4 Comments
Kruin Null
Kruin Null on 30 Dec 2019
Hi,
The updated solution worked perfectly!
Thanks!
Annie Leonhart
Annie Leonhart on 30 Dec 2019
Edited: Annie Leonhart on 30 Dec 2019
Very good.
You can use the same concept to build out all the other Algos following the API.
For example. Here's the Accumulate/Distribute algo in python converted to MATLAB
Screenshot_9.jpg
@staticmethod
def FillAccumulateDistributeParams(baseOrder: Order, componentSize: int,
timeBetweenOrders: int, randomizeTime20: bool, randomizeSize55: bool,
giveUp: int, catchUp: bool, waitForFill: bool, startTime: str,
endTime: str):
baseOrder.algoStrategy = "AD"
baseOrder.algoParams = []
baseOrder.algoParams.append(TagValue("componentSize", componentSize))
baseOrder.algoParams.append(TagValue("timeBetweenOrders", timeBetweenOrders))
baseOrder.algoParams.append(TagValue("randomizeTime20",
int(randomizeTime20)))
baseOrder.algoParams.append(TagValue("randomizeSize55",
int(randomizeSize55)))
baseOrder.algoParams.append(TagValue("giveUp", giveUp))
baseOrder.algoParams.append(TagValue("catchUp", int(catchUp)))
baseOrder.algoParams.append(TagValue("waitForFill", int(waitForFill)))
baseOrder.algoParams.append(TagValue("activeTimeStart", startTime))
baseOrder.algoParams.append(TagValue("activeTimeEnd", endTime))
Converted Matlab code
%% Connect to IBTWS or GATEWAY
ib = ibtws('',4001,0);
%% Create Contract
contract = ib.Handle.createContract;
contract.symbol = 'AAPL';
contract.secType = 'STK';
contract.exchange = 'SMART';
contract.primaryExchange = 'SMART';
contract.currency = 'USD';
%% Create Order
order = ib.Handle.createOrder;
%order.account = 'XXXXXXX'
order.action = 'BUY';
order.totalQuantity = 1;
order.orderType = 'LMT';
order.lmtPrice = 100;
%% Add properties for Algo order
FillAccumulateDistrubuteParams(ib, order, 10, 60, 1, 1, 1, 1, 1, '20191231-12:00:00',...
'20191231-16:00:00')
%% Place the order
id = orderid(ib);
exec = createOrder(ib,contract,order,id);
%% Check order status after 5 seconds
exec(1,1).STATUS
% IB Accumulate/Distribute Algo Function
function FillAccumulateDistrubuteParams(c, order, componentSize, timeBetweenOrders,...
randomTime20, randomSize55, giveUp, catchUp, waitForFill, startTime, endTime)
startTime = datestr(startTime,'yyyymmdd HH:MM:SS');
endTime = datestr(endTime,'yyyymmdd HH:MM:SS');
order.algoStrategy = "AD";
algo = c.Handle.createTagValueList;
addproperty(algo, 'componentSize'); algo.componentSize = componentSize;
addproperty(algo, 'timeBetweenOrders');algo.timeBetweenOrders = timeBetweenOrders;
addproperty(algo, 'randomTime20');algo.randomTime20 = randomTime20;
addproperty(algo, 'randomSize55');algo.randomSize55 = randomSize55;
addproperty(algo, 'giveUp');algo.giveUp = giveUp;
addproperty(algo, 'catchUp');algo.catchUp = catchUp;
addproperty(algo, 'waitForFill');algo.waitForFill = waitForFill;
addproperty(algo, 'startTime');algo.startTime = startTime;
addproperty(algo, 'endTime');algo.endTime = endTime;
disp('Algo configured.')
end

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!