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

Add trade models #9

Merged
merged 3 commits into from Nov 24, 2021
Merged

Add trade models #9

merged 3 commits into from Nov 24, 2021

Conversation

DemetreJou
Copy link
Collaborator

@DemetreJou DemetreJou commented Nov 19, 2021

Add simple models for company and stock trade

Don't want to do too much work before alpaca integration happens in case it needs to change
Should change the StockTrade, right now it's trying to model an end to end, buy to sell transaction but that's a faulty model. Should instead add a transaction_type field that's an enum, track buys and sells separately instead of trying to match a sell with a buy

Added many example tests to show off how that works
Not connected to alpaca, this functionality will come in another PR though

@DemetreJou DemetreJou marked this pull request as draft November 19, 2021 21:59
Comment on lines 21 to 24
class StockTradeSerializer(serializers.ModelSerializer):
class Meta:
model = StockTrade
fields = ('company', 'price', 'amount', 'bought_timestamp', 'sold_timestamp')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What is a Serializerr?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's how django will convert an ORM model into something that can be sent in a network request



class TradingbotTests(APITestCase):

def setUp(self) -> None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This runs before every test
There is also a special function that will run before the entire test suite if desired

name="Apple",
ticker="AAPL",
)
company.save()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can't do Company(.....).save() because .save() returns None and Company object is needed to create new StockTrade instance

Copy link
Member

Choose a reason for hiding this comment

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

what does .save() do (and why do we need it)? I am a little bit confused about this part

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When you create a model instance with model = Model(**kwargs) it's only created in local memory, the corresponding row isn't created in the database until model.save().
When one model "A" has a foreign key to another model "B" you need to pass an instance of "B" into "A"

@@ -11,13 +11,15 @@ def get_raw_data(ticker, limit, date_from=None, date_to=None):
if date_from is None:
url = "https://eodhistoricaldata.com/api/news?api_token={}&s={}&offset=0&limit={}".format(API_TOKEN, ticker, limit)
else:
url = "https://eodhistoricaldata.com/api/news?api_token={}&s={}&from={}&to={}&offset=0&limit={}".format(API_TOKEN, ticker, date_from, date_to, limit)
url = "https://eodhistoricaldata.com/api/news?api_token={}&s={}&from={}&to={}&offset=0&limit={}".\
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Line too long

@DemetreJou DemetreJou marked this pull request as ready for review November 22, 2021 05:50
Copy link
Member

@caixunshiren caixunshiren left a comment

Choose a reason for hiding this comment

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

Looks good to me and I will add the trading codes inside here

class StockTradeSerializer(serializers.ModelSerializer):
class Meta:
model = StockTrade
fields = ('company_id', 'price', 'amount', 'bought_timestamp', 'sold_timestamp')
Copy link
Member

Choose a reason for hiding this comment

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

This file looks good to me and I will expand it by adding the trading scripts

@DemetreJou DemetreJou merged commit 96d680e into main Nov 24, 2021
@DemetreJou DemetreJou deleted the trade-model branch November 24, 2021 02:13
caixunshiren added a commit that referenced this pull request Nov 25, 2021
add basic stock trade model and route
add tests and fix endpoints
fix unrelated linting issues

Co-authored-by: Demetre <demetre.jouras@gmail.com>
caixunshiren added a commit that referenced this pull request Jan 13, 2022
* created CICD

* Update django.yml

* past script

* Add trade models (#9) (#10)

add basic stock trade model and route
add tests and fix endpoints
fix unrelated linting issues

Co-authored-by: Demetre <demetre.jouras@gmail.com>

* updated apimanager class, added exception handling, optimized for django backend

* Modified the market_close function in apimanagers.py

* Modified the market_close function in apimanagers.py

* implemented get market status

* added g++ in docker file for windows machine

* changed requirement

* set up user authentication

* set up order model

* finished portfolio, stock instance and bot instance models

* fixed model bugs

* fixed minor auth0login bugs

* login page

* fixed minor bugs

* update models.py

* credential form prototype

* fix flake8 issues

* add missing requirement to requirements.txt

* deleted unnecessary folder mysite; moved non-user specific models from auth0login to tradingbot

* added simple form for user tosave their Alpaca API

* added sync_alpaca to sync and display alpaca account details on dashboard

* conver str to url type

* get user positions

* updated dashboard.html

* add css, slightly improve frontend

* synchronized non-user specific database with Alpaca

* further move models where they belong

* Revert "further move models where they belong"

This reverts commit 50c20fa.

* move models, not without a ton of additional files

* add migrations

* add basic stock buying option

* rename alpaca manager class

* minor changes

* synced with alpaca

* added more admin fields

* multiple forms enabled

* added more admin fields

* order form and refactoring

* modified dashboard

* updated form and order synchronization; added more fields in orders model

* modified dashboard

* finished order form; refactored sync alpaca; redesigned order model; wrote general order placing function (still needs to be refactored)

* refactored general order placing function that takes account of everything

* market order synced with alpaca. display order data at front end. cash now reflects pending orders

* made sure all queries are user specific. Fixed bug where filled price are not displayed for filled orders. Minor change in dashboard

* fixed datetime

* updated cash for user portfolio

* reformat code and fix lint issues

* further fixed lint issues

* further further fixed lint issues

* added time in force option for submit orders

* added portfolio strategy fields for portfortlio model

* added user portfolio strategy selection option. Connected to database

* slightly updated dashboard layout

* fixed bug where strategy tab disappear after submitting order

* commented out outdated unit tests

Co-authored-by: Jack Cai <jack.cai@mail.utoronto.ca>
Co-authored-by: Jack Cai <caixunshiren@gmail.com>
Co-authored-by: SazikQ <70537688+SazikQ@users.noreply.github.com>
Co-authored-by: ChristyQu <yangqu.0705@gmail.com>
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.

None yet

2 participants