Re: [R-SIG-Finance] PerformanceAnalytics

2021-09-08 Thread Brian G. Peterson via R-SIG-Finance
On Wed, 2021-09-08 at 09:14 +, Pankaj K Agarwal via R-SIG-Finance
wrote:
> Dear AllIn the PerformanceAnalytics package, there is a function that
> computes annualized Sharpe Ratios. However, the same is not available
> for computing annualized values of other metrics like Treynor's
> Ratio, Sortiono Ratio etc. Can someone please help? 

Pankaj,

The annualization of the Sharpe Ratio relies on the (incorrect) square
root of time estimate for scaling the variance of returns. 

The same imprecise scaling estimate is not relevant to the scaling of
different denominators, such as the MAR for the Sortino Ratio, or the
Beta for the Treynor Ratio, or drawdowns for the Calmar Ratio or
Sterling Ratio.  

You could argue that simply calculating anual metrics and taking the
ratios would solve the problem, but then you run into the issue of not
enough annual observations to have any trust in the annuallized numbers
(the standard errors would be very wide, because of the small number of
observations).

It probably makes more sense to calculate all of these ratios over the
period of time that you plan to hold the investment, so that you
understand the likely risk during your expected holding period.  

Regards,

Brian

___
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] Indicator as histogram or rectangle boxes instead of line

2021-04-05 Thread Brian G. Peterson
plot.xts was based on chart_Series, so adding panels with plot.xts
should work.

Also, you can absolutely use 'h' plots with add_TA.  See e.g.
chart.Posn in the blotter package:

https://github.com/braverock/blotter/blob/master/R/chart.Posn.R

Regards,

Brian

On Mon, 2021-04-05 at 14:28 +0200, Mike wrote:
> I use chart_Series for plotting. add_TA adds an indicator with line
> chart.
> 
> Can I use add_TA also for plotting an indicator as histogram or
> rectangle boxes like add_Vo?
> 
> I tried add_TA's parameter taType with 'h' or 'b' without success.
> 
> Mike
> 
> ___
> 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] Retrieving corporate event information for listed companies

2021-04-02 Thread Brian G. Peterson


On Fri, 2021-04-02 at 21:59 -0400, H wrote:
> 
> I don't know how quickly this database is updated and therefore the
> delay but if possible i would like to use a primary source rather
> than a secondary source. It might be that parsing XBRL information
> from SEC EDGAR filings is workable - too early to tell.
> [[alternative HTML version deleted]]

Not all EDGAR filings need to be electronic, depending on the size of
the company.  

CRSP is not timely.  A commercial service like Factset or similar will
have everything a few seconds to a few minutes after it is announced.
EDGAR is slower.

Regards,

Brian

[[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] Retrieving corporate event information for listed companies

2021-04-02 Thread Brian G. Peterson
CRSP is still the gold standard in daily data, including corporate
actions and most fundamentals (with the paired Compustat database).  It
is typically accessed via WRDS.  It's not cheap, so Mark's suggestion
of university access is probably among the best.  

For paid sources, something like Factset is probably much cheaper than
CRSP/Compustat/WRDS.

For free data, I don't really think there are any 'best' sources.  All
the data is public, but the collation of that data into a easily parsed
dataset is not.

Regards,

Brian

-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

On Fri, 2021-04-02 at 20:01 -0400, Mark Leeds wrote:
> Hi: I'm not absolutely certain if it has what you need but, if you
> are a
> student, you might be able to get access to
> crisp data for free.  It stands for center for research in security
> pricing
> and has some connection to the university of chicago.
> They sell ( or atleast used to )-provide lots of data on pricing and
> corporate actions. I don't know the current status of it but it
> might be worth looking into.
> 
> 
> 
> 
> 
> 
> On Fri, Apr 2, 2021 at 5:15 PM H  wrote:
> 
> > On 04/02/2021 06:14 AM, Andrew Piskorski wrote:
> > > On Mon, Mar 29, 2021 at 08:46:35PM -0400, H wrote:
> > > > I would appreciate thoughts on a question pertaining to
> > > > corporate event
> > information:
> > > > 
> > > > What would be the "best" source for automating retrieval of eg
> > dividends, ex-dividend dates, earning dates going forward etc.?
> > > For company fundamental data, in years past I used the S
> > > Compustat
> > > XpressFeed product, and was pretty happy with it.  It had all
> > > filings,
> > > plus daily prices and other ancillary data, arranged in a sane
> > > SQL
> > > schema, loadable into either Oracle or MS SQL Server (using their
> > > Windows C# XpressFeed Loader app, later converted to Java).  It
> > > was
> > > expensive, but if you're willing to pay, it's likely still a good
> > > choice today.
> > > 
> > > Since we had Compustat, I never tried parsing data myself from
> > > Edgar
> > > or wherever else.  But of course if all you really want are dates
> > > of
> > > earnings announcements and dividends, it wouldn't make sense to
> > > pay
> > > for a full-featured product Compustat.
> > > 
> > I have taken a very first look at parsing EDGAR filings in XML
> > format and
> > XBRL but let's say I want to begin by finding out past and upcoming
> > ex-dividend dates, dividends, split announcements and splits - I am
> > not
> > sure how I could "easily" retrieve those in an automated fashion.
> > Next item
> > of interest would be company announcements of other types of
> > upcoming key
> > events.
> > 
> > It is of course possible that the above items are announced in
> > various
> > press releases which would require reading those manually.
> > 
> > I do not know where eg Compustat gets its information but I am not
> > looking
> > for a commercial product. Yahoo lists some of these items such as
> > ex-dividend dates, dividends, splits, not sure where they get the
> > information.
> > 
> > ___
> > 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.

[[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] PnL data - PerformanceAnalytics /

2021-02-04 Thread Brian G. Peterson
VaR/ES should work with a distribution of cash P as well as a
distribution of returns.

Can you present an example that isn't behaving as you expect it to?

Regards,

Brian 

On Wed, 2021-02-03 at 17:58 +, Johan Palleschitz wrote:
> Good evening all,
> 
> I have PnL data in EUR that i want use for ES, VaR
> calculationw/reports, as described in the documentation.
> 
> I have read the PerformanceAnalytics guideline, searched on SO but i
> do not find a solution that works for me. 
> 
> Any suggestion or indication where i should look would be very
> appreciated!!
> 
> Have a nice evening!!
> Regards
> Johan
> 
> ___
> 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] Quantstrat Position Accounting System: Netting vs Hedging Systems

2020-11-12 Thread Brian G. Peterson
The chained order quantity isn't directly passed to the triggered chain
rule right now.

Rules have access to the full current state, since they are path
dependent.  

So, you have two options here.  You can either 

- write a custom order sizing function that queries the order status to
check the chained rule and order to get the quantity

- propose a patch/pull request to rules.R and ruleSignal.R that could
process a chain.qty optional argument

see the code in rules.R to see how we pass chain.price, and it should
inform how you would take either of the above approaches.

Regards,

Brian

-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

On Thu, 2020-11-12 at 13:43 +0300, Ayhan yuksel wrote:
> One more question to solve my case
> 
> We can access the parent rule’s order price using chain.price
> argument  in
> rule functions.
> 
> Is it also possible to access parent rule’s order size as well? I
> searched
> but couldn't find it
> 
> Assume that the parent order may take variable position size for each
> trade. It takes one long position and then adds a second long
> position
> before hitting the stop loss for the first one. After that the stop
> loss
> for the first position is hit. We need to know the position size of
> the
> first trade in writing stop loss rule. Is it possible to access the
> position size of the parent rule?
> 
> Thanks for your help
> Regards
> 
> 
> 
> 
> 
> On 9 Nov 2020 Mon at 11:09 Ayhan yuksel 
> wrote:
> 
> > Thank you for the clarification.
> > 
> > On Sun, 8 Nov 2020 at 16:21, Ilya Kipnis 
> > wrote:
> > 
> > > Look at your stoploss rule and the orderqty it calls for. You
> > > actively
> > > specify orderqty = 'all'. That means to flatten the entire
> > > position.
> > > 
> > > As far as a stop loss on a partial position, in your particular
> > > case, you
> > > can simply set that with a better specified orderqty argument by
> > > specifying
> > > 100 lots instead of 'all'.
> > > 
> > 
> 
> [[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.


Re: [R-SIG-Finance] How can I calculate annualized log returns when the year is different from a calendar year

2020-11-09 Thread Brian G. Peterson
I don't use tidyquant, but you can easily use endpoints() in xts to get
the end of March timestamps and calculate your period return that way.

Brian

On Mon, 2020-11-09 at 13:45 +0530, Maulik Bhatt wrote:
> Dear all,
> 
> I want to calculate annualized log return for a security. But the
> issue 
> is that the company follows financial year from April to March. So,
> all 
> its other ratios are calculated from April to March year. The data is
> taken using the package tidyquant.
> 
> library(tidyquant)
> 
> df<- tq_get("SBIN.NS")
> 
>  From the package vignette of tidyquant, the periodReturn can be 
> calculated for yearly, but in that case the year is calendar year.
> But I 
> want to get the return for financial year. In case of this company,
> the 
> financial year is from April to March. So, what modifications do I
> need 
> to make in order to calculate annual log return from April to March?
> 
> Thanks in advance,
> 
> Maulik.
> 
> ___
> 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] periodReturn at the acutual day

2020-08-10 Thread Brian G. Peterson
getSymbols and getQuote are not the same thing.

getSymbols("^IBEX",src="yahoo",from="1998-01-01")
last(IBEX)
# IBEX.Open IBEX.High IBEX.Low IBEX.Close IBEX.Volume IBEX.Adjusted
#2020-08-
#076929.16961.8   6876.7 6950.5   1841510006950.5

The data returned is from Friday, the 7th's close, not from Monday, the
10th, for me.

I assume that Yahoo probably updates their data once a day, sometime
after the official close is published, and their vendor updates.

The joys of trying to use free data.

Brian

-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

On Mon, 2020-08-10 at 23:43 +0200, Pedro páramo wrote:
> I think,
> 
> getQuote("IBEX")
>   Trade Time  Last Change % Change  Open High Low
> IBEX 2020-08-10 16:00:01 17.15   1.75 11.36364 15.65   18 15.0601
>  Volume
> IBEX 456965
> 
> > getQuote("IBEX")$Last
> 
> Is the final value for each periodReturn but the function
> periodReturn(IBEX,period="monthly") dont use Last price,
> 
> Is there a way to obtain monthly return with Last Price?
> 
> 
> 
> El lun., 10 ago. 2020 a las 21:37, Pedro páramo (<
> percentil...@gmail.com
> >)
> escribió:
> 
> > But I think there is a way to obtain last price, doesn´t it? With
> > getQuote? I have to look for it, so I see no way to use close, do
> > you now
> > another function to know the historial return by frequency
> > different to
> > periodReturn, to see if it is posible?
> > 
> > 
> > 
> > 
> > El lun., 10 ago. 2020 a las 20:40, Ethan Smith (<
> > smith_et...@hotmail.com
> > >)
> > escribió:
> > 
> > > Yahoo provides End of Day (EOD) data. You cannot get the current
> > > days
> > > closing price until the market closes for the day and the final
> > > closing
> > > price is captured
> > > 
> > > In fact, many EOD data provider require additional time after the
> > > close
> > > before the current day’s prices are available
> > > 
> > > 
> > > 
> > > *From: *Pedro páramo <
> > > percentil...@gmail.com
> > > >
> > > *Sent: *Monday, August 10, 2020 12:23 PM
> > > *To: 
> > > *r-sig-finance@r-project.org
> > > 
> > > *Subject: *[R-SIG-Finance] periodReturn at the acutual day
> > > 
> > > 
> > > 
> > > Hi all,
> > > 
> > > I am using to make some statistical analysis this functions:
> > > 
> > > library(quantmod)
> > > getSymbols("^IBEX",src="yahoo",from="1998-01-01")
> > > MensualR = periodReturn(IBEX,period="monthly")
> > > AnualR = periodReturn(IBEX,period="yearly")
> > > 
> > > The thing is that if I print MensualR or AnualR they "show" data
> > > to the
> > > previous friday 07/08/2020
> > > 
> > > There is some way to obtain data to the actual date (not previous
> > > friday
> > > close).
> > > 
> > > [[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.
> 
>

___
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] hist from a data frame that is a list

2020-07-30 Thread Brian G. Peterson
In the future, please direct general R questions to stack orverflow or
the general R-help list.  This question is for Finance and finance-
related packages.

try:

hist(bwchist$reval)

If that doesn't work, try 

class(bwchist)

I suspect that the long list of tidyverse packages in your prior emails
may have led you astray, and basic things won't work.

Regards,

Brian


On Thu, 2020-07-30 at 20:18 +0200, Pedro páramo wrote:
> Hi all,
> 
> I have made some calculus and creating this:
> 
> 
> bwfinal <- matrix(bestworst, nrow =35 , ncol = 1)
> bwfinal2 <- matrix(namebw, nrow =35 , ncol = 1)
> 
> bwc<-cbind(bwfinal2,bwfinal)
> colnames(bwc)=c("Accion","reval")
> bwc <- as.data.frame(bwc)
> colnames(bwchist)=c("Accion","reval")
> bwchist <-as.data.frame(bwc[order(bwc$reval), ])
> 
> It is supposed thar bwchist is a data frame but when I try to make an
> hist
> it says me
> 
> Error in hist.default(bwchist) : 'x' must be numeric
> 
> It looks like bwchist is a list but dont I have tried to use unnlist
> but
> always appears the same error
> 
> My bwchist is like this when run on the command line How should I
> treat
> this data frame to hist Accio on x axis and values of reval on Y
> axis?:
> 
>  bwchist
>   Accion   reval
> 20   IAG  -0.6962448
> 7   Sabadell  -0.6788028
> 22 Melia  -0.5636132
> 26Repsol  -0.4645528
> 23Merlin  -0.4545645
> 8  Santander  -0.4368512
> 5  ArcelorMittal  -0.3847631
> 6   BBVA  -0.3774718
> 4Amadeus  -0.3755909
> 28Telefonica  -0.3598171
> 35 Indra   -0.358055
> 2ACS   -0.354641
> 34  Almirall  -0.3510929
> 19  Colonial  -0.3501759
> 32   CIE   -0.315845
> 9  Bankinter  -0.2999523
> 21Mapfre  -0.2913895
> 3   Aena  -0.2759459
> 31Bankia   -0.262466
> 10 CaixaBank  -0.2539538
> 18   Inditex  -0.2505565
> 13  ENCE  -0.2414169
> 24   Naturgy  -0.2409723
> 30  Acerinox  -0.1881223
> 16   Grifols  -0.1819918
> 15 Ferrovial   -0.173434
> 12Enagas  -0.0312891
> 25   REE 0.006016704
> 1Acciona  0.04221062
> 14Endesa  0.07195485
> 33  MasMovil   0.1120944
> 27   SGamesa   0.2178233
> 29  Viscofan0.245229
> 17 Iberdrola   0.2585861
> 11   Cellnex0.429028
> 
>   [[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.
-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

___
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] Select best worst

2020-07-29 Thread Brian G. Peterson
Try a *minimal* example...  

I'm not going to take the time to sort through your code, especially
with all the totally unnecessary mutates and pipes.


cret <- rnorm(10) # dummy cumulative returns
names(cret) <- c('a','b','c','d','e','f','g','h','i','j')
sort(cret)

You should be able to easily use this to get the order of the columns
that you want.

Regards,

Brian


-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

On Wed, 2020-07-29 at 15:37 +0200, Pedro páramo wrote:
> Hi all,
> 
> I have differente stocks
> 
> AAcciona<- tq_get("ANA.MC",from = '2019-12-31',get = "stock.prices")
> ACS<- tq_get("ACS.MC",from = '2019-12-31',get = "stock.prices")
> Aena<- tq_get("AENA.MC",from = '2019-12-31',get = "stock.prices")
> Amadeus<- tq_get("AMS.MC",from = '2019-12-31',get = "stock.prices")
> ArcelorMittal<- tq_get("MTS.MC",from = '2019-12-31',get =
> "stock.prices")
> BBVA<- tq_get("BBVA.MC",from = '2019-12-31',get = "stock.prices")
> Sabadell<- tq_get("SAB.MC",from = '2019-12-31',get = "stock.prices")
> Santander<- tq_get("SAN.MC",from = '2019-12-31',get = "stock.prices")
> Bankinter<- tq_get("BKT.MC",from = '2019-12-31',get = "stock.prices")
>   Indra<- tq_get("IDR.MC",from = '2019-12-31',get = "stock.prices")
> And I have applied this code to know the year to date accumulated
> returns,
> the thing is that I want to order them by the last observation (year
> to
> date) and make a plot of the 3 best/worst (bigger/lower
> accumulated returns).
> 
> ¿Is there a way to compare and order the last observation of
> STOCKcum_return of each stock and select the 3 top and 3 bottom so I
> can
> plot like worst/best without going manually?
> 
> Many thanks in advance
> 
> Hope I can explain
> 
> 
> Indra_daily_returns <- Indra %>%
>   tq_transmute(select = adjusted,   # this specifies which
> column
> to select
>mutate_fun = periodReturn,   # This specifies what to
> do
> with that column
>period = "daily",  # This argument calculates
> Daily
> returns
>col_rename = "idr_returns") # renames the column
> Indra_cum_returns <- Indra_daily_returns %>%
>   mutate(cr = cumprod(1 + idr_returns)) %>%  # using the cumprod
> function
>   mutate(cumulative_returns = cr - 1)
> 
> AAcciona_daily_returns <- AAcciona %>%
>   tq_transmute(select = adjusted,   # this specifies which
> column
> to select
>mutate_fun = periodReturn,   # This specifies what to
> do
> with that column
>period = "daily",  # This argument calculates
> Daily
> returns
>col_rename = "idr_returns") # renames the column
> AAcciona_cum_returns <- AAcciona_daily_returns %>%
>   mutate(cr = cumprod(1 + idr_returns)) %>%  # using the cumprod
> function
>   mutate(cumulative_returns = cr - 1)
> 
> ACS_daily_returns <- ACS %>%
>   tq_transmute(select = adjusted,   # this specifies which
> column
> to select
>mutate_fun = periodReturn,   # This specifies what to
> do
> with that column
>period = "daily",  # This argument calculates
> Daily
> returns
>col_rename = "idr_returns") # renames the column
> ACS_cum_returns <- ACS_daily_returns %>%
>   mutate(cr = cumprod(1 + idr_returns)) %>%  # using the cumprod
> function
>   mutate(cumulative_returns = cr - 1)
> 
> AENA_daily_returns <- AENA %>%
>   tq_transmute(select = adjusted,   # this specifies which
> column
> to select
>mutate_fun = periodReturn,   # This specifies what to
> do
> with that column
>period = "daily",  # This argument calculates
> Daily
> returns
>col_rename = "idr_returns") # renames the column
> Aena_cum_returns <- Aaena_daily_returns %>%
>   mutate(cr = cumprod(1 + idr_returns)) %>%  # using the cumprod
> function
>   mutate(cumulative_returns = cr - 1)
> 
> Amadeus_daily_returns <- Amadeus %>%
>   tq_transmute(select = adjusted,   # this specifies which
> column
> to select
>mutate_fun = periodReturn,   # This specifies what to
> do
> with that column
>period = "daily",  # This argument calculates
> Daily
> returns
>col_rename = "idr_returns") # renames the column

Re: [R-SIG-Finance] Save a plot

2020-07-28 Thread Brian G. Peterson
The problem is that you need to open the png device, call your plots,
and then close the device.  You would do the same e.g. with a pdf.

Try something like this:

##

library(PerformanceAnalytics)
library(quantmod)
library(png)


png("plot-1.png", width=1000, height=600)
chart.CumReturns(ret,wealth.index = TRUE,legend.loc = "bottomleft",
yaxis.pct=100)
title("SP500 reval", adj = 0, line = 3, cex.main=1.25)
title("Eco", adj = 0.75, line = -22)

dev.off()
##
On Tue, 2020-07-28 at 22:46 +0200, Pedro páramo wrote:
> Hi all I want to save a code on a png but it saves me a png with
> 0k(nothing on the plot) but I see it on R-Studio. No error is
> happening, whatshould be happening?
> Many thanks for your help
> library(PerformanceAnalytics)library(dplyr)library(tibble)library(lub
> ridate)library(PerformanceAnalytics)library(quantmod)library(ggplot2)
> library(png)library(grid)library(RCurl)
> Eurostoxx50<-getSymbols(src='yahoo',"^STOXX50E",  env
> =parent.frame(),from="2019-12-31")Ibex35<-getSymbols("^IBEX", env =
> parent.frame(),from="2019-12-31")SP500<-getSymbols("^GSPC", env =
> parent.frame(),from="2019-12-31")Nasdaq<-getSymbols("^IXIC", env =
> parent.frame(),from="2019-12-30")
> ret <-
> na.omit(CalculateReturns(cbind(Cl(IBEX),Cl(STOXX50E),Cl(GSPC),Cl(IXIC
> ))) )# same as Cl(AAPL)/lag(Cl(AAPL)) -
> 1))chart.CumReturns(ret,wealth.index = TRUE,legend.loc =
> "bottomleft",yaxis.pct=100)title("SP500 reval", adj = 0, line = 3,
> cex.main=1.25)title("Eco", adj = 0.75, line = -22)png("plot-1.png",
> width=1000, height=600)
>   [[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.

I also wonder about the multiple calls to title().  chart.CumReturns has a 
title argument.
-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

[[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] A Simple Estimation of Bid-Ask Spreads from Daily Close, High, and Low Prices

2020-07-28 Thread Brian G. Peterson
Ajay,
The method you are proposing is easy to write, the authors flat out say
that they didn't really bother to check how accurate their measure is,
but what little checking they do gives highly *implausible* results.
 They also say that it only makes sense for liquid instruments (for
which intraday data is readily available anyway).  So I'm not sure this
makes the impossible any more possible.
Brian
On Tue, 2020-07-28 at 23:50 +0530, Ajay Shah wrote:
> perhaps something like:
> https://onlinelibrary.wiley.com/doi/full/10./j.1540-6261.1984.tb03897.x
> 
> ?
> is easy to write.
> 
> On Tue, 28 Jul 2020 at 23:10, diego peroni  >wrote:
> > Hi All,
> > I’m looking for a function in R to estimate Bid/Ask Spreads of
> > stocksusing Daily candlesticks.
> > Can anyone suggest some implemetations?
> > ThanksDiego___R-SIG-
> > fina...@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
> > questionsshould 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] A Simple Estimation of Bid-Ask Spreads from Daily Close, High, and Low Prices

2020-07-28 Thread Brian G. Peterson
On Tue, 2020-07-28 at 19:40 +0200, diego peroni wrote:
> I’m looking for a function in R to estimate Bid/Ask Spreads of stocks using 
> Daily candlesticks.
> 
> Can anyone suggest some implemetations?


Diego,

I would like to help you, but what you are asking for is simply impossible.

Daily Range, Volume, and Volatility tells you nothing about the intraday 
numbers except their upper/lower bounds.

It certainly doesn't tell you anything about intraday spreads.

You can certainly *guess* that less liquid instruments have larger effective 
spreads, since this is usually the case, but you can't know *what* the spread 
is from daily data.

Brian


[[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] Valuation of FID

2020-06-22 Thread Brian G. Peterson
Eric,

Agreed that this could be constructed as a structured note.

In that case there is an initial principal payment, which may be
leveraged.  Additionally, the principal may be 'principal-protected',
or not.

A structured note may be priced like a swap (if the note is not
principal protected), or it may be priced with a zero coupon bond and
embedded call option (for a principal protected note) or as a more
complex structure depending on the waterfall of payments to note
holders.  Especially in a leveraged and non-principal-protected
structure, the payoffs can be quite complex.

As you point out, the OP did not give us [m]any details about the
contract specification for what he is trying to price.  So I think we
need more specifics to add any more clarity to this thread.

-- 
Brian


On Mon, 2020-06-22 at 15:59 +0300, Eric Berger wrote:
> Christofer provided only a sketch of the structure, but presumably it
> is part of a general class of financial instruments called Structured
> Notes.
> There is a very short entry in Wikipedia that gives a bit of a
> flavor.
> https://en.wikipedia.org/wiki/Structured_note
> 
> 
> Structured notes would normally have a "buyer" who pays the
> issuer/sponsor when the deal is entered.
> By contrast, swaps generally have a value of zero at initiation. (Pre
> the 'big bang' in the CDS market, this was true of CDS swaps also.)
> 
> 
> On Mon, Jun 22, 2020 at 3:44 PM Brian G. Peterson <
> br...@braverock.com
> > wrote:
> > This sounds more like a swap contract than a bond. The principal is
> > some quantity of S (futures, index value* some initial capital,
> > something).
> > 
> > Perhaps look at pricing swaps.
> > 
> > --
> > 
> > Brian
> > 
> > On Sun, 2020-06-21 at 23:16 +0300, Eric Berger wrote:
> > 
> > Hi Christofer,
> > 
> > For this instrument its value today would be the sum of the present
> > 
> > value (pv) of its coupons and the pv of its redemption value.
> > 
> > You have not specified how the redemption value is determined, so I
> > 
> > won't deal with it. Regarding the coupons, you also did not say the
> > 
> > rate of the coupon, so let's say that is fixed, say at C (e.g.
> > C=3%).
> > 
> > Each coupon appears to be C x (Avg Value of the Index), which seems
> > to
> > 
> > be like holding C of an Average Rate Option (with a zero strike
> > 
> > price), also called an Average Price option (in this case an
> > Average
> > 
> > Price Call). Since each coupon is a position in such an option, the
> > 
> > set of coupons is a portfolio of Average Price Calls. Hull and
> > White
> > 
> > discuss valuation for such options, including a reference to Kemna
> > and
> > 
> > Vorst (1990) who treated the case when the average is calculated as
> > a
> > 
> > geometric average and the option is European.
> > 
> > 
> > Hopefully this provides enough clues for you to take it from here.
> > 
> > 
> > Best,
> > 
> > Eric
> > 
> > 
> > On Sun, Jun 21, 2020 at 10:47 PM Christofer Bogaso
> > 
> > <
> > 
> > bogaso.christo...@gmail.com
> > 
> > 
> > > wrote:
> > 
> > 
> > Hi,
> > 
> > 
> > I had placed this question in some other forums, however failed to
> > 
> > garner sufficient information till date. Presenting the same here
> > 
> > hoping to get some insightful ideas from experts here.
> > 
> > 
> > Typically in a Bond the Principal is constant over it's life.
> > However
> > 
> > I have come across a Bond whose principal is variable, say, average
> > of
> > 
> > S quote for the last one month and coupon is paid based on that,
> > 
> > coupon rate being constant. I was looking for some idea how such
> > bond
> > 
> > can be priced?
> > 
> > 
> > Any idea will be highly appreciated.
> > 
> > 
> > Thanks and regards,
> > 
> > 
> > ___
> > 
> > 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
> > 
> > 
> >  maili

Re: [R-SIG-Finance] Valuation of FID

2020-06-22 Thread Brian G. Peterson
This sounds more like a swap contract than a bond.  The principal is
some quantity of S (futures, index value* some initial capital,
something).
Perhaps look at pricing swaps.
On Sun, 2020-06-21 at 23:16 +0300, Eric Berger wrote:
> Hi Christofer,For this instrument its value today would be the sum of
> the presentvalue (pv) of its coupons and the pv of its redemption
> value.You have not specified how the redemption value is determined,
> so Iwon't deal with it. Regarding the coupons, you also did not say
> therate of the coupon, so let's say that is fixed, say at C (e.g.
> C=3%).Each coupon appears to be C x (Avg Value of the Index), which
> seems tobe like holding C of an Average Rate Option (with a zero
> strikeprice), also called an Average Price option (in this case an
> AveragePrice Call). Since each coupon is a position in such an
> option, theset of coupons is a portfolio of Average Price Calls. Hull
> and Whitediscuss valuation for such options, including a reference to
> Kemna andVorst (1990) who treated the case when the average is
> calculated as ageometric average and the option is European.
> Hopefully this provides enough clues for you to take it from here.
> Best,Eric
> On Sun, Jun 21, 2020 at 10:47 PM Christofer Bogaso<
> bogaso.christo...@gmail.com> wrote:
> > Hi,
> > I had placed this question in some other forums, however failed
> > togarner sufficient information till date. Presenting the same
> > herehoping to get some insightful ideas from experts here.
> > Typically in a Bond the Principal is constant over it's life.
> > HoweverI have come across a Bond whose principal is variable, say,
> > average ofS quote for the last one month and coupon is paid based
> > on that,coupon rate being constant. I was looking for some idea how
> > such bondcan be priced?
> > Any idea will be highly appreciated.
> > Thanks and regards,
> > ___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.
-- 
Brian



[[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] Back testing Expected Shortfall

2020-06-10 Thread Brian G. Peterson
On Wed, 2020-06-10 at 20:08 +0200, Daniel Cegiełka wrote:
> śr., 10 cze 2020 o 19:23 Brian G. Peterson <
> br...@braverock.com
> > napisał(a):
> > So, to backtesting...  the newest Basel standard replaces VaR with
> > ES,
> > and requires that banks justify their use of a particular ES model
> > that
> > they are using to calculate required regulatory capital.
> 
> In my opinion, there is one aspect that introduces some confusion. ES
> (CVaR) is now common, but many people, perhaps out of habit, maybe
> for
> historical reasons, still use the term VaR instead of the correct
> name
> (ES).

VaR and ES (CVaR, ETL) are mathematically related to each other, since
ES is the mean loss when the loss exceeds the VaR quantile.

Confusingly, one of the permissible tests of a bank's ES model under
Basel is the 'VaR test' which measures the number of VaR exceeding
events, and the degree of the loss eceeding VaR to evaluate whether the
*ES* model is likely valid.  This test has been widely criticized, and
should likely be avoided as anything other than a quick check of
possible suitability.

Regards,

Brian

___
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] Back testing Expected Shortfall

2020-06-10 Thread Brian G. Peterson
On Wed, 2020-06-10 at 15:08 +0530, Christofer Bogaso wrote:
> I was looking for an idea how banks backtest their models for
> Expected
> Shortfall. Backtesting VaR is well documented but I failed to get any
> practical idea about backtesting ES.
> 
> Any pointer towards the best practice will be really helpful.

If you are using Normal VaR, then you know the Expected Shortfall
estimate too.

If you are using a different mechanism, then of course the mean loss
when the loss exceeds the VaR may be significantly different than the
Normal ES.

So, to backetesting...  the newest Basel standard replaces VaR with ES,
and requires that banks justify their use of a particular ES model that
they are using to calculate required regulatory capital.

To the best of my knowledge, the most widely used and cited approaches
are outlined here:

https://dlu-umich.github.io/docs/Research_Insight_Backtesting_Expected_Shortfall_December_2014.pdf

Generally, I like the overall methodology presented by this paper.  The
only complexity is the need to store (or be able to recalculate) the
full distribution of the tail.  I don't see this as a giant roadblock,
since the tail distribution contains additional information of interest
anyway, the shape of the tail is useful in model validation and
fitting, and disk is cheap.

The models presented in the reference above, while not to my knowledge
directly implemented in R, should be able to be constructed from data
in the recent R packages by Ardia et. al. GAS:

https://journal.r-project.org/archive/2018/RJ-2018-064/RJ-2018-064.pdf

and MSGARCH:

https://www.sciencedirect.com/science/article/pii/S0169207018300840

Regards,

Brian


-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

___
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] Using optimize.portfolio

2020-06-07 Thread Brian G. Peterson
Roger,
If no Return cleaning method is specified, the default portfolio
moments function will use pairwise complete observations:

if you pass momentargs, the internal calculations for mu and sigma will
be replaced by momentargs$mu and momentargs$sigma
Alexios has already pointed out the return target, which is not
specified in your objectives for optimize.portfolio.
I also note that you're using DEoptim, which is unecessarily slow (and
may not always converge to the same result) for a simple mean variance
optimization.  You probably want optimize.method='ROI', which will use
the same direct quadratic approach.  DEoptim (or 'random' or 'GEnSA',
or 'pso') make sense for more complex objectives that aren't amenable
to convex solvers.
Regards,
Brian
 
-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock
On Sun, 2020-06-07 at 20:14 -0400, Roger Bos wrote:
> Thank you to everyone for your suggestions, but I am still having
> trouble.I see that there are two ways to pass custom moments into
> optimizeportfolio, a custom function and using momentargs list.  The
> example codeat the end of this email uses both of those methods, as
> well as the defaultmethod.  I also compare those to portfolio.optim
> and parma.
> I get quite different results for each method.  I know that they will
> notbe exactly the same, but surely I am doing something wrong given
> theresults I am getting.  I would hope that all three optimize
> portfoliomethods would give me the same results.  Here are the
> results I am getting:
>  opt.portf opt.portf.fun opt.portf.args portfolio.optim
> parmaMSFT 0.372 0.190  0.448   0.253
> 0.000AAPL 0.008 0.094  0.044   0.208
> 0.357AMZN 0.000 0.076  0.000   0.055
> 0.119NVDA 0.020 0.000  0.000   0.020
> 0.169CSCO 0.000 0.004  0.002   0.000
> 0.000ADBE 0.004 0.040  0.002   0.033
> 0.032AMGN 0.298 0.348  0.382   0.253
> 0.000ORCL 0.068 0.072  0.010   0.002
> 0.000QCOM0.072 0.000  0.000   0.000
> 0.000GILD   0.158 0.176  0.112   0.177
> 0.322
> opt.portf is optimize.portfolio with internal mu and
> sigmaopt.portf.fun is optimize.portfolio with mu and sigma provided
> in momentFUNopt.portf.args is optimize.portfolio with mu and sigma
> provided inmomentargs
> So if optimize portfolio just uses the column means for mu and cov
> forsigma, why am I getting different results than when I use a custom
> functionor pass in the moments?  Obviously I am doing something wrong
> since I getdifferent results when using momentFUN and
> momentargs.  Thanks in advancefor any help, Roger.
> ###
> library(tidyquant)symbols <-
> c("MSFT","AAPL","AMZN","NVDA","CSCO","ADBE","AMGN","ORCL","QCOM","GIL
> D")
> getYahooReturns <- function(symbols, return_column = "Ad") {  returns
> <- list()  for (symbol in symbols) {getSymbols(symbol, from =
> '2000-01-01', adjustOHLC = TRUE, env =.GlobalEnv, auto.assign =
> TRUE)return <-
> Return.calculate(Ad(get(symbol)))colnames(return) <-
> gsub("\\.Adjusted", "", colnames(return))returns[[symbol]] <-
> return  }  returns <- do.call(cbind, returns)  return(returns)}
> returns <- getYahooReturns(symbols)returns <- returns[-1,
> ]returns[is.na(returns)] <- 0
> # portfolio.optim from tseries packagelibrary(tseries)LB <- rep(0,
> ncol(returns))UB <- rep(1, ncol(returns))popt <- portfolio.optim(x =
> returns, covmat = sigma, reslow = LB, reshigh =UB)
> library(parma)# optimal reward to risk (covariance matrix)parmspec <-
> parmaspec(S = cov(returns), risk = "EV", forecast =colMeans(returns),
> riskType = "optimal", LB = LB, UB = UB)parm <- parmasolve(parmspec)
> library(PortfolioAnalytics)simple.moments <- function(R, ...)
> {  num_assets = ncol(R)  out <- list()  out$mu <- matrix(colMeans(R),
> ncol = 1)  out$sigma <- cov(R)  # out$m3 <-
> PerformanceAnalytics:::M3.MM(R)  # out$m4 <-
> PerformanceAnalytics:::M4.MM(R)  out$m3 <- matrix(0, nrow =
> num_assets, ncol = num_assets^2)  out$m4 <- matrix(0, nrow =
> num_assets, ncol = num_assets^3)  out}
> num_assets = ncol(returns)momentargs <- list()momentargs$mu <-
> matrix(colMeans(returns), ncol = 1)momentargs$sigma <-
> cov(returns)momentargs$m3 <- matrix(0, nrow = num_assets, ncol =
> num_assets^2)momentargs$m4 <- matrix(0, nrow = num_assets, ncol =
> num_assets^3)
> pspec <- portfolio.spec(assets = symbols)pspec &l

Re: [R-SIG-Finance] Using optimize.portfolio

2020-06-06 Thread Brian G. Peterson
On Sat, 2020-06-06 at 14:33 +0200, Enrico Schumann wrote:
> On Fri, 05 Jun 2020, Roger Bos writes:
> 
> > All,
> > 
> > I am comparing optimize.portfolio from the PortfolioAnalytics
> > package to
> > portfolio.optim from the tseries package.  portfolio.optim seems a
> > bit
> > easier to use, but I like the set up of optimize.portfolio.  I have
> > created
> > a minimal reprex below that compares the output of both in case
> > that helps
> > answer my questions.
> > Here are my two primary questions:
> > 
> > 1) What if I wanted to pass a custom covariance matrix to
> > optimize.portfolio, like from a risk model.  Is that possible?  I
> > can pass
> > it to portfolio.optim because covmat is one of the parameters.
> > 2) What if I wanted to pass forecasted returns to
> > optimize.portfolio?  How
> > would that be done.
> > 
> > If there is anything that can be improved in this example, that
> > would be
> > helpful as well.  Thank you in advance for any assistance, Roger.
> > 
> > ###
> > 
> > library(PortfolioAnalytics)
> > library(tidyquant)
> > 
> > symbols <-
> > c("MSFT","AAPL","AMZN","NVDA","CSCO","ADBE","AMGN","ORCL","QCOM","G
> > ILD")
> > 
> > getYahooReturns <- function(symbols, return_column = "Ad") {
> >   returns <- list()
> >   for (symbol in symbols) {
> > getSymbols(symbol, from = '2000-01-01', adjustOHLC = TRUE, env
> > =
> > .GlobalEnv, auto.assign = TRUE)
> > return <- Return.calculate(Ad(get(symbol)))
> > colnames(return) <- gsub("\\.Adjusted", "", colnames(return))
> > returns[[symbol]] <- return
> >   }
> >   returns <- do.call(cbind, returns)
> >   return(returns)
> > }
> > 
> > returns <- getYahooReturns(symbols)
> > returns <- returns[-1, ]
> > returns[is.na(returns)] <- 0
> > 
> > # portfolio.optim from tseries package
> > library(tseries)
> > sigma <- cov(returns)
> > reslow <- rep(0, ncol(returns))
> > reshigh <- rep(1, ncol(returns))
> > popt <- portfolio.optim(x = returns, covmat = sigma, reslow =
> > reslow,
> > reshigh = reshigh)
> > popt$pw
> > 
> > pspec <- portfolio.spec(assets = symbols)
> > pspec <- add.constraint(portfolio=pspec, type="box",
> > min = 0, max = 1, min_sum = 0.99, max_sum =
> > 1.01)
> > pspec <- add.objective(portfolio=pspec,
> >type="return",
> >name="mean")
> > pspec <- add.objective(portfolio=pspec,
> >type="risk",
> >name="var")
> > 
> > opt <- optimize.portfolio(R = returns,
> >portfolio = pspec,
> >optimize_method = "DEoptim", )
> > data.frame(optimize.portfolio = opt$weights, portfolio.optim =
> > round(popt$pw, 3))
> > 
> 
> If all else fails, and supposing that 'PortfolioAnalytics' per
> default computes means and covariances in the standard way, you could
> create input data (time series) that have exactly the desired
> covariances and means:
> 
>   
> https://stackoverflow.com/questions/58293991/how-to-use-fportfolio-package-in-r-for-non-time-series-input/58302451#58302451
> 

per default, PortfolioAnalytics uses sample moments as most users would
expect.  

As I already told the OP, the user may pass mu and sigma and m3 and m4
directly, or may construct custom moment functions to compute the
moments using any method they choose.

This is outlined in section 2 of the vignette:

https://cran.r-project.org/web/packages/PortfolioAnalytics/vignettes/custom_moments_objectives.pdf


and, of course, in the manual.

___
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] Using optimize.portfolio

2020-06-05 Thread Brian G. Peterson
Foger,
See the vignette for custom moment and objective functions:

On Fri, 2020-06-05 at 14:16 -0400, Roger Bos wrote:
> All,
> I am comparing optimize.portfolio from the PortfolioAnalytics package
> toportfolio.optim from the tseries package.  portfolio.optim seems a
> biteasier to use, but I like the set up of optimize.portfolio.  I
> have createda minimal reprex below that compares the output of both
> in case that helpsanswer my questions.Here are my two primary
> questions:
> 1) What if I wanted to pass a custom covariance matrix
> tooptimize.portfolio, like from a risk model.  Is that possible?  I
> can passit to portfolio.optim because covmat is one of the
> parameters.2) What if I wanted to pass forecasted returns to
> optimize.portfolio?  Howwould that be done.
> If there is anything that can be improved in this example, that would
> behelpful as well.  Thank you in advance for any assistance, Roger.
> ###
> library(PortfolioAnalytics)library(tidyquant)
> symbols <-
> c("MSFT","AAPL","AMZN","NVDA","CSCO","ADBE","AMGN","ORCL","QCOM","GIL
> D")
> getYahooReturns <- function(symbols, return_column = "Ad") {  returns
> <- list()  for (symbol in symbols) {getSymbols(symbol, from =
> '2000-01-01', adjustOHLC = TRUE, env =.GlobalEnv, auto.assign =
> TRUE)return <-
> Return.calculate(Ad(get(symbol)))colnames(return) <-
> gsub("\\.Adjusted", "", colnames(return))returns[[symbol]] <-
> return  }  returns <- do.call(cbind, returns)  return(returns)}
> returns <- getYahooReturns(symbols)returns <- returns[-1,
> ]returns[is.na(returns)] <- 0
> # portfolio.optim from tseries packagelibrary(tseries)sigma <-
> cov(returns)reslow <- rep(0, ncol(returns))reshigh <- rep(1,
> ncol(returns))popt <- portfolio.optim(x = returns, covmat = sigma,
> reslow = reslow,reshigh = reshigh)popt$pw
> pspec <- portfolio.spec(assets = symbols)pspec <-
> add.constraint(portfolio=pspec, type="box",min =
> 0, max = 1, min_sum = 0.99, max_sum = 1.01)pspec <-
> add.objective(portfolio=pspec,   type="return",  
>  name="mean")pspec <-
> add.objective(portfolio=pspec,   type="risk",
>name="var")
> opt <- optimize.portfolio(R = returns,   portfolio =
> pspec,   optimize_method = "DEoptim",
> )data.frame(optimize.portfolio = opt$weights, portfolio.optim
> =round(popt$pw, 3))
>   [[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.
https://cran.r-project.org/web/packages/PortfolioAnalytics/vignettes/custom_moments_objectives.pdf
You can simply pass mu and sigma, but you probably want to do a little
bit more work to make it more extensible.

Hopefully this is enough of a pointer.  If not, I can work on an
example based on your example later.

Regards,

Brian
-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

[[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] effects of events that happened at the same time

2020-05-21 Thread Brian G. Peterson
On Thu, 2020-05-21 at 19:17 +, Alec Schmidt wrote:
> I usually use some arma (+garch) model with dummy variables to study
> the effects of various events that happen on different days. I wonder
> if there is some way to discern their impacts if the events happen
> simultaneously, e.g. all macroeconomic announcements in some country
> are published on the last days of the month.Thanks! Alec

The usual way of dealing with intreaday events is to use intraday
data.  tick, minute, or hourly data is widely available globally.
Regards,
Brian


-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

[[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] Portfolio Composition Forecasting

2020-02-29 Thread Brian G. Peterson
On Sat, 2020-02-29 at 09:06 -0500, G Mac wrote:
> Is there an R-package (or other software) that can be used to
> forecast the next period's portfolio composition?  There are many
> portfolio optimization packages, but this is not the same
> question.  Say I take the past* x* periods, each period holds the
> percentage composition of an investment portfolio (sums to 1); the
> composition of assets will contain key assets held (or
> increased/decreased) through periods, but new assets will be
> added to the portfolio over time, while some holdings will be
> dropped, so we will have nuisance here.  I would like to model the
> past* x* periods, accept this mentioned error, and forecast or
> simulate for* x+1* period.
> 
> Does anyone have any experience with this, or have any pointers
> within the broader domain of statistics?
> 

Itr seems to me tomorrows portfolio is the same as today's portfolio
except for organic change in weights caused by market price
fluctuations, or by a rebalancing event.  The 'forecast' is the
standard naive forecast: today's portfolio will still be held tomorrow,
unless you rebalance.

I don't see any value in a simulation from the prior holdings. 
Portfolios are rebalanced for some business reason, and those reasons
are usually pretty well understood, and not the result of a random draw
from some distribution of prior holdings.

What am I missing?

Regards,

Brian

___
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] data differs

2020-01-21 Thread Brian G. Peterson
They are different symbols with  different tenors, so yes, it is normal
for them to differ.

iirc, IR is a 90-day interbank rate, and YT is a 3-yr bond  

It's been a while, so I might have those tenors wrong, but either way
they are futures for different rates.  Why would you expect them to be
the same?

On Wed, 2020-01-22 at 15:10 +1100, Stephen Choularton wrote:
> Hi
> 
> I am studying the spread between a couple of interest rate futures
> on 
> the ASX using R and IB
> 
> The futures are IR and IT.
> 
> This data line is produced by a callback looping on them at 4:30 pm
> when 
> trade finishes:
> 
> time ->  2020-01-21 16:29:59.818593 last IR -> 99.19 last YT ->
> 99.26 
> spread ->  -0.0774
> 
> This data is produced by a call to reqHistoricalData for close
> yesterday 
> (21 Jan)
> 
> last IR ->  99.2 last YT -> 
> 99.29 
> spread ->  -0.01993848
> 
> I know they are 'similar' but is it normal for them to differ?
>

___
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] R in Finance 2020

2019-10-09 Thread Brian G. Peterson
R/Finance 2020 is currently scheduled for June 5-6, 2020

As usual, we expect that it will be held on the UIC campus in Chicago,
IL, USA.

Jeff Ryan is currently working on a new website framework, and we hope
to launch the new website and the Call for Presentations 'soon'.

Regards,

Brian

On Wed, 2019-10-09 at 14:26 +0200, Anton Antonov wrote:
> Dear list,
> 
> I was wondering if you have any news about the R/Finance 2020. I’ve
> been searching any mentions of the event, but no luck so far. 
> 
> Best regards,
> 
> Anton Antonov, PhD
> Lead Quantitative Analyst, dxFeed Solutions DE GmbH

___
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] Resources for AI/ML in Risk Management

2019-09-24 Thread Brian G. Peterson
On Tue, 2019-09-24 at 09:06 +, Pankaj K Agarwal via R-SIG-Finance
wrote:
> Dear allI would be grateful if someone can suggest R-based
> resources/books on use of AI/ML in Financial Risk Management. 
> Regards,Pankaj K Agarwal

You need to be a little more clear about what it is that you intend to
do.

What types of models are you using today?  What do you hope to gain by
using more complicated models?  What challenges are the models you are
currently using encountering?

Both "Financial Risk Management" and "AI/ML" are vast topics.  A simple
intersection doesn't give anyone here any guidance on how to help you. 

Regards,

Brian

-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

___
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] Mothly Returns of Mutual funds

2019-08-04 Thread Brian G. Peterson
On Mon, 2019-08-05 at 05:17 +0530, Atul Agarawal wrote:
> I am New to R. I would like to create Mutual fund Portfolio. Can
> someone help me to understand from where I can download Monthly
> returns of Indian MF.

In the United States and most other places, I would consult with your
broker, or a service that sells that data.

> Also pls share the process document for Portfolio Optimisation and
> which R packages are required to achieve the above goal.

PortfolioAnalytics can do the optimization.  There is lots of
documentation in the package and elsewhere online.

-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

___
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] [PortfolioAnalytics] optimize.portfolio.rebalancing with changing stock universe

2019-08-04 Thread Brian G. Peterson
hus far:
> 
> 
> sjPrices =
> as.data.frame(read_excel("ALSI_Cleaned_Weekly_Prices.xlsx",
> sheet = 1, col_names = T,
> 
> na = "", skip = 0))
> 
> weeklyAmihud = as.data.frame(read_excel("weeklyAmihud.xlsx", sheet =
> 1,
> col_names = T,
> 
>   na = "", skip = 0))
> 
> 
> #create vector of medians (uses function from matrixStats package)
> 
> mid <- rowMedians(as.matrix(weeklyAmihud[, -1]))
> 
> 
> dates = as_date(sjPrices[,1])
> 
> 
> # remove the first column (of dates)
> 
> sjPrices <- sjPrices[, -1]
> 
> weeklyAmihud <- weeklyAmihud[, -1]
> 
> 
> #create xts objects
> 
> sjPrices <- xts(sjPrices, order.by=dates)
> 
> weeklyAmihud <- xts(weeklyAmihud, order.by=dates)
> 
> 
> sjReturns <- ROC(sjPrices)
> 
> 
> #remove first row (0 returns)
> 
> sjReturns <- sjReturns[-1,]
> 
> 
> #this function does penalise the very illiquid stocks but has other
> undesired side-effects
> 
> AMIHUD <- function(sjReturns, weights, wAmihud){
> 
>   return(sum(weights*wAmihud)*100)
> 
> }
> 
> 
> portf <- portfolio.spec(colnames(sjReturns))
> 
> 
> portf <- add.constraint(portf,type = "weight_sum", min_sum = 0.99,
> max_sum
> = 1.01)
> 
> portf <- add.constraint(portf, type="long_only")
> 
> 
> portf <- add.objective(portf, type = "risk", name = "StdDev")
> 
> portf <- add.objective(portf, type="risk", name="AMIHUD",
> 
>arguments=list(wAmihud=weeklyAmihud))
> 
> 
> opt_rebal <- optimize.portfolio.rebalancing(sjReturns,
> 
> portf,
> 
> optimize_method="DEoptim"
> ,
> 
>         rebalance_on="quarters",
> 
> training_period=52,
> 
> rolling_window=52,
> 
> trace=TRUE, traceDE=5,
> search_size=2000)
> 
>   [[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.
-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

___
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] Free financial data - equities, equity options and ETFs - for quantmod package (or other packages)

2019-04-04 Thread Brian G. Peterson

quantmod supports multiple data sources, both paid and free.

The documentation is installed with the package or you can find a pdf here:
https://cran.r-project.org/web/packages/quantmod/quantmod.pdf

The code is here:
https://github.com/joshuaulrich/quantmod/blob/master/R/getSymbols.R

For free daily data sources, I usually use Tiingo or Quandl in my public 
talks.  These certainly have US equities and ETF's.


Especially for intraday data, expect to have to pay for it.  We have had 
good luck with IQFeed on the low end of the cost scale. I am not aware 
of a simple online source for historical options data.  Also, check if 
your broker has an API.  e.g. Interactive Brokers(IB), TD Ameritrade, 
and Fidelity (at least) all have programmatic ways of retrieving data.


IB has an R package, as does Quandl.

For CSV data sources that would be downloaded from online, you can bring 
them directly into R without having to go through the intermediary step 
of saving the file to your local disk. R's data functions can use a url 
as a handle to open a data stream (this is how quantmod's downloaders 
work, see the code above).


Regards,

Brian

On 4/3/19 10:08 PM, H wrote:

I am relatively new to analyzing financial data but have some experience with 
R. I understand that the data available from Yahoo Finance via its API is often 
questionable in quality and Google Finance is no longer available.

Although Googling pointed me to some other sources such as Quandl etc., I am 
curious which other data sources quantmod itself supports for data retrieval, 
ie via an API, not via downloading and importing CSV-files?

My interest is really US equities, stock options and ETFs - if possible from 
the same data source...

Pointers to favorite data sources appreciated!

Thank you.


___
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] corrections vs drawdowns

2019-04-01 Thread Brian G. Peterson
Alec,
Very interesting paper.  Thanks for sharing the results of your
thoughts on this topic.
I note that you reference 'forecast' and 'rugarch' but do not place
them in your references.  They should appear in your
bibliography.  Also, it would be good if you could post the R code
somewhere so that an interested reader could replicate your analysis.
Regards,
Brian

