-
Notifications
You must be signed in to change notification settings - Fork 1
/
sell.go
51 lines (42 loc) · 1.02 KB
/
sell.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
package shresiesbot
import (
"context"
"errors"
"github.com/deividfortuna/sharesies"
)
var orderSellBuyType = "order_cost_sell"
type SellOrder struct {
Id string
Reference string
Shares float64
}
func (b *SharesiesBot) sellOrders(ctx context.Context, orders []SellOrder) error {
_, err := b.client.Authenticate(ctx, &sharesies.Credentials{
Username: b.config.Sharesies.Username,
Password: b.config.Sharesies.Password,
})
if err != nil {
b.logger.Println("Failed to authenticated Sharesies")
return err
}
var sellOrders []*sharesies.CostSellResponse
for _, v := range orders {
b.logger.Println("Checking order price for " + v.Reference)
cs, err := b.client.CostSell(ctx, v.Id, v.Shares)
if err != nil {
return err
}
if cs.Type != orderSellBuyType {
return errors.New(cs.Type)
}
sellOrders = append(sellOrders, cs)
}
for _, so := range sellOrders {
_, err = b.client.Sell(ctx, so)
if err != nil {
return err
}
}
b.logger.Println("Stonks! We sold everything")
return nil
}