Ayuda s/ Código y lanzamiento de órdenes a la TWS
Publicado: 31 Dic 2006 18:22
Después de haber trabajado con varios brokers (Caj Madrid Bolsa, Cortalconsors, Estubroker..) por fín he abierto cuenta en IB y he enviado fondos, además todo más facil de lo inicialmente previsto, al menos hasta ahora, ya veremos la operativa y "los cuelgues".
El paso en el que ando atascado es el de enviar órdenes con un clik sobre un botón de una hoja de cálculo.
Veo el código de la Trading Machine y el de la hoja que se descarga en c:\Jts\Excel\TwsDde.xls al instalar la API de IB, en particular el de esta última:
Option Explicit
Dim lastRow As Long
Dim orderId As Integer
Const serverCell = "orderServer"
Const OPEN_ACTION = "open"
Const topic = "ord"
Const START_INDICATOR = "Open Orders"
Const errorRange = "orderErrorPosition"
Const orderStatuses = "orderCells"
Const openOrdContracts = "openOrdContracts"
Const openOrdOrders = "openOrdOrders"
Const openOrdStatuses = "openOrdStatuses"
Const descOffset = 11
Const statusOffset = 16
Const extAttribColumn = 25
Const firstContractCell = "A"
Const lastContractCell = "K"
Const firstStatusCell = "Q"
Const lastStatusCell = "X"
Sub placeOrder()
Call OrderFunctions.order2(Selection.rows, serverCell, extAttribColumn, True)
End Sub
Sub cancelOrder()
Call OrderFunctions.cancelOrder(Selection.rows, serverCell)
End Sub
Sub applyTemplate()
Call OrderFunctions.applyTemplate(Selection.rows, extAttribColumn)
End Sub
Public Sub reqOpenOrder()
Dim server As String, pos As String, addr As String, id As String, descRangeName As String, _
descEndName As String, statusRangeName As String, statusEndName As String
server = util.getServerStr(serverCell)
If server = "" Then Exit Sub
Dim row As Integer
row = CStr(util.getNextAvailableRow(lastRow, START_INDICATOR))
descRangeName = firstContractCell & row
descEndName = lastContractCell & row
statusRangeName = firstStatusCell & row
statusEndName = lastStatusCell & row
Dim theRange As Excel.Range
Set theRange = Range(descRangeName)
Range(descRangeName & ":" & descEndName).Select
With Selection.Interior
.colorIndex = util.lavendarColorIndex
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Range(statusRangeName & ":" & statusEndName).Select
With Selection.Interior
.colorIndex = util.lavendarColorIndex
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
id = util.getIDpre(orderId)
theRange.offset(0, 0).value = util.composeLink(server, topic, id, "symbol")
theRange.offset(0, 1).value = util.composeLink(server, topic, id, "secType")
theRange.offset(0, 2).value = util.composeLink(server, topic, id, "expiry")
theRange.offset(0, 3).value = util.composeLink(server, topic, id, "strike")
theRange.offset(0, 4).value = util.composeLink(server, topic, id, "right")
theRange.offset(0, 5).value = util.composeLink(server, topic, id, "exchange")
theRange.offset(0, 6).value = util.composeLink(server, topic, id, "currency")
theRange.offset(0, descOffset).value = util.composeLink(server, topic, id, "side")
theRange.offset(0, descOffset + 1).value = util.composeLink(server, topic, id, "size")
theRange.offset(0, descOffset + 2).value = util.composeLink(server, topic, id, "orderType")
theRange.offset(0, descOffset + 3).value = util.composeLink(server, topic, id, "limitPrice")
theRange.offset(0, descOffset + 4).value = util.composeLink(server, topic, id, "auxPrice")
theRange.offset(0, statusOffset).value = util.composeLink(server, topic, id, OPEN_ACTION)
theRange.offset(0, statusOffset + 1).value = id
theRange.offset(0, statusOffset + 2).value = util.composeLink(server, topic, id, "status")
theRange.offset(0, statusOffset + 3).value = util.composeLink(server, topic, id, "filled")
theRange.offset(0, statusOffset + 4).value = util.composeLink(server, topic, id, "remaining")
theRange.offset(0, statusOffset + 5).value = util.composeLink(server, topic, id, "price")
theRange.offset(0, statusOffset + 6).value = util.composeLink(server, topic, id, "lastFillPrice")
theRange.offset(0, statusOffset + 7).value = util.composeLink(server, topic, id, "parentId")
End Sub
Sub onShowError()
Call showLastError(serverCell, errorRange)
End Sub
Sub clearLinks()
Call clearErrorDisplay(errorRange)
Call util.clearRange(orderStatuses, util.tanColorIndex)
Call util.clearRange(openOrdContracts, util.tanColorIndex, xlShiftUp)
Call util.clearRange(openOrdOrders, util.darkGreyColorIndex, xlShiftUp)
Call util.clearRange(openOrdStatuses, util.tanColorIndex, xlShiftUp)
End Sub
y me parece demasiado complicado.
Mi pregunta es:
Qué código debería escribirse en una macro para lanzar una orden con los siguientes atributos:
1) Compra ó Venta
2) DAX ó ESTX50
3) Cantidad de contratos
4) vto de marzo-2007 (ó el próximo que sea)
5) Fecha y hora desde la que será válida la orden
6) Fecha y hora hasta que se anula
7) Referencia del grupo OCA al que pertenece
Otra opción, en lugar de orden OCA sería un punto 7') siguiente:
7') Orden condicional a que el mismo futuro sea >=7000 (u otra cantidad).
Mis mejores deseos de felicidad y suerte para todos en este 2007 que llega en unas horas.
Un saludo.
El paso en el que ando atascado es el de enviar órdenes con un clik sobre un botón de una hoja de cálculo.
Veo el código de la Trading Machine y el de la hoja que se descarga en c:\Jts\Excel\TwsDde.xls al instalar la API de IB, en particular el de esta última:
Option Explicit
Dim lastRow As Long
Dim orderId As Integer
Const serverCell = "orderServer"
Const OPEN_ACTION = "open"
Const topic = "ord"
Const START_INDICATOR = "Open Orders"
Const errorRange = "orderErrorPosition"
Const orderStatuses = "orderCells"
Const openOrdContracts = "openOrdContracts"
Const openOrdOrders = "openOrdOrders"
Const openOrdStatuses = "openOrdStatuses"
Const descOffset = 11
Const statusOffset = 16
Const extAttribColumn = 25
Const firstContractCell = "A"
Const lastContractCell = "K"
Const firstStatusCell = "Q"
Const lastStatusCell = "X"
Sub placeOrder()
Call OrderFunctions.order2(Selection.rows, serverCell, extAttribColumn, True)
End Sub
Sub cancelOrder()
Call OrderFunctions.cancelOrder(Selection.rows, serverCell)
End Sub
Sub applyTemplate()
Call OrderFunctions.applyTemplate(Selection.rows, extAttribColumn)
End Sub
Public Sub reqOpenOrder()
Dim server As String, pos As String, addr As String, id As String, descRangeName As String, _
descEndName As String, statusRangeName As String, statusEndName As String
server = util.getServerStr(serverCell)
If server = "" Then Exit Sub
Dim row As Integer
row = CStr(util.getNextAvailableRow(lastRow, START_INDICATOR))
descRangeName = firstContractCell & row
descEndName = lastContractCell & row
statusRangeName = firstStatusCell & row
statusEndName = lastStatusCell & row
Dim theRange As Excel.Range
Set theRange = Range(descRangeName)
Range(descRangeName & ":" & descEndName).Select
With Selection.Interior
.colorIndex = util.lavendarColorIndex
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Range(statusRangeName & ":" & statusEndName).Select
With Selection.Interior
.colorIndex = util.lavendarColorIndex
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
id = util.getIDpre(orderId)
theRange.offset(0, 0).value = util.composeLink(server, topic, id, "symbol")
theRange.offset(0, 1).value = util.composeLink(server, topic, id, "secType")
theRange.offset(0, 2).value = util.composeLink(server, topic, id, "expiry")
theRange.offset(0, 3).value = util.composeLink(server, topic, id, "strike")
theRange.offset(0, 4).value = util.composeLink(server, topic, id, "right")
theRange.offset(0, 5).value = util.composeLink(server, topic, id, "exchange")
theRange.offset(0, 6).value = util.composeLink(server, topic, id, "currency")
theRange.offset(0, descOffset).value = util.composeLink(server, topic, id, "side")
theRange.offset(0, descOffset + 1).value = util.composeLink(server, topic, id, "size")
theRange.offset(0, descOffset + 2).value = util.composeLink(server, topic, id, "orderType")
theRange.offset(0, descOffset + 3).value = util.composeLink(server, topic, id, "limitPrice")
theRange.offset(0, descOffset + 4).value = util.composeLink(server, topic, id, "auxPrice")
theRange.offset(0, statusOffset).value = util.composeLink(server, topic, id, OPEN_ACTION)
theRange.offset(0, statusOffset + 1).value = id
theRange.offset(0, statusOffset + 2).value = util.composeLink(server, topic, id, "status")
theRange.offset(0, statusOffset + 3).value = util.composeLink(server, topic, id, "filled")
theRange.offset(0, statusOffset + 4).value = util.composeLink(server, topic, id, "remaining")
theRange.offset(0, statusOffset + 5).value = util.composeLink(server, topic, id, "price")
theRange.offset(0, statusOffset + 6).value = util.composeLink(server, topic, id, "lastFillPrice")
theRange.offset(0, statusOffset + 7).value = util.composeLink(server, topic, id, "parentId")
End Sub
Sub onShowError()
Call showLastError(serverCell, errorRange)
End Sub
Sub clearLinks()
Call clearErrorDisplay(errorRange)
Call util.clearRange(orderStatuses, util.tanColorIndex)
Call util.clearRange(openOrdContracts, util.tanColorIndex, xlShiftUp)
Call util.clearRange(openOrdOrders, util.darkGreyColorIndex, xlShiftUp)
Call util.clearRange(openOrdStatuses, util.tanColorIndex, xlShiftUp)
End Sub
y me parece demasiado complicado.
Mi pregunta es:
Qué código debería escribirse en una macro para lanzar una orden con los siguientes atributos:
1) Compra ó Venta
2) DAX ó ESTX50
3) Cantidad de contratos
4) vto de marzo-2007 (ó el próximo que sea)
5) Fecha y hora desde la que será válida la orden
6) Fecha y hora hasta que se anula
7) Referencia del grupo OCA al que pertenece
Otra opción, en lugar de orden OCA sería un punto 7') siguiente:
7') Orden condicional a que el mismo futuro sea >=7000 (u otra cantidad).
Mis mejores deseos de felicidad y suerte para todos en este 2007 que llega en unas horas.
Un saludo.