On Mon, 2019-04-01 at 13:39 +, Alec Schmidt wrote:
> Here is my piece about US equity market corrections:
> 
> https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3362361
> 
> 
> 
> 
> 
> 
> 
> I'll greatly appreciate your comments.
> 
> 
> 
> 
> 
> Alec
> 
> 
> 
> 
> 
> 
> 
> From: Brian G. Peterson 
> 
> Sent: Tuesday, January 8, 2019 11:55 AM
> 
> To: Alec Schmidt; r-sig-finance@r-project.org
> 
> Subject: Re: [R-SIG-Finance] corrections vs drawdowns
>  
> 
> 
> I think that this is correct.  NASDAQ was still in a drawdown. 
> NASDAQ
> 
> didn't make new all-time highs until 2014.
> 
> 
> 
> Some people define 'corrections' as drawdown from most recent peak. 
> 
> Charles Schwab's definition is in-line with generally accepted usage:
> 
> 
> 
> 
https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.schwab.com%2Fresource-center%2Finsights%2Fcontent%2Fmarket-correctidata=02%7C01%7Caschmid1%40stevens.edu%7C104e1f582d6242bfce0208d6758a227a%7C8d1a69ec03b54345ae21dad112f5fb4f%7C0%7C0%7C636825633496698033sdata=rkHsOOY4EdLB9LUu4bomU4%2F98T3kHidzSJY%2BGEQ4NsI%3Dreserved=0
> 
> on-what-does-it-mean
> 
> 
> 
> The Motley Fool uses a similar but not identical definition:
> 
> 
> 
> 
https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.schwab.com%2Fresource-center%2Finsights%2Fcontent%2Fmarket-correctidata=02%7C01%7Caschmid1%40stevens.edu%7C104e1f582d6242bfce0208d6758a227a%7C8d1a69ec03b54345ae21dad112f5fb4f%7C0%7C0%7C636825633496698033sdata=rkHsOOY4EdLB9LUu4bomU4%2F98T3kHidzSJY%2BGEQ4NsI%3Dreserved=0
> 
> on-what-does-it-mean
> 
> 
> 
> quantmod has a 'findPeaks' function, but this is dependent on you
> 
> setting a threshold for what defines a peak.
> 
> 
> 
> A related Stack Overflow question may provide something in the
> 
> direction of what you're looking for to look at drawdown from a
> recent
> 
> peak.
> 
> 
> 
> 
https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F14737899%2Fcalculate-cumulatve-growthdata=02%7C01%7Caschmid1%40stevens.edu%7C104e1f582d6242bfce0208d6758a227a%7C8d1a69ec03b54345ae21dad112f5fb4f%7C0%7C0%7C636825633496708038sdata=R9gkq2ILuqhdJQpjyijXw%2Flmogrxto8WP%2BvV05K6lgo%3Dreserved=0
> 
> -drawdown-from-local-min-max
> 
> 
> 
> I would certainly be happy to include a 'findCorrections' function in
> a
> 
> later version of PerformanceAnalytics if we could parameterize what
> 
> constitutes a 'recent high' for that purpose.
> 
> 
> 
> Regards,
> 
> 
> 
> Brian
> 
> 
> 
> 
> 
> On Tue, 2019-01-08 at 16:36 +, Alec Schmidt wrote:
> 
> > Thank you Brian,
> 
> > geometric=FALSE gave me additional corrections in 2011 and 2012 but
> 
> > still no bear market of 2008:
> 
> > 
> 
> >  
> 
> >  08/30/2018 - 12/24/2018 (-11.04%) [80 Days]
> 
> > 07/21/2015 - 02/11/2016 (-10.05%) [143 Days]
> 
> > 09/17/2012 - 11/15/2012 (-8.42%) [42 Days]
> 
> > 03/27/2012 - 06/01/2012 (-9.44%) [47 Days]
> 
> > 07/08/2011 - 08/19/2011 (-15.96%) [31 Days]
> 
> > 05/02/2011 - 06/17/2011 (-7.59%) [34 Days]
> 
> > 02/22/2011 - 03/16/2011 (-6.54%) [17 Days]
> 
> > 07/18/2000 - 10/09/2002 (-97.34%) [559 Days]
> 
> > Alec
> 
> > 
> 
> > 
> 
> > 
> 
> > From: Brian G. Peterson 
> 
> > Sent: Tuesday, January 8, 2019 11:17 AM
> 
> > To: Alec Schmidt; r-sig-finance@r-project.org
> 
> > Subject: Re: [R-SIG-Finance] corrections vs drawdowns
> 
> >  
> 
> > Alec,
> 
> > 
> 
> > I suspect that you may wish to start with setting geometric=FALSE
> in
> 
> > your call to findDrawdowns.
> 
> > 
> 
> > Corrections are usually defined as a peak to trough difference in
> 
> > *price*, as a percentage of the peak price.
> 
> > 
> 
> > So I think you do not want to compound the *returns* in calculating
> 
> > your drawdowns.
> 
> > 
> 
> > Regards,
> 
> > 
> 
> > Brian
> 
> > 
> 
> > 
> 
> > On Tue, 2019-01-08 at 16:09 +, Alec Schmidt wrote:
> 
> > > I tried to use the function findDrawdowns() to compile NASDAQ
> 
> > >

Re: [R-SIG-Finance] Fit skewed-t distribution

2019-04-01 Thread Brian G. Peterson
On Mon, 2019-04-01 at 13:56 +0300, Данир Зулькарнаев wrote:
> Could someone suggest any package to estimates parameters of skewed-t
> distribution?
> I didn't manage to find any.
> 

My personal favorite for the skewed Student's-t distribution family is
the 'sn' package.

There is also the 'skewt' package, but as I recall this only has
p/d/q/r functions, and does not contain a 'fit' function to fit from
existing data.

There are a couple of packages that do multivariate skew-t models as
well.

Regards,

Brian

___
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] corrections vs drawdowns

2019-01-08 Thread Brian G. Peterson
I think that this is correct.  NASDAQ was still in a drawdown.  NASDAQ
didn't make new all-time highs until 2014.

Some people define 'corrections' as drawdown from most recent peak. 
Charles Schwab's definition is in-line with generally accepted usage:

https://www.schwab.com/resource-center/insights/content/market-correcti
on-what-does-it-mean

The Motley Fool uses a similar but not identical definition:

https://www.schwab.com/resource-center/insights/content/market-correcti
on-what-does-it-mean

quantmod has a 'findPeaks' function, but this is dependent on you
setting a threshold for what defines a peak.

A related Stack Overflow question may provide something in the
direction of what you're looking for to look at drawdown from a recent
peak.

https://stackoverflow.com/questions/14737899/calculate-cumulatve-growth
-drawdown-from-local-min-max

I would certainly be happy to include a 'findCorrections' function in a
later version of PerformanceAnalytics if we could parameterize what
constitutes a 'recent high' for that purpose.

Regards,

Brian


On Tue, 2019-01-08 at 16:36 +, Alec Schmidt wrote:
> Thank you Brian,
> geometric=FALSE gave me additional corrections in 2011 and 2012 but
> still no bear market of 2008:
> 
>  
>  08/30/2018 - 12/24/2018 (-11.04%) [80 Days]
> 07/21/2015 - 02/11/2016 (-10.05%) [143 Days]
> 09/17/2012 - 11/15/2012 (-8.42%) [42 Days]
> 03/27/2012 - 06/01/2012 (-9.44%) [47 Days]
> 07/08/2011 - 08/19/2011 (-15.96%) [31 Days]
> 05/02/2011 - 06/17/2011 (-7.59%) [34 Days]
> 02/22/2011 - 03/16/2011 (-6.54%) [17 Days]
> 07/18/2000 - 10/09/2002 (-97.34%) [559 Days]
> Alec
> 
> 
> 
> From: Brian G. Peterson 
> Sent: Tuesday, January 8, 2019 11:17 AM
> To: Alec Schmidt; r-sig-finance@r-project.org
> Subject: Re: [R-SIG-Finance] corrections vs drawdowns
>  
> Alec,
> 
> I suspect that you may wish to start with setting geometric=FALSE in
> your call to findDrawdowns.
> 
> Corrections are usually defined as a peak to trough difference in
> *price*, as a percentage of the peak price.
> 
> So I think you do not want to compound the *returns* in calculating
> your drawdowns.
> 
> Regards,
> 
> Brian
> 
> 
> On Tue, 2019-01-08 at 16:09 +, Alec Schmidt wrote:
> > I tried to use the function findDrawdowns() to compile NASDAQ
> > (^IXIC)
> > corrections. For the sample starting on
> > 
> > 2007-01-01, I get the following start -to-trough periods with
> > drawdowns higher than 10%
> > 
> > 08/30/2018 - 12/24/2018 (-23.64%) [80 Days]
> > 07/21/2015 - 02/11/2016 (-18.24%) [143 Days]
> > 09/17/2012 - 11/15/2012 (-10.90%) [42 Days]
> > 03/27/2012 - 06/01/2012 (-12.01%) [47 Days]
> > 05/02/2011 - 10/03/2011 (-18.71%) [108 Days]
> > 11/01/2007 - 03/09/2009 (-55.63%) [339 Days]
> > 
> > 
> > However, if the sample starts on 2000-06-01, I get
> > 08/30/2018 - 12/24/2018 (-23.64%) [80 Days]
> > 07/21/2015 - 02/11/2016 (-18.24%) [143 Days]
> > 07/18/2000 - 10/09/2002 (-73.94%) [559 Days]
> > 
> > i.e. no bear market of 2008...
> > 
> > This is because ^IXIC didn't recover in 2007 from its fall from top
> > in 2000. This implies that various reports on market corrections do
> > not use the max drawdown. Is there consensus (and possibly R
> > scripts)
> > that address this problem?
> > 
> > Thanks! Alec

___
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] corrections vs drawdowns

2019-01-08 Thread Brian G. Peterson
Alec,

I suspect that you may wish to start with setting geometric=FALSE in
your call to findDrawdowns.

Corrections are usually defined as a peak to trough difference in
*price*, as a percentage of the peak price.

So I think you do not want to compound the *returns* in calculating
your drawdowns.

Regards,

Brian

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

On Tue, 2019-01-08 at 16:09 +, Alec Schmidt wrote:
> I tried to use the function findDrawdowns() to compile NASDAQ (^IXIC)
> corrections. For the sample starting on
> 
> 2007-01-01, I get the following start -to-trough periods with
> drawdowns higher than 10%
> 
> 08/30/2018 - 12/24/2018 (-23.64%) [80 Days]
> 07/21/2015 - 02/11/2016 (-18.24%) [143 Days]
> 09/17/2012 - 11/15/2012 (-10.90%) [42 Days]
> 03/27/2012 - 06/01/2012 (-12.01%) [47 Days]
> 05/02/2011 - 10/03/2011 (-18.71%) [108 Days]
> 11/01/2007 - 03/09/2009 (-55.63%) [339 Days]
> 
> 
> However, if the sample starts on 2000-06-01, I get
> 08/30/2018 - 12/24/2018 (-23.64%) [80 Days]
> 07/21/2015 - 02/11/2016 (-18.24%) [143 Days]
> 07/18/2000 - 10/09/2002 (-73.94%) [559 Days]
> 
> i.e. no bear market of 2008...
> 
> This is because ^IXIC didn't recover in 2007 from its fall from top
> in 2000. This implies that various reports on market corrections do
> not use the max drawdown. Is there consensus (and possibly R scripts)
> that address this problem?
> 
> Thanks! Alec
> 
>   [[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.


Re: [R-SIG-Finance] xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing

2018-09-26 Thread Brian G. Peterson
As Amit already said, zero is a better answer than -100.  log returns
don't behave well with -100x compounding returns.  This is what is
causing Inf/-Inf problems.  there is a 'zerofill' utility function
available in PerformanceAnalytics to make this replacement easier.  

see:

?zerofill

If you are *also* having a problem with order.by, your sample data
would b e insufficient to reproduce it, since the index in the subset
you pasted seems well-formed.

If Amit's and my suggestion to replace NA with 0 in your returns
doesn't solve the problem, then please reply to this thread with a
fully reproducible example (including data e.g. using dput function the
or reprex package).

Regards,

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

On Tue, 2018-09-25 at 17:03 -0400, Simon Hovmark wrote:
> I am trying to run the following optimize.portfolio.rebalancing:
> 
> opt <- optimize.portfolio.rebalancing(R=returns, portfolio=tranch1, 
> optimize_method="ROI",
> #momentFUN =
> tranch1_boudt,
> rebalance_on =
> rebal.freq, 
> training_period =
> training.period,
> rolling_window =
> rolling.window)
> But when I use summary(opt) I get the following error:
> 
> xts(x, order.by = order.by, frequency = frequency, ...) :'order.by'
> cannot contain 'NA', 'NaN', or 'Inf'
> I can see that other has had a similar problem, but I've not been
> able to solve it using their answers. When I sum NA, NaN and InF on
> returns$dato I get 0.
> 
> A subset of my data is here:
> 
> dato   stock_1stock_2stock_3
> 1999-10-14 -0.002006019 0.016164145 -100
> 1999-10-15 0.0 0.0 -100
> 1999-10-18 -0.036813973 -0.049017341 -100
> 1999-10-19 0.016529302 0.0 -100
> 1999-10-20 0.016260521 0.011996238 -100
> 1999-10-21 0.008032172 0.005806736 -100
> 1999-10-22 0.0 0.0 -100 
> 1999-10-25 0.039220713 0.023164955 -100
> 1999-10-26 0.028437935 0.002152853 -100
> 1999-10-27 -0.032291505 0.014941580 -100
> 1999-10-28 0.030420597 0.011061477 -100
> 1999-10-29 0.0 0.0 -100
> 1999-11-02 0.027702603 -0.003410734 -100
> 1999-11-03 0.007259560 -0.007650743 -100
> 1999-11-04 0.003610112 0.0 -100
> 1999-11-05 0.0 0.0 -100
> 1999-11-08 0.014311514 0.005546033 -100
> 1999-11-09 0.007079676 -0.002373106 -100
> 1999-11-10 0.039763233 0.024512309 -100
> 1999-11-11 -0.001696353 -0.018721296 -100
> 1999-11-12 0.0 0.0 -100
> And here is my full code.
> 
> rebal.freq <- "years"
> training.period <- 0
> rolling.window <- 120
> 
> returns <- read_excel("HEX.xlsx", sheet = 1, col_names = TRUE)
> returns <- xts(returns[,-1], order.by= returns[,1])
> returns <- Return.calculate(returns, method = "log")
> returns <- returns[-1,]
> 
> returns[!is.finite(returns)] <- NA
> returns[!is.finite(returns)] <- NA
> returns <- na.fill(returns, fill = -100)
> sum(is.nan(returns$dato)) #returns 0
> sum(is.infinite(returns$dato)) #returns 0
> sum(is.na(returns$dato)) #returns 0
> 
> fund.names <- colnames(returns)
> tranch1 <- portfolio.spec(assets = fund.names)
> tranch1 <- add.constraint(portfolio = tranch1, type = "leverage")
> tranch1 <- add.constraint(portfolio = tranch1, type = "long_only")
> tranch1 <- add.objective(portfolio=tranch1, type="return",
> name="mean")
> tranch1 <- add.objective(portfolio=tranch1, type="risk",
> name="StdDev")
> 
> opt <- optimize.portfolio.rebalancing(R=returns, portfolio=tranch1, 
> optimize_method="ROI",
> #momentFUN =
> tranch1_boudt,
> rebalance_on =
> rebal.freq, 
> training_period =
> training.period,
> rolling_window =
> rolling.window)
> 
> 
> summary(opt)
> And my sessioninfo:
> 
> R version 3.3.1 (2016-06-21)
> Platform: x86_64-apple-darwin13.4.0 (64-bit)
> Running under: OS X 10.13.3 (unknown)
> 
> locale:
> [1] C
> 
> attached base packages:
> [1] stats graphics  grDevices
> utils datasets  methods   base 
> 
> other attached packages:
> [1] RColorBrewer_1.1-
> 2readxl_0.1.1DEoptimR_1.0-
> 8   
> [4]
> PortfolioAnalytics_1.0.3636   Per

Re: [R-SIG-Finance] bitbucket code

2018-08-29 Thread Brian G. Peterson
> Thanks Brian, I did repost the query per suggestion but left in here
> for other users of `rmgarch` , `PerformanceAnalytics` and `GAS`
> 
> If RTOOLS is not updated how do we use new source from bit/git and
> others
> 

You need the right version of Rtools for your version of R.  

https://cran.r-project.org/bin/windows/Rtools/

You only need to update Rtools when you update R, for the most part.

The devtools install_* functions will download and build the latest
code from the default branch of whatver repository you point them at,
or a specific branch if that is what you need e.g. to test a proposed
patch.

Regards,

Brian


> On Wed 29 Aug, 2018, 21:34 Brian G. Peterson, 
> wrote:
> > This question really belongs on R-help or a forum like Stack
> > Overflow,
> > as it has nothing to do with finance.  I understand that this is
> > related to the thread on rmgarch, but it could have been asked in
> > that
> > thread with more specificity.
> > 
> > On Wed, 2018-08-29 at 15:32 +0530, prof.amit.mit...@gmail.com
> > wrote:
> > > I installed a local zip file a few days ago and the package was
> > > recently modified on bitbucket , with a stable zip version not
> > yet
> > > available.
> > > 
> > > How many days does it usually take to get a stable zip version. 
> > 
> > That depends on when a new version is released to CRAN, or when a
> > version is created in an alternative repository such as DRAT.  It
> > will
> > be completely dependent on the package.  For example, because of
> > the
> > hassle of testing and releasing a large package to CRAN, I
> > typically
> > release new versions of PerformanceAnalytics only about once a
> > year. 
> > Others with smaller packages or more spare time to meet CRAN's
> > ever-
> > changing standards or more automated testing release packages more
> > frequently.
> > 
> > > How can I `devtools` it from the bitbucket the fastest way? I
> > have my
> > > own sourcetree and the author has created in cloud.
> > 
> > I think the answer to your question is in the devtools
> > documentation:
> > 
> > https://www.rstudio.com/products/rpackages/devtools/
> > 
> > You'll also need Rtools on Windows, which I assume is relevant
> > since
> > you mention a zip file.
> > 
> > After you have devtools and Rtools installed, you can use
> > install_bitbucket() to build and install from the repository
> > (assumedly
> > rmgarch).
> > 
> > Regards,
> > 
> > Brian
> 
> -- 
> __
> Amit Mittal
> Pursuing Ph.D. in Finance and Accounting 
> Indian Institute of Management, Lucknow 
> Visit my SSRN author page:
> http://ssrn.com/author=2665511 
> * Top 10% Downloaded Author on SSRN
> Mob: +91 7525023664
> This message has been sent from a mobile device. I may contact you
> again.
> _
On Wed, 2018-08-29 at 21:56 +0530, Amit Mittal wrote:

___
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] bitbucket code

2018-08-29 Thread Brian G. Peterson
This question really belongs on R-help or a forum like Stack Overflow,
as it has nothing to do with finance.  I understand that this is
related to the thread on rmgarch, but it could have been asked in that
thread with more specificity.

On Wed, 2018-08-29 at 15:32 +0530, prof.amit.mit...@gmail.com wrote:
> I installed a local zip file a few days ago and the package was
> recently modified on bitbucket , with a stable zip version not yet
> available.
> 
> How many days does it usually take to get a stable zip version. 

That depends on when a new version is released to CRAN, or when a
version is created in an alternative repository such as DRAT.  It will
be completely dependent on the package.  For example, because of the
hassle of testing and releasing a large package to CRAN, I typically
release new versions of PerformanceAnalytics only about once a year. 
Others with smaller packages or more spare time to meet CRAN's ever-
changing standards or more automated testing release packages more
frequently.

> How can I `devtools` it from the bitbucket the fastest way? I have my
> own sourcetree and the author has created in cloud.

I think the answer to your question is in the devtools documentation:

https://www.rstudio.com/products/rpackages/devtools/

You'll also need Rtools on Windows, which I assume is relevant since
you mention a zip file.

After you have devtools and Rtools installed, you can use
install_bitbucket() to build and install from the repository (assumedly
rmgarch).

Regards,

Brian

___
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] Quantstrat - running applyStrategy in a loop

2018-08-20 Thread Brian G. Peterson
On Sun, 2018-08-19 at 17:16 -0400, James Hirschorn wrote:
> I plan to try it out myself, but I wanted to check here if running
> applyStrategy in a loop, while looping over different dates, will
> work? I could not find any examples of this.
> 
> There are 2 reasons for wanting to do this: First of all, one could
> have a couple of years of tick data, which is too big to fit in
> memory for each symbol. Of course, I am assuming that the orders
> placed by the strategy are sparse enough so that the order_book
> generated by applyStrategy can still fit in memory.
> 
> The second reason is that if this loop could moreover be run in
> parallel, then there could potentially be a 500x speed up for two
> years of data.

James,

The answer is 'it depends'.

There is a parallel version of applyStrategy in the sandbox on github. 
I haven't touched it in several years, so I wouldn't trust that code. I
mention it as an example of what is theoretically possible.  A better
example, which is already parallelized and much more highly utilized,
is apply.paramset().

First, to expand on Ilya's answer, let's talk about what *is* possible.

It is possible to wrap a foreach loop over applyStrategy that would
separate symbols to different workers (though your hypothesized 500x
speedup would require *at least* 500 worker nodes, spread out over
several physical machines, using something like doRedis, which we have
tested up to around 200 workers).  This assumes that each symbol is
completely independent, and that there is no interaction on things like
trade sizing or capital or risk among the symbols.  The simplest way to
do this would be to create separate portfolios per symbol, so that each
worker is completely independent.  See examples of a different kind of
splitting and parallelization in appply.paramset() (which is also used
in walk forward testing).

It is also possible, and we commonly do this, to segment the dates that
you want to run applyStrategy over.  As you hypothesized, a simple loop
over date regions, loading different non-conflicting time series, may
be applied to successively run each date range.  This, as you noted,
works well when even 64, 128, or 512GB+ of RAM is not enough for all of
your data.  We've made a number of changes over the years to make
quantstrat more memory efficient, but copies are still made when
unavoidable, state is kept between the various nested apply* functions,
and RAM use basically grows throughout the run of a strategy
evaluation.  So segmenting the use of market data by Dates can help,
though you may need to discard some intermediary results (like portions
of the order book) to make everything fit.  

In the first example of parallelizing by symbol, RAM is your most
likely issue still, since even very large machines rarely have more
than about 16GB per core/thread.

You still have some wrinkles here.  Again, you need to assess whether
there is any interaction.  Transactions cannot be added to a portfolio
out of order, as the P is (potentially) dependent on prior
transactions.  So you may again need to create multiple portfolios and
stitch the different period P together yourself.

So, in the 'don't do that' camp, don't try to apply transactions out of
order, the trade blotter won't allow it.

In the 'should work' camp are several variations of splitting your
computational problem so that it is amendable to looping and/or
parallelization, described above.

Regards,

Brian

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

___
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] R/Finance 2018 Registration

2018-05-18 Thread Brian G. Peterson
Adam,

This probably doesn't need to be on the R-SIG-Finance mailing list.

Just to clear things up...  The Bayesian seminar has *not* been
canceled.  I still see it in the registration system.

For schedule change assistance, please contact the UIC events team at c
baeve...@uic.edu . If they can't help you, you can contact the
committee directly at commit...@rinfinance.com.

For anyone else reading this email and wondering what it is about,
please consider joining us in Chicago in two weeks for R/Finance!

Register soon, prices go up on Monday:

http://www.rinfinance.com/

Regards,

Brian

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

On Fri, 2018-05-18 at 10:59 -0500, Adam Ginensky wrote:
> Hi Josh,
> 
> I have a few details that I need corrected.  I signed up for the
> Bayesian
> pre-conference course and I see that now has been cancelled and I
> have been
> moved to another seminar.  I have no interest in attending any of the
> other
> sessions, so I would like my money refunded.  In fact, I would also
> like to
> change my registration so that I am attending the Friday night
> conference
> dinner.  I don't see how I can do that either.  Please advise me on
> how I
> can accomplish both of these.  Thanks,
> 
> Adam Ginensky
> 
> On Mon, May 14, 2018 at 12:28 PM, Joshua Ulrich <josh.m.ulrich@gmail.
> com>
> wrote:
> 
> > Hi All,
> > 
> > A quick note that R/Finance registration is open, and early-bird
> > pricing ends a week from today, on 5/21.
> > 
> > Announcement below:
> > 
> > This year marks the 10th anniversary of the R/Finance
> > Conference!  As
> > in prior years, we expect more than 250 attendees from around the
> > world. R users from industry, academia, and government will joining
> > 50+ presenters covering all areas of finance with R.  The
> > conference
> > will take place on June 1st and 2nd, at UIC in Chicago.
> > 
> > You can find registration information on the conference website:
> > http://www.rinfinance.com/#registration
> > 
> > or you can go directly to the Cvent registration page:
> > http://go.uic.edu/rfinance
> > 
> > Note that registration fees will increase by 50% at the end of
> > early
> > registration on May 21, 2018.
> > 
> > We are very excited about keynote presentations by JJ Allaire, Li
> > Deng, and Norm Matloff.  The conference agenda (currently) includes
> > 18
> > full presentations and 33 shorter "lightning talks".  As in
> > previous
> > years, several (optional) pre-conference seminars are offered on
> > Friday morning.  We're still working on the agenda, but we have
> > another great lineup of speakers this year!
> > 
> > There is also an (optional) conference dinner at Wyndham Grand
> > Chicago
> > Riverfront in the 39th Floor Penthouse Ballroom and
> > Terrace.  Situated
> > directly on the riverfront, it is a perfect venue to continue
> > conversations while dining and drinking.
> > 
> > We would to thank our 2018 Sponsors for the continued support
> > enabling
> > us to host such an exciting conference:
> > 
> >   UIC Liautaud Master of Science in Finance
> >   Microsoft
> >   R Consortium
> >   RStudio
> >   William Blair
> >   Citadel
> >   Quasardb
> > 
> > On behalf of the committee and sponsors, we look forward to seeing
> > you
> > in Chicago!
> > 
> >   Gib Bassett, Peter Carl, Dirk Eddelbuettel, Brian Peterson, Dale
> > Rosenthal, Jeffrey Ryan, Joshua Ulrich
> > 
> > 
> > 
> > --
> > Joshua Ulrich  |  about.me/joshuaulrich
> > FOSS Trading  |  www.fosstrading.com
> > R/Finance 2018 | www.rinfinance.com
> > 
> > ___
> > 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.

