Skip to content

Commit

Permalink
Bug-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyMashukov committed Mar 13, 2024
1 parent 09879a9 commit eb52389
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 44 deletions.
9 changes: 8 additions & 1 deletion src/controller/trade_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,14 @@ func (t *TradeController) GetTradeStackAction(w http.ResponseWriter, req *http.R
return
}

stack := t.TradeStack.GetTradeStack(false, false, false, false, false, true)
stack := t.TradeStack.GetTradeStack(exchange.TradeStackParams{
SkipLocked: false,
SkipDisabled: false,
BalanceFilter: false,
SkipPending: false,
WithValidPrice: false,
AttachDecisions: true,
})

encodedRes, _ := json.Marshal(stack)
fmt.Fprintf(w, string(encodedRes))
Expand Down
25 changes: 14 additions & 11 deletions src/service/exchange/swap_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ func (s SwapUpdater) UpdateSwapPair(swapPair model.SwapPair) {
orderDepth := s.ExchangeRepository.GetDepth(swapPair.Symbol)
// save support + resistance levels
if len(orderDepth.Asks) >= 10 && len(orderDepth.Bids) >= 10 {
kline := s.Binance.GetKLinesCached(swapPair.Symbol, "1d", 1)[0]
swapPair.DailyPercent = s.Formatter.ToFixed(
(s.Formatter.ComparePercentage(kline.Open, kline.Close) - 100.00).Value(),
2,
)
kLines := s.Binance.GetKLinesCached(swapPair.Symbol, "1d", 1)
if len(kLines) > 0 {
kline := kLines[0]
swapPair.DailyPercent = s.Formatter.ToFixed(
(s.Formatter.ComparePercentage(kline.Open, kline.Close) - 100.00).Value(),
2,
)

swapPair.BuyPrice = orderDepth.Bids[0][0].Value
swapPair.SellPrice = orderDepth.Asks[0][0].Value
swapPair.SellVolume = s.Formatter.ToFixed(orderDepth.GetAskVolume(), 2)
swapPair.BuyVolume = s.Formatter.ToFixed(orderDepth.GetBidVolume(), 2)
swapPair.PriceTimestamp = time.Now().Unix()
_ = s.ExchangeRepository.UpdateSwapPair(swapPair)
swapPair.BuyPrice = orderDepth.Bids[0][0].Value
swapPair.SellPrice = orderDepth.Asks[0][0].Value
swapPair.SellVolume = s.Formatter.ToFixed(orderDepth.GetAskVolume(), 2)
swapPair.BuyVolume = s.Formatter.ToFixed(orderDepth.GetBidVolume(), 2)
swapPair.PriceTimestamp = time.Now().Unix()
_ = s.ExchangeRepository.UpdateSwapPair(swapPair)
}
}
}
50 changes: 29 additions & 21 deletions src/service/exchange/trade_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ type TradeStack struct {
Ctx *context.Context
}

type TradeStackParams struct {
SkipDisabled bool
SkipLocked bool
BalanceFilter bool
SkipPending bool
WithValidPrice bool
AttachDecisions bool
}

