# HG changeset patch
# User pyon@macmini
# Date 1557661878 -32400
# Node ID a55c5a0cbd046112b2f77f8c83a50d3f9e1bf0b4
# Parent a273d4792e48912788f910e446bb5104f5c0df50
add fx.
diff -r a273d4792e48 -r a55c5a0cbd04 src/fx/chart.svg
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fx/chart.svg Sun May 12 20:51:18 2019 +0900
@@ -0,0 +1,823 @@
+
diff -r a273d4792e48 -r a55c5a0cbd04 src/fx/fx.go
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fx/fx.go Sun May 12 20:51:18 2019 +0900
@@ -0,0 +1,203 @@
+package main
+
+import (
+ "bytes"
+ "encoding/csv"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "math"
+ "strconv"
+ "strings"
+)
+
+const debug = false
+
+func main() {
+ n, m := 20, 14
+ var x, xl, xh []float64
+ var date []string
+
+ // Read Exchange-data
+ content, err := ioutil.ReadFile("/Users/takayuki/Downloads/USDJPY.csv")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ r := csv.NewReader(bytes.NewReader(content))
+ for {
+ record, err := r.Read()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ f, err := strconv.ParseFloat(record[4], 64)
+ if err != nil {
+ continue
+ }
+ x = append(x, f)
+
+ f, err = strconv.ParseFloat(record[3], 64)
+ xl = append(xl, f)
+
+ f, err = strconv.ParseFloat(record[2], 64)
+ xh = append(xh, f)
+
+ date = append(date, strings.ReplaceAll(record[0], "/", "-"))
+ }
+
+ // Calc Moving-Average
+ xa := make([]float64, len(x))
+ for i := n; i < len(x); i++ {
+ for j := 0; j < n; j++ {
+ xa[i] += x[i-j]
+ }
+ xa[i] /= float64(n)
+ }
+
+ // Calc Variance & SD
+ v := make([]float64, len(x))
+ sd := make([]float64, len(x))
+ for i := n; i < len(x); i++ {
+ // variance
+ for j := 0; j < n; j++ {
+ v[i] += (x[i-j] - xa[i-j]) * (x[i-j] - xa[i-j])
+ }
+ v[i] /= float64(n)
+
+ // standard deviation
+ sd[i] = math.Sqrt(v[i])
+ }
+
+ // Calc Bollinger-Bands
+ bb1 := make([]float64, len(x))
+ bb2 := make([]float64, len(x))
+ for i := n * 2; i < len(x); i++ {
+ bb1[i], bb2[i] = x[i] - sd[i], x[i] + sd[i]
+ }
+
+ // Calc RSI
+ rsi := make([]float64, len(x))
+ for i := m; i < len(x); i++ {
+ var ru, rd float64
+ for j := 0; j < m; j++ {
+ z := x[i-j-1] - x[i-j]
+ if z > 0 {
+ ru += z
+ } else {
+ rd += -z
+ }
+ }
+ rsi[i] = rd / (rd + ru) * 100
+ }
+
+ // Make Graph
+ // [chart.svg]
+ w, h := 800, 600
+
+ term := 200
+ min, max := 200.0, 1.0
+ for i := len(x) - term; i < len(x); i++ {
+ min = math.Min(min, x[i])
+ max = math.Max(max, x[i])
+ min = math.Min(min, bb1[i])
+ max = math.Max(max, bb2[i])
+ }
+ sx := float64(w) / float64(term)
+ sy := float64(h) / (max - min)
+
+ svg := fmt.Sprintf("\n")
+ if err := ioutil.WriteFile("chart.svg", []byte(svg), 0644); err != nil {
+ log.Fatal(err)
+ }
+
+ // [rsi.svg]
+ w, h = w, 150
+ sy = float64(h) / 100
+ svg = fmt.Sprintf("\n")
+ if err := ioutil.WriteFile("rsi.svg", []byte(svg), 0644); err != nil {
+ log.Fatal(err)
+ }
+
+ // Make Html
+ html := "
"
+ html += "
Daily Chart
"
+ html += ""
+ html += "
RSI
"
+ html += ""
+ html += ""
+ if err := ioutil.WriteFile("fx.html", []byte(html), 0644); err != nil {
+ log.Fatal(err)
+ }
+
+ // Print Data
+ if false {
+ fmt.Println("[j] n x bb1 bb2 rsi | n x bb1 bb2")
+ fmt.Println("---")
+ for i, j := len(x) - term, 0; i < len(x); i++ {
+ fmt.Printf("[%04d] %04d %5.2f %5.2f %5.2f %3.1f | ", j, i, x[i], bb1[i], bb2[i], rsi[i])
+ fmt.Printf("%04.0f %5.2f %5.2f %5.2f ", float64(i) * sx, (x[i] - min) * sy, (bb1[i] - min) * sy, (bb2[i] - min) * sy)
+ fmt.Println()
+ j++
+ }
+ fmt.Println("---")
+ fmt.Printf("min=%5.2f max=%5.2f\n", min, max)
+ }
+}
+
+func make_line(h, x1 int, y1 float64, x2 int, y2 float64, os, sx, sy float64, c string, sw float64) string {
+ x1, y1 = int(float64(x1) * sx), (y1 - os) * sy
+ x2, y2 = int(float64(x2) * sx), (y2 - os) * sy
+ y1, y2 = float64(h) - y1, float64(h) - y2
+ return fmt.Sprintf("\n", x1, int(y1), x2, int(y2), c, sw)
+}
+
+func make_label(h, x, y int, os, sx, sy float64, label string) string {
+ x = int(float64(x) * sx)
+ y = int((float64(y) - os) * sy)
+ y = h - y
+ return fmt.Sprintf("%s", x, y, label)
+}
+
diff -r a273d4792e48 -r a55c5a0cbd04 src/fx/fx.html
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fx/fx.html Sun May 12 20:51:18 2019 +0900
@@ -0,0 +1,1 @@
+
Daily Chart
RSI
\ No newline at end of file
diff -r a273d4792e48 -r a55c5a0cbd04 src/fx/rsi.svg
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fx/rsi.svg Sun May 12 20:51:18 2019 +0900
@@ -0,0 +1,215 @@
+