___
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] Using quantstrat with options

2018-03-29 Thread Brian G. Peterson
There should conceptually be no difficulty with lots of symbols and 1-
minute data.  We pretty routinely use quantstrat on tick data or 1-
second data.

We pretty regularly segment our backtests with a loop around the calls
to applyStrategy that load data monthly or quarterly, add symbols to
portfolios or create new portfolios for each segment, and then run that
subset of the overall backtest series.

I do not trade options, so I can't help there, but you should be able
to manage the meta-data for the contracts by adding fields to your
instrument definitions.  If things like tick sizes and multipliers are
set correctly for the root symbol, then the accounting should work.

Regards,

Brian

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

On Thu, 2018-03-29 at 12:17 -0500, Sal Abbasi wrote:
> Has anyone used quantstrat with options?  I’ve found lots of examples
> where people are using it with equities and one where someone is
> using it with futures but have not been able to find any examples of
> people using it with options yet.  I’m trying to backtest some index
> options strategies, and wanted to ask about a pragmatic way of using
> quantstrat when there are so many symbols involved (one per strike /
> maturity).   Also there is a lot of market data involved since I’m
> using 1 minute bars, so I was wondering whether I should create a
> subset of my market data for the minimum required before feeding it
> to quantstrat or whether people have done something where quantstrat
> can look up the data it needs from indexed files on disk as it runs
> and does not need to hold it in memory.
> 
> Best,
> 
> Sal
> ___
> 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.


Re: [R-SIG-Finance] Any news about the R/Finance 2018 conference?

2018-03-26 Thread Brian G. Peterson

On 03/26/2018 06:38 AM, Sillas Gonzaga wrote:

http://www.rinfinance.com/


Does anybody know when the registration will be available? The above webpage 
says it would be open in the spring in March but the month is already ending.


Silas,

We're working on it.

Registration will open 'soon', hopefully this week.

We are looking forward to the tenth year of R/Finance, which attracts 
more than 300 people each year. I hope you can make it.


Regards,

Brian


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

___
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] Error using Performance Analytics package

2018-03-13 Thread Brian G. Peterson
Well, your code is completely mangled by posting in HTML, as Josh told
you would likely happen.

In any case, You haven't provided a reproducible example.

require(PerformanceAnalytics)
data(edhec) # also monthly data
VaR(edhec,
p=0.95, method='modified') 
# the last two parameters are defaults, and
didn't need to be specified

works fine, so I think you need to provide more information.

Your csv data file (as text) could easily be attached to your email, or
posted online somewhere, or added to a github issue on the
PerformanceAnalytics repository, or something.

I don't understand why you are doing as.matrix on your data when you
already have a time series.  You should prefer to use the time series
whenever possible. Just pass 'testdata' to the VaR function

One issue if you were doing component VaR is that you have a rank-
deficient moments matrix. You are windowing only 100-ish months of data
for 216 instruments.  For the univariate calculation in your example,
this shouldn't matter though.

Perhaps you should open a github issue that actually contains all the
information required to examine your problem.

Regards,

Brian

On Tue, 2018-03-13 at 14:30 +, Pankaj K Agarwal via R-SIG-Finance
wrote:
> Dear Joshua SirHere is my data and my code:
> R Version 3.4.2
> 1. Data:File:data.csvMonthly returns of 210 assets from April 2006
> till March 2015
> 2. Code:#Reading filedat1<-
> read.csv("data.csv",header=TRUE,stringsAsFactors=FALSE)#Load
> zoolibrary(zoo)#Creating a zoo objectdat1.z<-read.zoo(dat1, header =
> TRUE, FUN =function(x) as.yearmon(format(x, nsmall = 2),"%b-
> %y"))#Subseting datatestdata<-window(dat1.z,start=as.yearmon("Apr
> 2006"),end=as.yearmon("Mar 2010"))#Computing
> VaRlibrary(PerformanceAnalytics)library(PortfolioAnalytics)VaR(as.mat
> rix(testdata)/100,p=0.95,method="modified")
> Problem:The last command gives the following output:
> Error in dimnames(cd) <- list(as.character(index(x)), colnames(x))
> :   'dimnames' applied to non-array
> Kindly advise how to resolve it. 
> 
> Regards,Pankaj K Agarwal
> +91-98397-11444http://in.linkedin.com/in/pankajkagarwal/ 
> 
> On Monday, 12 March, 2018, 6:30:27 PM IST, Joshua Ulrich  ulr...@gmail.com> wrote:  
>  
>  It would be easier for others to help you if you provided a
> reproducible example.  Here are some tips on how to create one:
> https://stackoverflow.com/q/5963269/271616
> 
> Also, please follow the posting guide and send plain text emails, not
> HTML.  The latter are likely to be mangled by the list server, which
> makes them hard to read (and is another impediment to someone trying
> to help).
> 
> On Mon, Mar 12, 2018 at 7:52 AM, Pankaj K Agarwal via R-SIG-Finance
>  wrote:
> > Dear all,I am using a dataset of monthly asset returns. I subset it
> > with window() call of package zoo. When i try to compute Modified
> > VaR on this subset, i am getting the following error:
> > Error in dimnames(cd) <- list(as.character(index(x)), colnames(x))
> > :  'dimnames' applied to non-array
> > Can someone help? I googled it a lot but still nothing seems to be
> > working.
> > Regards,
> > Pankaj K Agarwal
> > +91-98397-11444http://in.linkedin.com/in/pankajkagarwal/
> > 
> > [[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.


Re: [R-SIG-Finance] Minimizing tracking error with restricted number of stocks

2018-03-08 Thread Brian G. Peterson
Alec,

I do not believe that there is a closed form optimization solution for
what you are trying to do.  In other words, I am agreeing with Coleman
et. al.

That is not the same thing as saying that you can't solve it with
PortfolioAnalytics.

First, add a position_limit constraint setting the number of non-zero
positions that you want in the portfolio

Second, add a tracking error objective as described in Ross'
presentation. 

(along with adding any other objectives or constraints you need in your
portfolio specification)

Third, utilize one of the global numerical solvers, e.g. DEoptim,
random portfolios, genSA, or pso.

The numerical solvers will not search the entire feasible space, but
rather search a subset of the feasible space stochastically.  You
should get acceptably close to the global optimum portfolio using these
methods in finite time.

Regards,

- Brian

On Thu, 2018-03-08 at 16:04 +, Alec Schmidt wrote:
> This is a very handy doc indeed. I'm not sure though that examples on
> #29 - #31 address the problem I'm looking into. Namely, I need
> minimum tracking error with explicit constraint on the number of
> stocks that is lower than in the benchmark portfolio. Coleman, Li,
> and Henniger (2004) state that it's NP-hard problem and review a few
> heuristic methods. Is there one implemented in PortfolioAnalytics?
> Thanks much! Alec
> 
> From: R-SIG-Finance <r-sig-finance-boun...@r-project.org> on behalf
> of Jason Hart <jasonha...@icloud.com>
> Sent: Thursday, March 8, 2018 9:46 AM
> To: Brian G. Peterson
> Cc: R-SIG-Finance
> Subject: Re: [R-SIG-Finance]  Minimizing tracking error with
> restricted number of stocks
> 
> Great presentation, thanks for sharing the link
> 
> Sent from my iPad
> 
> > On Mar 7, 2018, at 10:00 PM, Brian G. Peterson <br...@braverock.com
> > > wrote:
> > 
> > > On 03/07/2018 08:39 PM, Alec Schmidt wrote:
> > > Thank you Brian. I searched PortfolioAnalytics.pdf for 'tracking'
> > > but didn't find one. Are there any implementation examples?
> > 
> > See Ross Bennett's tutorial from R/Finance 2017:
> > 
> > https://rossb34.github.io/PortfolioAnalyticsPresentation2017/#1
> > 
> > Tracking Error example starts on slide 29, though you should find
> > the rest of the tutorial useful.
> > 
> > - Brian
> > 
> > > 
> > > From: R-SIG-Finance <r-sig-finance-boun...@r-project.org> on
> > > behalf of Brian G. Peterson <br...@braverock.com>
> > > Sent: Wednesday, March 7, 2018 9:14 PM
> > > To: r-sig-finance@r-project.org
> > > Subject: Re: [R-SIG-Finance]  Minimizing tracking error with
> > > restricted number of stocks
> > > > On 03/07/2018 07:55 PM, Alec Schmidt wrote:
> > > > Say I have a portfolio of 100 stocks and want to find a subset
> > > > of 20 stocks with minimum tracking error in respect to the
> > > > original portfolio. I wonder if a solver to this problem is
> > > > implemented in some R-based library.
> > > 
> > > PortfolioAnalytics can do this.
> > > 
> > 
> > ___
> > 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.
>

___
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] Minimizing tracking error with restricted number of stocks

2018-03-07 Thread Brian G. Peterson

On 03/07/2018 08:39 PM, Alec Schmidt wrote:

Thank you Brian. I searched PortfolioAnalytics.pdf for 'tracking' but didn't 
find one. Are there any implementation examples?


See Ross Bennett's tutorial from R/Finance 2017:

https://rossb34.github.io/PortfolioAnalyticsPresentation2017/#1

Tracking Error example starts on slide 29, though you should find the 
rest of the tutorial useful.


- Brian



From: R-SIG-Finance <r-sig-finance-boun...@r-project.org> on behalf of Brian G. 
Peterson <br...@braverock.com>
Sent: Wednesday, March 7, 2018 9:14 PM
To: r-sig-finance@r-project.org
Subject: Re: [R-SIG-Finance]  Minimizing tracking error with restricted number 
of stocks

On 03/07/2018 07:55 PM, Alec Schmidt wrote:

Say I have a portfolio of 100 stocks and want to find a subset of 20 stocks 
with minimum tracking error in respect to the original portfolio. I wonder if a 
solver to this problem is implemented in some R-based library.


PortfolioAnalytics can do this.



___
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] Minimizing tracking error with restricted number of stocks

2018-03-07 Thread Brian G. Peterson

On 03/07/2018 07:55 PM, Alec Schmidt wrote:

Say I have a portfolio of 100 stocks and want to find a subset of 20 stocks 
with minimum tracking error in respect to the original portfolio. I wonder if a 
solver to this problem is implemented in some R-based library.


PortfolioAnalytics can do this.

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

___
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] Stop Loss orders in quantstrat

2018-02-10 Thread Brian G. Peterson

On 02/10/2018 12:14 AM, Sanjay Mansabdar via R-SIG-Finance wrote:

I have been trying to model stoplosses in quantstrat. Since intra day lows can 
be spurious whipsaws, I would like to model a stop loss that triggers a market 
order at the open of the next day if the stop was triggered based on the close 
of the previous day. As of now it appears that using stoplimit orders triggers 
the market order based on the high( for short open positions) or the low( for 
long open positions). I’ve tried playing around with the prefer = option but I 
haven’t been able to see any changes based on this setting.
Any help on how I can model these stop orders would be appreciated.
Many thanks to Ilya Kipnis for pointing me to this group. Also many thanks in 
advance.


Sanjay,

There are two basic approaches to what you want to do:

- construct a signal to fire for the case that you are describing

There should be nothing stopping you from firing a signal based on the 
close price and using a regular market order to enter on the Open of the 
next bar.


Your rule (presumably calling ruleSignal) should use prefer='Open'

Stops on bars are inherently a guess.  So we've chosen to have the stop 
entry enter on the least favorable price.  This is documented, and 
shouldn't be surprising behavior.


- use higher frequency data

If you really must use stops for this, then use higher frequency data.


If this still produces what you feel to be surprising behavior, then 
please produce a *minimal* reproducible example and enter an issue on 
the quantstrat github.


Regards,

Brian

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

___
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] PortfolioAnalytics Package Questions on Initial Weights & Group Constraints

2018-01-28 Thread Brian G. Peterson

On 01/26/2018 03:05 PM, Ed Herranz wrote:

Dear R-sig-finance Group,

I have 3 of questions about the PortfolioAnalytics package:

1) I'm using DEOptim optimization.  And I use the following initialization:

   i.portf <- portfolio.spec(assets=allInstruments,
 weight_seq=generatesequence(min = 0.001,
 max = 0.06,
 by = 0.002))
I believe that the initial portfolios are generated randomly using
random_portfolios.   However, if I wanted to specifically pre-set one of
the portfolios to a given set of predefined weights, would that be possible
with DEOPtim?


You are correct that the initial population is generated via 
random_portfolios.


There are two ways to accomplish what you want, the first will work for 
only one 'special' seed portfolio, the second would work for any number 
of special seeds.


In the first method, when you call portfolio.spec, instead of just 
specifying the names of the assets, you also specify the weights using a 
named vector.  This will include your current seed portfolio as the 
second portfolio in the seed matrix sent to DEoptim as an initial 
population.


In the second method, you simply call random_portfolios by hand, 
specifying the number of portfolios to be slightly shorter than the 
number of portfolios that will be in each generation.  then add your 
seeds to this object, and specify your new matrix of weights including 
your seeds as the rp= parameter to optimize.portfolios.  You'll see that 
in many of our vignettes or seminar materials on PortfolioAnalytics we 
generate a random portfolio seed once, and then reuse it across many 
different scenarios.  You would be doing the same thing, only including 
your special seed portfolios in the initial population.



2) I'm using group constraints to specify the sum of weights by group.  For
example on Sectors:

   for( jj in 1:length(uniqueSectors)){
 group_indices <- which(sectors == uniqueSectors[jj])
 groupsum <- sum(benchmark_weights[group_indices])
 groupmax <- 0.1 + groupsum
 groupmin <- -0.1 + groupsum

 groupmins[jj]   <- groupmin
 groupmaxs[jj]   <- groupmax
 grouplist[[jj]] <- group_indices
   }

 i.portf <- add.constraint(portfolio=i.portf,
   type="group",
   groups=grouplis,
   group_min=groupmins,
   group_max=groupmaxs,
   group_labels=groupnames)

   There is nothing stopping me from making two separate add.constraint()
group calls by splitting the initial groups' constraints into 2

 i.portf <- add.constraint(portfolio=i.portf,
   type="group",
   groups=grouplist[1:5],
   group_min=groupmins[1:5],
   group_max=groupmaxs[1:5],
   group_labels=groupnames[1:5])
 i.portf <- add.constraint(portfolio=i.portf,
   type="group",
   groups=grouplist[5:10],
   group_min=groupmins[5:10],
   group_max=groupmaxs[5:10],
   group_labels=groupnames[5:10])

What is the difference when I split the group constraints into two instead
of 1?  Based on my tests these two are not the same; it seems that
splitting the group constraints into 2 is less restrictive(?) than if I
just have one group add.constraint()


I discussed this with Ross Bennett, who added the group constraint code 
to PortfolioAnalytics, and we are of the opinion that the two 
formulations should be the same.  So any variation you are seeing may be 
random.  This could be verified by using the same seed population rp as 
described above.


Ross also noticed that both your groups contain asset 5, which may not 
be what you intended.



3) There is a maximum position constraint--for example

pspec <- add.constraint(portfolio=pspec, type="position_limit", max_pos=3)

Is there a reason that a minimum position constraint was not/cannot be
implemented?


I think a minimum position box constraint will do what you want, but 
otherwise no, we'd need to look at the code and think about it some more 
as to why it was implemented the way that it was.


Regards,

Brian

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

___
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] R packages/resources for Financial Risk Management

2017-10-16 Thread Brian G. Peterson
On Mon, 2017-10-16 at 05:43 -0500, Brian G. Peterson wrote:
> On 10/16/2017 04:35 AM, Pankaj K Agarwal via R-SIG-Finance wrote:
> > Dear allCan someone suggest some good resources/package on
> > Financial Risk Management based on R? I would like to use them for
> > a graduate class i teach. Thanks in advance.
> > Regards,Pankaj K Agarwal
> > +91-98397-11444http://in.linkedin.com/in/pankajkagarwal/
> 
> For a book length survey treatment of packages for these topics, see 
> Bernhard Pfaff's excellent 'Financial Risk Modeling and Portfolio 
> Optimization with R, 2nd Edition'.
> 
> Enrico's suggestion of the task view certainly includes 'very 
> specialized' resources, but also includes a number of more
> fundamental resources, such as PerformanceAnalytics and rugarch.
> 

I should also have mentioned McNeil, Frey, and Embrechts "Quantitative
Risk Management" and the accompanying R package QRM.

If you are a bit more specific about what topics you plan to cover, I
am certain that the list could suggest more resources.

Regards,

Brian

___
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] R packages/resources for Financial Risk Management

2017-10-16 Thread Brian G. Peterson

On 10/16/2017 04:35 AM, Pankaj K Agarwal via R-SIG-Finance wrote:

Dear allCan someone suggest some good resources/package on Financial Risk 
Management based on R? I would like to use them for a graduate class i teach. 
Thanks in advance.
Regards,Pankaj K Agarwal
+91-98397-11444http://in.linkedin.com/in/pankajkagarwal/


For a book length survey treatment of packages for these topics, see 
Bernhard Pfaff's excellent 'Financial Risk Modeling and Portfolio 
Optimization with R, 2nd Edition'.


Enrico's suggestion of the task view certainly includes 'very 
specialized' resources, but also includes a number of more fundamental 
resources, such as PerformanceAnalytics and rugarch.


Regards,

Brian

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

___
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] Accessing "row names" in an object created by quantmod

2017-09-12 Thread Brian G. Peterson
Ilya has already pointed out the index function.

Slightly more information is that xts objects don't have row names,
they have a time/date based index, and facilities to subset using that
index.  For the most part, you can do this as you would with a
data.frame, by exact index or by row number, but there are also many
other things that you can do as well.

I suggest taking a close look at the xts FAQ:

https://joshuaulrich.github.io/xts/xts_faq.html

Regards,

Brian
 

On Tue, 2017-09-12 at 07:42 -0700, Dennis Fisher wrote:
> R 3.4.1 
> OS X
> 
> Colleagues,
> 
> I am just learning about quantmod and I don’t understand the behavior
> of xts objects.
> 
> For example:
>   getSymbols("AAPL”)
> Creates an object AAPL
> 
>   tail(AAPL)
> yields:
>    AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
> AAPL.Adjusted
> 2017-08-
> 31163.64164.52   163.48 164.0026785100164.00
> 2017-09-
> 01164.80164.94   163.63 164.0516591100164.05
> 2017-09-
> 05163.75164.25   160.56 162.0829468500162.08
> 2017-09-
> 06162.71162.99   160.52 161.9121651700161.91
> 2017-09-
> 07162.09162.24   160.36 161.2621928500161.26
> 2017-09-
> 08160.86161.15   158.53 158.6328352600158.63
> 
> I would like to extract a row based on the date column.  The usual
> approaches (e.g., rownames) do not work.  
> 
> So, how do I extract the date column?
> 
> Dennis
> 
> 
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
> 
> ___
> 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.

Re: [R-SIG-Finance] Some problems while reading Diethelm Würtz's Portfolio Optimization with R book

2017-08-27 Thread Brian G. Peterson
Regrettably, Diethelm died in a car accident last year.  He is missed by 
many in this community who appreciated his advice, openness, and 
fostering of the community.


fPortfolio has not been updated for some time.  I would suggest looking 
at Berhard Pfaff's excellent risk and portfolio management book, as well 
as at packages such as PortfolioAnalytics for actually doing portfolio 
optimization in R.


I still refer to Diethelm's book for ideas and visualizations, but I do 
not use that code.


Regards,

Brian

On 08/27/2017 08:35 AM, Baki UNAL via R-SIG-Finance wrote:

Hello
I am reading Diethelm Würtz's Portfolio Optimization with R book. I encountered a problem 
at "17.2 How to Compute a Minimum Risk Efficient Portfolio" part. I entered the 
commands:


minriskSpec <- portfolioSpec()> targetReturn <- 
getTargetReturn(ewPortfolio@portfolio)["mean"]> setTargetReturn(minriskSpec) <- 
targetReturn
minriskPortfolio <- efficientPortfolio(data = lppData,spec = minriskSpec,constraints = 
"LongOnly")> print(minriskPortfolio)




But I got the following output:


Title: MV Efficient Portfolio  Estimator: covEstimator  Solver: 
   solveRquadprog  Optimize:  minRisk  Constraints:   LongOnly
Portfolio Weights:SBI SPI SII LMI MPI ALT   0   0   0   0   0   0
Covariance Risk Budgets:SBI SPI SII LMI MPI ALT
Target Returns and Risks:mean  Cov CVaR  VaR0000
Description: Sun Aug 27 16:00:42 2017 by user: win7120

As you notice R does not compute Portfolio Weights, Target Returns and Risks... 
etc.
I also tried to run the code at "17.3 How to Compute the Global Minimum Variance 
Portfolio" which is:


globminSpec <- portfolioSpec()> globminPortfolio <- minvariancePortfolio(data = lppData,spec 
= globminSpec,constraints = "LongOnly")> print(globminPortfolio)


But I got the following output:

Title:
  MV Minimum Variance Portfolio
  Estimator: covEstimator
  Solver:solveRquadprog
  Optimize:  minRisk
  Constraints:   LongOnly

Portfolio Weights:
SBI SPI SII LMI MPI ALT
   0   0   0   0   0   0

Covariance Risk Budgets:
SBI SPI SII LMI MPI ALT
 


Target Returns and Risks:
mean  Cov CVaR  VaR
0000

Description:
  Sun Aug 27 16:12:21 2017 by user: win7120



As you see R does not make computations again.

I also tried to run the code at "17.4 How to Compute the Tangency Portfolio" 
which is:


tgSpec <- portfolioSpec()> setRiskFreeRate(tgSpec) <- 0



tgPortfolio <- tangencyPortfolio(data = lppData,spec = tgSpec,constraints = 
"LongOnly")


This time I got the following error:


Error in if (STATUS != 0) { : argument is of length zero

What may be the problem? Could you help?
Thanks







[[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.




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

___
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] Return.portfolio issue

2017-05-29 Thread Brian G. Peterson

On 05/29/2017 02:32 AM, ROUX, Nicolas wrote:

Hello all,

I am trying to backtest  a long/short portfolio using Return.Portfolio
but am running into some sort of error, or I do not fully understand how
to use return.portfolio.

For a quarterly rebalancing of shorts, I am providing monthly returns, a
set of negative
weights in an xts format on quarter dates (created by to.period), the
final argument is rebalance_on="quarters".

The result is an set of regular zigzags where the position changes too
regularly. Inline images 2
The code:

<...>

This isn't exactly a reproducible example.

Here is a reproducible example:

require(PerformanceAnalytics)
data(edhec)
data(weights)
sweights <- -1 * weights
pr <- Return.rebalancing(R=edhec, weights=sweights)
charts.PerformanceSummary(pr)

It is clearly a bug.  We'll take a look.


**

I am not sure I understand how the short weights work? Perhaps I should
just use positive weights and minus the returns afterwards?

Any help much appreciated.

Nicolas Roux


___
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] Adding "Stoploss disabled" to the parameter distribution for apply.paramset

2017-05-26 Thread Brian G. Peterson
On Fri, 2017-05-26 at 10:34 +, Atakan Okan wrote:
> Is it possible to add a "disabled" option to the parameter
> distribution (for stoploss, stoptrailing, takeprofit)? So the typical
> parameter distribution contains a vector of integers, but how would I
> go about adding the "disabled stoploss" configuration to the
> parameter distribution? Adding "0" to the parameter distribution did
> not disable stoploss/takeprofit but made them execute immediately
> when there is a price change.

paramsets are designed for *parameters*.

So they aren't really designed for enabling or disabling rules.  That
said, in theory you can change any element of the specification.

Something like this might work (untested, I have a meeting to run to),
please report back:

assume your rule is labeled 'my.stop'


add.distribution( strategy=strategy.st
, paramset.label='stop.onoff'
, component.type='exit'
, component.label='my.stop'
, variable = list(enabled = c(TRUE,FALSE))
, label = 'onoff.test'
)


Please let us know, and if this doesn't work, maybe create a really
minimal example and we'll look more closely.

Regards,

Brian

___
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] [Fwd: Re: Fwd: Re: [Fwd: Performance Analytics internal multivariateMoments calculations]]

2017-05-23 Thread Brian G. Peterson
Dries is our Google Summer of Code student working on this project for
GSoC 2017:

https://github.com/rstats-gsoc/gsoc2017/wiki/Improved-Functionality-for
-Higher-Order-Comoment-Estimation-in-PerformanceAnalytics

A working paper discussing co-moment estimation that goes into more
detail about the sparse forms is here:

https://ssrn.com/abstract=2839781

with supplemental details here:

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2970015

By the end of summer, all the methods in PerformanceAnalytics should be
using the sparse forms, which offer more than an order of magnitude
faster performance, along with some other improvements for numerical
stability of the estimation, and a framework for more estimating
methods to be added over time.

Regards,

Brian 

 Forwarded Message 
From: Joe W. Byers via R-SIG-Finance <r-sig-finance@r-project.org>
Reply-to: "Joe W. Byers" <ecjb...@aol.com>
To: Brian G. Peterson <br...@braverock.com>, Dries Cornilly , r-sig-finance@r-project.org
Subject: Re: [R-SIG-Finance] Fwd: Re: [Fwd: Performance Analytics
internal multivariateMoments calculations]
Date: Tue, 23 May 2017 10:50:37 -0400

Gentlemen,

I so appreciate this feedback.  I do have a followup just so I fully 
understand.  The functions mVaR.MM and mES.MM both require the raw 3rd 
and 4th moments, not the standardized skewness and kurtosis
statistics.  
And, would Dries mind sharing the simplified formulas that he mentions.

Again, thank you both very much.


Joe

On 05/23/2017 05:14 AM, Brian G. Peterson wrote:
> 
> 
> 
>  Forwarded Message 
> Subject: Re: [Fwd: [R-SIG-Finance] Performance Analytics
> internal 
> multivariateMoments calculations]
> Date: Tue, 23 May 2017 02:10:21 +
> From: Dries Cornilly <dries.corni...@kuleuven.be>
> To: Brian G. Peterson <br...@braverock.com>
> 
> 
> 
> Also, Joe Byers did not run in the same issue. He is confusing the 
> kurtosis and the excess kurtosis.
> 
> The function “kurtosis.MM” in “mVaR.MM” does not return the excess 
> kurtosis, but rather m4 / sd^4 and hence the -3 still needs to be 
> done. His example where he claims that GVaR and MVaR should return
> the 
> same is not correct. He uses 0 as fourth order moment in the input, 
> which is only valid for a degenerate distribution. The input for 
> equality should rather be
> GVaR(w, Mean, Stdev, .95)
> MVaR(w, Mean, Stdev, 0, 3 * Stdev^4, .95) # note the difference in 
> fourth moment.
> For a Gaussian the excess kurtosis is zero, or equivalently, the 
> standardised fourth moment (m4 / sd^4) is equal to 3, but the fourth 
> moment of a Gaussian is not equal to zero, it is equal to a function 
> of the variance.
> 
> Additionally, he will generate problems when using the skewness
> input 
> as in his ‘corrected’ version. The function “mVaR.MM” takes the raw 
> third order central moment(s) and standardises in the function
> itself, 
> whereas his altered function will standardise again on the already 
> standardised input and hence give a wrong result.
> 
> Regards
> Dries
> 
> 
> On 22 May 2017 at 20:32:04, Dries Cornilly (dries.cornilly@kuleuven.b
> e 
> <mailto:dries.corni...@kuleuven.be>) wrote:
> 
> > I showed it to Joshua this morning and it seemed like a pure
> > integer
> > problem.
> > 
> > library(PerformanceAnalytics)
> > X <- matrix(1:12, ncol = 3)
> > M3.MM(X) # this one behaves strangely (at this time, X is still
> > filled
> > with integers)
> > M3.MM(X * 1.0)  # multiplying by 1.0 to cast to double provides a
> > zero
> > coskewness matrix at it should
> > 
> > However, I do have some code replicating the modified VaR and
> > modified
> > Expected shortfall using simplified formulas (coming from a working
> > paper of Doug Martin if I recall correctly). I was going to suggest
> > to
> > replace it since the output is identical and it seems more stable
> > to
> > compute. When I have the alternative moment estimators using the
> > unique elements, it might be a good idea to replace the inside of
> > mVaR
> > and mES using the multivariate moments to work with the vector of
> > unique elements (and extract the unique elements if the full
> > matrices
> > are given). This will be a good memory improvement, especially when
> > computing the portfolio moments for mVaR and mES and the
> > derivatives
> > needed for the component VaR and component ES.
> > 
> > 
> > Regards
> > Dries
> > 
> > 
> > On 22 May 2017 at 16:20:22, Brian G. Peterson (br...@braverock.com
> > <mailto:br...@braverock.com>) wrote:
> > 
> > > Loo

