-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade.go
179 lines (147 loc) · 4.22 KB
/
trade.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"log"
"github.com/Sagleft/uexchange-go"
"github.com/fatih/color"
)
func (b *bot) resetLap() {
b.Lap = lap{}
}
func (b *bot) getIntervalDepositPercent(currentPrice float64) float64 {
if b.isFirstInterval() {
return b.Config.IntervalDepositMaxPercent
}
intervalMaxDeposit := b.Config.Deposit * b.Config.IntervalDepositMaxPercent / 100
minPrice := b.Lap.LastPriceLevel * (1 - b.Config.IntervalDepositMaxPercent/2)
maxPrice := b.Lap.LastPriceLevel * (1 + b.Config.IntervalDepositMaxPercent/2)
if currentPrice < minPrice {
return 100
}
if currentPrice > maxPrice {
return 0
}
return (1 - (currentPrice-minPrice)/intervalMaxDeposit) * b.Config.IntervalDepositMaxPercent
}
func (b *bot) updateDepositUsed(orderData uexchange.OrderData) {
log.Printf("add lap coins qty: %v\n", orderData.Amount)
log.Printf("add lap depo spent: %v\n", orderData.ExecutedValue)
b.Lap.CoinsQty += orderData.Amount
b.Lap.DepositSpent += orderData.ExecutedValue
}
func (b *bot) incrementIntervalNumber() {
b.Lap.IntervalNumber++
}
func (b *bot) handleInterval() error {
debug(
"handle interval %v..\n",
b.Lap.IntervalNumber,
)
defer b.incrementIntervalNumber()
// calc market order
debug("calc market order..")
defOrder, err := b.calcMarketOrder()
if err != nil {
return fmt.Errorf("calc market order: %w", err)
}
debug("check order: %s", defOrder.ToString())
orderIsOK, err := b.checkMarketOrder(defOrder)
if err != nil {
return fmt.Errorf("check market order before place: %w", err)
}
if !orderIsOK {
return nil
}
if b.isTPPlaced() {
debug("cancel old TP order: %v\n", b.Lap.TPOrderID)
if err := b.Client.Cancel(b.Lap.TPOrderID); err != nil {
return fmt.Errorf("cancel old TP order: %w", err)
}
}
// market buy
log.Printf("send order: %s\n", defOrder.ToString())
orderData, err := b.sendOrder(defOrder)
if err != nil {
return fmt.Errorf("send market order: %w", err)
}
debug("order data: %s\n", toJSON(orderData))
success("order placed: %v", orderData.OrderID)
b.updateDepositUsed(orderData)
b.setLastPriceLevel(defOrder.Price)
// place TP
log.Println("calc TP order..")
tpOrder := b.calcTPOrder(orderData.Price)
debug("check order: %s", tpOrder.ToString())
orderIsOK, err = b.checkTPOrder(tpOrder)
if err != nil {
return fmt.Errorf("check TP order before place: %w", err)
}
if !orderIsOK {
return nil
}
log.Printf("place TP order: %s\n", tpOrder.ToString())
tpOrderID, err := b.sendTPOrder(tpOrder)
if err != nil {
return fmt.Errorf("send TP order: %w", err)
}
success("TP placed: %v", tpOrderID)
// update TP order ID
b.Lap.TPOrderID = tpOrderID
return nil
}
func (b *bot) getTPExecutedState() (orderExecutedState, error) {
tpOrderData, err := b.getOrderData(b.Lap.TPOrderID)
if err != nil {
return orderExecutedState{}, err
}
return orderExecutedState{
IsFullExecuted: tpOrderData.ExecutedValue == tpOrderData.OriginalValue,
IsPartiallyExecuted: tpOrderData.ExecutedValue > 0,
Data: tpOrderData,
}, nil
}
func (b *bot) getLapProfit(tpState orderExecutedState) (float64, error) {
return tpState.Data.ExecutedValue - b.Lap.DepositSpent, nil
}
func (b *bot) markTPPartiallyExecuted() {
b.Lap.TPAlreadyPartiallyExecuted = true
}
func (b *bot) checkExchange() error {
if !b.HandleIntervalLock.TryLock() {
return nil // prevent parallel run
}
defer b.HandleIntervalLock.Unlock()
if b.Lap.IntervalNumber == 0 {
if err := b.handleInterval(); err != nil {
return fmt.Errorf("handle interval: %w", err)
}
return nil
}
if b.isTPPlaced() {
tpState, err := b.getTPExecutedState()
if err != nil {
return fmt.Errorf("get TP executed state: %s", err)
}
if tpState.IsPartiallyExecuted && !b.Lap.TPAlreadyPartiallyExecuted {
b.markTPPartiallyExecuted()
if tpState.IsFullExecuted {
color.HiYellow("TP was executed")
} else {
color.HiYellow("TP was partially executed")
}
}
if tpState.IsFullExecuted {
lapProfit, err := b.getLapProfit(tpState)
if err != nil {
return fmt.Errorf("get lap profit: %w", err)
}
success("💰 Lap finished! Profit: %v", lapProfit)
b.resetLap()
return nil
}
}
if err := b.handleInterval(); err != nil {
return fmt.Errorf("handle interval: %w", err)
}
return nil
}