func (t *TradeStack) CanBuy(limit model.TradeLimit) bool {
// Allow to process existing order
binanceOrder := t.OrderRepository.GetBinanceOrder(limit.Symbol, "BUY")
Expand All @@ -43,7 +52,14 @@ func (t *TradeStack) CanBuy(limit model.TradeLimit) bool {
return true
}

result := t.GetTradeStack(true, true, true, true, true, false)
result := t.GetTradeStack(TradeStackParams{
SkipLocked: true,
SkipDisabled: true,
BalanceFilter: true,
SkipPending: true,
WithValidPrice: true,
AttachDecisions: false,
})

if len(result) == 0 {
return false
Expand All @@ -52,7 +68,7 @@ func (t *TradeStack) CanBuy(limit model.TradeLimit) bool {
return limit.Symbol == result[0].Symbol
}

func (t *TradeStack) GetTradeStack(skipDisabled bool, skipLocked bool, balanceFilter bool, skipPending bool, withValidPrice bool, attachDecisions bool) []model.TradeStackItem {
func (t *TradeStack) GetTradeStack(params TradeStackParams) []model.TradeStackItem {
balanceUsdt, err := t.BalanceService.GetAssetBalance("USDT", true)
stack := make([]model.TradeStackItem, 0)

Expand All @@ -65,17 +81,13 @@ func (t *TradeStack) GetTradeStack(skipDisabled bool, skipLocked bool, balanceFi
count := 0

for index, tradeLimit := range t.ExchangeRepository.GetTradeLimits() {
go func(tradeLimit model.TradeLimit, index int64) {
go func(limit model.TradeLimit, index int64, params TradeStackParams) {
resultChannel <- t.ProcessItem(
index,
tradeLimit,
skipDisabled,
skipLocked,
withValidPrice,
skipPending,
attachDecisions,
limit,
params,
)
}(tradeLimit, int64(index))
}(tradeLimit, int64(index), params)
count++
}

Expand Down Expand Up @@ -143,7 +155,7 @@ func (t *TradeStack) GetTradeStack(skipDisabled bool, skipLocked bool, balanceFi
}
}

if !balanceFilter {
if !params.BalanceFilter {
for _, stackItem := range impossible {
balanceUsdt -= stackItem.BudgetUsdt

Expand Down Expand Up @@ -173,21 +185,17 @@ func (t *TradeStack) GetTradeStack(skipDisabled bool, skipLocked bool, balanceFi
func (t *TradeStack) ProcessItem(
index int64,
tradeLimit model.TradeLimit,
skipDisabled bool,
skipLocked bool,
withValidPrice bool,
skipPending bool,
attachDecisions bool,
params TradeStackParams,
) *model.TradeStackItem {
isEnabled := tradeLimit.IsEnabled

if !isEnabled && skipDisabled {
if !isEnabled && params.SkipDisabled {
return nil
}

isBuyLocked := t.OrderRepository.HasBuyLock(tradeLimit.Symbol)

if skipLocked && isBuyLocked {
if params.SkipLocked && isBuyLocked {
return nil
}

Expand All @@ -203,18 +211,18 @@ func (t *TradeStack) ProcessItem(
lastPrice = lastKLine.Close
}

if !isPriceValid && withValidPrice {
if !isPriceValid && params.WithValidPrice {
return nil
}

// Skip if order has already opened
binanceOrder := t.OrderRepository.GetBinanceOrder(tradeLimit.Symbol, "BUY")
if binanceOrder != nil && skipPending {
if binanceOrder != nil && params.SkipPending {
return nil
}

decisions := make([]model.Decision, 0)
if attachDecisions {
if params.AttachDecisions {
decisions = t.ExchangeRepository.GetDecisions(tradeLimit.Symbol)
}

Expand Down
14 changes: 8 additions & 6 deletions src/service/ml/python_ml_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type PythonMLBridge struct {
ExchangeRepository *repository.ExchangeRepository
SwapRepository *repository.SwapRepository
TimeService *utils.TimeHelper
Mutex sync.RWMutex
Mutex *sync.RWMutex
LearnLock *sync.RWMutex
RDB *redis.Client
Ctx *context.Context
CurrentBot *model.Bot
Expand Down Expand Up @@ -61,24 +62,25 @@ from sklearn.metrics import mean_squared_error
pyCodeC := C.CString(pyCode)
defer C.free(unsafe.Pointer(pyCodeC))
C.PyRun_SimpleString(pyCodeC)
p.Mutex = sync.RWMutex{}
p.Mutex = &sync.RWMutex{}
p.LearnLock = &sync.RWMutex{}
}

func (p *PythonMLBridge) Finalize() {
C.Py_Finalize()
}

func (p *PythonMLBridge) IsLearning() bool {
p.Mutex.Lock()
p.LearnLock.Lock()
isLearning := p.Learning
p.Mutex.Unlock()
p.LearnLock.Unlock()
return isLearning
}

func (p *PythonMLBridge) setLearning(value bool) {
p.Mutex.Lock()
p.LearnLock.Lock()
p.Learning = value
p.Mutex.Unlock()
p.LearnLock.Unlock()
}

func (p *PythonMLBridge) getPythonCode(symbol string, datasetPath string) string {
Expand Down
7 changes: 2 additions & 5 deletions src/service/strategy/market_trade_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ func (m *MarketTradeListener) ListenAll() {
kLine := <-klineChannel
m.ExchangeRepository.AddKLine(kLine)

go func(channel chan string, symbol string) {
go func(symbol string) {
predictChannel <- symbol
}(predictChannel, kLine.Symbol)
}(kLine.Symbol)

m.ExchangeRepository.SetDecision(m.BaseKLineStrategy.Decide(kLine), kLine.Symbol)
m.ExchangeRepository.SetDecision(m.OrderBasedStrategy.Decide(kLine), kLine.Symbol)
Expand Down Expand Up @@ -132,9 +132,6 @@ func (m *MarketTradeListener) ListenAll() {
smaDecision := m.SmaTradeStrategy.Decide(tradeEvent.Trade)
m.ExchangeRepository.SetDecision(smaDecision, tradeEvent.Trade.Symbol)

go func(channel chan string, symbol string) {
predictChannel <- symbol
}(predictChannel, tradeEvent.Trade.Symbol)
break
case strings.Contains(string(message), "kline"):
var event model.KlineEvent
Expand Down

0 comments on commit eb52389

Please sign in to comment.