Quantstrat - extracting current symbol

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Quantstrat - extracting current symbol

Oskar Gottlieb
Hi,

I have multiple symbols (futures contracts) of one product and I would like
to access a data frame with their meta information. I would then parse the
meta info into an add.indicator argument list. Is there a function, which
would return the current symbol over which we are looping?

Thanks!
Oskar

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quantstrat - extracting current symbol

braverock
On 05/12/2017 03:58 AM, Oskar Gottlieb wrote:
> I have multiple symbols (futures contracts) of one product and I would like
> to access a data frame with their meta information. I would then parse the
> meta info into an add.indicator argument list. Is there a function, which
> would return the current symbol over which we are looping?

quantstrat would expect the metadata for these symbols to be in
FinancialInstrument, not in a data.frame.  That is where quantstrat and
blotter expect to get things like tick size, multiplier, expiration
date, and currency for each instrument.

Inside quantstrat, there will always be a 'symbol' variable which you
could access to find out what symbol is currently being processed
(unless we're in a rebalancing rule, when that doesn't apply).

What I would recommend is to put all your instrument metadata in
FinancialInstrument (since that is what it is for) and have your
indicator and signal functions access that data with

getInstrument(symbol)

To further guess at what you're trying to do, you talk about using this
list for add.indicator.  I don't understand what you're trying to do
with that statement.  The strategy object is a specification of how the
strategy will work.  The indicators, signals, and rules should all work
on the mktdata passed to them by the applyStrategy function (and inside
that by applyIndicators, applySignals, applyRules).  So any call to
add.indicator should be generic for the entire strategy specification.

If you wanted an indicator to do something different based on tenor or
days to expiration, for example, you'd handle that inside the indicator
function, not as parameters to the call to add.indicator. (probably
after calling getInstrument to get the metadata, as described above).

If this hasn't answered your question, perhaps you could follow up with
something more specific including a *minimal* example.

Regards,

Brian

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quantstrat - extracting current symbol

Oskar Gottlieb
Hi Brian,

Thanks for the quick reply!
I was aware of the instrument objects, I just did not know there were the
instrument_attr(), instrument.table() functions. This solves the need for
external meta info dataframe, which is extremely convinient.
The best way to get the attribute then, would be to call
'getInstrument(symbol)$newAttribute'?

About add.indicator - I did a poor job explaining what I was trying to
achieve, but it's exactly what you're saying. I will create a new indicator
function, which will get some metadata from the instrument and all of that
will be handled inside the indicator function, there is no need to pass it
as a parameter to the add.indicator function.

Thanks again!
Oskar

2017-05-12 11:16 GMT+02:00 Brian G. Peterson <[hidden email]>:

> On 05/12/2017 03:58 AM, Oskar Gottlieb wrote:
>
>> I have multiple symbols (futures contracts) of one product and I would
>> like
>> to access a data frame with their meta information. I would then parse the
>> meta info into an add.indicator argument list. Is there a function, which
>> would return the current symbol over which we are looping?
>>
>
> quantstrat would expect the metadata for these symbols to be in
> FinancialInstrument, not in a data.frame.  That is where quantstrat and
> blotter expect to get things like tick size, multiplier, expiration date,
> and currency for each instrument.
>
> Inside quantstrat, there will always be a 'symbol' variable which you
> could access to find out what symbol is currently being processed (unless
> we're in a rebalancing rule, when that doesn't apply).
>
> What I would recommend is to put all your instrument metadata in
> FinancialInstrument (since that is what it is for) and have your indicator
> and signal functions access that data with
>
> getInstrument(symbol)
>
> To further guess at what you're trying to do, you talk about using this
> list for add.indicator.  I don't understand what you're trying to do with
> that statement.  The strategy object is a specification of how the strategy
> will work.  The indicators, signals, and rules should all work on the
> mktdata passed to them by the applyStrategy function (and inside that by
> applyIndicators, applySignals, applyRules).  So any call to add.indicator
> should be generic for the entire strategy specification.
>
> If you wanted an indicator to do something different based on tenor or
> days to expiration, for example, you'd handle that inside the indicator
> function, not as parameters to the call to add.indicator. (probably after
> calling getInstrument to get the metadata, as described above).
>
> If this hasn't answered your question, perhaps you could follow up with
> something more specific including a *minimal* example.
>
> Regards,
>
> Brian
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
>
> _______________________________________________
> [hidden email] 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]]

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quantstrat - extracting current symbol

braverock
You asked:

> The best way to get the attribute then, would be to call
> 'getInstrument(symbol)$newAttribute'?

Yes.

Here's an example:

###

getInstrument('ESH8')

