[R-SIG-Finance] IBrokers and fill Price

2012-05-06 Thread Mark Harrison
I have been trying to figure out how to use IBrokers and the API to
place orders, etc.  I have read through the support archives, google,
the docs, presentations, etc and I still can not figure out where I am
going wrong.

Specifically I am trying to place a market order, get my fill price,
and then put in a stop order higher/lower than the fill price.  I am
able to get ids, place the orders, and even seeing what my fill price
is via reqOpenOrders but I am not sure how to get the fill price from
IB in a way that lets me use it - i.e. not just 'cat' to the screen.

Any help would be appreciated.

# Session Info:

R version 2.14.0 (2011-10-31)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] graphics  grDevices utils datasets  stats methods   base

other attached packages:
 [1] blotter_0.8.4  twsInstrument_1.4-0
IBrokers_0.9-3 qmao_1.2.1
 [5] FinancialInstrument_0.13.6 quantmod_0.3-17TTR_0.21-1
   Defaults_1.1-1
 [9] xts_0.8-7  zoo_1.7-7

loaded via a namespace (and not attached):
[1] grid_2.14.0lattice_0.20-6 tools_2.14.0

# Code Examples
tws <- twsConnect(clientId=2)

symbol <- c("GC")
contract <- buildIBcontract(twsFUT(symbol, "NYMEX", '201206'))

id <- reqIds(tws)
a <- twsOrder(id, action = 'BUY', totalQuantity = '1', orderType = 'MKT')
placeOrder(tws, contract, a)
reqOpenOrders(tws)

TWS Message: 2 -1 2104 Market data farm connection is OK:cusfuture
TWS Message: 2 -1 2104 Market data farm connection is OK:usfuture
TWS Message: 2 -1 2104 Market data farm connection is OK:eurofarm
TWS Message: 2 -1 2104 Market data farm connection is OK:cashfarm
TWS Message: 2 -1 2104 Market data farm connection is OK:usopt
TWS Message: 2 -1 2104 Market data farm connection is OK:usfarm
TWS Message: 2 -1 2106 HMDS data farm connection is OK:ushmds
TWS Execution: orderId=1 time=2012-05-06 18:02:10 side=BOT shares=1
symbol=GC conId=45008148 price=1640.50
TWS OrderStatus: orderId=1 status=Filled filled=1 remaining=0
averageFillPrice=1640.50
TWS OrderStatus: orderId=1 status=Filled filled=1 remaining=0
averageFillPrice=1640.50
TWS OrderStatus: orderId=1 status=Filled filled=1 remaining=0
averageFillPrice=1640.50

twsDisconnect(tws)

___
R-SIG-Finance@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.


Re: [R-SIG-Finance] panel data in R

2012-05-06 Thread Richard Herron
I think the splitting is fairly expensive computationally (esp if you
have 14mm observations and 30k firms), so you probably want to
minimize the amount of splits and recombines that you do.

If you can wrap everything in one function (i.e., convert to xts/zoo
and do all transformations) that you can call from -ddply-, then the
two techniques should be roughly equivalent.

If you -ddply- over and over again, then you're better off creating a
list, then applying the functions to the list.

Richard Herron


On Sat, May 5, 2012 at 7:06 PM, Alexander Chernyakov
 wrote:
