Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(BTC Markets): Wrapper SubmitOrder parameter order fix & IsOrderPlaced condition correction #394

Merged
merged 20 commits into from Dec 16, 2019

Conversation

xtda
Copy link
Contributor

@xtda xtda commented Dec 10, 2019

PR Description

Corrects parameter order for BTCMarkets SubmitOrder() wrapper to NewOrder() Method
(OrderType & OrderSide were incorrect)

Also fixes the condition check on IsOrderPlaced in order manager Submit() Method that will cause it to return order cannot be placed error if IsOrderPlaced is true

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration and
also consider improving test coverage whilst working on a certain feature or package.

  • go test ./... -race
  • golangci-lint run

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation and regenerated documentation via the documentation tool
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally and on Travis with my changes
  • Any dependent changes have been merged and published in downstream modules

@xtda xtda added the bug label Dec 10, 2019
@xtda xtda self-assigned this Dec 10, 2019
@codecov
Copy link

codecov bot commented Dec 11, 2019

Codecov Report

Merging #394 into master will decrease coverage by 0.03%.
The diff coverage is 15.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #394      +/-   ##
==========================================
- Coverage    41.4%   41.36%   -0.04%     
==========================================
  Files         163      163              
  Lines       38341    38366      +25     
==========================================
- Hits        15874    15870       -4     
- Misses      21352    21372      +20     
- Partials     1115     1124       +9
Impacted Files Coverage Δ
exchanges/btcmarkets/btcmarkets.go 23.76% <ø> (ø) ⬆️
engine/timekeeper.go 0% <0%> (ø) ⬆️
exchanges/btcmarkets/btcmarkets_wrapper.go 25.05% <0%> (-0.98%) ⬇️
engine/orders.go 0% <0%> (ø) ⬆️
common/common.go 89.22% <100%> (+0.61%) ⬆️
ntpclient/ntpclient.go 37.5% <33.33%> (-40.28%) ⬇️
database/models/sqlite3/boil_types.go 66.66% <0%> (-33.34%) ⬇️
database/models/postgres/psql_upsert.go 91.17% <0%> (-8.83%) ⬇️
dispatch/dispatch.go 86.15% <0%> (-2.57%) ⬇️
database/models/postgres/audit_event.go 51.84% <0%> (-1.31%) ⬇️
... and 33 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 473092a...f821351. Read the comment docs.

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice find! tACK

@thrasher-
Copy link
Collaborator

Just realised that there's another bug: https://github.com/thrasher-corp/gocryptotrader/blob/master/exchanges/btcmarkets/btcmarkets_wrapper.go#L547

This will return all orders (inactive/cancelled etc) so we can specify "open" as the last param to retrieve only open orders

@xtda
Copy link
Contributor Author

xtda commented Dec 11, 2019

Just realised that there's another bug: https://github.com/thrasher-corp/gocryptotrader/blob/master/exchanges/btcmarkets/btcmarkets_wrapper.go#L547

This will return all orders (inactive/cancelled etc) so we can specify "open" as the last param to retrieve only open orders

Addressed this and some other issues with GetActiveOrders()

At quick glance GetOrderHistory() also needs to be reworked will push through another update soon and get you to rereview it

BTC markets batch end points have limits (20 for cancel 50 for query) adds new method SplitStringSliceByLimit in common to split a slice by limit and return slice of slice
@xtda xtda requested a review from thrasher- December 11, 2019 22:38
Copy link
Contributor

@MadCozBadd MadCozBadd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing these issues! Just have 2 basic nits

common/common.go Outdated Show resolved Hide resolved
tempResp.OrderSide = order.Bid
if tempData[y].Side == ask {
tempResp.OrderSide = order.Ask
}
tempResp.OrderDate = tempData[y].CreationTime