Re: [R-SIG-Finance] blotter package funciton addTxns

2017-05-16 Thread Brian G. Peterson
The code and documentation for the addTxns function are in the addTxn.R
file (lines 167-201).

On Tue, 2017-05-16 at 20:03 +, Bos, Roger wrote:
> All,
> 
> I cloned the blotter package on github and took a look at the
> amzn_test.R demo file and I see a call to addTxns, but I don't see
> that function listed in R folder of the package.  I see addTxn, but
> not addTxns.  addTxns is listed in the NAMESPACE folder as an
> exported function.  I can still see the function code by typing the
> function name, but  Could someone please point out something simple
> that I am obviously missing?
> 
> Thanks,
> 
> Roger
> 
> ### Start Example Code ###
> 
> library(blotter)
> 
> # Remove portfolio and account data if run previously
> try(rm("portfolio.amzn_port","account.amzn_acct",pos=.blotter),
> silent = TRUE)
> 
> # load the example data
> data("amzn")
> currency("USD")
> stock("amzn",currency="USD",multiplier=1)
> # Initialize the Portfolio
> initPortf("amzn_port",symbols="amzn",initDate="2010-01-14")
> initAcct("amzn_acct",portfolios="amzn_port",initDate="2010-01-14",
> initEq=1)
> # look at the transactions data
> amzn.trades
> # Add the transactions to the portfolio
> blotter:::addTxns("amzn_port","amzn",TxnData=amzn.trades,verbose=TRUE
> )
> 
> 
> 
> 
> This message and any attachments are for the intended recipient’s use
> only. This message may contain confidential, proprietary or legally
> privileged information. No right to confidential or privileged
> treatment of this message is waived or lost by an error in
> transmission.
> If you have received this message in error, please immediately notify
> the sender by e-mail, delete the message, any attachments and all
> copies from your system and destroy any hard copies. You must not,
> directly or indirectly, use, disclose, distribute, print or copy any
> part of this message or any attachments if you are not the intended
> recipient.
> 
> 
> 
> 
> ___
> 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.

Re: [R-SIG-Finance] Quantstrat - extracting current symbol

2017-05-12 Thread Brian G. Peterson
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 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 <br...@braverock.com>:
> 
> > 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. Pete

Re: [R-SIG-Finance] Quantstrat - extracting current symbol

2017-05-12 Thread Brian G. Peterson

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

___
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] Luxor Demo Question

2017-04-24 Thread Brian G. Peterson

On 04/24/2017 12:38 PM, John Klingensmith wrote:

Hi,
 Does anyone know the purpose behind why the parameters in the Luxor demo
are preceded by dots?

# optimization range
.FastSMA = (1:30)
.SlowSMA = (20:80)


It seems to make them hidden, which also makes them difficult to remove.


They're just named parameters with dots in the name.  They are used in 
the parameter demos.


You can easily see hidden objects with

ls(all=TRUE)

and can remove any named object:

rm(.FastSMA)

with or without a dot.

Regards,

Brian

___
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] quanstrat exit rules

2017-04-18 Thread Brian G. Peterson
Change side to long in your exit rule.

You're not going short, you're selling a long position.  Those are
different things.  Going short in an equity requires a margin account
and a short locate at least, and will incur interest charges on the
borrow. 

In any case, your strategy is trying to enter long, and then exit your
long position.  Both the entry and exit orders are on the same
side='long' of the market.

See the demo script rsi.R in the demo directory for a long/short
version of the same idea.

The other thing that is likely tripping you up is rule order.  Rules
are evaluated in a specific order, as described in the documentation
for add.rule and other places.  exits are evaluated before entries.

Regards,

Brian

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

On Tue, 2017-04-18 at 10:31 -0400, Jon Golenbock wrote:
> Hi, trying to get a handle on quantstrat and was hoping somebody
> could
> explain the what I'm doing wrong.
> 
> The strategy itself is simple enough (and just for testing purposes -
> buy
> when RSI goes below 30, sell back out when RSI goes above 70.
> 
> What is unintended, is that short trades (exit) are processed before
> long
> trades (entry) ever are. Why is this happening?
> 
> Also, I was playing around with order sizing - it seems like one
> should be
> able to specify "all" for exit trades, but that doesn't seem to work
> for me
> when I make the substitution in the below code... the issues may be
> related, though, so I am putting this on the back burner for now.
> 
> Any help appreciated, and thanks in advance.
> 
> Jon
> 
> 
> 
> rm(list=ls(.blotter), envir=.blotter)
> rm(list=ls(.strategy), envir=.strategy)
> .blotter <- new.env()
> .strategy <- new.env()
> 
> 
> 
> 
> initEq <- 5000
> n <- 14
> nSMA <- 200
> days_count <- 100
> initDate <- Sys.Date()-days(days_count + 1)
> .orderqty = 100
> 
> 
> 
> 
> 
> symbols <- getSymbols(c("AAPL","QQQ"))
> 
> 
> currency("USD")
> Sys.setenv(TZ="UTC")
> stock(symbols, currency="USD", multiplier=1)
> 
> strategy.st <- 'teststrat'
> portfolio.st <- 'teststrat'
> account.st <- 'teststrat'
> initPortf(portfolio.st, symbols = symbols,
> initDate=initDate,currency='USD')
> initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
> initEq=initEq, currency='USD')
> initOrders(portfolio=portfolio.st, initDate=initDate)
> 
> 
> 
> strategy(name = strategy.st,
>  store = TRUE)
> 
> 
> 
> 
> 
> 
> 
> add.indicator(
>   strategy = strategy.st,
>   name = 'RSI',
>   arguments = list(price=quote(Cl(mktdata)),n=n),
>   label = "rsi")
> 
> 
> add.signal(
>   strategy = strategy.st,
>   name = 'sigThreshold',
>   arguments=list(
> threshold = 30,
> column = "rsi",
> relationship="lt",
> cross = TRUE),
>   label = "RSI_long"
> )
> 
> add.signal(
>   strategy = strategy.st,
>   name = 'sigThreshold',
>   arguments=list(
> threshold = 70,
> column = "rsi",
> relationship="gt",
> cross = TRUE),
>   label = "RSI_short"
> )
> 
> 
> 
> add.rule(
>   strategy=strategy.st,
>   name='ruleSignal',
>   arguments=list(
> sigcol='RSI_long',
> sigval = TRUE,
> orderside = 'long',
> ordertype='market',
> orderqty=.orderqty,
> replace=FALSE
>   ),
>   type='enter',
>   label='Enterlong',
>   path.dep = TRUE
> )
> 
> add.rule(
>   strategy=strategy.st,
>   name='ruleSignal',
>   arguments=list(
> sigcol='RSI_short',
> sigval = TRUE,
> orderside = 'short',
> ordertype='market',
> orderqty=75,
> replace=TRUE
>   ),
>   type="exit",
>   label='Exitlong',
>   path.dep = TRUE
> 
> )
> 
> 
> t1<-Sys.time()
> out<-applyStrategy(strategy=strategy.st,portfolios = portfolio.st)
> t2<-Sys.time()
> print(t2-t1)
> get.orderbook(portfolio.st)
> 
>   [[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.

Re: [R-SIG-Finance] A quick custom data question

2017-04-08 Thread Brian G. Peterson

On 04/08/2017 03:58 AM, Michael Chen wrote:

Hi,


I didn't get a satisfactory answer after searching the archives.  I just
like to know how do we access market data other than OHLC? for example:



head(J.DCE)Open High  LowClose Volume hmmState
2010-01-04 2886.193 2886.193 2811.193 2833.193  01
2010-01-05 2886.193 2886.193 2811.193 2833.193  01
2010-01-06 2886.193 2886.193 2811.193 2833.193  01
2010-01-07 2886.193 2886.193 2811.193 2833.193  01
2010-01-08 2886.193 2886.193 2811.193 2833.193  01
2010-01-11 2886.193 2886.193 2811.193 2833.193  01



In my quantstrat set up,  I simply set up this as an indicator:


add.indicator(strategy.st, name = "getHMM",
  arguments = list(x = quote(getPrice( mktdata, prefer =
"hmmState"))),
  label = "HMMState")


getHMM is a simple function I wrote to pass on the data as an indicator,
anyhow the error occurs at the getting data part:



test <- applyIndicators(strategy.st, OHLC(J.DCE))

Show Traceback
Rerun with Debug
Error in getPrice(mktdata, prefer = "hmmState") : subscript out of
bounds, no price was discernible from the data


The data was converted to an xts for quantstrat.  I am probably making a
simple error somewhere, would someone please point it out to me,
thanks.   Also, is there a better way to or a more correct way to access
custom data easily for quantstrat indicators/signals??


The list doesn't terribly like HTML posts.

Anyway, it seems likely that your problem is here:

OHLC(J.DCE)

when you call applyIndicators.

You're only passing in the OHLC columns.

just test applyIndicators with

applyIndicators(strategy.st, mktdata=J.DCE)

and getPrice should be able to find your column in your getHMM function.

Even simpler, since the data is already there, and being passed into to 
the apply* functions with the original data is to simply refer to the 
column in whatever other indicator needs it, or in your signal 
functions.  I don't see a need for a custom indicator function at all.


In any case, from your non-reproducible example, and just reading the 
code, the main problem appears to be your call to OHLC(), which will 
strip your other columns.  Skip that, and things should be fine.


Regards,

Brian


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

___
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] Rblpapi package data limits?

2017-04-05 Thread Brian G. Peterson
On Wed, 2017-04-05 at 08:54 -0400, Jon Golenbock wrote:
> Hi, I was hoping somebody might be a regular user of this package to
> interact with Bloomberg terminal. I've been having an issue pulling
> historical data, it seems to cut off at a seemingly arbitrary point
> --
> 
> 
> library(rRblpapi)
> library(dplyr)
> library(purrr)
> library(lubridate)
> library(magrittr)
> 
> striplist <- c("NGK7","NGM7", "NGN7","NGQ7", "NGU7","NGV7","NGX7")
> striplist <- paste(striplist, "Comdty")
> 
> 
> df <- striplist %>%
>   map(~ getBars(., barInterval = 60 *24,startTime =
> floor_date(Sys.time(),"day") - days(300))) %>%
>   map(~select(., times, close))
> names(df) <- striplist


Many people do not use the 'tidyverse', especially in finance where
data is often very large and ultimately needs to be a 'wide' time
series and one tends to do more matrix math than factor munging.

Please create a *minimal* reproducible example using *just* the Rblpapi
package. It will make it easier for others to help you.


> this should be producing close data for 300 days, yet you will see
> that the
> data only goes back 140.
> 
> Anybody run into this before, should I have expected this?
> 
> Thanks,
> Jon
>

___
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] random portfolios

2017-03-21 Thread Brian G. Peterson
The process you describe is pretty standard for an investment-committee 
driven process.


I'm going to suggest that you don't really want to change the 
constraints that often.  For example, box constraints should be as large 
as your overall investment mandate allows to give you the greatest 
possible room for allocations.  Sector or Factor constraints likewise 
should be as minimal as possible just to guarantee the degree of 
diversification described in your investment mandate.


The reason I'm suggesting this minimal constraint set is one of the 
reasons we wrote the random portfolio code in the first place.  To see 
what I mean, generate a set of unconstrained random portfolios (or e.g. 
only with a full-investment constraint).  Then generate sets of 
constrained random portfolios, adding your various constraint sets. 
Plot the different sets on the same risk/return scatter plot, using 
different colors for each set.  Note how small the feasible space 
becomes, very quickly.


This shrinkage of the feasible space has some good shrinkage 
properties...  moderate shrinkage actually decreases the possible impact 
of estimation error in the various inputs, a little.  Large amounts of 
shrinkage (overly restrictive constraints) will do the opposite, and 
magnify the negative out of sample impact of estimation error.


The academic literature mostly focuses on analytical solvers (e.g. 
quadratic, linear, etc) and simple constraint sets.  We've cited papers 
by Patrick Burns as well as papers on the simplex models in 
PortfolioAnalytics, but the literature is not vast.


Numerical solvers become important as the feasible space becomes 
non-smooth.  One of the things that can create a non-smooth feasible 
space is a complex, overlapping constraint set.


The rportfolios package proposed by Frederick Novomestky also seems to 
be an R-only implementation, at a glance relying on truncated random 
binomial vectors rather than truncated random uniform vectors.  I 
believe it will have similar performance characteristics to the 
Burns-style random sample portfolios, and it seems to support fewer 
constraint sets (no overlapping sector, group, or factor constraints 
that I see).  In any case, it generates matrices of weights that are 
likely compatible with the PortfolioAnalytics random or seed portfolio 
inputs.  So if it works for you, that's great.


You also discuss using rejection after generating the portfolios.  This 
is the method used internally by random.portfolios to reject individual 
weights if a constraint is violated.  I'll have to evaluate whether the 
truncdist package used by rportfolios could be more efficient than the 
runif that is used by the current code.  PortfolioAnalytics also allows 
portfolios to be penalized in the solver, so that more complex cases can 
be considered, or interactions between constraints and objectives.


To answer the question of whether Rcpp will help is somewhat complex. 
I'm confident that some of the nested loops in the generation code will 
be sped up by Rcpp.  It is possible that more efficient algorithms are 
available for constructing the weight vectors.  A reason that this 
hasn't been a huge priority though is that construction of the random 
portfolio matrix is usually not the time limiter in a large 
optimization: your objective function is.  I think it will be possible 
to improve the efficiency of this step, though it is unclear how much of 
an impact this should have in practice to a large and complicated 
numerically solved portfolio optimization problem.


Regards,

Brian



On 03/20/2017 07:06 PM, Kevin Dhingra wrote:

Brian,

Yes I think that will be a good starting point. My universe would not
change a lot (I will be working with 10-15 benchmarks at a time and I guess
I can generate a reusable set for each independently before running it
through my main algorithm). Having said that, I envision the investment
mandates/constraints changing quite a lot (both in the cross section and
also over time for the same manager). I am hoping there must be a way
around it using rejection sampling but have not done enough research to
comment on how that solution works for such big dimensions. It will be
really helpful if you could point me to any specific resources from
academia for the same (Haven't been able to find much about random
portfolios myself except Portfolio Analytics and Patrick Burns work on
Portfolio Probe). As a side note - Do you think translating it using Rcpp
would be time well spent or you think there must be a smarter way to get
around it still using R?

I really appreciate your help on this thread.

Regards,
Kshitij Dhingra

On Mon, Mar 20, 2017 at 7:21 PM, Brian G. Peterson <br...@braverock.com>
wrote:


For this type of problem, I would probably generate one set of random
portfolios and just reuse that set of feasible portfolios...  My usual rule
is n-assets + 1-2k feasible portfolios.  You can get a better number e.g.
from sampling 

Re: [R-SIG-Finance] random portfolios

2017-03-20 Thread Brian G. Peterson
For this type of problem, I would probably generate one set of random 
portfolios and just reuse that set of feasible portfolios...  My usual 
rule is n-assets + 1-2k feasible portfolios.  You can get a better 
number e.g. from sampling theory, but this should be enough.


Once you have this weights matrix rp, you only need to regenerate rp if 
your universe changes.


Still interested in a more efficient implementation, of course, or we 
can work with you to see if we can find resources to work on it, e.g. 
from academia.


Regards,

Brian

On 03/20/2017 05:28 PM, Kevin Dhingra wrote:

Hi Ross,

Sure. Even though I have not profiled the bottlenecks quite in detail as of
yet, i will give you a decent idea of the problem I am working with. I can
have multiple indices with as much as 2000 assets with group, position and
turnover limits (Not sure if i can increase the speed by removing
constraints and doing rejection sampling later). In order to generate a
daily possible set for the market in this case, I was playing around with
~4-5 thousand permutations. Also I think I will end up using the "sample"
method because of the type of constraints we have and as you already have
mentioned that method is the slowest (takes about 30 times the time using
"simplex" for the same constraints). Adding box and position limit
constraints are causing it to run a bit slower (but its not a big
difference). I can always provide a more thorough analysis of the potential
bottlenecks with a lot more detail when I have a chance to start working on
translating it to cpp

Thank you,

On Mon, Mar 20, 2017 at 4:04 PM, Ross Bennett <rossbennet...@gmail.com>
wrote:


Kevin,

Can you give us a sense of the number of assets in the portfolio and
the constraints? That will help us understand where the potential
bottlenecks are in the random portfolio generation. For example,
generating a set of random portfolios for box and weight constraints
if relatively fast, but adding group or position limit constraints
makes the algorithm more complicated and slower.

Thanks,
Ross


On Mon, Mar 20, 2017 at 2:35 PM, Kevin Dhingra
<kevin.dhin...@appliedacademics.com> wrote:

Brian,

Thank you for a quick reply. I will soon be working on that problem and
from what I have played with so far, it is unlikely that for our example
~2k portfolios will be enough (really hoping it would) to get a good

sense

of the feasible space and seems like I need to implement an Rcpp version

of

the random portfolios function. I will be happy to collaborate and share

my

code once i get a decent handle on it locally for the purposes of our
current project.

Regards,
Kshitij Dhingra



On Mon, Mar 20, 2017 at 3:17 PM, Brian G. Peterson <br...@braverock.com>
wrote:


On Mon, 2017-03-20 at 15:09 -0400, Kevin Dhingra wrote:

I have been using the random_portfolios function from the
`PortfolioAnalytics` package to simulate the range of possibilities
for return paths at each step under various portfolio constraints /
mandates for evaluating mutual fund managers. As more managers are
added to the universe, however, and more simulations are needed, the
pure R implementations get pretty heavy and hard to scale. I was
wondering if there has been any work out there thus far on
implementing any of the three random portfolio generation methods
(sample, simplex, and grid search) at a lower level, using something
like `Rcpp` to enhance the efficiency of these algorithms?


We've discussed it, but I can't say that it is terribly high on our
list of priorities.

In most cases, no more than 1-2k portfolios should be required to get a
fair view of the feasible space given your constraints and objectives.

We'd be happy to work with you if you want to craft a patch to use C or
Rcpp for this.

Regards,

Brian





--
Kshitij Dhingra
Applied Academics LLC
Office: +1.917.262.0516
Mobile: +1.206.696.5945
Email: kshitij.dhin...@appliedacademics.com
Website: http://www.AppliedAcademics.com

[[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.








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

___
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] random portfolios

2017-03-20 Thread Brian G. Peterson
On Mon, 2017-03-20 at 15:09 -0400, Kevin Dhingra wrote:
> I have been using the random_portfolios function from the
> `PortfolioAnalytics` package to simulate the range of possibilities
> for return paths at each step under various portfolio constraints /
> mandates for evaluating mutual fund managers. As more managers are
> added to the universe, however, and more simulations are needed, the
> pure R implementations get pretty heavy and hard to scale. I was
> wondering if there has been any work out there thus far on
> implementing any of the three random portfolio generation methods
> (sample, simplex, and grid search) at a lower level, using something
> like `Rcpp` to enhance the efficiency of these algorithms?

We've discussed it, but I can't say that it is terribly high on our
list of priorities.  

In most cases, no more than 1-2k portfolios should be required to get a
fair view of the feasible space given your constraints and objectives.

We'd be happy to work with you if you want to craft a patch to use C or
Rcpp for this.

Regards,

Brian

___
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] apply.paramset stopping on condition

2017-03-14 Thread Brian G. Peterson
How are you planning to calculate this statistic to 'stop trading'? 

If you're using net profit, drawdown, etc., these are typically
calculated after the backtest is over, e.g. using updatePortf and
tradeStats.

Obviously this is too late to bail out early.

You could conceivably check some 'partial' profit or loss in a rule
(maybe a rebalance rule) from the trades that have happened, zero out
all your signals, adjust the next dindex to the last observation to
skip to the end.

I'd argue that your question suggests that you're not doing analysis of
 your signals before running the expensive walk-forward or
apply.paramset processes.  Wouldn't it make more sense to apply your
proposed parameters over your signal process, where the code should be
vectorized, fast, and cheap, than to wait till you're running the
comparatively slower path dependent rules?

quantstrat has many functions for doing signal analysis, including
signal analysis over a parameter set.  See the signal analysis demos.

Also, I suspect you may be doing something else computationally
expensive in your backtest. We can typically get to about one core-
minute per symbol per day on tick data.  So we've certainly seen tests
that take hours or even days to run here, but those are over massive
amounts of data.  Have you profiled a single run of the backtest to see
where it is spending its time? 

Regards,

Brian


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

On Tue, 2017-03-14 at 17:52 +0100, Diego Peroni wrote:
> Hi Frank,
> 
> I don't have code to share: every simulation is fine...
> 
> In other words my problem is:
> 
> 1) In which function of quantstrat can I insert some custom code to
> stop 
> simulation execution at a specific moment based on my personal raw 
> criteria (ex. Profit or Drawdown)?
> 
> 2) The custom code must be placed in a place where I can collect
> last 
> trade result step by step to calculate approximately "partial" Profit
> or 
> Drawdown
> 
> I'm looking at "ruleOrderProc.R" function but I'm not sure it is the 
> best place...
> 
> Thanks in advance
> 
> Diego
> 
> 
> 
> On 14/03/2017 17:35, Frank wrote:
> > Would you attach an example to your post? Something that we can run
> > using
> > copy and paste to R console, with some data?
> > 
> > Thanks,
> > 
> > Frank
> > Chicago
> > 
> > -Original Message-
> > From: R-SIG-Finance [mailto:r-sig-finance-boun...@r-project.org] On
> > Behalf
> > Of Diego Peroni
> > Sent: Tuesday, March 14, 2017 2:51 AM
> > To: r-sig-finance@r-project.org
> > Subject: [R-SIG-Finance] apply.paramset stopping on condition
> > 
> > Hello everybody,
> > 
> > I'm using quantstrat apply.paramset to optimize some parameters but
> > my
> > simulations runs for a long time...
> > 
> > I'm trying to stop bad simulations BEFORE they finish basing on
> > some
> > indicators (ex. Net.Profit or drawdown).
> > 
> > Is there a way to control this beaviour including some custom code
> > in
> > quantstrat?
> > 
> > Thanks
> > 
> > Diego
> > 
> > ___
> > 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.

___
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] Parallelizing applyStrategy to multiple symbols

2017-03-06 Thread Brian G. Peterson
I suspect you're running up against communication and memory management
time and resource contention.  

applyIndicators and applySignals should all be using vectorized code,
so the potential benefit from parallelization will likely be negative,
as communication and memory management swap any benefit from the
calculations.

applyRules might benefit from parllelization, but you would need to
come back together on any rebalancing period.  You would also have
significant copying time.

If you were going to make this work, you'd need to minimize copies. 
Your effective 'reduce' operation at the end by only returning
tradeStats could do this for the end of the calculation, but at the
start, you'd need to be smarter about how you segment market data to
each worker. 

Just putting getSymbols on the workers might run into I/O contention
issues.  You also don't need to redeclare the strategy object.  You
could just copy that to each worker.

When we've done things as a one-off, we typically create portfolios for
each segment, and try to avoid as many copies as we can.
 
You'd need to profile to see exactly where you're getting hung up, but
this approach seems too simplistic (see my first sentence for hints).

We haven't bothered to do this in the package itself since with a
little work we can usually get to around one core minute per symbol per
day on L1 tick data, which means that even a large backtest on tick
data can finish in a few hours.  The cost of optimizing execution time
doesn't seem to be worth the cost in programming and testing time.

