Skip to content

Commit

Permalink
feat(backtesting) include button to export csv (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
panapol-p authored Jan 22, 2022
1 parent f0d2aac commit 3cec963
Show file tree
Hide file tree
Showing 4 changed files with 368 additions and 3 deletions.
4 changes: 2 additions & 2 deletions model/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestOrder_String(t *testing.T) {
order := Order{
ID: 1,
ExchangeID: 2,
Pair: "BTCUSDT",
Pair: "BNBUSDT",
Side: SideTypeSell,
Type: OrderTypeLimit,
Status: OrderStatusTypeFilled,
Expand All @@ -20,5 +20,5 @@ func TestOrder_String(t *testing.T) {
CreatedAt: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
UpdatedAt: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
}
require.Equal(t, "[FILLED] SELL BTCUSDT | ID: 1, Type: LIMIT, 1.000000 x $10.000000 (~$10)", order.String())
require.Equal(t, "[FILLED] SELL BNBUSDT | ID: 1, Type: LIMIT, 1.000000 x $10.000000 (~$10)", order.String())
}
16 changes: 15 additions & 1 deletion plot/assets/chart.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,20 @@
top: 60px;
}

ul li {
ul {
width: 100%;
}

li {
display: inline-block;
list-style-type: none;
padding-right: 20px;
float: left;
}

li:last-child {
float: right;
}
</style>
<body>
<nav class="menu">
Expand All @@ -108,6 +116,12 @@
>
</li>
{{end}}
<li>
<a
class="btn"
href="/history?pair={{ $.pair }}"
>History</a>
</li>
</ul>
</nav>
<div id="graph"></div>
Expand Down
55 changes: 55 additions & 0 deletions plot/chart.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package plot

import (
"bytes"
"embed"
"encoding/csv"
"encoding/json"
"fmt"
"html/template"
"net/http"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -267,6 +270,18 @@ func (c *Chart) shapesByPair(pair string) []Shape {
return shapes
}

func (c *Chart) orderStringByPair(pair string) [][]string {
orders := make([][]string, 0)
for id := range c.ordersByPair[pair].Iter() {
o := c.orderByID[id]
orderString := fmt.Sprintf("%s,%s,%d,%s,%f,%f,%.2f,%s",
o.Status, o.Side, o.ID, o.Type, o.Quantity, o.Price, o.Quantity*o.Price, o.CreatedAt)
order := strings.Split(orderString, ",")
orders = append(orders, order)
}
return orders
}

func (c *Chart) handleIndex(w http.ResponseWriter, r *http.Request) {
var pairs = make([]string, 0, len(c.candles))
for pair := range c.candles {
Expand Down Expand Up @@ -326,6 +341,45 @@ func (c *Chart) handleData(w http.ResponseWriter, r *http.Request) {
}
}

func (c *Chart) handleTradingHistoryData(w http.ResponseWriter, r *http.Request) {
pair := r.URL.Query().Get("pair")
if pair == "" {
w.WriteHeader(http.StatusNotFound)
return
}

w.Header().Set("Content-type", "text/csv")
w.Header().Set("Content-Disposition", "attachment;filename=history_"+pair+".csv")
w.Header().Set("Transfer-Encoding", "chunked")

orders := c.orderStringByPair(pair)

buffer := bytes.NewBuffer(nil)
csvWriter := csv.NewWriter(buffer)
err := csvWriter.Write([]string{"status", "side", "id", "type", "quantity", "price", "total", "created_at"})
if err != nil {
log.Errorf("failed writing header file: %s", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}

err = csvWriter.WriteAll(orders)
if err != nil {
log.Errorf("failed writing data: %s", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
csvWriter.Flush()

w.WriteHeader(http.StatusOK)
_, err = w.Write(buffer.Bytes())
if err != nil {
log.Errorf("failed writing response: %s", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
}

func (c *Chart) Start() error {
http.Handle(
"/assets/",
Expand All @@ -337,6 +391,7 @@ func (c *Chart) Start() error {
fmt.Fprint(w, c.scriptContent)
})

http.HandleFunc("/history", c.handleTradingHistoryData)
http.HandleFunc("/data", c.handleData)
http.HandleFunc("/", c.handleIndex)

Expand Down
Loading

0 comments on commit 3cec963

Please sign in to comment.