ラベル ローソク足candlechart の投稿を表示しています。 すべての投稿を表示
ラベル ローソク足candlechart の投稿を表示しています。 すべての投稿を表示

2019年4月24日水曜日

How to draw horizontal and vertical lines on candlechart.



How to draw horizontal and vertical lines on candlechart.

# candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]),theme='white')
#
# draw candle chart.
#
candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]+as.xts(fas_c2+fas_c3+fas_c4+spxl_c1+spxl_c2,index(fas_shares))),theme='white')
# t <- as.xts(rep(2150000,length(weekly_pf[,1])),index(weekly_pf))
# addTA(t,on=1,legend="",lty=2,order=10)
open_v <- as.vector(weekly_pf[1,1])
close_v <- as.vector(weekly_pf[length(weekly_pf[,1]),4])
ratio <- (close_v/open_v)**(1/length(weekly_pf[,1]))
t <- as.xts(open_v*ratio**(1:length(weekly_pf[,1])),index(weekly_pf))
addTA(t,on=1,legend="",lty=2,order=10)
plot(addLines(v=seq(1,length(index(weekly_pf)),26)))
plot(addLines(h=2100000))


2018年9月20日木曜日

addTA,approx


Draw the line between given 2 positions on the candle chart.

my_draw_line_on_candle <- function(par_xts,start_val,start_date,end_val,end_date){
  len <- length(seq(as.Date(start_date),as.Date(end_date),by='weeks'))
  plot_data <- approx(seq(1,2,1),c(start_val,end_val),n=len,method='linear')$y
  tmp_xts <- as.xts(plot_data,seq(as.Date(start_date),as.Date(end_date),by='weeks'))
  addTA(tmp_xts,on=1,legend="slope")
}


> my_draw_line_on_candle(weekly_pf,2273486,"2018-01-26",2189051,"2018-09-19")
> my_draw_line_on_candle(weekly_pf,as.vector(first(weekly_pf)[,1]),index(first(weekly_pf)),2188051,"2018-09-19")
> my_draw_line_on_candle(weekly_pf,1160008,"2017-01-06",2163476,"2018-09-18")



> last(weekly_pf)[,4]/as.vector(first(weekly_pf)[,1])
              close
2018-09-19 3.450367
> length(index(weekly_pf))
[1] 247
> 3.450367**(1/247)
[1] 1.005027
> seq(1,247,1)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27
<skip>
[244] 244 245 246 247
> 1.005027**seq(1,247,1)
  [1] 1.005027 1.010079 1.015157 1.020260 1.025389 1.030544 1.035724 1.040931 1.046163 1.051423 1.056708 1.062020
 <skip>
[241] 3.348365 3.365197 3.382114 3.399116 3.416203 3.433376 3.450636
> tmp <- as.xts(634440.2*1.005027**seq(1,247,1),index(weekly_pf))
> addTA(tmp,on=1,legend="powered")

my_draw_line_on_candle(weekly_pf,as.vector(first(weekly_pf)[,1]),index(first(weekly_pf)),last(weekly_pf)[,4],index(last(weekly_pf)[,4]))
tmp <- as.xts((as.vector((last(weekly_pf)[,4]/as.vector(first(weekly_pf)[,1]))**(1/length(index(weekly_pf))))**seq(1,length(index(weekly_pf)),1))*as.vector(first(weekly_pf)[,1]),index(weekly_pf))
addTA(tmp,on=1,legend="powered")
tmp <- as.xts((as.vector((1800000/as.vector(first(weekly_pf)[,1]))**(1/length(index(weekly_pf))))**seq(1,length(index(weekly_pf)),1))*as.vector(first(weekly_pf)[,1]),index(weekly_pf))

addTA(tmp,on=1,legend="")


2018年9月15日土曜日

draw the line between given points on candle chart addTA() addLines()



> candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]+as.xts(c2+c3+c4,index(fas_shares))),theme='white')
> weekly_pf[106]
               open     high      low    close