Regards,

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

On Mon, 2017-03-06 at 18:53 +, Atakan Okan wrote:
> Hello,
> 
> I am trying to parallelize applyStrategy() to make it faster when
> applied to multiple symbols. The reproducible code below only
> contains 3 symbols thus it finishes fast however when I apply it to
> 100 symbols in an index, sequential computing takes a lot of time.
> What is the best way to accomplish this? Using foreach loop does not
> seem to work and couldn't find any info on stackexchange or the usual
> mailing lists. 
> 
> Thanks.
> 
> Atakan Okan
> 
> Code with applyStrategy (foreach is below this):
> 
> library(quantmod)
> library(quantstrat)
> 
> symbols <- c("AAPL","GOOGL","MSFT")
> 
> getSymbols(Symbols = symbols, from = "2010-01-01")
> 
> currency('USD')
> stock(symbols, currency="USD")
> 
> strategy.st <- "multiple_symbols_parallel_applystrategy"
> rm.strat(strategy.st)
>   
>   
> initPortf(strategy.st, symbols = symbols)
> initAcct(strategy.st, portfolios=strategy.st, initEq=10)
> initOrders(portfolio=strategy.st)  
> strategy(strategy.st,store=TRUE)
>    
> rule.longenter  = TRUE  
> rule.longexit   = TRUE  
> rule.shortenter = TRUE 
> rule.shortexit  = TRUE 
> 
> txn.model <- 0
> 
> add.indicator(strategy.st,  
>   name = "MACD", 
>   arguments = list(x=Cl(get(symbols))), 
>   label='macd') 
> 
> add.signal(strategy.st,name="sigCrossover",
>    arguments = list(columns=c("macd.macd","signal.macd"),
> relationship="gt"),
>    label="macd.gt.signal") 
> 
> add.signal(strategy.st,name="sigCrossover",
>    arguments = list(columns=c("macd.macd","signal.macd"),
> relationship="lt"),
>    label="macd.lt.signal")
> 
> add.rule(strategy.st,
>  name='ruleSignal',
>  arguments = list(sigcol="macd.gt.signal",
>   sigval=TRUE,
>   prefer="Open", 
>   orderqty= 1000, 
>   #osFUN="osAllInLong",  
>   ordertype='market',
>   orderside='long',
>   orderset='ocolong',
>   TxnFees = txn.model),
>  type='enter',
>  label='longenter',
>  enabled=FALSE
> )
> 
> add.rule(strategy.st,
>  name='ruleSignal',
>  arguments = list(sigcol="macd.lt.signal",
>   sigval=TRUE,
>   prefer="Open", 
>   orderqty='all',
>   ordertype='market',
>   orderside='long',
>   orderset='ocolong',
>   TxnFees

Re: [R-SIG-Finance] Custom Indicator and apply.paramset problem

2017-02-25 Thread Brian G. Peterson
led=TRUE
)


macdFastMARange <- seq(2,12,by=5)
macdSlowMARange <- seq(12,24,by=6)
macdSignalRange <- seq(5,15,by=5)


paramset.label.name <- "macd_opt"


add.distribution(strategy.st,
 paramset.label = paramset.label.name,
 component.type = 'indicator',
 component.label = "macd",
 variable = list( nFast = macdFastMARange ),
 label = "macdFastMARANGE")


add.distribution(strategy.st,
 paramset.label = paramset.label.name,
 component.type = 'indicator',
 component.label = "macd",
 variable = list( nSlow = macdSlowMARange ),
 label = "macdSlowMARANGE")


add.distribution(strategy.st,
 paramset.label = paramset.label.name,
 component.type = 'indicator',
 component.label = "macd",
 variable = list( nSig = macdSignalRange ),
 label = "macdSignalRANGE")


add.distribution.constraint(strategy.st,
paramset.label = paramset.label.name,
distribution.label.1 = 'macdFastMARANGE',
distribution.label.2 = 'macdSlowMARANGE',
operator = '<',
label = 'FastMA<SlowMA')


#Single Core - Works
#applyStrategy(strategy=strategy.st,portfolios=strategy.st, verbose=TRUE)
#updatePortf(strategy.st)
#updateAcct(strategy.st)
#updateEndEq(strategy.st)




#DoSNOW Parallel on Windows - Does Not Work
library(doSNOW)
library(parallel)
paramsetenv <- new.env()
cl <- snow::makeCluster(detectCores(), type = "SOCK")
registerDoSNOW(cl)
results <- apply.paramset(strategy.st,
  paramset.label=paramset.label.name,
  portfolio=strategy.st,
  account=strategy.st,
      nsamples=0,
  verbose = TRUE,
  audit=paramsetenv,
  calc = "slave")
snow::stopCluster(cl)

___
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.




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

___
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] Custom Indicator Problem

2017-02-25 Thread Brian G. Peterson

You're overcomplicating your indicator function.

MACD_W1 <- function(mktdata=quote(mktdata),
nFast = 12,
nSlow = 26,
nSig = 9)
{
  y <- MACD(Cl(to.weekly(mktdata)),
nFast = nFast,
nSlow = nSlow,
nSig = nSig,
maType = "EMA")
  y <- cbind(xts(order.by=index(mktdata)),y)
  y <- na.locf(y)
  y
}


On 02/25/2017 03:33 AM, Atakan Okan wrote:

Just remembered that Outlook sent emails in HTML format, so here is the problem 
in plain text format in case you couldn't see it before. Still couldn't find a 
way to make this work.


From: Atakan Okan <atakano...@outlook.com>
Sent: Wednesday, February 15, 2017 11:54 AM
To: r-sig-finance@r-project.org
Subject: Custom Indicator Problem


Hi everyone,

I am facing a problem regarding a higher-time-frame custom indicator. I have 
daily yahoo finance data but would like to use the technical indicator MACD on 
weekly close data. I have written a custom function as I have seen in a 
previous post some years ago  but couldn't manage to add the weekly indicator 
output to mktdata. Any help is appreciated, thank you

Reproducible example:



#Custom Indicator Problem


library(quantmod)
library(quantstrat)
library(TTR)


Sys.setenv(TZ = "UTC")


.strategy <- new.env()
.blotter  <- new.env()


getSymbols("GARAN.IS")




#Stock
symbol.name = "GARAN.IS"
tick.size = 0.01
currency('TRY')
stock(symbol.name, currency="TRY", multiplier=1,tick_size= tick.size)


initialEquity = 1000
port.acct.currency <- "TRY"


strategy.st <- 'Custom_Prob'

rm.strat(strategy.st)


initDate = as.character(as.Date(index(GARAN.IS[1])-1))
initPortf(strategy.st, symbol.name, initDate=initDate, currency = 
port.acct.currency)
initAcct(strategy.st, portfolios=strategy.st, initDate=initDate,
 initEq=initialEquity, currency = port.acct.currency)
initOrders(portfolio=strategy.st,initDate=initDate)
strategy(strategy.st,store=TRUE)
summary(getStrategy(strategy.st))


#MACD W1 indicator
MACD_W1 <- function(mktdata=quote(mktdata),
nFast = 12,
nSlow = 26,
nSig = 9){
  y <- GARAN.IS
  is.xts(y)
  y <- to.weekly(y)
  y <- Cl(y)
  y <- MACD(y,
nFast = nFast,
nSlow = nSlow,
nSig = nSig,
maType = "EMA")
  y <- cbind(mktdata, y[paste(first(index(mktdata)),
  last(index(mktdata)),
  sep = "/")])
  y <- y[-which(is.na(y[,1])),]
  y <- na.locf(y)
  y <- y[,c((ncol(y)-1),ncol(y))]
  y
}


add.indicator(strategy.st,
  name = "MACD",
  arguments = list(x=Cl(GARAN.IS)),
  label='macd')


add.indicator(strategy.st,
  name = "MACD_W1",
  arguments = list(mktdata=quote(mktdata)))




apply.indicators.df <- applyIndicators(strategy.st, mktdata=GARAN.IS)
#testing indicator calculations




-Atakan Okan




___
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.




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

___
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] Portfolio management in R for private use

2017-01-12 Thread Brian G. Peterson

On 01/12/2017 02:16 AM, Johannes Lips wrote:

Hi all,

I've just wanted to share some code I've written to keep track of my
small portfolio in R, since I couldn't find anything suitable on the
internet.
https://github.com/hannes101/myRportfolio/
I would be glad if someone could offer me some nice documents on good
ways to keep track on the performance of the whole portfolio and
possible summaries one could easily obtain. I couldn't really find a lot
of documentation on all these packages and most of it was rather
academic and not really suited for a portfolio, mainly following a
Buy strategy.
If you have any suggestions or additional comments, please let me know.


Johannes,

Thanks for sharing your code.  It is always useful to see how other 
people are using the packages.


I'm a little unclear on what your question is.

I'm also not clear what you are referring to when you discuss 'not 
really suited for a portfolio', since among other things we use blotter 
for reconciliation of accounts doing many thousands of trades per day, 
which is not quite 'buy and hold' nor 'academic'.  Perhaps you could 
provide links to what you've referenced so far?


Anyway, I'll guess at what you might be asking, and see if that 
clarifies things.


You're using addTxns() to load your trades into blotter, which is as 
expected for trade accounting on actual trades.


After you've called updatePortf() and updateAcct(), you have all the 
'tracking' and 'performance' data for your portfolio.


I don't see calls to tradeStats(), perTradeStats(), dailyStats(), or 
dailyEqPL(), which might give you more of the 'tracking' information 
you're looking for.


I also don't see a call to PortfReturns(), which would extract %-returns 
from your blotter portfolio, and make all the standard portfolio 
analysis of packages such as PerformanceAnalytics or PortfolioAnalytics, 
which generally expect %-returns, available.


So, maybe this answers your question, and maybe it doesn't.  If I 
haven't answered your question, could you please be more specific about 
what you want to do?


Regards,

Brian

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

___
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] Clarification on trailing stop.

2016-12-28 Thread Brian G. Peterson

On 12/27/2016 10:09 PM, Michael Chen wrote:

Thanks Brian, as always.Just to make sure, for all order types,
"prefer=",  sets the column data for calculation and therefore for
excution.   So, stoplimit/trailing stop orders are excute at that
limit ('prefer=high' ± threshold), but market orders are executed at
the next bar (if my indicator uses Cl for normal entry), then useing
"prefer=open" will get the transaction price at open of the next
bar.


It would be prefer="open", but yes.

Also be aware that this is a bit sloppy.  Real markets don't trade in bars.

Most investors don't really have access to the opening or closing 
auctions if building backtests on daily data.  As noted in the 
documentation and I have noted elsewhere, intraday historical data is 
readily available, so in most cases you would likely be better served 
using intraday data and something like a VWAP price if you have the data 
available.


in any case, the prefer= argument is a pass-through to getPrice. 
quantstrat will make reasonable guesses based on your data if it is not 
set.  see


?getPrice

for more information on how getPrice works to extract price data.

Regards,

Brian

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

___
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] Clarification on trailing stop.

2016-12-27 Thread Brian G. Peterson
On Tue, 2016-12-27 at 08:07 +, Michael Chen wrote:
> I am little confused regarding the "prefer" setting in trailing stop
> orders (may also apply to normal orders).  Setting prefer =
> "High",  as presented in the Luxor example, means to me that the
> trailing stop is set at the preset threshold level less than the most
> recent high.  Default prefer is set for 'Close'.   Prefer= sets the
> price column to use from data object (mktdata),  but I also read from
> well-informed bloggers that setting prefer = 'Open' means that after
> the condition is satisfied, the execution of the order will be at the
> Open of next bar instead of the Close.  So, is "Prefer=" setting
> defines the data column to be used in the apply rule calculations or
> is it used for preference for the point of execution for the
> order?  or both?
> 
> thanks again and I apologize if this has been answered before.  my
> search didn't reveal anything.


The prefer= argument is passed to the getPrice() fn, and will determine
the price column that is used to set your threshold.

stoplimit orders will execute at the limit, so it should be 'High' +/-
threshold (depending on whether you are buying or selling)

Regards,

Brian

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

___
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] Quantstrat - applystrategy on subset of mktdata

2016-12-15 Thread Brian G. Peterson
On Thu, 2016-12-15 at 06:31 +, Mayank Singhal via R-SIG-Finance
wrote:
> I am interested in backtesting the strategy on subset of mktdata  but
> somehow the below approach is resulting into errors.
> # yy="2016"out<-applyStrategy(strat.st ,
> portfolios=portfolio.st, mktdata = mktdata[yy])
> ##
> I understand i can do it in 2 steps where in first step i calculate
> indicators/signals on the full time series and in second step only
> supply the subset of mktdata to portfolio for backtest.  But i was
> wondering if there is a way to do this in single step itself ? I
> think the problem is that the full mktdata is added to portfolio.st
> and not the subset of it ?
> Any pointers will be helpful,
> Regards,Mayank

You don't give enough information here for anyone to help you.

Please follow the Posting Guide:

https://www.r-project.org/posting-guide.html

and create a minimal reproducible example.

Also, per the posting guide, don't post HTML, as it makes your
formatting impossible to cut and paste from, even if there were a full
example here.

Brian

___
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] Quantstrat to backtest portfolio strategy. User Defined Weights

2016-12-14 Thread Brian G. Peterson
On Wed, 2016-12-14 at 14:28 +, Vineet Gupta wrote:
> I am trying to use Quantstrat to backtest a portfolio strategy with
> user defined weights at each re-balance date. The user defined
> weights, are given in a matrix (example shown below)
> 
>  Date ABC.UN   ABT.UN ACN.UN ADBE.UW
> 
> 20 2015-05-15 0.5000 0.000.5   0
> 21 2015-05-22 0.5000 0.000.5   0
> 22 2015-05-29 0.5000 0.000.5   0
> 23 2015-06-05 0.5000 0.000.5   0
> 24 2015-06-12 0.08865296 0.4113470.5   0
> 25 2015-06-19 0. 0.500.5   0
> 
> Simply put, how can I backtest such a strategy, using Quantstrat? I
> have looked at the rule, 'rulePctEquity', but this appears to only
> govern maximum trade sizes. 
> 
> What information does Quantstrat need to re-balance my portfolio at
> each user-defined re-balance date?
> 
> Thanks in advance. Very much appreciated

You haven't really given us enough information to answer your question,
so I'm just guessing based on the information you did provide.

If you are simply 'trading' a portfolio strategy where the portfolio is
rebalanced form one set of weights to another on a specified
rebalancing period, you probably don't want quantstrat at all.

In that case, you likely want the function Return.portfolio in
PerformanceAnalytics or you want to use PortfolioAnalytics to construct
your target portfolio.

Working in returns and weights is different than working in trades and
cash.

rulePctEquity simply governs how much of the total equity is available
for each instrument in the strategy.  It doesn't reduce the size of an
existing position on the rebalance period, if there is one.  It could
certainly be extended to enter orders or transactions to rebalance, if
you wanted it to.

You'll have to provide a minimal reproducible example and more
information about what you'd like to do for someone to be able to help
you further.

Regards,

Brian

___
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] Basket stop loss implementation quantstrat

2016-12-13 Thread Brian G. Peterson
Yes, it comes at a cost, as I said in my message, and as the
documentation for applyStrategy.rebalancing describes.

In the demo folder, see any of the 'rebalancing' demos for sample code.

Regards,

Brian

On Tue, 2016-12-13 at 19:37 +, Aaron wrote:
> Hi Brian,
> 
> 
> Thank you for the reply.
> 
> 
> I'm going to go out on a limb and assume that rebalancing in this way
> will come at the cost of considerable overhead(?). I plan on testing
> 28 symbols with a minimum data periodicity of 15 minutes (over a
> period of ~10 years). How significant would the extra overhead be in
> a situation like this?
> 
> 
> I have not yet got into rebalancing rules in quantstrat. Is there an
> example I could look up which could point me in the right direction?
> 
> 
> Also, as my objective appears to be outside the scope of normal
> quantstrat applications, are you able to provide some sketch code as
> to how such an operation would be managed? I'm more than happy to
> have a dig at this myself but somewhere to start would be nice.
> 
> 
> Cheers,
> 
> Aaron
> 
> 
> 
> From: Brian G. Peterson <br...@braverock.com>
> Sent: Tuesday, 13 December 2016 5:32 AM
> To: Aaron; r-sig-finance@r-project.org
> Subject: Re: [R-SIG-Finance] Basket stop loss implementation
> quantstrat
> 
> On Mon, 2016-12-12 at 17:17 +, Aaron wrote:
> > 
> > Is it possible to implement a basket or portfolio stoploss in
> > quantstrat? That is, I would like to trade a number of symbols
> > simultaneously and use accumulated p/l across all symbols as a
> > global
> > stoploss/take profit.
> > 
> > I have not seen any examples of this in quanstrat, likely as this
> > method of position management is normally seen in foreign exhange
> > trading and not stocks.
> > 
> > Is it possible to do or would it require getting a list of possible
> > entry prices for all symbols and performing post-hoc  position
> > management?
> quantstrat isn't really optimized for this kind of rule.
> 
> You could do it with a rebalance rule, but it will add a cross-symbol
> loop on every rebalance period.
> 
> Brian
> 
> 
>   [[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.

Re: [R-SIG-Finance] Need help with replication of a strategy using 'Quantstrat' & 'IKTrading' packages

2016-12-12 Thread Brian G. Peterson
On Mon, 2016-12-12 at 23:27 +0530, Adarsh KP wrote:
> I am trying to replicate core strategy (for a single instrument) from
> the book 'Following the trend' by Andreas Clenow. Details about the
> strategy are in the picture in this link (https://drive.google.com/fi
> le/d/0B5GQskyO5V5SVU1SdTFhY2txWEk/view?usp=sharing). I was able to
> add entry indicators, signals & rules, but need help with the exit
> part (6th & 7th bullet points in the picture). Also, I am not able to
> update the portfolio, hence cannot plot performance charts. I would
> be very grateful for your help if you could look into the code
> attached and help me resolve this issue. 

The most efficient way to accomplish this is likely to use a trailing
order.

- Calculate ATR and 3xATR as an indicator
- Chain a trailing exit with prefer='High' and threshold = 3xATR

Regards,

Brian

___
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] Basket stop loss implementation quantstrat

2016-12-12 Thread Brian G. Peterson
On Mon, 2016-12-12 at 17:17 +, Aaron wrote:
> Is it possible to implement a basket or portfolio stoploss in
> quantstrat? That is, I would like to trade a number of symbols
> simultaneously and use accumulated p/l across all symbols as a global
> stoploss/take profit.
> 
> I have not seen any examples of this in quanstrat, likely as this
> method of position management is normally seen in foreign exhange
> trading and not stocks.
> 
> Is it possible to do or would it require getting a list of possible
> entry prices for all symbols and performing post-hoc  position
> management?

quantstrat isn't really optimized for this kind of rule.

You could do it with a rebalance rule, but it will add a cross-symbol
loop on every rebalance period.

Brian

___
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] blotter updatePortf

2016-11-11 Thread Brian G. Peterson

Without a minimal reproducible example, I'm just guessing.

I would hope that what you really mean is that you are using daily data, 
since daily data is widely available. Even if you plan to trade only 
once a week (Monday, Friday, Wednesday, whatever) I would certainly want 
more data points to check risk and other metrics against. In any event, 
without more information, I would assume a subsetting problem.


The first thing I would check is that in your periodic runs of 
updatePortf, you should not be using a date range. Differences are most 
likely due to incorrect subsetting with your 'weekly' data.  You say 
below that you are calling something like:


updatePortf(strategy, symbols, Dates=tradeDate)

just call

updatePortf(strategy, symbols)

instead.

If my guess is correct, forcing the  subset is not providing the 
'starting' values to calculate the difference on your 'tradeDate' period 
from.


Regards,

Brian

On 11/11/2016 04:04 AM, Michael Chen wrote:


Hi,

So, in running my custom strategy using blotter, I ran into this
problem.  I'll describle the problem first before I give the
simplified code to save some trouble and time for all.I  ran
updatePortf(strategy, symbols, Dates=tradeDate), updpateAcct, and
updateEndEq after each weekly transactions (addTxn).Data is also
in weekly format.  I also ran my program entirely first, then ran
updatePortf(Strategy) at end. The codes is otherwise the same.
The two runs have differene Equity curves and  performance analystics
results.Since in both runs, the trades(transactions) are the
same, shouldn't the equity curve and performance results the same?
Thanks for any general suggestions and advice that I can try to fix
this first.   If not, I will provide data and simplified code.

thank you all again,

Michael  From: R-SIG-Finance
<r-sig-finance-boun...@r-project.org> on behalf of Brian G. Peterson
<br...@braverock.com> Sent: Wednesday, November 9, 2016 3:13 AM To:
r-sig-finance@r-project.org Subject: Re: [R-SIG-Finance] blotter
updatePortf

On 11/09/2016 12:41 AM, Michael Chen wrote:

Reading these answers reminded me to inquire the same about
Quantstrat.  I looked into the code for applyStrategy, I don't see
any explicit calls to updatePort.  Is this because in my runs of
quanstrat, I didn't use "real time" re-balancing? So, no update
was necessary?  Usually, l  just run updatePortf (strategy) which
updates every symbol and calculates PnL for each period prices
available after I ran applyStrategy in quantstrat.   Please let me
know if I am missing something.


updatePortf will be called automatically in quantstrat for
rebalancing, for paramsets, and for walk.forward. It will only be
called by the default applyStrategy call if wrapup=TRUE and you're
using the (experimental, incomplete?) wrapup code.

Regards,

Brian


on Sunday, November 6, 2016 11:15 AM, Brian G. Peterson wrote: On
11/06/2016 12:10 PM, Cameron McLean wrote:

Is there example code you can share or an existing demo in
quantstrat that shows how to update the portfolio equity curve
and size future orders based on the current amount?


Look at any of the rebalancing demos.  There are rebalancing
versions of the Faber and macd demos, at least.

These rebalance (adjust max trade size) periodically based on
current portfolio equity.


A simple example would be something similar to the faber demo
where he invests 20% of equity in each of the 5 asset classes
(currently the faber demo buys and sells lots of 500 shares) or
a more complex example could be using a indicator such as ATR to
size trades based on risk.


Sizing based on risk would be done via a custom order sizing
function that used the indicator value to adjust trade size.  I
believe Ilya has examples of this on his blog.

Regards,

Brian


___
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.




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

___
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] blotter updatePortf

2016-11-09 Thread Brian G. Peterson

On 11/09/2016 12:41 AM, Michael Chen wrote:

Reading these answers reminded me to inquire the same about
Quantstrat.  I looked into the code for applyStrategy, I don't see
any explicit calls to updatePort.  Is this because in my runs of
quanstrat, I didn't use "real time" re-balancing? So, no update was
necessary?  Usually, l  just run updatePortf (strategy) which
updates every symbol and calculates PnL for each period prices
available after I ran applyStrategy in quantstrat.   Please let me
know if I am missing something.


updatePortf will be called automatically in quantstrat for rebalancing,
for paramsets, and for walk.forward. It will only be called by the
default applyStrategy call if wrapup=TRUE and you're using the
(experimental, incomplete?) wrapup code.

Regards,

Brian


on Sunday, November 6, 2016 11:15 AM, Brian G. Peterson wrote:
On 11/06/2016 12:10 PM, Cameron McLean wrote:

Is there example code you can share or an existing demo in
quantstrat that shows how to update the portfolio equity curve and
size future orders based on the current amount?


Look at any of the rebalancing demos.  There are rebalancing
versions of the Faber and macd demos, at least.

These rebalance (adjust max trade size) periodically based on current
portfolio equity.


A simple example would be something similar to the faber demo
where he invests 20% of equity in each of the 5 asset classes
(currently the faber demo buys and sells lots of 500 shares) or a
more complex example could be using a indicator such as ATR to
size trades based on risk.


Sizing based on risk would be done via a custom order sizing function
that used the indicator value to adjust trade size.  I believe Ilya
has examples of this on his blog.

Regards,

Brian


___
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] blotter updatePortf

2016-11-06 Thread Brian G. Peterson

On 11/06/2016 12:10 PM, Cameron McLean wrote:

Is there example code you can share or an existing demo in quantstrat
that shows how to update the portfolio equity curve and size future
orders based on the current amount?


Look at any of the rebalancing demos.  There are rebalancing versions of 
the Faber and macd demos, at least.


These rebalance (adjust max trade size) periodically based on current 
portfolio equity.



A simple example would be something similar to the faber demo where he
invests 20% of equity in each of the 5 asset classes (currently the
faber demo buys and sells lots of 500 shares) or a more complex
example could be using a indicator such as ATR to size trades based on
risk.


Sizing based on risk would be done via a custom order sizing function 
that used the indicator value to adjust trade size.  I believe Ilya has 
examples of this on his blog.


Regards,

Brian

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

___
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] CVaR and Penalty Augmented objective function

2016-10-13 Thread Brian G. Peterson
Yes, agreed.  This gets handled correctly, I think in
optimize.portfolio(), but when constrained_objective is called directly,
it looks like set.portfolio.moments doesn't merge arguments from ... or
the arguments=list correctly.

I think that the 'correct' way to deal with this would probably be to
handle it in dots, and make sure those get merged correctly.  I think it
would be much more challenging to process it from the argument=list()
for an individual objective.

Regards,

Brian

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


