[R-SIG-Finance] IBrokers Risk Management Backend Solution Help with twsPortfolioValue

Gei Lin gmonaie at gmail.com
Sun Feb 10 19:47:20 CET 2013


To get around the dialog box for incoming connections make sure the TWS is set with the local host being trusted 127.0.0.1, this will stop it from asking since the request is coming from your local host.

Sent from my iPhone

On 10 Feb 2013, at 18:33, JohnnyPaper <brad.saterfiel at gmail.com> wrote:

Hi Everyone,

I have a couple of questions to ask, and I absolutely don't mind if this
gets resubmitted anywhere else.  It seems as though this may be helpful for
many people trying to manage portfolio risk with IBrokers, which as I may
add is an excellent addition to R.  Very nice work on Jeff's part, and many
many thanks.

I am just trying to keep a loop monitoring my position and shutting them
down if they hit a stop or profit target based on my position value.  Mind
you I am submitting these orders after my offline analysis is done at the
EOD.  So they are market orders executed at the open, hence the reason for
management of the portfolio in this sense through the twsPortfolioValue
updates in the data.frame .

Here is the code I am using and the results:

con <- twsConnect()

while (TRUE){
   Offset<-50
   Val<-twsPortfolioValue(reqAccountUpdates(con),zero.pos=FALSE)
       for (i in 1:nrow(Val)){
           if ( 0.01 * -abs(Val[i,4]) > Val[i,8] ){
               if (Val[i,6] < 0){
                   IBrokers:::.placeOrder( con, twsSTK(toString(Val[i,1])),
twsOrder((i+Offset),"BUY",abs(Val[i,6]),"MKT") )
                   Offset<- Offset +1
               } else if (Val[i,6] > 0){
                   IBrokers:::.placeOrder( con, twsSTK(toString(Val[i,1])),
twsOrder((i+Offset),"SELL",abs(Val[i,6]),"MKT") )
                   Offset<- Offset +1
               }
           } else if (    0.03 * abs(Val[i,4]) < Val[i,8] ){
               if (Val[i,6] < 0){
                   IBrokers:::.placeOrder( con, twsSTK(toString(Val[i,1])),
twsOrder((i+Offset),"BUY",abs(Val[i,6]),"MKT") )
                   Offset<- Offset +1
               } else if (Val[i,6] > 0){
                   IBrokers:::.placeOrder( con, twsSTK(toString(Val[i,1])),
twsOrder((i+Offset),"SELL",abs(Val[i,6]),"MKT") )
                   Offset<- Offset +1
               }
           }
       }
   print(Val)
   Sys.sleep(5)
}

2 -1 2104 Market data farm connection is OK:usfarm
2 -1 2106 HMDS data farm connection is OK:ushmds
  local sectype marketValue averageCost        return position realizedPNL
unrealizedPNL
2   AKAM     STK    -8022.18   -8011.965 -0.0012749681     -227           0       
-10.22
6   ETFC     STK    -7964.32   -7987.240  0.0028695755     -728           0        
22.92
8   FISV     STK    -8048.00   -7994.000 -0.0067550663     -100           0       
-54.00
9     GS     STK    -8006.97   -7985.040 -0.0027463856      -53           0       
-21.93
10   HAR     STK    -8066.78   -8015.330 -0.0064189493     -177           0       
-51.45
11   HNZ     STK    -7987.07   -7990.070  0.0003754658     -131           0         
3.00
12   ICE     STK     8047.89    8042.140  0.0007149839       54           0         
5.75
14  MOLX     STK    -8024.33   -8004.405 -0.0024892541     -291           0       
-19.92
15   MRK     STK    -8017.43   -8007.650 -0.0012213320     -195           0        
-9.78
16   MWV     STK     8053.07    8009.890  0.0053908356      254           0        
43.18
19   OXY     STK    -8041.22   -8027.020 -0.0017690250      -91           0       
-14.20
21  SIAL     STK     8081.32    8013.160  0.0085060074      104           0        
68.16
23   VFC     STK    -7962.50   -8028.320  0.0081984775      -52           0        
65.82
25   ZMH     STK    -8115.41   -8060.680 -0.0067897497     -107           0       
-54.73

2 -1 2100 API client has been unsubscribed from account data.
  local sectype marketValue averageCost        return position realizedPNL
unrealizedPNL
2   AKAM     STK    -8022.18   -8011.965 -0.0012749681     -227           0       
-10.22
6   ETFC     STK    -7964.32   -7987.240  0.0028695755     -728           0        
22.92
8   FISV     STK    -8048.00   -7994.000 -0.0067550663     -100           0       
-54.00
9     GS     STK    -8006.97   -7985.040 -0.0027463856      -53           0       
-21.93
10   HAR     STK    -8066.78   -8015.330 -0.0064189493     -177           0       
-51.45
11   HNZ     STK    -7987.07   -7990.070  0.0003754658     -131           0         
3.00
12   ICE     STK     8047.89    8042.140  0.0007149839       54           0         
5.75
14  MOLX     STK    -8024.33   -8004.405 -0.0024892541     -291           0       
-19.92
15   MRK     STK    -8017.43   -8007.650 -0.0012213320     -195           0        
-9.78
16   MWV     STK     8053.07    8009.890  0.0053908356      254           0        
43.18
19   OXY     STK    -8041.22   -8027.020 -0.0017690250      -91           0       
-14.20
21  SIAL     STK     8081.32    8013.160  0.0085060074      104           0        
68.16
23   VFC     STK    -7962.50   -8028.320  0.0081984775      -52           0        
65.82
25   ZMH     STK    -8115.41   -8060.680 -0.0067897497     -107           0       
-54.73


The main issue that I am having is the connection gets shut down after the
first query where I am getting Val populated so that I can monitor the
positions and take care of orders as need be.  There is the 2100 error
message after the first query...you can see it is shut down. So my question
is this... What is it I need to change for the socket to remain open and
this loop to stay in tact throughout the trading day?  (Also as a side...is
there any way to get around the box that pops up asking to "Allow incoming
API connections" in IB that you may have found?)

Thanks very much for any help to get this issue resolved.

Kind Regards,

Brad



--
View this message in context: http://r.789695.n4.nabble.com/IBrokers-Risk-Management-Backend-Solution-Help-with-twsPortfolioValue-tp4658101.html
Sent from the Rmetrics mailing list archive at Nabble.com.

_______________________________________________
R-SIG-Finance at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list