# primary_id        :"ES_H8"
# currency          :"USD"
# multiplier        :50
# tick_size         :0.25
# identifiers       :List of 2
# ..$ X.RIC:"ESH8"
# ..$ local:"ESH8"
# type              :"future_series" "future"
# root_id           :"ES"
# suffix_id         :"H8"
# expires           :"2018-03-16"
# exchange          :"GLOBEX"
# underlying_id     :"ES"
# exchange_id       :"ES"
# series_description:"E-mini S&P 500 2018-03"
# timeZoneId        :"CTT"
# tradingHours      :"T15:30:00/T16:30:00" "T17:00:00/T15:15:00"
# liquidHours       :"T08:30:00/T15:15:00"
# primary_start     :"08:30:00"
# primary_end       :"15:15:00"
# electronic_start  :"15:30:00"
# electronic_end    :"16:30:00"
# validExchanges    :"GLOBEX" "MIBSX"
# updated           : POSIXct, format: "2012-02-27 15:26:53.52408"
# contract_month    :"2018-03"

getInstrument('ESH8')$tick_size
#  [1] 0.25

###

So the method you ask about works just fine.  If you're going to be
extracting multiple attributes, it would make sense to call
getInstrument only once to minimize the search time (we have many
thousands of instruments in our instrument environment) and then take
the attributes you want out of your local copy.

Regards,

Brian

On Fri, 2017-05-12 at 12:07 +0200, Oskar Gottlieb wrote:

> Hi Brian,
>
> Thanks for the quick reply!
> I was aware of the instrument objects, I just did not know there were
> the instrument_attr(), instrument.table() functions. This solves the
> need for external meta info dataframe, which is extremely convinient.
> The best way to get the attribute then, would be to call
> 'getInstrument(symbol)$newAttribute'?
>
> About add.indicator - I did a poor job explaining what I was trying
> to achieve, but it's exactly what you're saying. I will create a new
> indicator function, which will get some metadata from the instrument
> and all of that will be handled inside the indicator function, there
> is no need to pass it as a parameter to the add.indicator function.
>
> Thanks again!
> Oskar
>
> 2017-05-12 11:16 GMT+02:00 Brian G. Peterson <[hidden email]>:
>
> > On 05/12/2017 03:58 AM, Oskar Gottlieb wrote:
> >
> > > I have multiple symbols (futures contracts) of one product and I would
> > > like
> > > to access a data frame with their meta information. I would then parse the
> > > meta info into an add.indicator argument list. Is there a function, which
> > > would return the current symbol over which we are looping?
> > >
> >
> > quantstrat would expect the metadata for these symbols to be in
> > FinancialInstrument, not in a data.frame.  That is where quantstrat and
> > blotter expect to get things like tick size, multiplier, expiration date,
> > and currency for each instrument.
> >
> > Inside quantstrat, there will always be a 'symbol' variable which you
> > could access to find out what symbol is currently being processed (unless
> > we're in a rebalancing rule, when that doesn't apply).
> >
> > What I would recommend is to put all your instrument metadata in
> > FinancialInstrument (since that is what it is for) and have your indicator
> > and signal functions access that data with
> >
> > getInstrument(symbol)
> >
> > To further guess at what you're trying to do, you talk about using this
> > list for add.indicator.  I don't understand what you're trying to do with
> > that statement.  The strategy object is a specification of how the strategy
> > will work.  The indicators, signals, and rules should all work on the
> > mktdata passed to them by the applyStrategy function (and inside that by
> > applyIndicators, applySignals, applyRules).  So any call to add.indicator
> > should be generic for the entire strategy specification.
> >
> > If you wanted an indicator to do something different based on tenor or
> > days to expiration, for example, you'd handle that inside the indicator
> > function, not as parameters to the call to add.indicator. (probably after
> > calling getInstrument to get the metadata, as described above).
> >
> > If this hasn't answered your question, perhaps you could follow up with
> > something more specific including a *minimal* example.
> >
> > Regards,
> >
> > Brian
> >
> > --
> > Brian G. Peterson
> > http://braverock.com/brian/
> > Ph: 773-459-4973
> > IM: bgpbraverock
> >
> > _______________________________________________
> > [hidden email] 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]]
>
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quantstrat - extracting current symbol

Oskar Gottlieb
Hi Brian,

thanks! Last thing, I believe I had it working prior to the weekend, but
now calling getInstrument(symbol) inside the 'add.indicator' function
results simply in error of "object 'symbol' not found".
Do I understand it correctly, that symbol can be called inside of a
function, which is then called by add.indicator()? If so, what am I doing
wrong?

My function looks as follows, its purpose is to get you flat around
expiration:

FLAT <- function(x, t = '12:00'){
  expires <- as.POSIXct(paste(getInstrument(symbol)$expires, t))
  df <- as.data.frame(index(x) >= expires)
  colnames(df) <- 'FLAT'
  FLAT <- xts(df, index(x))
  return(FLAT)
}

Regards
Oskar

2017-05-12 13:35 GMT+02:00 Brian G. Peterson <[hidden email]>:

