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

docstrings #45

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions examples/api_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
This file contains examples for every api call.
"""

from dotenv import load_dotenv
from time import sleep
import schwabdev
import datetime
import logging
import os
from time import sleep

from dotenv import load_dotenv

import schwabdev


def main():
Expand Down Expand Up @@ -112,7 +114,9 @@ def main():
print("\nGet an option chain")
print("There is a lot to print so this is not shown, the demo code is commented out")
# print(client.option_chains("AAPL").json())
# Here is another example for SPX, note that if you call with just $SPX then you will exceed the buffer on Schwab's end hence the additional parameters to limit the size of return.
# Here is another example for SPX, note that if you call with just $SPX
# you will exceed the buffer on Schwab's end
# hence, the additional parameters to limit the size of return.
# print(client.option_chains("$SPX", contractType="CALL", range="ITM").json())
sleep(3)

Expand Down
182 changes: 96 additions & 86 deletions examples/jupyter_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,202 +2,212 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "initial_id",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#import packages\n",
"import schwabdev\n",
"# import packages\n",
"import logging\n",
"import os\n",
"\n",
"import dotenv\n",
"import os"
],
"outputs": [],
"execution_count": null
"\n",
"import schwabdev"
]
},
{
"metadata": {},
"cell_type": "code",
"execution_count": null,
"id": "99702f6a92a22ca7",
"metadata": {},
"outputs": [],
"source": [
"# set logging level\n",
"logging.basicConfig(level=logging.INFO)\n",
"\n",
"#load environment variables and make client\n",
"# load environment variables and make client\n",
"dotenv.load_dotenv()\n",
"client = schwabdev.Client(os.getenv('app_key'), os.getenv('app_secret'), os.getenv('callback_url'))"
],
"id": "99702f6a92a22ca7",
"outputs": [],
"execution_count": null
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Basic API calls",
"id": "5cfc8e6c5d21383"
"id": "5cfc8e6c5d21383",
"metadata": {},
"source": [
"# Basic API calls"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"id": "99904111f851f396",
"metadata": {},
"outputs": [],
"source": [
"# get account number and hashes for linked accounts\n",
"linked_accounts = client.account_linked().json()\n",
"print(linked_accounts)\n",
"# select the first account to use for orders\n",
"account_hash = linked_accounts[0].get('hashValue')"
],
"id": "99904111f851f396"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"id": "7ee8091c36c0d066",
"metadata": {},
"outputs": [],
"source": [
"# get positions for selected account\n",
"print(client.account_details(account_hash, fields=\"positions\").json())"
],
"id": "7ee8091c36c0d066"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"id": "706a555f317f99ee",
"metadata": {},
"outputs": [],
"source": [
"# get a list of quotes\n",
"print(client.quotes([\"AAPL\", \"AMD\"]).json())"
],
"id": "706a555f317f99ee"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"id": "791f4d37b7a1cb5e",
"metadata": {},
"outputs": [],
"source": [
"# get an option chain\n",
"print(client.option_expiration_chain(\"AAPL\").json())"
],
"id": "791f4d37b7a1cb5e"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Order example",
"id": "c7e3dc8274757630"
"id": "c7e3dc8274757630",
"metadata": {},
"source": [
"# Order example"
]
},
{
"metadata": {},
"cell_type": "code",
"execution_count": null,
"id": "12ce8ea103853c49",
"metadata": {},
"outputs": [],
"source": [
"# place an order for INTC at limit price $10.00\n",
"order = {\"orderType\": \"LIMIT\", \n",
" \"session\": \"NORMAL\", \n",
" \"duration\": \"DAY\", \n",
" \"orderStrategyType\": \"SINGLE\", \n",
"order = {\"orderType\": \"LIMIT\",\n",
" \"session\": \"NORMAL\",\n",
" \"duration\": \"DAY\",\n",
" \"orderStrategyType\": \"SINGLE\",\n",
" \"price\": '10.00',\n",
" \"orderLegCollection\": [\n",
" {\"instruction\": \"BUY\", \n",
" \"quantity\": 1, \n",
" \"instrument\": \n",
" {\"symbol\": \"INTC\", \n",
" {\"instruction\": \"BUY\",\n",
" \"quantity\": 1,\n",
" \"instrument\":\n",
" {\"symbol\": \"INTC\",\n",
" \"assetType\": \"EQUITY\"\n",
" }\n",
" }\n",
" ]}\n",
"resp = client.order_place(account_hash, order)\n",
"print(f\"Response code: {resp}\") \n",
"print(f\"Response code: {resp}\")\n",
"\n",
"# get the order ID - if order is immediately filled then the id might not be returned\n",
"order_id = resp.headers.get('location', '/').split('/')[-1] \n",
"order_id = resp.headers.get('location', '/').split('/')[-1]\n",
"print(f\"Order id: {order_id}\")"
],
"id": "12ce8ea103853c49",
"outputs": [],
"execution_count": null
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f95a82378df8e73",
"metadata": {
"jupyter": {
"is_executing": true
}
},
"cell_type": "code",
"outputs": [],
"source": [
"# cancel the order\n",
"print(client.order_cancel(account_hash, order_id))"
],
"id": "4f95a82378df8e73",
"outputs": [],
"execution_count": null
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Streaming example",
"id": "20bb6b09489bf1a9"
"id": "20bb6b09489bf1a9",
"metadata": {},
"source": [
"# Streaming example"
]
},
{
"metadata": {},
"cell_type": "code",
"execution_count": null,
"id": "96d8a48e39b05465",
"metadata": {},
"outputs": [],
"source": [
"# create streamer\n",
"streamer = client.stream"
],
"id": "96d8a48e39b05465",
"outputs": [],
"execution_count": null
]
},
{
"metadata": {},
"cell_type": "code",
"execution_count": null,
"id": "9449da0e5ab23a6b",
"metadata": {},
"outputs": [],
"source": [
"# create a list to store responses\n",
"responses = []\n",
"\n",
"\n",
"def add_to_list(message):\n",
" responses.append(message)"
],
"id": "9449da0e5ab23a6b",
"outputs": [],
"execution_count": null
]
},
{
"metadata": {},
"cell_type": "code",
"execution_count": null,
"id": "7cdeaaa69efb6f3f",
"metadata": {},
"outputs": [],
"source": [
"#start stream and send request\n",
"# start stream and send request\n",
"streamer.start(add_to_list)\n",
"streamer.send(streamer.level_one_equities(\"AMD\", \"0,1,2,3,4,5,6,7,8\"))"
],
"id": "7cdeaaa69efb6f3f",
"outputs": [],
"execution_count": null
]
},
{
"metadata": {},
"cell_type": "code",
"source": [
"#check responses\n",
"print(responses)"
],
"execution_count": null,
"id": "4241513b2fbf9f6e",
"metadata": {},
"outputs": [],
"execution_count": null
"source": [
"# check responses\n",
"print(responses)"
]
},
{
"metadata": {},
"cell_type": "code",
"source": [
"#stop stream\n",
"streamer.stop()"
],
"execution_count": null,
"id": "cb5789c2248b6b54",
"metadata": {},
"outputs": [],
"execution_count": null
"source": [
"# stop stream\n",
"streamer.stop()"
]
}
],
"metadata": {
Expand All @@ -216,7 +226,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
9 changes: 6 additions & 3 deletions examples/playground.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""
This file functions as a "terminal emulator" so you can enter python code to test the api without restarting th whole program.
This file functions as a "terminal emulator".
It allows you to enter python code to test the api without restarting th whole program.
"""

from dotenv import load_dotenv
import schwabdev
import logging
import os

from dotenv import load_dotenv

import schwabdev


def main():
# place your app key and app secret in the .env file
Expand Down
Loading