HOME FORUMS BROKERS SOFTWARE BOOKS CONTACT US
Elite Trader Your Account  •  Become a Member  •  Help  •  Search    
    Forums ›› Technically Speaking ›› Programming ›› IB API Close all Positions  


Post A Reply
    Page 2 of 4:   1   2  3  4  
tradelink
 

Registered: Feb 2008
Posts: 577

 

08-23-12 02:03 AM

there's email and assembla.com ticket notification.

I don't recall syntax offhand for email but pretty sure it's something straightforward like Email.send("from","to","sub","body"), you can insert it anywhere. docs on tradelink.org or one of the other users can probably help you with it.

    Edit/Delete Quote Complain
Mike805
 

Registered: Jul 2004
Posts: 1768

 

08-23-12 02:53 AM


Quote from crmorris:

could you add some sort of email writer to that class? i'd love to know when one of your users is about to run that.



LOL...

Maybe we can start with something simple. Take this snippet of code from IB's reference:

If Not (objTWSControl Is Nothing) Then
If objTWSControl.m_isConnected Then
Call objTWSControl.m_TWSControl.reqAccountUpdates(True, "XXXXXXX")
End If
End If

We can set this on some timer interval and wait for the callback to finish, right? So, the waiting part is what confuses me. How do we wait and check to make sure we have something back that is real? If the call fails and nothing returns ... then what?

This is specific to the Active X API as its the environment I'm working in at the moment. Can I make VBA wait and timeout amd then do something more drastic when a timeout occurs?

    Edit/Delete Quote Complain
braincell
 

Registered: Jul 2011
Posts: 533

 

08-23-12 10:19 AM

Mike, the reqAccountUpdates will send you positions one at a time. The responses from reqAccountUpdates comes via event, so you need to create an event handler for Tws.updatePortfolioEx. Then you can get the symbol and position via "e.contract.symbol, e.position" that your handler function should also contain. From there you can send a counter order for that contract, just invert the position. So it's all events, you dont have to use a timer for anything. It will reliably launch those events, so don't worry. Just make sure you have a check that a position is closed once you send the order, so you don't close it twice because sometimes the same status message can come in twice. There's a bunch of docs for the IB API at : http://www.interactivebrokers.com/p...de/apiguide.htm

btw, in VB.Net you create an event handler like this (Debug.AddLine is my own code, not the .Net debugger... just for illustration purposes)

code:
Private Sub updatePortfolioEx(ByVal sender As Object, ByVal e As AxTWSLib._DTwsEvents_updatePortfolioExEvent) Handles Tws.updatePortfolioEx Debug.AddLine("Symbol = " & e.contract.symbol) Debug.AddLine("Position = " & e.position) End Sub

    Edit/Delete Quote Complain
Mike805
 

Registered: Jul 2004
Posts: 1768

 

08-24-12 06:47 PM


Quote from braincell:

Mike, the reqAccountUpdates will send you positions one at a time. The responses from reqAccountUpdates comes via event, so you need to create an event handler for Tws.updatePortfolioEx. Then you can get the symbol and position via "e.contract.symbol, e.position" that your handler function should also contain. From there you can send a counter order for that contract, just invert the position. So it's all events, you dont have to use a timer for anything. It will reliably launch those events, so don't worry. Just make sure you have a check that a position is closed once you send the order, so you don't close it twice because sometimes the same status message can come in twice. There's a bunch of docs for the IB API at : http://www.interactivebrokers.com/p...de/apiguide.htm

btw, in VB.Net you create an event handler like this (Debug.AddLine is my own code, not the .Net debugger... just for illustration purposes)

code:
Private Sub updatePortfolioEx(ByVal sender As Object, ByVal e As AxTWSLib._DTwsEvents_updatePortfolioExEvent) Handles Tws.updatePortfolioEx Debug.AddLine("Symbol = " & e.contract.symbol) Debug.AddLine("Position = " & e.position) End Sub



Thanks for this - very useful information.

I am still having trouble synching on events, is there any difference in the TWS demo versus the real thing? It seems that no matter what I do, the portfolio update either takes a very long time, or, the request updates call doesn't return. Not sure what to do...

In general, maybe you guys can recommend a course of action as I'm spending too much time on this already?

I have an existing risk manager Excel spreadsheet that is key to my operation; it basically parses all trade signals from my signal generation system, assigns allocation amounts and then sends orders to TWS for execution.

What I would like to see are a set of functions incorporated in this sheet that do the following:

1. Close 50% of the portfolio of either the following:
--Close 50% of all longs : adding liquidity on BATS.
--Close 50% of all shorts : adding liquidity on BATS.
--Close 50% of all longs : taking liquidity on SMART.
--Close 50% of all shorts : taking liquidity on SMART.

2. Close 100% of all positions by adding on BATS.

3. Close 100% of portfolio by sending MOC's for all positions.

4. Cancel all open orders.

I am willing to pay for this, or, exchange some of the functionality I've already built up.

Thanks,
Mike

    Edit/Delete Quote Complain
crmorris
 

Registered: Oct 2008
Posts: 24

 

