-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy_buy_everyday_if_lower_than.go
54 lines (42 loc) · 1.58 KB
/
strategy_buy_everyday_if_lower_than.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
package order
import (
"strconv"
"github.com/go-co-op/gocron"
krxcode "github.com/sunglim/go-korea-stock-code/code"
"github.com/sunglim/systemtrading/log"
"github.com/sunglim/systemtrading/order/koreainvestment"
ki "github.com/sunglim/systemtrading/pkg/koreainvestment"
)
// Buy a stock if the price is lower than ...
func buyLowerOrder(codePrices []StrategryOrder, logger *log.Logger) {
for _, codePrice := range codePrices {
currentPrice := koreainvestment.ApiInqueryPrice{}.Call(codePrice.Code)
currentPriceInt, _ := strconv.Atoi(currentPrice)
if currentPriceInt > codePrice.Price {
continue
}
logger.Println("name", krxcode.CodeToName(codePrice.Code), "orderPrice",
codePrice.Price, "currentPrice", currentPriceInt)
BuyLowerOrderCash(codePrice, logger)
}
}
func BuyLowerOrderCash(code StrategryOrder, logger *log.Logger) {
response := ki.CreateApiOrderCash(code.Code,
koreainvestment.GetDefaultKoreaInvestmentInstance().GetCredential(),
koreainvestment.GetDefaultAccount(),
koreainvestment.GetDefaultKoreaInvestmentInstance().GetBearerAccessToken()).Call()
if !response.IsSuccess() {
logger.Printf("orde failed with error[%s]", response.Msg1)
return
}
logger.Printf("An order is successfully sent [%v]", response)
}
type StrategryOrder = CodeAndQuantityAndPrice
func StrategryBuyEveryDayIfLowerThan(buytime string, codePrices []StrategryOrder) *gocron.Scheduler {
logger := log.Default()
logger.SetPrefix("[Buy if average is lower than] ")
s := NewSeoulScheduler().Every(1).Day().At(buytime)
s.Do(buyLowerOrder, codePrices, logger)
s.StartAsync()
return s
}