On Thu, 2016-10-13 at 12:54 -0500, Michael Weylandt wrote:
> The issue seems to be in the calculation of the co-skewness and co-kurtosis.
> 
> In particular, when calling ES directly, the user-supplied mu gets
> used to calculate M3 and M4. When called through PortfolioAnalytics,
> M3 and M4 are calculated (without using mu) before calling ES.
> 
> A pure PerformanceAnalytics example:
> 
> ## < BEGIN EXAMPLE > ##
> ## Marco's problem
> library(PerformanceAnalytics)
> data(indexes)
> 
> R <- indexes[,1:4]
> 
> w <- rep(1/4, 4)
> mu <- rep(0.01, 4)
> 
> M2 <- cov(R)
> M3 <- M3.MM(R)
> M4 <- M4.MM(R)
> 
> ES(R, portfolio_method="single", weights=w,
>sigma=M2, m3=M3, m4=M4, mu=mu, invert=FALSE)
> 
> ES(R, portfolio_method="component", weights=w, mu=mu)$MES
> 
> ## Adding mu to the calculation of M3, M4 gives consistent answers
> M2_mu <- cov(R)
> M3_mu <- M3.MM(R, mu=mu)
> M4_mu <- M4.MM(R, mu=mu)
> 
> ES(R, portfolio_method="single", weights=w,
>sigma=M2, m3=M3_mu, m4=M4_mu, mu=mu, invert=FALSE)
> 
> ES(R, portfolio_method="component", weights=w, mu=mu)$MES
> 
> # < END EXAMPLE > ##
> 
> It looks like PortfolioAnalytics::set.portfolio.moments does not
> attempt to pass a user supplied mu to PerformanceAnalytics::M3.MM and
> PerformanceAnalytics::M4.MM, even if its given as an argument to
> constrained_objective():
> 
> >From 
> >https://github.com/cran/PortfolioAnalytics/blob/master/R/moment.functions.R#L327
> 
> switch(method,
> sample = {
> if(is.null(momentargs$mu)) momentargs$mu = matrix(
> as.vector(apply(tmpR, 2, 'mean')), ncol=1);
> if(is.null(momentargs$sigma)) momentargs$sigma = cov(tmpR)
> if(is.null(momentargs$m3)) momentargs$m3 =
> PerformanceAnalytics::M3.MM(tmpR)
>     if(is.null(momentargs$m4)) momentargs$m4 =
> PerformanceAnalytics::M4.MM(tmpR)
> },
> 
> [Code for the current development version looks to behave similarly]
> 
> 
> Hope this helps,
> Michael
> 
> 
> On Wed, Oct 12, 2016 at 5:21 AM, Brian G. Peterson <br...@braverock.com> 
> wrote:
> > I attach what I think is a syntactically correct version of the email, which
> > looks like it was pasted from HTML.
> >
> > We'll try to take a look.
> >
> > Regards,
> >
> > Brian
> >
> > On 10/12/2016 04:34 AM, Marco Mastrangeli wrote:
> >>
> >> Hi Michael,
> >>
> >> thanks for your reply, I apologize for the not full clarity of my
> >> question.
> >> In the following, I try to report a full example.
> >>
> >> #Library
> >> *library(PerformanceAnalytics)*
> >> *library(PortfolioAnalytics)*
> >>
> >> #Returns data present in "PortfolioAnalytics"
> >> *data(indexes)*
> >> *indexes <- indexes[,1:4]*
> >>
> >> #New Portfolio Object
> >> *Wcons <- portfolio.spec(assets=colnames(indexes))*
> >>
> >> #Add box constraints
> >> *Wcons <- add.constraint(portfolio=Wcons, type='box', min=0, max=1)*
> >>
> >> #Add the full investment constraint
> >> *Wcons <- add.constraint(portfolio=Wcons, type="full_investment")*
> >>
> >> #Add Objective specification: VaR with default parameter for vector "mu"
> >> (EXAMPLE 1)
> >> *VaRObjSpec <- add.objective(portfolio=Wcons, type="risk", name="VaR",
> >> arguments=list(p=0.95), enabled=TRUE)*
> >>
> >> #The value of the objective function is:
> >> *constrained_objective(w=rep(1/4,4), R=indexes, portfolio=VaRObjSpec)
> >> #* VaR
> >> *0.0499467*
> >>
> >> #This is the VaR of the equal-weight portfolio as computed by the function
> >> VaR in the PerformanceAnalytics package.
> >> *VaRout <- VaR(indexes, weights=rep(1/4,4), p=0.95,
> >> portfolio_method="component")*
> >> *VaRout$MVaR   # *[1,]* 0.0499467*
> >>
> >&g

Re: [R-SIG-Finance] CVaR and Penalty Augmented objective function

2016-10-12 Thread Brian G. Peterson
koverflow.com/questions/5963269/how-to-make-
a-great-r-reproducible-example
[2] http://adv-r.had.co.nz/Reproducibility.html

On Tue, Oct 11, 2016 at 11:46 AM, Marco Mastrangeli
<marco.mastrang...@gmail.com> wrote:

I have a question about the use of the "mu" parameter in the functions
StdDev, VaR e CVaR.
As reference data we can use data in the paper "Vignette: Portfolio
Optimization with CVaR budgets in PortfolioAnalytics".
If we use the default parameters for "mu" and "sigma", there is a
match between


constrained_objective( w = rep(1/4,4) , R = indexes, portfolio = ObjSpec)

[,1]
ES 0.1253199

and


out<-ES(indexes, weights = rep(1/4,4),p=0.95,

portfolio_method="component")

out$MES

 [,1]
[1,] 0.1253199

as explained by the authors.
If I insert a user-defined sigma matrix for the "sigma" parameter, the
match is still there between this two exspressions. If I insert a
user-defined vector for the "mu" parameter (for example "mu=rep(0.01,

4)",

the result of the two exspressions is the same only for portafolio with
risk objective function StdDev and VaR, not for CVaR.

VaR case:

ObjSpec = add.objective(portfolio=Wcons, type="risk", name="VaR",

arguments=list(p=0.95, mu=rep(0.01,4)), enabled=TRUE)

constrained_objective(w=rep(1/4,4), R=indexes, portfolio=ObjSpec)

   [,1]
VaR 0.04638622


out<-VaR(indexes, weights=rep(1/4,4), p=0.95, mu=rep(0.01,4),

portfolio_method="component")

out

$MVaR
  [,1]
[1,] 0.04638622


CVaR case:

ObjSpec = add.objective(portfolio=Wcons, type="risk", name="CVaR",

arguments=list(p=0.95, mu=rep(0.01,4)), enabled=TRUE)

constrained_objective(w=rep(1/4,4), R=indexes, portfolio=ObjSpec)

 [,1]
ES 0.1217594


out<-ES(indexes, weights=rep(1/4,4), p=0.95, mu=rep(0.01,4),

portfolio_method="component")

out

$MES
 [,1]
[1,] 0.1235878

I can't find the explanation for this thing. Thanks a lot for your
attention.

Marco

 [[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.




--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
# Reported by Marco Mastrrangeli <marco.mastrang...@gmail.com>
# 2016-10-12
# possible issue in constrained_objective

#Library
library(PerformanceAnalytics)
library(PortfolioAnalytics)

#Returns data present in "PortfolioAnalytics"
data(indexes)
indexes <- indexes[, 1:4]

#New Portfolio Object
Wcons <- portfolio.spec(assets = colnames(indexes))

#Add box constraints
Wcons <- add.constraint(
  portfolio = Wcons,
  type = 'box',
  min = 0,
  max = 1
)

#Add the full investment constraint
Wcons <- add.constraint(portfolio = Wcons, type = "full_investment")

# Add Objective specification: VaR with default parameter for vector "mu"
# (EXAMPLE 1)

VaRObjSpec <-
  add.objective(
portfolio = Wcons,
type = "risk",
name = "VaR",
arguments = list(p = 0.95),
enabled = TRUE
  )

#The value of the objective function is:

constrained_objective(w = rep(1 / 4, 4),
  R = indexes,
  portfolio = VaRObjSpec)

# VaR  0.0499467

# This is the VaR of the equal-weight portfolio as computed by the function
# VaR in the PerformanceAnalytics package.
VaRout <- VaR(
  indexes,
  weights = rep(1 / 4, 4),
  p = 0.95,
  portfolio_method = "component"
)

VaRout$MVaR   # [1,] 0.0499467

# Now, I repet the VaR example with a user-defined
# vector for the parameter "mu".

# User-defined vector "mu"
myMu = rep(0.01, 4)

# Add Objective specification: VaR with user-defined parameter for vector "mu"

myVaRObjSpec <-
  add.objective(
portfolio = Wcons,
type = "risk",
name = "VaR",
arguments = list(p = 0.95, mu = myMu),
enabled = TRUE
  )

# The value of the objective function is:
constrained_objective(w = rep(1 / 4, 4),
  R = indexes,
  portfolio = myVaRObjSpec)

# VaR 0.04638622

# This is the VaR of the equal-weight portfolio as computed by the
# function VaR in the PerformanceAnalytics package with mu = myMu.
myVaRout <- VaR(
  indexes,
  weights 

Re: [R-SIG-Finance] Search Function

2016-10-09 Thread Brian G. Peterson

On 10/08/2016 10:29 PM, Michael Chen wrote:

I am looking for an answer to a specific question, I have been going
through the threads to look for anything relevant first before I post
a new thread.  I haven't found anything.  Is there a search function?
I don't think it's efficient to go though every thread.

This is my first post, please excuse any unintentional breaking of
the forum rules.


I will refer you to Pat Burns' excellent post on this exact question:

http://www.portfolioprobe.com/2012/01/19/how-to-search-the-r-sig-finance-archives/

I located the Portfolio Probe post using method B and Googling

'r-sig-finance search'

Regards,

Brian

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

___
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] Error:subscript out of bounds: no column name containing "Close

2016-10-09 Thread Brian G. Peterson

On 10/08/2016 10:55 PM, Ramesh wrote:

I have downloaded the time series of crude oil in to R ( from a local excel
file(csv/text tab delimited) in C drive)and the data ( OHLC) is being
displayed in my R.Studio.
Am able to ascertain the Fibonacci pivot levels ( using a custom program
that I have developed). But when I use package Quantmod and try to
calculate the values using MACD,RSI etc,I get an error  *subscript out of
bounds: no column name containing "Close" *
I googled and found that many R users have  asked help for this issue in
the past.By and large this question looks to be remaining unresolved.
Can you please help me with a solution?


Please follow the posting guide:

https://www.r-project.org/posting-guide.html

You haven't provided a minimal reproducible example, or nearly enough 
data to answer your question with certainty.



For the purposes of my guestimate, we'll assume your variable is called 'x'

I suspect that your 'OHLC' data doesn't contain any columns that 
quantmod's OHLC utility functions can recognize as close data.


Check the column names of your data.

Try using quantmod functions such as Cl(x) or is.OHLC(x) to see if 
quantmod recognizes your column names. (They are looking for variations 
of English words such as 'open, 'high', 'low', and 'close', 
capitalization unimportant)


The TTR functions MACD and RSI expect univariate data, not OHLC data, as 
is noted in the documentation.  You could try something like:


RSI(getPrice(x))
MACD(getPrice(x))

and the quantmod function 'getPrice' will try to guess at a column that 
may contain price data.  This may suffer from the same problem as above.


In short, your problem is almost certainly that the column names of your 
imported data are unintelligible:  Check this first.


If you're still having trouble, please follow the posting guide and 
provide a minimal reproducible example.


--
Brian

___
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] Constrained portfolio optimization with DEoptim

2016-10-04 Thread Brian G. Peterson
On Tue, 2016-10-04 at 13:04 +0200, Kristian Lind wrote:
> Hi guys,
> 
> I’m working on a portfolio optimization problem
> 
> The problem I’m trying to solve is of the form Min risk s.t. return = X
> 
> For each asset I have simulated 1000 paths for the development in asset
> prices over 10 years.
> 
> The risk measure I’d like to minimize is YoY shortfall risk, but could be
> any risk metric that takes the information from the scenarios into
> consideration.
> 
> Likewise, the return is the mean annualized return over all the scenarios.
> 
> I have tried solving this problem using DEoptim, where deviations from the
> required return is penalized. Similar to the approach applied here:
> https://cran.r-project.org/web/packages/DEoptim/vignettes/
> DEoptimPortfolioOptimization.pdf
> 
> I am unable to achieve convergence where the constraint is fulfilled.
> 
> My object function is of the form
> 
> Obj <- function(Return data, portfolio weights, target return){
> 
> Normalize weights to 1
> calculate portfolio return
> calculate risk metric
> calculate deviation from target return
> return (risk metric + deviation from target return*1e6)
> 
> }
> 
> Any pointers on how to formulate and solve this problem would be much
> appreciated.

You haven't provided a reproducible example, making it hard to help you.

You can easily do this type of optimization with PortfolioAnalytics,
including the use of DEoptim, pso, GenSA, or random search as a global
solver.  The modular objective function of PortfolioAnalytics should
allow you to specify your problem pretty easily.  You'll need to pass in
your 'scenarios', and use functions in your objective that expect this
data and know how to apply weights to it, but that should be pretty
simple.

To get to a general answer to your question, you need to determine a few
things.

Is there a solution that meets your constraints?  Many portfolios are
over-constrained, and are unable to meet the objectives as defined.  In
these cases you need to be able to relax constraints to get to the
closest solution.  It often helps to plot the unconstrained and
constrained feasible spaces (e.g. with random portfolios from
PortfolioAnalytics) to see if you've overconstrained things.  

You don't appear to be using 'fnMap' to apply your full investment
(cardinality) constraint.  Using this tells DEoptim what the final
parameter values were, and doesn't hide them inside your custom
objective.

You're attempting to perform a penalized optimization with:

return (risk metric + deviation from target return*1e6)

Is this the right weighting/penalty between your objectives? Typically,
if you have no significant preference between your objectives, you want
to use a multiplier first to get them to similar overall scale.  Then
you want to separately penalize (with a large multiplier) deviation from
your constraint.  In this case, you are penalizing overshooting and
missing below the return in the same way.  This will give the optimizer
very little indication on direction to improve the solution.  It can
also make sense to plot the path the optimizer takes to see if you're
getting close to a solution.

Your use of 'scenarios' could be messing you up.  DEoptim permutes
'parameters', which are typically weights in portfolio optimization
space.  It then applies the objective function passing the parameters
into the objective. You've said that you're using a annualized mean
across the scenarios and an annualized shortfall risk across the
scenarios.  You're losing quite a lot of dimensionality by reducing 1000
scenarios on each asset to one number.  Doing simple linear combinations
of these will not take things like covariances into account, even though
by saying that you care about shortfall risk, I'd assume what you really
want is *portfolio* shortfall.  I would be concerned that the loss of
dimensionality suggested by your description would make it difficult to
get a realistic solution.   

There are several more things that could cause this to fail to converge.
I've covered the most likely ones, but further investigation would
require a reproducible example.

Regards,

Brian


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

___
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] PortfolioAnalytics: unused argument error

2016-09-20 Thread Brian G. Peterson
On Mon, 2016-09-19 at 23:08 +, Jason Hart wrote:
> I've never been able to get portfolioanalytics to work for me.  It
> looks like a nice little addition to the R arsenal but we just weren't
> meant to work together.  I figured what the heck I'll try this code
> and I got the same error message I usually get:

> Error in optimize.portfolio(R = returns, portfolio = pspec,
> optimize_method = "pso", : 
> unused arguments (R = returns, portfolio = pspec, optimize_method =
> "pso", trace = TRUE)
> 
> I've installed all the dependent (or at least I think I have) but to
> no avail.  I installed pso, all the different ROI packages, Rcmdr.
>  I've seen other people with the same issue but did not see a
> solution.

Really?  Which "other people"?  Link?  

My Google-fu only turned up one other instance of this error, and the
error was reportedly resolved by installing the dependencies.

> Any help is much appreciated.

Jason,

Please don't hijack the thread.  People who use threaded mail readers
will have difficulty following or finding your question.

Assuming you used the script from the prior thread, I had no problem
running that code on multiple machines and R versions.  

The error itself suggests that something is masking objects like
optimize.portfolio in your .GlobalEnv, but without more information
there's nothing to diagnose.

Please try to follow the posting guide:

https://www.r-project.org/posting-guide.html

At a minimum, please provide the output of 

sessionInfo()

Regards,

Brian

___
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] PortfolioAnalytics: Custom Constraint

2016-09-20 Thread Brian G. Peterson
The attached .R file should demonstrate what Ross was talking about.

The cardinality constraint is directly supported by the stochastic
global solvers 'DEoptim' and 'random', as described in the
documentation.  

It can also be formulated as a mixed integer linear problem for certain
objective functions, but because you're looknig for the Markowitz
mean/variance portfolio, you have a quadratic problem, and can't
formulate it using an MILP.

The attached file fixes the minimum position box constraint to 0, which
aalows all solvers to converge.  

pso still doesn't get a good solution, but this solution may be improved
by increasing the maximum number of iterations via the parameter
'maxit'. After increasing maxit, it gets closer to the solutions
returned by 'random' and 'DEoptim'.

Both 'random' and 'DEoptim' produce similar solutions which meet the
constraints and objectives, but they are not identical, and change over
multiple runs.  (This is stochastic optimization, after all, so the only
way to get the same answer would be to artificially, and incorrectly,
set the random seed.).

This suggests two things:

1> that there are not enough random portfolios (for 'random') or that
the population size and number of generations are insufficient to find
the global solution (for DEoptim).

2> that the problem may still be somewhat over constrained, most likely
by the max weight box constraint.  You might want to use random
portfolios and portfolio sets to compare and plot the difference between
the unconstrained search space and the constrained search space to sort
out whether this is really what you want to do.

Regards,

Brian

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


On Tue, 2016-09-20 at 07:18 -0500, Ross Bennett wrote:
> Hi Abhay,
> 
> The cardinality constraint is not directly support by the pso solver so we
> have to implement that constraint as a penalized objective. Also note that
> your box constraint is greater than 0, so the box constraint and position
> limit constraint are fighting each other (i.e your problem is
> overconstrained).
> 
> I recommend using DEoptim or random portfolios as a solver when dealing
> with position limit constraints. Position limit constraints are supported
> directly by the algorithm in the random portfolios 'sample' method. We use
> the mapping function supported by DEoptim to handle more complex
> constraints such as position limit when using DEoptim as the solver.
> 
> Hope that helps.
> 
> Ross
> 
> 
> On Mon, Sep 19, 2016 at 10:25 AM, Abhay Bhadani <abhad...@gmail.com> wrote:
> 
> > Thanks, Brian!
> >
> > I implemented the following:
> > 
> > --
> > data("edhec")
> > returns <- edhec[,1:12]
> > colnames(returns) <-
> > c("CA","CTAG","DS","EM","EN","ED","FIA","GMLS","MA","RV","SS","FF")
> > print(head(returns,5))
> > fund.names <- colnames(returns)
> >
> >
> > #Giving Portfolio Specifications
> > pspec <- portfolio.spec(assets=fund.names)
> > print.default(pspec)
> >
> > #Adding Constraints
> > #Full investment constraint: sum of all x_i is 1
> > pspec <- add.constraint(portfolio= pspec, type =
> > "weight_sum",min=0.99,max=1.01)
> >
> > #Box constraint: value of x_i varies between 0.2 to 0.8
> > pspec <- add.constraint(portfolio= pspec, type="box", min = 0.01, max =
> > 0.25)
> >
> > #Cardinality constraint
> > pspec <- add.constraint(portfolio= pspec, type="position_limit",max_pos =
> > 6,enabled=TRUE)
> >
> > #Adding Objective
> > pspec <- add.objective(portfolio=pspec, type="risk", name="var")
> > pspec <- add.objective(portfolio=pspec, type="return", name="mean")
> >
> > opt_meanvar <- optimize.portfolio(R=returns,portfolio = pspec,
> >   optimize_method="pso", trace=TRUE)
> >
> > --------
> > --
> >
> > Results:
> >
> > Optimal Weights:
> > CA   CTAG DS EM EN EDFIA   GMLS MA RV
> > SS FF
> > 0.1319 0.0551 0.0312 0.1312 0.1127 0.1467 0.0641 0.0218 0.0805 0.1101
> > 0.0296 0.0952
> >
> > _
> >
> > I obtained weights for all 12 assets. That's why I was not sure whether
> > position_limit constraint is same as card

Re: [R-SIG-Finance] PortfolioAnalytics: Custom Constraint

2016-09-19 Thread Brian G. Peterson
On Mon, 2016-09-19 at 20:22 +0530, Abhay Bhadani wrote:
> I just started exploring PortfolioAnalytics package.
> 
> Similar to setting up custom objective functions, is there a way to set up
> custom constraints too?
> 
> I would like to know how to set up cardinality constraint (i.e., limiting
> number of assets in a portfolio).

cardinality constraints are already supported via the 'position_limit'
constraint which is an integer constraint limiting the maximum number of
non-zero weight positions in the portfolio.  It may be added like this:


pspec <- add.constraint(portfolio=pspec,
type="position_limit",
max_pos=3,
enabled=TRUE)

assuming that your portfolio specification object is 'pspec'.

As with other constraint types, this may not be efficiently supported by
all optimization engines. (This is a limitation of the underlying
optimizers/solvers, not of PortfolioAnalytics). 

On a more general note, any constraint may be expressed as an objective
by creating a penalty for violating the constraint.  As noted above,
this may lead to very inefficient or non-converging optimization.

Regards,

Brian

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

___
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] Fwd: Multi-Asset Portfolio Performance Attribution

2016-08-25 Thread Brian G. Peterson
install.packages("PortfolioAttribution",repos="http://R-Forge.R-project.org;)

Regards,

Brian

On Thu, 2016-08-25 at 02:50 +0200, Olasunkanmi Obanubi via R-SIG-Finance
wrote:
> Hi all,
> 
> I am interested in knowing if there are any packages that deal with
> performance attribution for a multi-asset portfolio in general.
> 
> In particular, I am interested in capturing the performance attribution at
> each level of a top-down approach (say, for example, starting with asset
> allocation at the highest level all the way down to manager selection at
> the lowest level) to investing in a multi-asset portfolio.

___
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] Backtesting without long-only constraint

2016-08-19 Thread Brian G. Peterson

On Fri, 2016-08-19 at 18:31 +0100, d.ind...@iee.org wrote:
> Are there any libraries enabling the portfolio back testing a la
> PortfolioAnalytics's Portfolio.return() with -ve weightings, or relaxed
> leverage constraint?

Return.portfolio in PerformanceAnalytics supports whatever you want for
the weights vector, including negative weights and leverage.

You speak about 'portfolio back testing' without specifics, so I'll
assume that you mean a portfolio optimization with periodic rebalancing.

If my assumption is correct, you can use the package PortfolioAnalytics
for rebalancing optimizations of long/short portfolios with or without
leverage constraints.  It can also support selective constraint
relaxation to get to a feasible portfolio.  

Be aware that more complicated constraints and objectives rapidly get
into the territory of stochastic global solvers like differential
evolution, particle swarms, and simulated annealing, or constraint-based
random search of the feasible space.   These are all supported, but may
take a long time or be difficult to get convergence.

Regards,

Brian

___
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] Multi Asset portfolio failing at applyStrategy with 'data' must be of a vector type, was 'NULL'

2016-08-10 Thread Brian G. Peterson
On Wed, 2016-08-10 at 11:56 +, AIE ATUMA via R-SIG-Finance wrote:
> Sequel to your response below, what package can other nationals whose
> data are not in R use to do analysis and manage assets?
> 
I don't see that this question has anything to do with the thread.  If
you have a new question, please post with a new subject line, so that
people who use threaded email readers can see that it is a new thread.

People get data from many sources.  That has been discussed on this list
countless times.

This is a list for using R in Finance.  So the only answer to the second
half of your question is to take your data and put it into R, where
there are many many packages available for doing the analysis.

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

___
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] Help required in getting SMA triggered entry with quantstrat add.rule

2016-08-01 Thread Brian G. Peterson


Instant execution can't happen.

If you insist, pass 

allowMagicalThinking=TRUE 

in the call to applyStrategy.

If you want to execute at your signal price, the 'right answer' is to
use higher frequency data, and enter an order after you get the signal.
If you insist on believing that you can execute at the signal price, add
a column for the signal price, e.g. "magic_signal_price" when you add
your indicators, and pass

prefer="magical_signal_price" in the arguments list for your entry rule.

This is *not* a realistic "proof of concept", where your goal should be
to focus on *disproving* your assumptions, not insisting on unrealistic
execution assumptions.  

Yes, you can do it, and quantstrat will let you.  That doesn't make it a
good idea.  

We've tried to make it hard to make most of the common mistakes, and try
to make more realistic assumptions the default (and have documented them
in the manual).

Brian 

On Mon, 2016-08-01 at 19:31 +0400, golam sakline wrote:
> Hi Brian,
> 
> 
> Really appreciate you responding to my query. While I understand what
> you are saying, I I am just using the SMA indicator to assess whether
> it is possible to execute On Signal date trade to build our proof of
> concept in R Quantstrat. Our signal generator is using a
> predictive pricing mechanism and when the real market price becomes
> equal to/crosses the predicted price, a trade is executed at market
> within some parameters of slippage.  From your reply should I presume
> that a day late execution from signal date is deliberate and you have
> a mechanism of making it on signal date execution?
> 
> 
> thanks
> 
> 
> Golam
> 
> On Mon, Aug 1, 2016 at 2:22 PM, Brian G. Peterson
> <br...@braverock.com> wrote:
> On 08/01/2016 05:07 AM, golam sakline wrote:
> In Quantstrat, is it possible to use add.rule that can
> pick the crossover
> between SMA and the price as entry rather than the bar
> close price? Is
> there a way I can add buy-stop and sell-stop order
> types using limits that
> look into the SMA figures.
> 
> Also why does it seem like that the trade registration
> date is one day
> late? How can we fix this to register as per the day
> of the signal?-- See
> the mktdata and getTxns output.
> 
> Real markets are not instant. It is physically impossible to
> observe a signal and act on it at the same instant.  You must
> observe your signal, do some calculation, place an order, the
> market must receive your order, and the market must match your
> order.
> 
> If you want action at higher frequencies, use higher frequency
> data.
> 
> If you insist on proceeding with unrealistic expectations,
> pass
> 
> allowMagicalThinking=TRUE
>

___
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] Help required in getting SMA triggered entry with quantstrat add.rule

2016-08-01 Thread Brian G. Peterson

On 08/01/2016 05:07 AM, golam sakline wrote:

In Quantstrat, is it possible to use add.rule that can pick the crossover
between SMA and the price as entry rather than the bar close price? Is
there a way I can add buy-stop and sell-stop order types using limits that
look into the SMA figures.

Also why does it seem like that the trade registration date is one day
late? How can we fix this to register as per the day of the signal?-- See
the mktdata and getTxns output.


Real markets are not instant. It is physically impossible to observe a 
signal and act on it at the same instant.  You must observe your signal, 
do some calculation, place an order, the market must receive your order, 
and the market must match your order.