08-24-12 08:41 PM


Quote from Mike805:

Thanks for this - very useful information.

I am still having trouble synching on events, is there any difference in the TWS demo versus the real thing? It seems that no matter what I do, the portfolio update either takes a very long time, or, the request updates call doesn't return. Not sure what to do...

In general, maybe you guys can recommend a course of action as I'm spending too much time on this already?

I have an existing risk manager Excel spreadsheet that is key to my operation; it basically parses all trade signals from my signal generation system, assigns allocation amounts and then sends orders to TWS for execution.

What I would like to see are a set of functions incorporated in this sheet that do the following:

1. Close 50% of the portfolio of either the following:
--Close 50% of all longs : adding liquidity on BATS.
--Close 50% of all shorts : adding liquidity on BATS.
--Close 50% of all longs : taking liquidity on SMART.
--Close 50% of all shorts : taking liquidity on SMART.

2. Close 100% of all positions by adding on BATS.

3. Close 100% of portfolio by sending MOC's for all positions.

4. Cancel all open orders.

I am willing to pay for this, or, exchange some of the functionality I've already built up.

Thanks,
Mike




use the activex/excel page for positions in the morning, collect opening day positions to insert in system, then use ONLY ats state positions (incrementing and decrementing during the day on fills, not querying ib server). if you have accurate updated positions, set up a vba clock in your main function:

If time > #3:59:00 PM# Then
#turn off enableevents, screenupdating, calculation
closePositions
turnATSOFF
End If

this routine grabs your orders, cancels them, and submits the orders you're talking about. if your system already submits orders, this should be trivially easy to implement. the key, however, is to make sure the system shuts off BEFORE you run your mass cancel / modify. you'd hate to have the system recursively react to your own shutdown actions.

    Edit/Delete Quote Complain
braincell
 

Registered: Jul 2011
Posts: 533

 

08-25-12 11:49 AM


Quote from Mike805:

Thanks for this - very useful information.

I am still having trouble synching on events, is there any difference in the TWS demo versus the real thing? It seems that no matter what I do, the portfolio update either takes a very long time, or, the request updates call doesn't return. Not sure what to do...

In general, maybe you guys can recommend a course of action as I'm spending too much time on this already?

I have an existing risk manager Excel spreadsheet that is key to my operation; it basically parses all trade signals from my signal generation system, assigns allocation amounts and then sends orders to TWS for execution.

What I would like to see are a set of functions incorporated in this sheet that do the following:

1. Close 50% of the portfolio of either the following:
--Close 50% of all longs : adding liquidity on BATS.
--Close 50% of all shorts : adding liquidity on BATS.
--Close 50% of all longs : taking liquidity on SMART.
--Close 50% of all shorts : taking liquidity on SMART.

2. Close 100% of all positions by adding on BATS.

3. Close 100% of portfolio by sending MOC's for all positions.

4. Cancel all open orders.

I am willing to pay for this, or, exchange some of the functionality I've already built up.

Thanks,
Mike



I'd say that the demo account is unlikely to work at all for this. You should log into your paper trading account. If you don't have an IB account, it's a little more difficult, ie not possible. Assuming you have an account, if you use paper trading account, the portfolio updates should work as expected.

As for the functionality you talk about, it's all about knowing the positions you have and doing the opposite.
This is what you need to know how to do:
-Track your positions in quantity (via updatePortfolio, my previous code)
-Send market and limit orders
-Track your sent orders via order ID
-Cancel your order with the assigned order ID

Which of the above do you need help with?

    Edit/Delete Quote Complain
    Page 2 of 4:   1   2  3  4  
Post A Reply


Receive an email whenever a new post is added to this thread by subscribing to it.
 
Rate This Thread:

Forum Jump:
 

 

   Conduct Rules  -  Privacy Policy  -  Day Trader -  Day Trader Forum -  Best Trading Software -  Sitemap Copyright © 2013, Elite Trader. All rights reserved.    
 
WHILE YOU'RE HERE, TAKE A MINUTE TO VISIT SOME OF OUR SPONSORS:
Advantage Futures
Futures Brokerage & Clearing
AMP Global Clearing
Futures and FX Trading
Bright Trading
Professional Equities Trading
CTS
Futures Trading Software
DaytradingBias.com
Professional Trading Analytics
ECHOtrade
Professional Trading Firm
eSignal
Trading Software Provider
FXCM
Forex Trading Services
Global Futures
Futures, Options & FX Trading
Interactive Brokers
Pro Gateway to World Markets
JC Trading Group
Direct Access Trading
MB Trading
Direct Access Trading
MultiCharts
Trading Software Provider
NinjaTrader
Trading Software Provider
OANDA
Currency Trading
optionshouse
Option Trading & Education
Rithmic
Futures Trade Execution Platform
SpeedTrader
Direct Access Trading
SpreadProfessor
Spread Trading Instruction
thinkorswim by TD Ameritrade
Direct Access TradingAdvertisement
TradersStudio
System Building & Backtesting
Trading Technologies
Trading Software Provider
Trend Following
Trading Systems Provider