Code Monkey home page Code Monkey logo

Comments (10)

nassuphis avatar nassuphis commented on July 30, 2024

"Individual pair returns" takes too long to render

image

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

the code for "individual pair returns":

backtest_ordered_pair_returns_table<-data.table(
  pm=structure(hdr="{\\bf PM}",paste0("{\\bf ",the_pairs,"}")),
  do.call(data.table,mapply(function(i)structure(
    hdr=tbl(
        c(
          stri_sub(weekdays(as.Date(i)),1,3),
          stri_sub(i,6,-1)
        ),
        align="@{}c@{}"
    ),
    mapply(function(p){
      if(FALSE)cat(p,":",i,"\n")
      the_day<-i
      if(is.na(the_day))return(0)
      ndx<-bar_intervals$date==the_day & bar_intervals$day>0
      if(sum(ndx)<1){
        res<-make_plot(expression({
          plot(NA,type="n",xlab="",ylab="",xlim=c(0,1),ylim=c(0,1),axes=FALSE)
          text(0.5,0.5,labels="No data",cex=5)
        }),width="2cm",height="2cm")
        return(res)
      }
      pnls<-mc(intraday_pair,paste0("^",p))[ndx,,drop=FALSE]
      cum_pnls<-apply(pnls,2,cumsum)
      xlim<-c(1,nrow(pnls))
      ylim<-c(min(cum_pnls),max(cum_pnls))
      rr<-colorRamp(c("white","red"))
      gr<-colorRamp(c("white","green"))
      x<-as.vector(row(cum_pnls))
      y<-as.vector(cum_pnls)
      up_col<-if(all(pmax(y,0)<1e-10)){
        rep(rgb(1,1,1),length(y))
      } else rgb(gr(pmax(y,0)/max(y))/255)
      dn_col<-if(all(pmin(y,0)>(-1e-10))){
        rep(rgb(1,1,1),length(y))
      } else rgb(rr(pmin(y,0)/min(y))/255)
      max_pnl<-max(cum_pnls)
      min_pnl<-min(cum_pnls)
      max_name<-colnames(cum_pnls)[which(cum_pnls==max_pnl,arr.ind = TRUE)[1,2]]
      max_text<-paste0(max_name," : ",ifelse(max_pnl>0,"+","-"),round(10000*abs(max_pnl),digits=1))
      min_name<-colnames(cum_pnls)[which(cum_pnls==min_pnl,arr.ind = TRUE)[1,2]]
      min_text<-paste0(min_name," : ",ifelse(min_pnl>0,"+","-"),round(10000*abs(min_pnl),digits=1))
      make_plot(expression({
        par(mai=c(0,0,0,0))
        plot(
          x=xlim,
          y=ylim,
          type="n",
          xlab="",
          ylab="",
          axes=FALSE
        )
        points(
          x=x,
          y=y,
          col=ifelse(y>0,up_col,dn_col),
          cex=2,
          pch=19
        )
        abline(h=0,col=rgb(0,0,1,0.5),lwd=10)
        text(
          x=mean(xlim),
          y=mean(ylim),
          labels=paste0(max_text,"\n",min_text),
          col="black",
          font=2,
          cex=5
        )
      }),width="2cm",height="2cm",envir=environment())
    },p=the_pairs)
  ),days_in_data,SIMPLIFY=FALSE))
)

a1<-c("m{1cm}",rep("@{}|@{}m{2cm}",ncol(backtest_ordered_pair_returns_table)-1))

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

and the Latex:

\newpage
\section{Individual pair returns}

\vskip 5mm

\noindent The cross-section of daily pair returns allows us to distinguish between specific and 
systematic P\&L drivers. This grid plot shows P\&L contributions from individual pairs at 10-minute
intervals. The pairs with the highest gains and losses are displayed in the center of each plot.

\vskip 10mm

\begin{center}
\Sexpr{ntable(
  backtest_ordered_pair_returns_table,
  add_rownames=FALSE,
  title=paste0(
    "Pairs contributions, ",
    "color-coded by  P\\&L ", 
    "(",dot("green"),") positive",
    "(",dot("red"),") negative"
  ),
  scale=0.75,
  alternating=FALSE,
  align=a1,
  halign="c",
  row_end="\\hline\\\\[-7mm]"
)}
\end{center}

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

removing these worked fine

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

"Short-term vs long term volatility, correlation, hedge efficiency ranks" is not really useful

image

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

neither is this one
image

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

or this one:
image

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

or this one:
image

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

or this:
image

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

or this:
image

from pairs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.