> Interesting, thank you! do you find it faster to do lists of xts objects or
> use plyr on dataframes (converting to a zoo, then doing what you want,
> converting back to a dataframe and returning which is what i currently do)?
>
> thanks
> alex
>
>
> On Sat, May 5, 2012 at 6:47 PM, Richard Herron 
> wrote:
>>
>> I think I would work with the daily data in lists of xts objects (or
>> one wide xts if only return series), but once I aggregated to the
>> month/year level I would use plm. I don't know of a width limit for
>> xts or data.frame, but I never go close to 30,000. I put each security
>> as an xts object in a list.
>>
>> I typically see people aggregate daily data to monthly data, requiring
>> that there are at least 15 or so daily observations (e.g., generating
>> idiosyncratic vol from a daily return series). This should generate a
>> (unbalanced) panel that you can feed to plm. You can specify formulas
>> with lags on the fly. If you try to lag a variable, but the lag isn't
>> there, plm drops the observation.
>>
>> To use the time series operators in plm estimators you just have to
>> properly format your data (either i,t in the first two columns or use
>> -plm.data-).
>>
>> Richard Herron
>>
>>
>> On Sat, May 5, 2012 at 3:14 PM, Alexander Chernyakov
>>  wrote:
>> > Sure. I will be using fixed effects for some things.  I will mostly be
>> > running regressions (sometimes fixed effect but a lot of the time they
>> > will
>> > be fama-macbeth type) but the key thing I am looking for is the ability
>> > to
>> > lag things on the fly without having to run an apply statement to split
>> > everything up to a list and lag each firm individually, recombine the
>> > list
>> > and then run a regression.
>> >
>> > I currently use zoo but I was under the impression that there is some
>> > limit
>> > to the number of columns one can have, no?  With 30k firms it might not
>> > be
>> > possible to have such a wide zoo object... am I incorrect about this?
>> >
>> >
>> > On Sat, May 5, 2012 at 3:08 PM, Richard Herron
>> > 
>> > wrote:
>> >>
>> >> What kind of models are you estimating? I would use PLM if I were
>> >> doing models with firm fixed effects (FE). But I don't think I see
>> >> firm FE with daily observations. I usually see firm FE at the annual
>> >> level.
>> >>
>> >> If you're either estimating time series models or aggregating daily
>> >> observations to the month-level for cross-sectional models, then a
>> >> list of firm-level time series would be best (or if you're only using
>> >> the return series you could put this in one wide xts or zoo object).
>> >>
>> >> Re: missing data. xts has -na.locf- for carrying forward the last
>> >> non-missing observation. I tend to leave missing observations as
>> >> missing.
>> >>
>> >> Could you provide an example of what you would like to estimate?
>> >>
>> >> Richard Herron
>> >>
>> >>
>> >> On Sat, May 5, 2012 at 11:30 AM, Alexander Chernyakov
>> >>  wrote:
>> >> > Hi Richard,
>> >> > Thanks for your response.  One issue I have run into with PLM is it
>> >> > seems to be fairly slow with large data sets (14 mil date, firm
>> >> > points).  Any tricks with this? Also, it seems to not handle
>> >> > irregularly spaced time points.. it fills in the missing ones with NA
>> >> > so when doing lagging or differencing things don't work correctly.
>> >> >  Do
>> >> > you have any advice on fixing this?
>> >> >
>> >> > Thanks,
>> >> > Alex
>> >> >
>> >> > On Sat, May 5, 2012 at 8:43 AM, Richard Herron
>> >> >  wrote:
>> >> >> What kind of models do plan on using?
>> >> >>
>> >> >> If you plan on using time series models, then I suggest generating a
>> >> >> list where each entry is one firm. This will make it easy to fit
>> >> >> models with lapply.
>> >> >>
>> >> >> If you plan on using panel models, then I suggest using PLM. It is
>> >> >> easy enough to manually code within and between estimators, but if
>> >> >> you
>> >> >> use clustered standard errors or dynamic panel models, then PLM will
>> >> >> make you life a lot easier.
>> >> >>
>> >> >> Richard Herron
>> >> >>
>> >> >>
>> >> >> On Fri, May 4, 2012 at 6:30 PM, Alexander Chernyakov
>> >> >>  wrote:
>> >> >>>
>> >> >>> Hi,
>> >> >>> This question is of a general nature: How do people handle panel
>> >> >>> data
>> >> >>> in R?  For example,  I have returns of firms and each firm has
>> >> >>> daily
>> >> >>> observations.  One way is to use t

Re: [R-SIG-Finance] object .blotter not found

2012-05-06 Thread Peter Chan
Hi Garrett

Yes your guess is good and solution works. Also just found upon the 
getAnywhere() function, which can find hidden objects.  Thanks very much.

Peter




 From: G See 

Cc: "r-sig-finance@r-project.org"  
Sent: Sunday, May 6, 2012 10:31 PM
Subject: Re: [R-SIG-Finance] object .blotter not found

My guess is that either you used a "clear workspace" button on your
GUI, or you did something to the effect of rm(list=ls(all=TRUE)).

.blotter is an environment that is stored under your .GlobalEnv when
you load the package.  If you delete it, you have to put it back with
something like this
    if (!exists('.blotter')) .blotter <- new.env()

Prior to 2012, the .instrument environment was also stored in the
.GlobalEnv, but with FinancialInstrument version 0.10.0, it was moved
to live in the top level of the package.  Therefore, the .instrument
environment doesn't get wiped out if the user clears their workspace.
But, it appears to be missing unless you know where to look.
This should work
    ls(FinancialInstrument:::.instrument)
But, it's not as robust as using
   ls_instruments()

.blotter should probably be moved out of the .GlobalEnv as well, but
for now, either don't clear your .GlobalEnv, or recreate a .blotter
environment after you do.

HTH,
Garrett