2016-01-08 796732.6 804098.1 684234.9 684234.9
> length(index(weekly_pf))-106
[1] 140
>  tmp <- as.xts(append(rep(684234.9,106),approx(seq(1,2,1),c(684234.9,2170000),n=140,method="linear")$y),append(rep(as.Date("2016-01-08"),106),last(index(weekly_pf),n=140)))
> addTA(tmp,on=1,legend="slope")
# length(index(weekly_pf)) - 158 = 88
> tmp <- as.xts(append(rep(1189422,158),approx(seq(1,2,1),c(1189422,2170000),n=88,method="linear")$y),append(rep(as.Date("2017-01-06"),158),last(index(weekly_pf),n=88)))
> addTA(tmp,on=1,legend="slope")
> weekly_pf[158]
              open    high     low   close
2017-01-06 1160008 1189422 1160008 1189422
> weekly_pf[210]
              open    high     low   close
2018-01-05 1906271 1991231 1906271 1991231
> plot(addLines(v=c(106,158,210)))
> addTA(as.xts(approx(seq(1,2,1),c(600000,2170000),n=246,method="linear")$y,index(weekly_pf)),on=1,name='2100000')
> addTA(tmp,on=1,legend="slope")


2018年8月5日日曜日

TIPS 20180805



警告の抑止(未検証) 

options(warn=-1)
library(・・・)
# 読み込んだよーというメッセージがでなくなる

警告の抑止(検証済)  その2

        sink(file="/tmp/r.log") # console output is redirected to "/tmp/r.log"
     
        sink()  # reset to the console.

警告の抑止(未検証) その3

        options("getSymbols.yahoo.warning"=FALSE).

ヒストグラムの出力


  # kikan like kikan or "2018::06-21::2018-07-13"
  # len is a number of samples.
  # loc_x is x-axis position kikan 0 to 1.
  # loc_y is y-axis.
  # br is breaks for hist()
  # ymax is max value of y-axis.
  # xmin and xmax are for x-axis
  # color is from 1 to 9?
  hist(as.vector(last(par_xts[kikan][,highlow],n=len)),breaks=br,xlim=c(xmin,xmax),ylim=c(0,ymax),col=color)
  axis(side=2, pos=round(mean(par_xts[kikan][,highlow])),labels=F)
 #
 # par + text to output overlay messages on the graph.
 # graph's dimension data to graph_dim
 # "graph_dim[1] + graph_dim[2] *  loc_x " is 50% of horizontal loc
 #
  graph_dim <- par('usr')
  text( graph_dim[1] + graph_dim[2] *  loc_x   ,(graph_dim[4] - graph_dim[3]) * (loc_y+0) + graph_dim[3] ,paste("#",len,sep="="),adj=c(0,0))

垂直線をxtsグラフに引く


events <- xts(c("natrix","weight"),as.Date(c("2018-06-20", "2018-07-14")))
addEventLines(events, srt=90, pos=2,col=10)

   未検証だが以下もOKなはず。

addEventLines(as.xts(c("natrix","weight"),as.Date(c("2018-06-20", "2018-07-14"))), srt=90, pos=2,col=10)

多面プロットのやり方(plot when multi.panel=T and yaxis.same=F)

plot(merge(to.monthly(N225["2007-01::"])[,4],as.vector(residuals(result)/to.monthly(N225)[,4])),multi.panel=TRUE,yaxis.same=FALSE)

季節調整 case shiller 10 city composite.


CS.stl <- stl(ts(as.numeric(CS),frequency=12), s.window="periodic")
last(merge(CS,as.numeric(CS.stl$time.series[,2]),suffixes = c("","season")),n=24)
plot(last(merge(CS,as.numeric(CS.stl$time.series[,2])),n=48)[,1] - last(merge(CS,as.numeric(CS.stl$time.series[,2])),n=48)[,2])

chartSeries(
  weekly_pf,
  show_grid=TRUE,
  type="candlesticks"
)

ffmpegの使い方 再生できないファイルがある場合


再生できないファイルがある場合、mp4に変換してやるとうまくvlcで再生できることが多い。以下はcodecを変更しないでコンテナだけをmp4に変えてやる使い方。以下はaviファイルでオーディオストリームに問題があるらしい場合のエラーメッセージ。

[avi @ 0x7ffaf7800000] Could not find codec parameters for stream 1 (Audio: mp3 (mp3float) (U[0][0][0] / 0x0055), 48000 Hz, 2 channels, fltp, 128 kb/s): unspecified frame size


で、オーディオストリームはそのままにしてコンテナだけをmp4に変更する。

# no conversion just switch container. highly reliable.

ffmpeg -i <INPUT>.avi -codec:v copy -codec:a copy <OUTPUT>.mp4

OR

ffmpeg -i <INPUT>.avi -codec:a copy out.mp4

こちらは ビデオストリームをコピーする例。やはり、コンテナはmp4に変更する。

ffmpeg -i <INPUT>.mkv -vcodec copy <OUTPUT>.mp4

OR

ffmpeg -i sample_input.mkv -vcodec copy sample_output.mp4

for文とカウンタ in bash


j=0;for i in  *.mkv; do echo $i;  let ++j ; echo $j ; ffmpeg -y -i $i -vcodec copy $j.mp4; done


  • ;を書く文の最後に
  • let をつかってカウンタを加算する
  • ffmpeg -yで既存ファイルがあっても強制上書き
  • forはdo とdoneで受ける
  • 変数は初期化すること

以下と等価となる。

j=0;for i in  *.mkv; 
> do echo $i
> let ++j 
> echo $j 
> ffmpeg -y -i $i -vcodec copy $j.mp4
> done

j=0;for i in *.mp4; do let ++j; mv $j.mp4 hibike_euphonuim_S2E$j.mp4; done


  • -e 大事。これがないと\1 が使えない。(間違い。なくてもOK)
  • さいごの「2」で出現順序を指定して置換する。
  • \(<patter>\)で指定して\1で受けてる。

Zero Padding by sed

ただし、awkのgensub関数を使ったほうが良いと思う

Hibike Euphonium 1 [BD 720p]$ ls *.mp4 | awk '{ print "mv "$1" "$1}' | gsed -e 's/E\([1-9]\)\./E0\1./2'
mv hibike_euphonuim_S1E1.mp4 hibike_euphonuim_S1E01.mp4
mv hibike_euphonuim_S1E10.mp4 hibike_euphonuim_S1E10.mp4
mv hibike_euphonuim_S1E11.mp4 hibike_euphonuim_S1E11.mp4

awk のgensub関数を使えば、sedは使わなくて良いはず。第二引数の"g"はグローバル?例えば2を指定すると2番めにヒットした正規表現だけを処理してくれるらしい。(要研究)

ls *.mp4 | awk '{ print "ls "$1" "gensub("0([1-9])","1\\1","g",$1)}' 

gensub関数 in awk

gensub(regexp, replacement, how [, target])
gensubは汎用的な置換関数である。subやgsubのように 対象文字列targetから正規表現regexpに マッチする部分を検索する。subやgsubと違うのは、 関数の戻り値として置換が行われた文字列を返し、 元の文字列を変更しないという点である。

gensub を使うとこうなる。

K-On!$ ls | awk '{print "ffmpeg -i \""$0"\" K-OnS1E" gensub(/(\y.\y)/,"0\\1",1,$3)".mp4"}'

ffmpeg -i "K-ON! Ep 01 - Dissolution!.mkv" K-OnS1E01.mp4
ffmpeg -i "K-ON! Ep 02 - Instruments!.mkv" K-OnS1E02.mp4
ffmpeg -i "K-ON! Ep 03 - Special Lessons!.mkv" K-OnS1E03.mp4
ffmpeg -i "K-ON! Ep 04 - Training Camp!.mkv" K-OnS1E04.mp4
ffmpeg -i "K-ON! Ep 05 - Advisor!.mkv" K-OnS1E05.mp4
ffmpeg -i "K-ON! Ep 06 - School Festival!.mkv" K-OnS1E06.mp4
ffmpeg -i "K-ON! Ep 09 - New Club Member!.mkv" K-OnS1E09.mp4
ffmpeg -i "K-ON! Ep 10 - Another Training Camp!.mkv" K-OnS1E10.mp4
ffmpeg -i "K-ON! Ep 11 - Crisis!.mkv" K-OnS1E11.mp4
ffmpeg -i "K-ON! Ep 12 (Season Finale) - Light Music!.mkv" K-OnS1E12.mp4
ffmpeg -i "K-ON! Ep 13 (Extra) - Winter Days!.mkv" K-OnS1E13.mp4
ffmpeg -i "K-ON! Ep 14 (OVA) - Live House!.mkv" K-OnS1E14.mp4
ffmpeg -i "K-ON! Ep 7 - Christmas!.mkv" K-OnS1E07.mp4
ffmpeg -i "K-ON! Ep 8 - Freshman Reception!.mkv" K-OnS1E08.mp4

gawk 正規表現

\w
これは単語を構成する任意のキャラクタ、つまり 文字、数字、それとアンダースコアにマッチする演算子である。 これは [[:alnum:]_] の簡潔な表現とみなして良い。
\W
これは単語を構成する要素にならない任意のキャラクタにマッチする 演算子である。これは [^[:alnum:]_] の簡潔な表現とみなして良い。
\<
これは単語の先頭にある空文字列にマッチする演算子である。 例えば、/\<away/`away'にマッチするが、 `stowaway'にはマッチしない。
\>
これは単語の末尾にある空文字列にマッチする演算子である。 例えば、/stow\>/`stow'にマッチするが、 `stowaway'にはマッチしない。
\y
これは単語の先頭、あるいは末尾の空文字列とマッチする演算子である (つまり語の区切りとマッチするということである)。 例えば、`\yballs?\y'は独立した単語として `ball' にも `balls'にもマッチする。注意! 他のGNU softwareは単語の区切りに\bを使うらしい。
\B
この演算子は単語中の空文字列にマッチする。言い換えると、`\B'は二つ の単語の構成要素文字の間にある空文字列にマッチするということである。例え ば、/\Brat\B/ は`crate'にマッチする。しかし、`dirty rat' にはマッチしない。`B'は簡単にいうと`\y'の反対語である。
\( .... \) () でマッチの塊を扱う
\n  \n は n 番目の () に対応 ([a-z]*) \1*




2017年12月22日金曜日

calculate portfolio historical record after 2017-12-15


# omit getsymbols when data are up-to-dated.
getSymbols('FAS',src="yahoo")
getSymbols('SPXL',src="yahoo")

# built time series for FAS share holding.
l <- seq(as.Date("2014-01-01"),Sys.Date(),by="days")
fas_shares <- (as.xts(rep(3472*4,length(l)),l))

# sold 750 shares at 2016-12-19
l2 <- seq(as.Date("2016-12-19"),Sys.Date(),by="days")
s2 <- c(rep(0,length(l)-length(l2)),rep(750,length(l2)))
# build time series record for materialized cash after 2016-12-19
#
# 41.44 was the price at the end of 2016-12-09
c2 <- c(rep(0,length(l)-length(l2)),rep(750*41.44,length(l2)))

# sold 638 shares at 2017-12-04
l3 <- seq(as.Date("2017-12-04"),Sys.Date(),by="days")
s3 <- c(rep(0,length(l)-length(l3)),rep(638,length(l3)))
# record for materialized cash after 2017-12-04
# 69.3 was the price to be soldl at  2017-12-04.
c3 <- c(rep(0,length(l)-length(l3)),rep(638*69.3,length(l3)))
# special capital gain divident for SPXL $1.6. 10% US income tax for Firstrade
#
l4 <- seq(as.Date("2017-12-15"),Sys.Date(),by="days")
c4 <- c(rep(0,length(l)-length(l4)),rep(23412+9326,length(l4)))
# calculate time series for FAS
#
# take changes of ownership by 2016-12-19 and 2017-12-04 trade
#
fas_shares <- fas_shares - s2
fas_shares <- fas_shares - s3

# calculate time series for SPXL
spxl_shares <- as.xts(rep(21000,length(l)),l)

# candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]),theme='white')

candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]+as.xts(c2+c3+c4,index(fas_shares))),theme='white')

# store xts data into weekly_pf

weekly_pf <- to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]+as.xts(c2+c3+c4,index(fas_shares)))
colnames(weekly_pf)[1] <- 'open'
colnames(weekly_pf)[2] <- 'high'
colnames(weekly_pf)[3] <- 'low'
colnames(weekly_pf)[4] <- 'close'

Add the horizontal line if you like.

# add the horizontal line
> tmp <- as.xts(rep(2200000,length(index(weekly_pf))),index(weekly_pf))
> addTA(tmp,on=1,legend="tmp")
# add slope.
> length(index(weekly_pf))
[1] 243
> addTA(as.xts(approx(seq(1,2,1),c(600000,1100000),n=243,method="linear")$y,index(weekly_pf)),on=1,name='2100000')
# add the vertical line at "2018-01-05"
> length(weekly_pf[,1][paste(index(weekly_pf[1]),index(weekly_pf["2018"][1]),sep="::")])
> index(weekly_pf)[210]
[1] "2018-01-05"
> plot(addLines(v=210))
# calculate sequence # of the first week of each year.
>  for(i in seq(2014,2018,1)){cat(i);cat(" ");cat(length(weekly_pf[,1][paste(head(index(weekly_pf),n=1),head(index(weekly_pf[as.character(i)]),n=1),sep="::")]));cat("\n")}
2014 1
2015 53
2016 106
2017 158
2018 210 
> plot(addLines(v=c(106,158,210)))
> weekly_pf[,4][c(106,158,210)]
               close
2016-01-08  684234.9
2017-01-06 1189421.8
2018-01-05 1991231.4

2017年12月5日火曜日

calculate portfolio historical record after 2017-12-04



# retrieve price data from gogole
getSymbols('FAS',src="google")
getSymbols('SPXL',src="google")

# built time series for FAS share holding.
l <- seq(as.Date("2014-01-01"),Sys.Date(),by="days")
fas_shares <- (as.xts(rep(3472*4,length(l)),l))

# sold 750 shares at 2016-12-19
l2 <- seq(as.Date("2016-12-19"),Sys.Date(),by="days")
s2 <- c(rep(0,length(l)-length(l2)),rep(750,length(l2)))
# build time series record for materialized cash after 2016-12-19
#
# 41.44 was the price at the end of 2016-12-09
c2 <- c(rep(0,length(l)-length(l2)),rep(750*41.44,length(l2)))

# sold 638 shares at 2017-12-04
l3 <- seq(as.Date("2017-12-04"),Sys.Date(),by="days")
s3 <- c(rep(0,length(l)-length(l3)),rep(638,length(l3)))
# record for materialized cash after 2017-12-04
# 69.3 was the price to be soldl at  2017-12-04.
c3 <- c(rep(0,length(l)-length(l3)),rep(638*69.3,length(l3)))
# calculate time series for FAS
#
# take changes of ownership by 2016-12-19 and 2017-12-04 trade
#
fas_shares <- fas_shares - s2
fas_shares <- fas_shares - s3

# calculate time series for SPXL
spxl_shares <- as.xts(rep(21000,length(l)),l)

candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]),theme='white')

candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]+as.xts(c2+c3,index(fas_shares))),theme='white')

fas_shares * apply.monthly(FAS,last)[,4]
spxl_shares * apply.monthly(SPXL,last)[,4]

fas_shares * apply.monthly(FAS,last)[,4] +spxl_shares * apply.monthly(SPXL,last)[,4] #monthly historcal price

# weekly closed price chart
plot(fas_shares * apply.weekly(FAS,last)[,4] +spxl_shares * apply.weekly(SPXL,last)[,4])

# store weekly balance
weekly_pf <- fas_shares * apply.weekly(FAS,last)[,4] +spxl_shares * apply.weekly(SPXL,last)[,4]

2017年9月26日火曜日

Calculate Portfolio market value. and draw the graph.


getSymbols('FAS',src="google")
getSymbols('SPXL',src="google")

l <- seq(as.Date("2014-01-01"),Sys.Date(),by="days")
fas_shares <- (as.xts(rep(3472*4,length(l)),l))

l2 <- seq(as.Date("2016-12-19"),Sys.Date(),by="days")
s2 <- c(rep(0,length(l)-length(l2)),rep(750,length(l2)))

fas_shares <- fas_shares - s2
spxl_shares <- as.xts(rep(21000,length(l)),l)


fas_shares * apply.monthly(FAS,last)[,4]
spxl_shares * apply.monthly(SPXL,last)[,4]


fas_shares * apply.monthly(FAS,last)[,4] +spxl_shares * apply.monthly(SPXL,last)[,4] #monthly historcal price


# weekly closed price chart
plot(fas_shares * apply.weekly(FAS,last)[,4] +spxl_shares * apply.weekly(SPXL,last)[,4])
# weekly candle chart
candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]),theme='white')

# store weekly balance
weekly_pf <- fas_shares * apply.weekly(FAS,last)[,4] +spxl_shares * apply.weekly(SPXL,last)[,4]