Skip to content

Commit

Permalink
add method to delete cookies bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
maxxrk committed Feb 11, 2024
1 parent d173816 commit 7c8cbc8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__/
*.py[cod]
*$py.class
testme.py
*.pkl

# C extensions
*.so
Expand Down
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ The code below will:
- Print out the order confirmation

```
from firstrade import account
from firstrade import symbols
from firstrade import order
from firstrade import account, order, symbols
# Create a session
ft_ss = account.FTSession(username='', password='', pin='')
ft_ss = account.FTSession(username="", password="", pin="")
# Get account data
ft_accounts = account.FTAccountData(ft_ss)
if len(ft_accounts.account_numbers) < 1:
raise Exception('No accounts found or an error occured exiting...')
raise Exception("No accounts found or an error occured exiting...")
# Print ALL account data
print(ft_accounts.all_accounts)
Expand All @@ -46,7 +44,7 @@ print(ft_accounts.account_numbers[0])
print(ft_accounts.account_balances)
# Get quote for INTC
quote = symbols.SymbolQuote(ft_ss, 'INTC')
quote = symbols.SymbolQuote(ft_ss, "INTC")
print(f"Symbol: {quote.symbol}")
print(f"Exchange: {quote.exchange}")
print(f"Bid: {quote.bid}")
Expand All @@ -61,34 +59,38 @@ print(f"Company Name: {quote.company_name}")
# Get positions and print them out for an account.
positions = ft_accounts.get_positions(account=ft_accounts.account_numbers[1])
for key in ft_accounts.securities_held:
print(f"Quantity {ft_accounts.securities_held[key]['quantity']} of security {key} held in account {ft_accounts.account_numbers[1]}")
print(
f"Quantity {ft_accounts.securities_held[key]['quantity']} of security {key} held in account {ft_accounts.account_numbers[1]}"
)
# Create an order object.
# Create an order object.
ft_order = order.Order(ft_ss)
# Place order and print out order confirmation data.
ft_order.place_order(
ft_accounts.account_numbers[0],
symbol='INTC',
symbol="INTC",
price_type=order.PriceType.MARKET,
order_type=order.OrderType.BUY,
quantity=1,
duration=order.Duration.DAY,
dry_run=True
dry_run=True,
)
# Print Order data Dict
print(ft_order.order_confirmation)
# Check if order was successful
if ft_order.order_confirmation['success'] == 'Yes':
print('Order placed successfully.')
if ft_order.order_confirmation["success"] == "Yes":
print("Order placed successfully.")
# Print Order ID
print(f"Order ID: {ft_order.order_confirmation['orderid']}.")
else:
print('Failed to place order.')
print("Failed to place order.")
# Print errormessage
print(ft_order.order_confirmation['actiondata'])
print(ft_order.order_confirmation["actiondata"])
# Delete cookies
ft_ss.delete_cookies()
```
This code is also in test.py

Expand Down
21 changes: 11 additions & 10 deletions firstrade/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ def load_cookies(self):
Dict: Dictionary of cookies. Nom Nom
"""
cookies = {}
if self.profile_path is not None:
directory = os.path.abspath(self.profile_path)
if not os.path.exists(directory):
os.makedirs(directory)
for filename in os.listdir(directory):
if filename.endswith(f"{self.username}.pkl"):
filepath = os.path.join(directory, filename)
with open(filepath, "rb") as f:
cookies = pickle.load(f)
directory = os.path.abspath(self.profile_path) if self.profile_path is not None else "."

if not os.path.exists(directory):
os.makedirs(directory)

for filename in os.listdir(directory):
if filename.endswith(f"{self.username}.pkl"):
filepath = os.path.join(directory, filename)
with open(filepath, "rb") as f:
cookies = pickle.load(f)
return cookies

def save_cookies(self):
Expand All @@ -110,7 +111,7 @@ def save_cookies(self):
with open(path, "wb") as f:
pickle.dump(self.session.cookies.get_dict(), f)

def delete_cookeis(self):
def delete_cookies(self):
"""Deletes the session cookies."""
if self.profile_path is not None:
path = os.path.join(self.profile_path, f"ft_cookies{self.username}.pkl")
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

setuptools.setup(
name="firstrade",
version="0.0.13",
version="0.0.14",
author="MaxxRK",
author_email="[email protected]",
description="An unofficial API for Firstrade",
long_description=long_description,
long_description_content_type="text/markdown",
license="MIT",
url="https://github.com/MaxxRK/firstrade-api",
download_url="https://github.com/MaxxRK/firstrade-api/archive/refs/tags/0013.tar.gz",
download_url="https://github.com/MaxxRK/firstrade-api/archive/refs/tags/0014.tar.gz",
keywords=["FIRSTRADE", "API"],
install_requires=["requests", "beautifulsoup4", "lxml"],
packages=["firstrade"],
Expand Down
2 changes: 2 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@
print("Failed to place order.")
# Print errormessage
print(ft_order.order_confirmation["actiondata"])
# Delete cookies
ft_ss.delete_cookies()

0 comments on commit 7c8cbc8

Please sign in to comment.