switch tempData[y].Type {
case "Limit":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use consts:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those changes! I just noticed another 2 but they're related to the ntpclient

@@ -38,7 +37,10 @@ func NTPClient(pool []string) (time.Time, error) {

defer con.Close()

con.SetDeadline(time.Now().Add(5 * time.Second))
err = con.SetDeadline(time.Now().Add(5 * time.Second))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change to

if err := con.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
	log.Warnf(log.TimeMgr, "Unable to SetDeadline. Error: %s\n", err)
        con.Close()
	continue
}

The other thing I noticed with this is we are deferring con.Close() but on each for loop iteration, we are reassigning con. The defer will only execute upon func exit, so it's possible for lingering cons

@@ -55,5 +57,6 @@ func NTPClient(pool []string) (time.Time, error) {

return time.Unix(int64(secs), nanos), nil
}
return time.Unix(0, 0), errors.New("no valid time servers")
log.Warnln(log.Global, "No valid ntp server found returning current system time")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use log.TimeMgr here, and just a basic grammar fix:

No valid NTP servers found, using current system time

@@ -30,30 +29,36 @@ type ntppacket struct {
// NTPClient create's a new NTPClient and returns local based on ntp servers provided timestamp
func NTPClient(pool []string) (time.Time, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't use err, we could do a check at the top to check if len(pool) == 0 and complain if it's empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove the error return value
a pool should never be empty because of https://github.com/thrasher-corp/gocryptotrader/blob/master/config/config.go#L1250

if len(req.Currencies) == 0 {
allPairs := b.GetEnabledPairs(asset.Spot)
allPairs := b.GetAvailablePairs(asset.Spot)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep this as GetEnabledPairs, so that we're only interested in the ones the user has enabled (otherwise they can set it in req.Currencies). Our order manager will use enabled pairs in a future update.

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those requested changes!

Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Epic changes - Few nits

slice50 := initStringSlice(t, 50)
out := SplitStringSliceByLimit(slice50, 20)
if len(out) != 3 {
t.Errorf("test failed expected len() to be 3 instead received: %v", len(out))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we got rid of test failed strings. Might do that as well.

@@ -499,3 +500,30 @@ func TestChangePermission(t *testing.T) {
}
}
}

func initStringSlice(t *testing.T, size int) (out []string) {
t.Helper()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no error messages here so this can be rm'd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper() is called here to cause this function to be ignored when printing out warnings/errors/line information

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually shifts the error to the calling function line if it has a t.Error or t.Fatal, it doesn't add anything to the output.

for example:

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func initStringSlice(t *testing.T, size int) (out []string) {
	t.Helper()
	for x := 0; x < size; x++ {
		out = append(out, "gct-"+strconv.Itoa(x))
	}
	t.Error("Cats")
	return
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will indicate error at ln 514 instead of ln 509

_, err = NTPClient(firstInvalid)
if err != nil {
t.Errorf("failed to get time %v", err)
if reflect.TypeOf(v) != reflect.TypeOf(time.Time{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always be a time.Time so can RM this check. Or could force a time.Now().UTC() check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly this was just a lazy way to maintain test coverage to at least know its returning a time.Now()

I can do a time.Now.UTC() check but i would have to also parse the time into a format that doen't include anything above milliseconds or return value of NTPClient and time.Now.UTC() will be out and cause a failure

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one is just comparing the year/day/month is the same, that's a good enough check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the type check because you can never have to much test coverage right :D?
Also added a check for IsZero() and using 2006-01-02T15:04:05 format

Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those changes!

Copy link
Contributor

@MadCozBadd MadCozBadd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reapproved!

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reACK!

@thrasher- thrasher- merged commit a727bee into thrasher-corp:master Dec 16, 2019
@xtda xtda deleted the btcmarketsorderplacedfix branch February 9, 2020 21:34
MadCozBadd pushed a commit to MadCozBadd/gocryptotrader that referenced this pull request Mar 24, 2020
…d condition correction (thrasher-corp#394)

* corrected param order Side -> Type, also corrected condition check for IsOrderPlaced

* send open status for GetActiveOrders

* GetActiveOrder() changes to include OrderID and status matching

* BTC Markets batch order limit fixes & SplitStringSliceByLimit  method

BTC markets batch end points have limits (20 for cancel 50 for query) adds new method SplitStringSliceByLimit in common to split a slice by limit and return slice of slice

* rm line :D

* Added test for SplitStringSliceByLimit and moved to const

* ntp client reworked to not return error if no valid time servers are found but default to system

* clean up

* new line added

* use TimeMgr sublogger and wording correction on output

* Moved to DialTimeout() & Removed SetDeadline call

* removed line

* added setdeadline fix

* goimport file

* removed unused error from NTPClient as we now default to system time if no server can be reached

* Added checks for number overflows

* converted to uint as you should not be passing a negative number in

* Increased test cases for NTPClient

* Removed Helper call as no longer outputting any data from function

* removed unused param
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants