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

Make changes to long-short.py to make it more Pythonic and beginner friendly #425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Sciguymjm
Copy link

As an example, it was destructive for a beginner to the Python language. This does not change the core design which is still flawed, but it allows the code to run fully.

Summary of changes:

  • 4 spaces instead of 2 (PEP8: https://www.python.org/dev/peps/pep-0008/)
  • camelCase to snake_case (PEP8)
  • Remove parentheses from if statements (PEP8)
  • Add some type hints where useful (syntax)
  • Avoid using un-imported pandas (pd) library to create timestamps, instead use proper datetime.date (syntax)
  • Remove all threading usage as it is all unnecessary when threads are immediately joined (design)
  • Remove all passing of lists to functions to retrieve values and instead use return statements for said values (design)
  • Add URL wrapping on API initialization to comply with the type hints of the library (misc)

This is by no means a complete fix of all issues.

…riendly

As an example, it was destructive for a beginner to the Python language. This does not change the core design which is still flawed, but it allows the code to run fully.

Summary of changes:
- 4 spaces instead of 2 (PEP8: https://www.python.org/dev/peps/pep-0008/)
- camelCase to snake_case (PEP8)
- Remove parentheses from if statements (PEP8)
- Add some type hints where useful (syntax)
- Avoid using un-imported pandas (pd) library to create timestamps, instead use proper datetime.date (syntax)
- Remove all threading usage as it is all unnecessary when threads are immediately joined (design)
- Remove all passing of lists to functions to retrieve values and instead use return statements for said values (design)
- Add URL wrapping on API initialization to comply with the type hints of the library (misc)
order_side = 'sell'
else:
order_side = 'buy'
qty = abs(int(float(position.qty)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please explain why we need the float conversion here?

Copy link
Contributor

Choose a reason for hiding this comment

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

There are a lot more similar double int(float(...)) conversions throughout the file.

Comment on lines +269 to +272
bars = self.alpaca.get_bars(stock[0], TimeFrame.Minute,
datetime.date.today().isoformat(),
datetime.date.today().isoformat(), limit=length,
adjustment='raw')
Copy link
Contributor

Choose a reason for hiding this comment

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

This will return the first 10 minute bars of today for the given symbol, instead of the last 10 minutes. I know it's also broken in the original but please fix it.

datetime.date.today().isoformat(),
datetime.date.today().isoformat(), limit=length,
adjustment='raw')
self.all_stocks[i][1] = (bars[stock[0]][len(bars[stock[0]]) - 1].c -
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor] You can simply use -1

Suggested change
self.all_stocks[i][1] = (bars[stock[0]][len(bars[stock[0]]) - 1].c -
self.all_stocks[i][1] = (bars[stock[0]][-1].c -

tSO = threading.Thread(target=self.submitOrder, args=[abs(int(float(position.qty))), position.symbol, side, respSO])
tSO.start()
tSO.join()
if self.long.count(position.symbol) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor]

Suggested change
if self.long.count(position.symbol) == 0:
if position.symbol not in self.long.count(position.symbol):


resp_get_tp_short = self.get_total_price(self.short)

self.q_long = int(self.long_amount // resp_get_tp_long)
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor] no need for the int conversion after //

Comment on lines +227 to +235
total_price = 0
for stock in stocks:
bars = self.alpaca.get_bars(stock, TimeFrame.Minute,
datetime.date.today().isoformat(),
datetime.date.today().isoformat(), limit=1,
adjustment='raw')

total_price += bars[stock][0].c
return total_price
Copy link
Contributor

Choose a reason for hiding this comment

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

While this logic is correct, I think using the latest trade price is much more accurate. The price can move a lot since the last minute bars closing price.

Suggested change
total_price = 0
for stock in stocks:
bars = self.alpaca.get_bars(stock, TimeFrame.Minute,
datetime.date.today().isoformat(),
datetime.date.today().isoformat(), limit=1,
adjustment='raw')
total_price += bars[stock][0].c
return total_price
return sum(self.alpaca.get_latest_trade(stock).price for stock in stocks)

self.blacklist.add(position.symbol)

# Send orders to all remaining stocks in the long and short list.
resp_send_bo_long = self.send_batch_order(self.q_long, self.long, "buy")
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor] tuple unpacking would be much nicer:

Suggested change
resp_send_bo_long = self.send_batch_order(self.q_long, self.long, "buy")
executed, incomplete = self.send_batch_order(self.q_long, self.long, "buy")

Copy link
Contributor

@gnvk gnvk left a comment

Choose a reason for hiding this comment

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

This PR is amazing, thank you very much! 🥇

I agree it's unfortunately not complete, but it's a huge step to the right direction. Still, I left some comments on issues I feel should be included, especially the ones that make the implementation incorrect (e.g. querying the first 10 minute bars of the day instead of the latest 10 minute bars).

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

Successfully merging this pull request may close these issues.

2 participants