func (db *DB) RunMatchingBestPricesWithinTransaction(pair *match.Pair, tx *sql.Tx) (err error) {
	// First get all the sell prices so we have something to iterate through and match
	getSellPricesQuery := fmt.Sprintf("SELECT DISTINCT price FROM %s WHERE side='%s' ORDER BY price ASC;", pair.String(), "sell")
	sellPriceRows, err := tx.Query(getSellPricesQuery)
	if err != nil {
		return
	}

	var sellPrices []float64

	for sellPriceRows.Next() {
		var sellPrice float64
		if err = sellPriceRows.Scan(&sellPrice); err != nil {
			return
		}

		sellPrices = append(sellPrices, sellPrice)
	}
	if err = sellPriceRows.Close(); err != nil {
		return
	}

	// First get all the buy prices so we have something to iterate through and match
	getBuyPricesQuery := fmt.Sprintf("SELECT DISTINCT price FROM %s WHERE side='%s' ORDER BY price DESC;", pair.String(), "buy")
	buyPriceRows, err := tx.Query(getBuyPricesQuery)
	if err != nil {
		return
	}

	var buyPrices []float64

	for buyPriceRows.Next() {
		var buyPrice float64
		if err = buyPriceRows.Scan(&buyPrice); err != nil {
			return
		}

		buyPrices = append(buyPrices, buyPrice)
	}
	if err = buyPriceRows.Close(); err != nil {
		return
	}

	// this is a really really basic / naive algorithm that should run matching for the "best price"
	if shouldMatch(buyPrices, sellPrices) {
		if buyPrices[0] == sellPrices[0] {
			// If they're the same then great we can just match all this stuff at price
			if err = db.RunMatchingForPriceWithinTransaction(pair, buyPrices[0], tx); err != nil {
				return
			}
		} else {
			// otherwise we have to figure out the crossed trades
			// this situation puts us at a point where the lowest sell price is less than the highest buy price
			// if the lower sell order was placed before the high buy order then the trade gets executed at the sell order price
			// if the higher buy order was placed before the low sell order then the trade gets executed at the buy order price

			if err = db.RunMatchingCrossedPricesWithinTransaction(pair, buyPrices[0], sellPrices[0], tx); err != nil {
				return
			}
		}
	}

	return
}
// RunMatchingBestPricesWithinTransaction matches the best prices within a transaction.
RunMatchingBestPricesWithinTransaction matches the best prices within a transaction.
// RunMatchingBestPricesWithinTransaction matches the best prices within a transaction .
func (db *DB) RunMatchingBestPricesWithinTransaction(pair *match.Pair, tx *sql.Tx) (err error) {
	getSellPricesQuery := fmt.Sprintf("SELECT DISTINCT price FROM %s WHERE side='%s' ORDER BY price ASC;", pair.String(), "sell")
	sellPriceRows, err := tx.Query(getSellPricesQuery)
	if err != nil {
		return
	}
	var sellPrices []float64
	for sellPriceRows.Next() {
		var sellPrice float64
		if err = sellPriceRows.Scan(&sellPrice); err != nil {
			return
		}
		sellPrices = append(sellPrices, sellPrice)
	}
	if err = sellPriceRows.Close(); err != nil {
		return
	}
	getBuyPricesQuery := fmt.Sprintf("SELECT DISTINCT price FROM %s WHERE side='%s' ORDER BY price DESC;", pair.String(), "buy")
	buyPriceRows, err := tx.Query(getBuyPricesQuery)
	if err != nil {
		return
	}
	var buyPrices []float64
	for buyPriceRows.Next() {
		var buyPrice float64
		if err = buyPriceRows.Scan(&buyPrice); err != nil {
			return
		}
		buyPrices = append(buyPrices, buyPrice)
	}
	if err = buyPriceRows.Close(); err != nil {
		return
	}
	if shouldMatch(buyPrices, sellPrices) {
		if buyPrices[0] == sellPrices[0] {
			if err = db.RunMatchingForPriceWithinTransaction(pair, buyPrices[0], tx); err != nil {
				return
			}
		} else {
			if err = db.RunMatchingCrossedPricesWithinTransaction(pair, buyPrices[0], sellPrices[0], tx); err != nil {
				return
			}
		}
	}
	return
}
func ( db * DB ) RunMatchingBestPricesWithinTransaction ( pair * match . Pair , tx * sql . Tx ) ( err error ) { getSellPricesQuery := fmt . Sprintf ( " " , pair . String ( ) , " " ) sellPriceRows , err := tx . Query ( getSellPricesQuery ) if err != nil { return } var sellPrices [ ] float64 for sellPriceRows . Next ( ) { var sellPrice float64 if err = sellPriceRows . Scan ( & sellPrice ) ; err != nil { return } sellPrices = append ( sellPrices , sellPrice ) } if err = sellPriceRows . Close ( ) ; err != nil { return } getBuyPricesQuery := fmt . Sprintf ( " " , pair . String ( ) , " " ) buyPriceRows , err := tx . Query ( getBuyPricesQuery ) if err != nil { return } var buyPrices [ ] float64 for buyPriceRows . Next ( ) { var buyPrice float64 if err = buyPriceRows . Scan ( & buyPrice ) ; err != nil { return } buyPrices = append ( buyPrices , buyPrice ) } if err = buyPriceRows . Close ( ) ; err != nil { return } if shouldMatch ( buyPrices , sellPrices ) { if buyPrices [ 0 ] == sellPrices [ 0 ] { if err = db . RunMatchingForPriceWithinTransaction ( pair , buyPrices [ 0 ] , tx ) ; err != nil { return } } else { if err = db . RunMatchingCrossedPricesWithinTransaction ( pair , buyPrices [ 0 ] , sellPrices [ 0 ] , tx ) ; err != nil { return } } } return }
RunMatchingBestPricesWithinTransaction matches the best prices within a transaction.
RunMatchingBestPricesWithinTransaction matches the best prices within a transaction .
// First get all the sell prices so we have something to iterate through and match // First get all the buy prices so we have something to iterate through and match // this is a really really basic / naive algorithm that should run matching for the "best price" // If they're the same then great we can just match all this stuff at price // otherwise we have to figure out the crossed trades // this situation puts us at a point where the lowest sell price is less than the highest buy price // if the lower sell order was placed before the high buy order then the trade gets executed at the sell order price // if the higher buy order was placed before the low sell order then the trade gets executed at the buy order price