> You asked:
>
> > The best way to get the attribute then, would be to call
> > 'getInstrument(symbol)$newAttribute'?
>
> Yes.
>
> Here's an example:
>
> ###
>
> getInstrument('ESH8')
>
> # primary_id        :"ES_H8"
> # currency          :"USD"
> # multiplier        :50
> # tick_size         :0.25
> # identifiers       :List of 2
> # ..$ X.RIC:"ESH8"
> # ..$ local:"ESH8"
> # type              :"future_series" "future"
> # root_id           :"ES"
> # suffix_id         :"H8"
> # expires           :"2018-03-16"
> # exchange          :"GLOBEX"
> # underlying_id     :"ES"
> # exchange_id       :"ES"
> # series_description:"E-mini S&P 500 2018-03"
> # timeZoneId        :"CTT"
> # tradingHours      :"T15:30:00/T16:30:00" "T17:00:00/T15:15:00"
> # liquidHours       :"T08:30:00/T15:15:00"
> # primary_start     :"08:30:00"
> # primary_end       :"15:15:00"
> # electronic_start  :"15:30:00"
> # electronic_end    :"16:30:00"
> # validExchanges    :"GLOBEX" "MIBSX"
> # updated           : POSIXct, format: "2012-02-27 15:26:53.52408"
> # contract_month    :"2018-03"
>
> getInstrument('ESH8')$tick_size
> #  [1] 0.25
>
> ###
>
> So the method you ask about works just fine.  If you're going to be
> extracting multiple attributes, it would make sense to call
> getInstrument only once to minimize the search time (we have many
> thousands of instruments in our instrument environment) and then take
> the attributes you want out of your local copy.
>
> Regards,
>
> Brian
>
> On Fri, 2017-05-12 at 12:07 +0200, Oskar Gottlieb wrote:
> > Hi Brian,
> >
> > Thanks for the quick reply!
> > I was aware of the instrument objects, I just did not know there were
> > the instrument_attr(), instrument.table() functions. This solves the
> > need for external meta info dataframe, which is extremely convinient.
> > The best way to get the attribute then, would be to call
> > 'getInstrument(symbol)$newAttribute'?
> >
> > About add.indicator - I did a poor job explaining what I was trying
> > to achieve, but it's exactly what you're saying. I will create a new
> > indicator function, which will get some metadata from the instrument
> > and all of that will be handled inside the indicator function, there
> > is no need to pass it as a parameter to the add.indicator function.
> >
> > Thanks again!
> > Oskar
> >
> > 2017-05-12 11:16 GMT+02:00 Brian G. Peterson <[hidden email]>:
> >
> > > On 05/12/2017 03:58 AM, Oskar Gottlieb wrote:
> > >
> > > > I have multiple symbols (futures contracts) of one product and I
> would
> > > > like
> > > > to access a data frame with their meta information. I would then
> parse the
> > > > meta info into an add.indicator argument list. Is there a function,
> which
> > > > would return the current symbol over which we are looping?
> > > >
> > >
> > > quantstrat would expect the metadata for these symbols to be in
> > > FinancialInstrument, not in a data.frame.  That is where quantstrat and
> > > blotter expect to get things like tick size, multiplier, expiration
> date,
> > > and currency for each instrument.
> > >
> > > Inside quantstrat, there will always be a 'symbol' variable which you
> > > could access to find out what symbol is currently being processed
> (unless
> > > we're in a rebalancing rule, when that doesn't apply).
> > >
> > > What I would recommend is to put all your instrument metadata in
> > > FinancialInstrument (since that is what it is for) and have your
> indicator
> > > and signal functions access that data with
> > >
> > > getInstrument(symbol)
> > >
> > > To further guess at what you're trying to do, you talk about using this
> > > list for add.indicator.  I don't understand what you're trying to do
> with
> > > that statement.  The strategy object is a specification of how the
> strategy
> > > will work.  The indicators, signals, and rules should all work on the
> > > mktdata passed to them by the applyStrategy function (and inside that
> by
> > > applyIndicators, applySignals, applyRules).  So any call to
> add.indicator
> > > should be generic for the entire strategy specification.
> > >
> > > If you wanted an indicator to do something different based on tenor or
> > > days to expiration, for example, you'd handle that inside the indicator
> > > function, not as parameters to the call to add.indicator. (probably
> after
> > > calling getInstrument to get the metadata, as described above).
> > >
> > > If this hasn't answered your question, perhaps you could follow up with
> > > something more specific including a *minimal* example.
> > >
> > > Regards,
> > >
> > > Brian
> > >
> > > --
> > > Brian G. Peterson
> > > http://braverock.com/brian/
> > > Ph: 773-459-4973
> > > IM: bgpbraverock
> > >
> > > _______________________________________________
> > > [hidden email] 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]]
> >
> > _______________________________________________
> > [hidden email] 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.
>



--
Oskar Gottlieb
Email: [hidden email]
Tel: +420 725 005 642

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Loading...