> Hi there
>
>
> I am running demo(longtrend) but it is failing on initPortf (although the 
> blotter package is loaded):
>
>
>> ltportfolio='longtrend' > ltaccount='longtrend' > 
>> initPortf(ltportfolio,'GSPC', initDate=initDate) Error in 
>> exists(paste("portfolio", name, sep = "."), envir = .blotter,  :  object 
>> '.blotter' not found
>
> Any ideas?  I also noticed that .instrument environment isn't found in 
> [[elided Yahoo spam]]
>
>> ls(envir=.instrument) Error in ls(envir = .instrument) : object 
>> '.instrument' not found > getInstrument('USD') primary_id :"USD"
> currency   :"USD"
> multiplier :1
> tick_size  :0.01
> identifiers: list()
> type       :"currency" > ls(envir=.blotter) Error in ls(envir = .blotter) : 
> object '.blotter' not found
>
> Thanks
>
> Peter
>
>> sessionInfo() R version 2.15.0 (2012-03-30)
> Platform: x86_64-pc-mingw32/x64 (64-bit) locale:
> [1] LC_COLLATE=English_Singapore.1252  LC_CTYPE=English_Singapore.1252
> [3] LC_MONETARY=English_Singapore.1252 LC_NUMERIC=C
> [5] LC_TIME=English_Singapore.1252     attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base      
> other attached packages:
> [1] blotter_0.8.8              FinancialInstrument_0.14.2 quantmod_0.3-17
> [4] TTR_0.21-1                 xts_0.8-6                  zoo_1.7-7
> [7] Defaults_1.1-1             loaded via a namespace (and not attached):
> [1] grid_2.15.0    lattice_0.20-6 tools_2.15.0
>
>        [[alternative HTML version deleted]]
>
> ___
> R-SIG-Finance@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.
[[alternative HTML version deleted]]

___
R-SIG-Finance@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.

Re: [R-SIG-Finance] object .blotter not found

2012-05-06 Thread G See
My guess is that either you used a "clear workspace" button on your
GUI, or you did something to the effect of rm(list=ls(all=TRUE)).

.blotter is an environment that is stored under your .GlobalEnv when
you load the package.  If you delete it, you have to put it back with
something like this
if (!exists('.blotter')) .blotter <- new.env()

Prior to 2012, the .instrument environment was also stored in the
.GlobalEnv, but with FinancialInstrument version 0.10.0, it was moved
to live in the top level of the package.  Therefore, the .instrument
environment doesn't get wiped out if the user clears their workspace.
But, it appears to be missing unless you know where to look.
This should work
ls(FinancialInstrument:::.instrument)
But, it's not as robust as using
   ls_instruments()

.blotter should probably be moved out of the .GlobalEnv as well, but
for now, either don't clear your .GlobalEnv, or recreate a .blotter
environment after you do.

HTH,
Garrett

On Sun, May 6, 2012 at 1:23 AM, Peter Chan  wrote:
> Hi there
>
>
> I am running demo(longtrend) but it is failing on initPortf (although the 
> blotter package is loaded):
>
>
>> ltportfolio='longtrend' > ltaccount='longtrend' > 
>> initPortf(ltportfolio,'GSPC', initDate=initDate) Error in 
>> exists(paste("portfolio", name, sep = "."), envir = .blotter,  :  object 
>> '.blotter' not found
>
> Any ideas?  I also noticed that .instrument environment isn't found in my 
> workspace, but I can use getInstrument!
>
>> ls(envir=.instrument) Error in ls(envir = .instrument) : object 
>> '.instrument' not found > getInstrument('USD') primary_id :"USD"
> currency   :"USD"
> multiplier :1
> tick_size  :0.01
> identifiers: list()
> type       :"currency" > ls(envir=.blotter) Error in ls(envir = .blotter) : 
> object '.blotter' not found
>
> Thanks
>
> Peter
>
>> sessionInfo() R version 2.15.0 (2012-03-30)
> Platform: x86_64-pc-mingw32/x64 (64-bit) locale:
> [1] LC_COLLATE=English_Singapore.1252  LC_CTYPE=English_Singapore.1252
> [3] LC_MONETARY=English_Singapore.1252 LC_NUMERIC=C
> [5] LC_TIME=English_Singapore.1252     attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base      
> other attached packages:
> [1] blotter_0.8.8              FinancialInstrument_0.14.2 quantmod_0.3-17
> [4] TTR_0.21-1                 xts_0.8-6                  zoo_1.7-7
> [7] Defaults_1.1-1             loaded via a namespace (and not attached):
> [1] grid_2.15.0    lattice_0.20-6 tools_2.15.0
>
>        [[alternative HTML version deleted]]
>
> ___
> R-SIG-Finance@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.

___
R-SIG-Finance@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.