forked from mamady/gobacktest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.go
74 lines (62 loc) · 1.94 KB
/
execution.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package backtest
import (
// "fmt"
"github.com/dirkolbrich/gobacktest/pkg/utils"
)
// ExecutionHandler is the basic interface for executing orders
type ExecutionHandler interface {
ExecuteOrder(OrderEvent, DataHandler) (*Fill, error)
}
// Exchange is a basic execution handler implementation
type Exchange struct {
Symbol string
ExchangeFee float64
}
// ExecuteOrder executes an order event
func (e *Exchange) ExecuteOrder(order OrderEvent, data DataHandler) (*Fill, error) {
// fetch latest known data event for the symbol
latest := data.Latest(order.GetSymbol())
// simple implementation, creates a direct fill from the order
// based on the last known data price
f := &Fill{
Event: Event{Timestamp: order.GetTime(), Symbol: order.GetSymbol()},
Exchange: e.Symbol,
Qty: order.GetQty(),
Price: latest.LatestPrice(), // last price from data event
}
switch order.GetDirection() {
case "buy":
f.Direction = "BOT"
case "sell":
f.Direction = "SLD"
}
f.Commission = e.calculateCommission(float64(f.Qty), f.Price)
f.ExchangeFee = e.calculateExchangeFee()
f.Cost = e.calculateCost(f.Commission, f.ExchangeFee)
return f, nil
}
// calculateComission() calculates the commission for a stock trade
//
// based on the conditions of IngDiba
// see https://www.ing-diba.de/wertpapiere/direkt-depot/konditionen
func (e *Exchange) calculateCommission(qty, price float64) float64 {
var comMin = 9.90
var comMax = 59.90
var comRate = 0.0025 // in percent
switch {
case (qty * price * comRate) < comMin:
return comMin
case (qty * price * comRate) > comMax:
return comMax
default:
return utils.Round(qty*price*comRate, 4)
}
}
// calculateExchangeFee() calculates the exchange fee for a stock trade
func (e *Exchange) calculateExchangeFee() float64 {
return e.ExchangeFee
}
// calculateCost() calculates the total cost for a stock trade
func (e *Exchange) calculateCost(commission, fee float64) float64 {
return commission + fee
}