Skip to content

Commit

Permalink
Swap validation changed
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyMashukov committed Jul 8, 2024
1 parent 1838083 commit c134b91
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 93 deletions.
38 changes: 25 additions & 13 deletions src/config/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,25 @@ func InitServiceContainer() Container {
SwapRepository: &swapRepository,
Formatter: &formatter,
SBSSwapFinder: &exchange.SBSSwapFinder{
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
SwapFirstAmendmentSteps: 10,
SwapSecondAmendmentSteps: 50,
SwapThirdAmendmentSteps: 250,
},
SSBSwapFinder: &exchange.SSBSwapFinder{
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
SwapFirstAmendmentSteps: 10,
SwapSecondAmendmentSteps: 50,
SwapThirdAmendmentSteps: 250,
},
SBBSwapFinder: &exchange.SBBSwapFinder{
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
SwapFirstAmendmentSteps: 10,
SwapSecondAmendmentSteps: 50,
SwapThirdAmendmentSteps: 250,
},
}

Expand Down Expand Up @@ -401,13 +410,16 @@ func InitServiceContainer() Container {
CallbackManager: &callbackManager,
SwapRepository: &swapRepository,
SwapExecutor: &exchange.SwapExecutor{
BalanceService: &balanceService,
SwapRepository: &swapRepository,
OrderRepository: &orderRepository,
Binance: exchangeApi,
Formatter: &formatter,
TimeService: &timeService,
CurrentBot: currentBot,
BalanceService: &balanceService,
SwapRepository: &swapRepository,
OrderRepository: &orderRepository,
Binance: exchangeApi,
Formatter: &formatter,
TimeService: &timeService,
CurrentBot: currentBot,
SwapFirstAmendmentSteps: 10,
SwapSecondAmendmentSteps: 50,
SwapThirdAmendmentSteps: 250,
},
SwapValidator: &swapValidator,
Formatter: &formatter,
Expand Down
13 changes: 8 additions & 5 deletions src/service/exchange/sbb_swap_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
)

type SBBSwapFinder struct {
ExchangeRepository repository.SwapPairRepositoryInterface
Formatter *utils.Formatter
ExchangeRepository repository.SwapPairRepositoryInterface
Formatter *utils.Formatter
SwapFirstAmendmentSteps float64
SwapSecondAmendmentSteps float64
SwapThirdAmendmentSteps float64
}