If you want action at higher frequencies, use higher frequency data.

If you insist on proceeding with unrealistic expectations, pass

allowMagicalThinking=TRUE

--
Brian

___
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] Imputing Missing Values

2016-06-26 Thread Brian G. Peterson
On Sun, 2016-06-26 at 12:53 +, Pankaj K Agarwal via R-SIG-Finance
wrote:
> This might be a very basic query for this erudite group. However, i am
> hopeful some help will be forthcoming nevertheless.I have a monthly
> time series of annualized t-bill rates on Indian markets. For some
> months, the values are missing randomly. I need to convert the
> annualized yields into daily as well as monthly yields. I have two
> questions:1. I am using package zoo. Which of the methods of NA
> imputations will be advisable for this series, viz., na.agggregate,
> na.locf, na.spline or na.approx etc.?2. Should the imputation be done
> on monthly annual yields and then the conversion to daily and monthly
> yields be performed or imputation be done afterwards?3. Are there
> better methods than above for this task? I will be extremely grateful
> for comments. Thanks a ton. Regards,Pankaj

The short answer is: Don't do it.  This is a bad idea.

You need to find a better source of data.  Daily data on Indian 3-mo
bill yields is widely available from free sources.  See, e.g.

http://www.investing.com/rates-bonds/india-3-month-bond-yield

There are many other sources of this data as well, I have no opinion on
the data quality of one over the other, but *any* of them would likely
be orders of magnitude better than what you're asking to do.

You can't impute from lower-frequency data to higher frequency data with
any confidence.  

These NA imputation methods are designed to fill some occasionally
missing data, or to do something like Last Observation Carried Forward
on Bid/Ask spreads (which is not imputation at all, since that is the
prevailing market). 

Basically, it always makes sense to start with the highest frequency
data available, and aggregate to lower frequencies.  In the case of your
query, start with the spot yield and do whatever adjustments you need.

Regards,

Brian

___
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] rule delays

2016-05-29 Thread Brian G. Peterson
quot; "Open" NA "-8"
"ruleSignal.rule" ""
2003-11-21 "all" "3.66852349535765" "market"   "long"
NA  "closed" "2003-11-24 00:00:00" "Open" NA "-8"
"ruleSignal.rule" ""
2003-12-10 "2638""3.46333489307494" "market"   "long"
NA  "closed" "2003-12-11 00:00:00" "Open" NA "-8"
"ruleSignal.rule" ""
2003-12-22 "all" "3.01565066991264" "market"   "long"
NA  "closed" "2003-12-23 00:00:00" "Open" NA "-8"
"ruleSignal.rule" ""
2004-01-13 "3591""3.07782903424074" "market"   "long"
NA  "closed" "2004-01-14 00:00:00" "Open" NA "-8"
"ruleSignal.rule" ""
2004-01-23 "all" "2.9223831234205"  "market"   "long"
NA  "closed" "2004-01-26 00:00:00" "Open" NA "-8"
"ruleSignal.rule" ""
2004-01-28 "3603""2.76693721260026" "market"   "long"
NA  "closed" "2004-01-29 00:00:00" "Open" NA "-8"
"ruleSignal.rule" ""
2004-01-30 "all" "2.81046206762993" "market"   "long"
NA  "closed" "2004-02-02 00:00:00" "Open" NA "-8"
"ruleSignal.rule" ""
Please note the last line shows a purchase and sale only two calendar
days apart.  They were weekdays.  That is not in accordance with the
indicator or rule I wrote.   In fact mktdata shows:

2004-01-28 1 0 1 0
2004-01-29 0 0 0 0
2004-01-30 0 0 0 0
2004-02-02 0 0 0 0
2004-02-03 0 0 0 0
2004-02-04 0 0 0 0
2004-02-05 0 0 0 0
2004-02-06 0 0 0 0
2004-02-09 0 0 0 1

with the signals on the correct days.

Just a sanity check.  Can anyone tell me where I am going wrong,
presumably in writing that rule.

---

Stephen Choularton PhD, FIoD

On 27/05/2016 4:43 PM, Stephen Choularton wrote:

Hi

I am trying to implement Harry's Connor's RSI from his book
Quantative Trading ...

I have it working as is and have varied it to some shares in the ASX.

However, I want to pursue the idea of a fixed time (8 day) sell rule
that takes place if the normal rule doesn't produce a sale before.

I added this rule at line 127 of the file 3. strategy.R:

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longExit",
    sigval = TRUE,
orderqty = "all", delay = 691200,
ordertype = "market",
orderside = "long", TxnFees = txnFees,
replace = FALSE,
prefer = "Open"), type= "exit", path.dep = TRUE)

but it seems to have no effect.

I attach all the code. You run it in the numbered order of the files.

I wonder if anyone can help me make this sort of rule work.

---

Stephen Choularton PhD, FIoD
0413 545 182


___
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.



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

___
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] Exit Order By Current Position Info

2016-04-22 Thread Brian G. Peterson
On Fri, 2016-04-22 at 17:26 +0200, Diego Peroni wrote:
> Hello,
> 
> I'd like to integrate my strategy creating a Stop Loss Order that must 
> be sent to the market if my current Open Position reach N points up 
> (profit).
> 
> So this rule must evaluate: Open Price Position, Side Position and Quantity.
> 
> Which is the best way to add this behaviuor to my strat?
> In which component do I have to work?
> In case does exist a working example?


Work through the luxor examples that replicate Tomasini.

The takeprofit examples are luxor.5.strategy.ordersets.R and
luxor.6.paramset.takeprofit.R

Fundamentally, you need ordersets and chained orders.  We covered all of
this in great detail in our quantstrat seminar a few years ago at
R/Finance:

http://www.rinfinance.com/agenda/2013/workshop/Humme+Peterson.pdf

don't cut and paste code out of the slides.  The demo scripts are
updated to adjust for changes in R and the required packages, the slides
are obviously static.

Regards,

Brian

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

___
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] adjustOHLC.R issues

2016-04-12 Thread Brian G. Peterson
The output of adjustOHLC and {yahoo}.Adjusted will only rarely, if ever, 
agree, since we don't know when they are starting the adjustment, or how 
they are doing the adjustment.  There are many threads over the years on 
this list about this issue which I'm sure Google can locate for you.


Nevertheless ...

What 'most of the references' are those?

Your equation doesn't subtract dividends or apply splits to arrive at 
the new ratio, and is not a continuous-time process.  (as such, it 
cannot be correct) The ratio changes over time based on the accumulation 
of splits and dividends.


We can easily reference the code, by typing

adjustOHLC

at the prompt, or you could produce an equation which actually includes 
the splits and dividends for which we must adjust ...


I suggest that in addition to the code we use a slightly more 
authoritative source than Yahoo, e.g. CRSP:


http://www.crsp.com/products/documentation/crsp-calculations

From my read of the code, this is what adjustOHLC is doing.

I believe that your error is in not realizing that the ratio changes 
over time.


Brian

On 04/11/2016 08:48 PM, Ernie Stokely wrote:

OK, I am prepared for the stones that will be thrown at me for being
naive, but here goes anyway.

Most of the references on adjusting OHLC values for splits and dividends
say to use the following adjustment:

K = Ca/C
Om = K * O
Hm = K * H
Lm = K * L
Cm = K * C = Ca

where: Ca = adjusted close, OHLC are the unadjusted values, and OmHmLmCm
are the modified (adjusted) values of OHLC.

If I use adjustOHLC() in quantmod to do the adjustment I find these are
not the equations being used. For example, for VTI here is the
unadjusted OHLCadjC data read from Yahoo:

VTI.Open VTI.High VTI.Low VTI.Close VTI.Volume VTI.Adjusted
2006-03-01 128.15 128.90 127.79 128.70 216400 52.56795
2006-03-02 128.50 128.76 128.01 128.61 148800 52.53119
2006-03-03 128.26 129.43 128.20 128.40 323200 52.44541
2006-03-06 128.64 128.64 127.27 127.46 279800 52.06147
2006-03-07 127.15 127.31 126.65 126.94 218400 51.84907
2006-03-08 126.72 127.49 126.20 127.30 491800 51.99612

If I apply adjustOHLC() to these data I get:

VTI.Open VTI.High VTI.Low VTI.Close VTI.Volume VTI.Adjusted
2006-03-01 59.15007 59.49625 58.98391 59.40394 216400 52.56795
2006-03-02 59.31162 59.43163 59.08545 59.36240 148800 52.53119
2006-03-03 59.20084 59.74088 59.17315 59.26546 323200 52.44541
2006-03-06 59.37624 59.37624 58.74389 58.83159 279800 52.06147
2006-03-07 58.68851 58.76235 58.45772 58.59158 218400 51.84907
2006-03-08 58.49003 58.84544 58.25001 58.75774 491800 51.99612

Obviously, VTI.Adjusted (Ca) is not equal to VTI.Close (Cm) as required
by the formula above. I have a colleague who has been trading for 40
years and he says this is an erroneous result (that Cm must equal Ca).
When I explore the package reference file for adjustOHLC() I see this
paragraph:

"New columns are derived by taking the ratio of adjusted value to
original Close, and multiplying by the difference of the respective
column and the original Close. This is then added to the modified Close
column to arrive at the remaining ‘adjusted’ Open, High, Low column
values."

I am not quite sure how to translate this verbal description into an
equation, but it doesn't sound like the same correction I see
recommended elsewhere and described at the beginning of this note.

Can someone shed some light on this discrepancy or send me a link to
more enlightenment? Thanks in advance.

Ernie


___
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] Processing time of backtests on a single computer

2016-04-09 Thread Brian G. Peterson

On Fri, 2016-04-08 at 08:50 +0300, Jersey Fanatic wrote:

So here are my latest results of testing using the same dataset and
rules
(no trailing stoploss):
8-core using doSNOW -> 1.07 hours
4-core using doSNOW -> 59.7 minutes
single-core -> 2.11 hours

So yeah, I guess it is normal for a strategy with high number of
transactions to take this long to backtest.


I tested a version of this strategy on a 6-core (12 thread) i7-4930K
CPU @ 3.40GHz with 64GB of RAM. [Ref. 1 processingtime_q.ps.R]

4.64 hrs one core (registerDoSEQ)
2.12 hrs six cores using doMC (defaults)
1.77 hrs twelve threads using DoMC (defaults)
1.32 hrs six cores doMC (mc.prescheduling=FALSE)
  44 min twelve threads using DoMC (mc.prescheduling=FALSE)

My script was based on the script originally posted to this thread, and
likely had more rules and tighter parameters than the test reported by
the OP above.

Given a single script and parameter combination verified with the OP,
this machine was about twice as fast. [Ref. 1 
processingtime_q_rsigfinance.R]

5.15 min. for OP and 1.99 min on this reported test machine

Some observations:

RAM:
- all tests consumed more than 8GB of RAM at some point (8.1GB for the
single thread version, 11.8GB for the six thread version, and 18GB for
the 12-thread version)

CPU load:
- the 6-thread test had a load average of about 8, and the 12-thread
 test had a load average below 7, suggesting that the 12-thread test
is using resources less efficiently. with prescheduling FALSE, load
averages were higher for both tests, about 10 for the 6-core test
and about 16 for the 12-thread test.

Load Balancing:
- after about an hour, a load balancing problem was observed, fewer than
half the cores/threads were still executing at 100%.  In the case of the
twelve-thread version, after an hour and half, only one CPU was still
spinning at 100%. It is probable that use of a backend like doRedis or
zmq that are designed for load balancing or taking advantage of a
different prescheduling method in the multicore or SNOW backends would
shorten the execution time, potentially by a lot.

Economic Justification:
- I observed that the shortest timeframe indicator and signal processes
are the most aggressive, often scratching trades.  If trade costs were
taken into account when analyzing the signal process (before even
contemplating rules and a backtest, these short-timeframe signals would
have been ruled out before a brute force parameter search.  These
parameter combinations also take the longest to run, in addition to
likely being economically unfeasible.


References:

[1] The data file and script have been added to quantstrat's sandbox
directory in SVN, for those who are interested.

   /pkg/quantstrat/sandbox/paramtest201604/




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

___
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] rbind and duplicates in monthly futures

2016-04-07 Thread Brian G. Peterson
You need to decide how you want to roll.  You're telling us that you'll
trade two contracts simultaneously.  That's different from all the
standard roll algorithms.  

You've confirmed what Josh told you by saying you don't want to create a
continuous series.

You should subset your data over the periods you want to trade before
calling quantstrat.  quantstrat loops over instruments, so it will trade
them when the data is available. 

I don't understand your query about summarizing results.  If they are
cash P recorded in the portfolio or account, they will be at whatever
frequency you market the portfolio at, typically at least daily.  You'll
probably pull then using something like dailyEqPL or dailyTxnPL or
dailyReturn or dailyStats.

You can summarize and display your results however you like, and there
are a host of tools in blotter for price/cash space and
PerformanceAnalytics for %-returns space.  Very few of them presume
monthly summaries except for the few standardized fund returns tables.

Regards,

Brian

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


On Thu, 2016-04-07 at 12:25 +0100, Peter Neumaier wrote:
> I agree, I could have been more explicit about the nature of my problem and
> the specific issue arising from the decision of
> when/how to roll futures contracts.
> 
> However, I was hoping for a workaround within the quantstrat framework that
> might help me. My research shows
> that the last six weeks of my traded contract are the most liquid period
> over lifetime. As a result, I end up wth an overlap of two
> weeks, where in practical terms I would be trading two contracts: the
> actual frontmonth and the following month.
> 
> Currently, I am backtesting each contract separately, ending up with
> monthly output, which is not 100% satisfying as I'd like to
> evaluate the yearly performance and not look into monthly output.
> 
> Is there a way to overcome the issue of overlapping periods, i.e. run the
> data for the actual frontmonth (six weeks) and continue thereafter
> with the overlap period, essentially forcing sequential execution of the
> time series instead of merging the two months and mix
> the data during the overlap period? I hope I am not confusing you further.
> 
> Thanks
> Peter
> 
> 
> 
> On Thu, Apr 7, 2016 at 1:57 AM, Joshua Ulrich <josh.m.ulr...@gmail.com>
> wrote:
> 
> > On Tue, Apr 5, 2016 at 3:06 PM, Peter Neumaier <peter.neuma...@gmail.com>
> > wrote:
> > > Hi all, in the interim I've managed to import the halfhourly data, cast
> > to a
> > > matrix, then rbind the two months
> > > and convert to XTS for later use by my algo strategy.
> > >
> > > I am wondering how the data overlap will be treated by the quantstrat
> > > framework and how
> > > it might be impacting the calculations (i.e. P during overlaps).
> > >
> > It wasn't clear to me from your original question, but I can now see
> > that you want to create a single continuous futures series using
> > adjacent futures contracts.  As you probably guessed, the overlap in
> > data in your APR_MAY object would be very bad.
> >
> > There is not a single correct way to create a continuous futures
> > series for every futures market.  You could roll to the next contract
> > when the current contract expires, when volume or open interest in the
> > current contract drops below some threshold over the next contract's
> > volume or open interest, etc.
> >
> > I realize this doesn't completely answer your question... because your
> > question is not just about programming.  It's also about the
> > microstructure of the market you're researching.  Searching for
> > "continuous futures" or "futures roll" should help give you some ideas
> > about how to solve the problem for this specific futures market.
> >
> > > Below the files and the code I am using:
> > >
> > > APR <-
> > > as.matrix(read.zoo("NGF201404_6weeks30mins.csv",sep=",",tz="",header=T))
> > > MAY <-
> > > as.matrix(read.zoo("NGF201405_6weeks30mins.csv",sep=",",tz="",header=T))
> > >
> > > APR_MAY <- as.xts(rbind(APR,MAY))
> > >
> > >
> > > http://tempsend.com/BBA86DB4ED
> > >
> > > http://tempsend.com/0EDA07753B
> > >
> > > Thanks
> > > Peter
> > >
> > > On Mon, Apr 4, 2016 at 2:30 PM, Joshua Ulrich <josh.m.ulr...@gmail.com>
> > > wrote:
> > >>
> > >> On Mon, Apr 4, 2016 at 7:24 AM, Peter Neumaier <
> > peter.neuma...@gmail.com>
> >

Re: [R-SIG-Finance] Processing time of backtests on a single computer

2016-04-06 Thread Brian G. Peterson
On Wed, 2016-04-06 at 23:58 +0300, Jersey Fanatic wrote:
> I will try running the same code without trailing stops and see what
> effect
> it has on the processing time. I will report back as soon as it is
> finished.

Running the macd demo code over 10 years of daily data on my machine (no
trailing stops) takes 0.5262365 secs for a single run.

___
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] Processing time of backtests on a single computer

2016-04-06 Thread Brian G. Peterson
You didn't say that you were doing parameter optimization.

The length of time that a parameter optimization using brute force will
take is a linear combination of the number of parameter combinations
that you choose to search.

Typically, you should only include parameter distributions for
parameters for which you feel you have a strong economic justification.
Your strategy contains eight parameter distributions and two
constraints.  The choices of these distributions appears arbitrary, and
will result in hundreds of combinations to test.  So, you should expect
your strategy to take approximately a linear combination of additional
time based on the number of parameters you wish to test. 

Another thing that will make this test take a long time is the inclusion
of trailing stops.  As is described in the documentation, trailing stops
require evaluating the strategy at more points in the path dependent
loop.  The number of observations that need to be evaluated in the path
dependent rules loop has another linear effect on the time to evaluate
your backtest.  Have you validated that there is a theoretical
justification for a trailing stop?  Does this increase the positive
expectation of your resulting signal process?

Most of the validation of your indicator and signal processes should be
possible long before you get to parameter optimization.

Regards,

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


On Wed, 2016-04-06 at 23:20 +0300, Jersey Fanatic wrote:
> The email with the CSV data attached is waiting for moderator's approval.
> The reproducible code is below.
> 
> 
> 2016-04-06 23:17 GMT+03:00 Jersey Fanatic <jerseyfanat...@gmail.com>:
> 
> > Thanks for the quick reply. I did not understand what you meant by path
> > dependent looping or copying, but my aim is to decrease the processing time
> > of the backtesting of several combinations of parameters.
> >
> > Here is the reproducible example, and the data I am using is attached as
> > well:
> >
> > library(lattice)
> > library(foreach)
> > library(doSNOW)
> > library(ggplot2)
> > library(PerformanceAnalytics)
> > library(doSNOW)
> > require(latticeExtra)
> > require(grid)
> > library(gridExtra)
> > library(reshape)
> > library(quantstrat)
> > no.cores <- 8
> >
> > .strategy<- new.env()
> > .blotter<- new.env()
> >
> > currency(c('USD', 'EUR'))
> > exchange_rate(primary_id="EURUSD", tick_size=0.0001)
> >
> > data.location.r <- "processingtime_q_rsigfinance.csv"
> > symbol.data <- as.xts(read.zoo(data.location.r, sep=',',
> > tz="",header=TRUE, format='%d/%m/%Y %H:%M', index.column = 1))
> > symbol.data <- symbol.data[symbol.data$VOLUME!=0,]
> ># Delete rows with no volume (when the
> > market is closed)
> > symbol.data[,c(1,2,3,4)] <-
> > round(as.numeric(symbol.data[,c(1,2,3,4)]),abs(log10(0.0001))) #datayı tick
> > sizela uyumlu yapma
> > assign("EURUSD", symbol.data)
> >
> > strategy.st <- "rsigfinance"
> > rm.strat(strategy.st)
> >
> >
> > initDate = as.character(as.Date(index(symbol.data[1])-1))
> >
> > initPortf(strategy.st, "EURUSD", initDate=initDate, currency = "USD")
> > initAcct(strategy.st, portfolios=strategy.st, initDate=initDate,
> > initEq=10, currency = "USD")
> > initOrders(portfolio=strategy.st,initDate=initDate)
> >
> > strategy(strategy.st,store=TRUE)
> > summary(getStrategy(strategy.st))
> >
> >
> > macdFastMARange <- seq(2,17,by=5)
> > macdSlowMARange <- seq(5,35,by=10)
> > macdSignalRange <- seq(2,18,by=8)
> >
> > StopLossDistanceRange <- seq(0.01,0.02,by=0.01)
> >
> > TrailingDistanceRange <- seq(0.01,0.02,by=0.01)
> >
> >
> > positionSizeLong  =round(10 / as.numeric(symbol.data$CLOSE[1]),-2)
> > positionSizeShort =  - round(10 / as.numeric(symbol.data$CLOSE[1]),-2)
> > txn.model <- 0
> > sltsltp.txn.fee <- 0
> >
> >
> > add.indicator(strategy.st,
> >   name = "MACD",
> >   arguments = list(x=Cl(eval(parse(text = "EURUSD",
> >   label='macd')
> >
> > add.signal(strategy.st,name="sigCrossover",
> >arguments =
> > list(columns=c("macd.macd","signal.macd"),relationship="gt"),
> >label="macd.gt.signal")
> >
> > add.signal(strategy.st,name="sigCrossover",
> >

Re: [R-SIG-Finance] Processing time of backtests on a single computer

2016-04-06 Thread Brian G. Peterson
On Wed, 2016-04-06 at 20:58 +0300, Jersey Fanatic wrote:
> Hi everyone,
> 
> I am trying to backtest a simple strategy of mine, but it took approx 10
> hours of processing so I was wondering if it is normal.
> 
> My computer has an i7 core 2.10 GHz, 8GB RAM with Windows 7 Ultimate OS. My
> code uses doSNOW package for parallel processing and all the 8 cores, so
> the CPU is %100 all the time for the complete 10 hours.
> 
> The data the backtest is done on is M5 OHLC FX (24 hour) data for approx 1
> year  which makes a total of 58000 data points (counting OHLC as 1). The
> strategy has 4 entry and 4 stoploss rules.
> 144 combinations of parameters are tested.
> 
> I would just like to know if this time is normal for just 1 home-use
> computer. And I would be grateful for any recommendations to speed up this
> process to decrease computing time.

No, this sounds very high.

You haven't told us enough about what you're trying to do for anyone
here to help you in a specific way.  You'd need to provide a minimal
reproducible example.

However, we typically use a benchmark time of one core minute per day
per instrument on tick data (millions of observations per day).

Is seems likely that you are doing too much path dependent looping,
copying, or both.

Regards,

Brian

___
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] Remove first two weeks of data in half hourly resolution

2016-03-31 Thread Brian G. Peterson
Peter,

You haven't published a reproducible example, and I'm not going to take
the time to write a complete example from scratch.

We use xts subsetting for this type of thing, so I suggest using xts for
your time series (this is always good advice for time series in R).

Here's a partial example to get you started.

###

#load some data from the PerformanceAnalytics package
data(edhec)

#check the range
range(index(edhec))

#add 14 days from the start
first(index(edhec))+14

#now assume that you have an object 'z' with  intraday data
range(z)

#check the range of Dates by forcing the index to Date type
range(as.Date(index(z)))

#add 114 days, as before
first(as.Date(index(z)))+14

# now subset by cutting off the first 14 calendar days 
# from the start of the series
zs <- z[paste0(first(as.Date(index(z)))+14,'/')]

#check the range
range(as.Date(index(zs)))

##

Regards,

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


On Thu, 2016-03-31 at 11:50 +0100, Peter Neumaier wrote:
> Hi all,
> 
> I am doing some analysis on monthly futures contracts from 2011-2016.
> Each monthly contract goes for eight weeks, in half hourly resolution.
> 
> I'd like to remove first two weeks of eight weeks history for each monthly
> contract.
> My approach was to work out the start and end date and cut the 1st two weeks
> data off, but problem is that the half hourly resolution is sometimes
> incomplete(
> i.e. a trading day goes from 7:00am - 4:00pm but sometimes starts at
> 7:30am).
> 
> Any suggestion on how to resolve this? Below a sample trading day in half
> hourly:
> 
> NGFH6.Open NGFH6.High NGFH6.Low NGFH6.Close
> NGFH6.Volume NGFH6.WAP NGFH6.hasGaps NGFH6.Count
> 2016-01-06 07:30:00 0.3395 0.33950.3375  0.3380
> 45   0.33811 0   5
> 2016-01-06 08:00:00 0.3400 0.34000.3387  0.3395
> 140   0.33928 0  12
> 2016-01-06 08:30:00 0.3395 0.33950.3379  0.3379
> 70   0.33884 0   5
> 2016-01-06 09:00:00 0.3379 0.33790.3379  0.3379
> 0   0.33790 0   0
> 2016-01-06 09:30:00 0.3379 0.33790.3379  0.3379
> 0   0.33790 0   0
> 2016-01-06 10:00:00 0.3375 0.33800.3373  0.3373
> 230   0.33738 0  14
> 2016-01-06 10:30:00 0.3376 0.33790.3376  0.3379
> 20   0.33775 0   2
> 2016-01-06 11:00:00 0.3370 0.33700.3370  0.3370
> 105   0.33700 0   5
> 2016-01-06 11:30:00 0.3366 0.33660.3365  0.3365
> 65   0.33658 0   4
> 2016-01-06 12:00:00 0.3370 0.33700.3370  0.3370
> 10   0.33700 0   1
> 2016-01-06 12:30:00 0.3372 0.33720.3361  0.3361
> 125   0.33686 0   9
> 2016-01-06 13:00:00 0.3360 0.33600.3357  0.3360
> 225   0.33585 0  17
> 2016-01-06 13:30:00 0.3357 0.33570.3355  0.3355
> 50   0.33560 0   5
> 2016-01-06 14:00:00 0.3350 0.33590.3350  0.3359
> 25   0.33554 0   2
> 2016-01-06 14:30:00 0.3359 0.33590.3359  0.3359
> 0   0.33590 0   0
> 2016-01-06 15:00:00 0.3352 0.33520.3348  0.3352
> 150   0.33492 0  15
> 2016-01-06 15:30:00 0.3352 0.33520.3334  0.3341
> 280   0.33364 0  24
> 2016-01-06 16:00:00 0.3341 0.33750.3341  0.3370
> 145   0.33543 0  17
> 2016-01-06 16:30:00 0.3380 0.33850.3380  0.3385
> 25   0.33830 0   3
> 
> Many Thanks
> Peter
> 
>   [[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.


  1   2   3   4   5   >