func (s *SBBSwapFinder) Find(asset string) model.BBSArbitrageChain {
Expand All @@ -35,7 +38,7 @@ func (s *SBBSwapFinder) Find(asset string) model.BBSArbitrageChain {

// Do not validate first order for gainer/looser and bull/bear

option0Price := option0.SellPrice - (option0.MinPrice * SwapFirstAmendmentSteps)
option0Price := option0.SellPrice - (option0.MinPrice * s.SwapFirstAmendmentSteps)
option0Price = s.Formatter.FormatPrice(option0, option0Price)
//log.Printf("[%s] formatted [3] %f -> %f", option0.Symbol, option0.SellPrice, option0Price)
sell0Quantity := initialBalance //s.Formatter.FormatQuantity(option0, initialBalance)
Expand Down Expand Up @@ -68,7 +71,7 @@ func (s *SBBSwapFinder) Find(asset string) model.BBSArbitrageChain {
continue
}

option1Price := option1.BuyPrice + (option1.MinPrice * SwapSecondAmendmentSteps)
option1Price := option1.BuyPrice + (option1.MinPrice * s.SwapSecondAmendmentSteps)
option1Price = s.Formatter.FormatPrice(option1, option1Price)
//log.Printf("[%s] formatted [4] %f -> %f", option1.Symbol, option1.BuyPrice, option1Price)
buy0Quantity := sell0.Balance //s.Formatter.FormatQuantity(option1, sell0.Balance)
Expand Down Expand Up @@ -101,7 +104,7 @@ func (s *SBBSwapFinder) Find(asset string) model.BBSArbitrageChain {
continue
}

option2Price := option2.BuyPrice + (option2.MinPrice * SwapThirdAmendmentSteps)
option2Price := option2.BuyPrice + (option2.MinPrice * s.SwapThirdAmendmentSteps)
option2Price = s.Formatter.FormatPrice(option2, option2Price)
//log.Printf("[%s] formatted [5] %f -> %f", option2.Symbol, option2.BuyPrice, option2Price)
buy1Quantity := buy0.Balance //s.Formatter.FormatQuantity(option2, buy0.Balance)
Expand Down
13 changes: 8 additions & 5 deletions src/service/exchange/sbs_swap_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
)

type SBSSwapFinder struct {
ExchangeRepository repository.SwapPairRepositoryInterface
Formatter *utils.Formatter
ExchangeRepository repository.SwapPairRepositoryInterface
Formatter *utils.Formatter
SwapFirstAmendmentSteps float64
SwapSecondAmendmentSteps float64
SwapThirdAmendmentSteps float64
}

func (s *SBSSwapFinder) Find(asset string) model.BBSArbitrageChain {
Expand All @@ -35,7 +38,7 @@ func (s *SBSSwapFinder) Find(asset string) model.BBSArbitrageChain {

// Do not validate first order for gainer/looser and bull/bear

option0Price := option0.SellPrice - (option0.MinPrice * SwapFirstAmendmentSteps)
option0Price := option0.SellPrice - (option0.MinPrice * s.SwapFirstAmendmentSteps)
option0Price = s.Formatter.FormatPrice(option0, option0Price)
//log.Printf("[%s] formatted [3] %f -> %f", option0.Symbol, option0.SellPrice, option0Price)
sell0Quantity := initialBalance //s.Formatter.FormatQuantity(option0, initialBalance)
Expand Down Expand Up @@ -68,7 +71,7 @@ func (s *SBSSwapFinder) Find(asset string) model.BBSArbitrageChain {
continue
}

option1Price := option1.BuyPrice + (option1.MinPrice * SwapSecondAmendmentSteps)
option1Price := option1.BuyPrice + (option1.MinPrice * s.SwapSecondAmendmentSteps)
option1Price = s.Formatter.FormatPrice(option1, option1Price)
//log.Printf("[%s] formatted [4] %f -> %f", option1.Symbol, option1.BuyPrice, option1Price)
buy0Quantity := sell0.Balance //s.Formatter.FormatQuantity(option1, sell0.Balance)
Expand Down Expand Up @@ -101,7 +104,7 @@ func (s *SBSSwapFinder) Find(asset string) model.BBSArbitrageChain {
continue
}

option2Price := option2.SellPrice - (option2.MinPrice * SwapThirdAmendmentSteps)
option2Price := option2.SellPrice - (option2.MinPrice * s.SwapThirdAmendmentSteps)
option2Price = s.Formatter.FormatPrice(option2, option2Price)
//log.Printf("[%s] formatted [5] %f -> %f", option2.Symbol, option2.BuyPrice, option2Price)
buy1Quantity := buy0.Balance //s.Formatter.FormatQuantity(option2, buy0.Balance)
Expand Down
13 changes: 8 additions & 5 deletions src/service/exchange/ssb_swap_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
)

type SSBSwapFinder struct {
ExchangeRepository repository.SwapPairRepositoryInterface
Formatter *utils.Formatter
ExchangeRepository repository.SwapPairRepositoryInterface
Formatter *utils.Formatter
SwapFirstAmendmentSteps float64
SwapSecondAmendmentSteps float64
SwapThirdAmendmentSteps float64
}

func (s *SSBSwapFinder) Find(asset string) model.BBSArbitrageChain {
Expand All @@ -35,7 +38,7 @@ func (s *SSBSwapFinder) Find(asset string) model.BBSArbitrageChain {

// Do not validate first order for gainer/looser and bull/bear

option0Price := option0.SellPrice - (option0.MinPrice * SwapFirstAmendmentSteps)
option0Price := option0.SellPrice - (option0.MinPrice * s.SwapFirstAmendmentSteps)
option0Price = s.Formatter.FormatPrice(option0, option0Price)
//log.Printf("[%s] formatted [1] %f -> %f", option0.Symbol, option0.BuyPrice, option0Price)
buy0Quantity := initialBalance //s.Formatter.FormatQuantity(option0, initialBalance)
Expand Down Expand Up @@ -64,7 +67,7 @@ func (s *SSBSwapFinder) Find(asset string) model.BBSArbitrageChain {
continue
}

option1Price := option1.SellPrice - (option1.MinPrice * SwapSecondAmendmentSteps)
option1Price := option1.SellPrice - (option1.MinPrice * s.SwapSecondAmendmentSteps)
option1Price = s.Formatter.FormatPrice(option1, option1Price)
//log.Printf("[%s] formatted [1] %f -> %f", option1.Symbol, option1.BuyPrice, option1Price)
buy1Quantity := buy0.Balance //s.Formatter.FormatQuantity(option1, buy0.Balance)
Expand Down Expand Up @@ -97,7 +100,7 @@ func (s *SSBSwapFinder) Find(asset string) model.BBSArbitrageChain {
continue
}

option2Price := option2.BuyPrice + (option2.MinPrice * SwapThirdAmendmentSteps)
option2Price := option2.BuyPrice + (option2.MinPrice * s.SwapThirdAmendmentSteps)
option2Price = s.Formatter.FormatPrice(option2, option2Price)
//log.Printf("[%s] formatted [2] %f -> %f", option2.Symbol, option2.BuyPrice, option2Price)
sell1Quantity := buy1.Balance //s.Formatter.FormatQuantity(option2, buy1.Balance)
Expand Down
36 changes: 21 additions & 15 deletions src/service/exchange/swap_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ import (
"time"
)

const SwapFirstAmendmentSteps = 5
const SwapSecondAmendmentSteps = 10
const SwapThirdAmendmentSteps = 15
const SwapFirstAmendmentSteps = 10
const SwapSecondAmendmentSteps = 50
const SwapThirdAmendmentSteps = 250
const SwapStepCommission = 0.002

type SwapExecutorInterface interface {
Execute(order model.Order)
}

type SwapExecutor struct {
SwapRepository repository.SwapBasicRepositoryInterface
OrderRepository repository.OrderUpdaterInterface
BalanceService BalanceServiceInterface
Binance client.ExchangeOrderAPIInterface
TimeService utils.TimeServiceInterface
Formatter *utils.Formatter
CurrentBot *model.Bot
SwapRepository repository.SwapBasicRepositoryInterface
OrderRepository repository.OrderUpdaterInterface
BalanceService BalanceServiceInterface
Binance client.ExchangeOrderAPIInterface
TimeService utils.TimeServiceInterface
Formatter *utils.Formatter
CurrentBot *model.Bot
SwapFirstAmendmentSteps float64
SwapSecondAmendmentSteps float64
SwapThirdAmendmentSteps float64
}

func (s *SwapExecutor) Execute(order model.Order) {
Expand Down Expand Up @@ -112,7 +115,7 @@ func (s *SwapExecutor) ExecuteSwapOne(swapAction *model.SwapAction, order model.
swapPrice := swapAction.SwapOnePrice
swapPair, err := s.SwapRepository.GetSwapPairBySymbol(swapAction.SwapOneSymbol)
// Price can grow before we start processing, take max price for swap
swapPrice = math.Max(swapPrice, swapPair.SellPrice-(swapPair.MinPrice*SwapFirstAmendmentSteps))
swapPrice = math.Max(swapPrice, swapPair.SellPrice-(swapPair.MinPrice*s.SwapFirstAmendmentSteps))

binanceOrder, err := s.Binance.LimitOrder(
swapAction.SwapOneSymbol,
Expand Down Expand Up @@ -310,7 +313,7 @@ func (s *SwapExecutor) ExecuteSwapTwo(

if swapChain.IsSSB() {
// Price can grow before we start processing, take max price for swap
swapPrice = math.Max(swapPrice, swapPair.SellPrice-(swapPair.MinPrice*SwapSecondAmendmentSteps))
swapPrice = math.Max(swapPrice, swapPair.SellPrice-(swapPair.MinPrice*s.SwapSecondAmendmentSteps))

binanceOrder, err = s.Binance.LimitOrder(
swapAction.SwapTwoSymbol,
Expand All @@ -323,7 +326,7 @@ func (s *SwapExecutor) ExecuteSwapTwo(

if swapChain.IsSBB() || swapChain.IsSBS() {
// Price can fall down before we start processing, take min price for swap
swapPrice = math.Min(swapPrice, swapPair.BuyPrice+(swapPair.MinPrice*SwapSecondAmendmentSteps))
swapPrice = math.Min(swapPrice, swapPair.BuyPrice+(swapPair.MinPrice*s.SwapSecondAmendmentSteps))

binanceOrder, err = s.Binance.LimitOrder(
swapAction.SwapTwoSymbol,
Expand Down Expand Up @@ -472,6 +475,7 @@ func (s *SwapExecutor) ExecuteSwapThree(
quantity = swapTwoOrder.ExecutedQty
}

// todo: check difference and validate...
if quantity > balance {
quantity = balance
}
Expand All @@ -492,7 +496,7 @@ func (s *SwapExecutor) ExecuteSwapThree(

if swapChain.IsSSB() || swapChain.IsSBB() {
// Price can fall down before we start processing, take min price for swap
swapPrice = math.Min(swapPrice, swapPair.BuyPrice+(swapPair.MinPrice*SwapThirdAmendmentSteps))
swapPrice = math.Min(swapPrice, swapPair.BuyPrice+(swapPair.MinPrice*s.SwapThirdAmendmentSteps))

binanceOrder, err = s.Binance.LimitOrder(
swapAction.SwapThreeSymbol,
Expand All @@ -505,7 +509,7 @@ func (s *SwapExecutor) ExecuteSwapThree(

if swapChain.IsSBS() {
// Price can grow before we start processing, take max price for swap
swapPrice = math.Max(swapPrice, swapPair.SellPrice-(swapPair.MinPrice*SwapThirdAmendmentSteps))
swapPrice = math.Max(swapPrice, swapPair.SellPrice-(swapPair.MinPrice*s.SwapThirdAmendmentSteps))

binanceOrder, err = s.Binance.LimitOrder(
swapAction.SwapThreeSymbol,
Expand Down Expand Up @@ -709,6 +713,7 @@ func (s *SwapExecutor) TryRollbackSwapTwo(
price = swapPair.BuyPrice + (swapPair.MinPrice * i)
quantity := swapOneOrder.CummulativeQuoteQty

// todo: check difference and validate...
if quantity > balance {
quantity = balance
}
Expand Down Expand Up @@ -833,6 +838,7 @@ func (s *SwapExecutor) TryForceSwapThree(
return err
}

// todo: check difference and validate...
if quantity > balance {
quantity = balance
}
Expand Down
4 changes: 2 additions & 2 deletions src/validator/swap_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (v *SwapValidator) validateSwap(chain model.SwapChainEntity, order model.Or
return errors.New(fmt.Sprintf("Swap [%s:%s] price is expired", entity.Operation, entity.Symbol))
}

if entity.IsBuy() && v.Formatter.ComparePercentage(entity.Price, swapCurrentKline.BuyPrice).Gte(100.10) {
if entity.IsBuy() && v.Formatter.ComparePercentage(entity.Price, swapCurrentKline.BuyPrice).Gte(100.50) {
return errors.New(fmt.Sprintf("Swap [%s:%s] price is too high", entity.Operation, entity.Symbol))
}

if entity.IsSell() && v.Formatter.ComparePercentage(entity.Price, swapCurrentKline.SellPrice).Lte(99.90) {
if entity.IsSell() && v.Formatter.ComparePercentage(entity.Price, swapCurrentKline.SellPrice).Lte(99.50) {
return errors.New(fmt.Sprintf("Swap [%s:%s] price is too low", entity.Operation, entity.Symbol))
}

Expand Down
Loading

0 comments on commit c134b91

Please sign in to comment.