API Trading w/ Thinkorswim

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

https://github.com/TreyThomas93/python-trading-bot-with-thinkorswim

I created this program for auto trading through TDAmeritrades API, using scanners and alerts from the Thinkorswim platform.

Hope this helps.
@TreyThomas673 Can you share your success thus far using this code for auto trade? I wanted to use this to trigger auto trade based on a study condition similar to conditional trades (1st trgs seq), but do it on GTC_EXT hours on all my watchlist that have buy signal based on a specific study/studies.. I would love to pick your brain on this..
 
Last edited:
@TreyThomas673 Can you share your success thus far using this code for auto trade? I wanted to use this to trigger auto trade based on a study condition similar to conditional trades (1st trgs seq), but do it on GTC_EXT hours on all my watchlist that have buy signal based on a specific study/studies.. I would love to pick your brain on this..
The results here are simulated results using the strategies developed through Thinkorswim. What makes these simulated is that these buy and sell every alert triggered and received from TOS. Unlike live trading, which limits you based on buying power. So basically you are paper trading through the program. As for live trading results, since November 2020, the program has made close to $450. This is obviously subject to be different per user based on how successful the users strategies are. My program is a mere vessel to trade through using those strategy based alerts. The past several months have been mainly debugging and enhancing the program to where it is at currently.

As for the watchlists/scanners with GTC_EXT hours, the program runs on a while loop every 5 seconds, so it will trade after hours if thats what you are wondering. Like I said, most of the customization can be done through TOS.
 
Thanks for sharing. Members probably know, but just want to post a reminder nevertheless, TD API's do not work on paper trading.
This is indeed interesting stuff. It might be possible, for those wishing to paper trade, to set up the code to use ToS Strategies, the same email setup as the original code, but route trade orders through Alpaca, for example, which has a paper trade API. Not saying this is a good idea, and if the code polls your positions via ToS it wouldn't work... but it could offer a paper trading method without having to create data pipelines and indicators in python.

-mashume
 
1. Am looking for the scanner name format, you specified. how it is formed at ToS client side ?
2. say, i have a study with certain alert in it. how do i send that alert to gmail account. (alert to gmail setup i understand.)
am looking for info on how to setup a scanner with my study ?

looks i need to create a scanner with a name you specified with tag SELL as one scanner, BUY as another scanner ?

Can you help me in understanding how to create the scanner , with the name format you have, and is it one scanner per strategy ?



or how does the scanner run everytime and send alert to gmail periodically ?
 
Last edited:
Also, which strategy we can get better buy and sell signal alerted ? any pointers on which one to use ?

There could be plenty out there,
But as advanced/seasoned traders you guys might have some favorite ones, or some might be already discussed in this forum..
 
Pretty neat program. How difficult would it be to implement MySQL DB instead of MongoDB? Any recommendations on the code?
 
I will ask my partner and see what he says. Like I said, I know next to nothing about code.
If you wanted to learn, Python is one of the easier languages to learn. I would recommend using either PyCharm or Visual Studio Code as an IDE. PyCharm has pretty much everything you need "out of the box" while VSC necessitates installing some free plugins. There are some good courses on Udemy for learning Python, but it will take an investment of time. Once you get your head around one language, subsequent languages are easier to pick up in my experience.
 
This looks awesome Trey. This is one of the first python specific walkthrus I’ve seen for ToS. Im definitely more familiar coding in thinkscript and backtesting strategies on ToS but what you’re doing is exactly what I’m trying to implement in my trading plan.

I’m going to play around the code and see if I can code my strategy in it. Thanks for sharing! Are you exploring other strategies? Are you actively working on this in any space? Discord/Twitter/etc?
 
This looks awesome Trey. This is one of the first python specific walkthrus I’ve seen for ToS. Im definitely more familiar coding in thinkscript and backtesting strategies on ToS but what you’re doing is exactly what I’m trying to implement in my trading plan.

I’m going to play around the code and see if I can code my strategy in it. Thanks for sharing! Are you exploring other strategies? Are you actively working on this in any space? Discord/Twitter/etc?
I haven't really been working on this since I made it public on Github. I have been busy with other things. I may look at adding a risk management aspect to it though. Maybe add bracket orders to allow for SL and TP setups. I never considered a Discord. May have to look into that.
 
I am at just the beginning level of developing stock trading strategies and found this interesting cloud based approach for scheduling and running a strategy (simulated or real):
https://alpaca.markets/learn/algorithmic-trading-bot-7-steps/

If I am reading this right some other broker has made it possible to automate the execution of trading strategies via a function call that is scheduled by a cloud service offered by Google.

I like this approach if a long strategy is desired to run for many days, weeks, or months looking to take advantage of abnormal events that do not occur frequently. I want something like this that does not require getting into a desktop application on my local computer at home every day.

Maybe something like this can already be achieved with Ameritrade's API or thinkorswim scripts where once a strategy is launched it continues to run for many days regardless of whether my home computer is on of off?
 
@JDW1953 Read post#14 and onwards provides some context to your question.
Then the below thread shows some of the API steps. What I surmise from my reading is accessing ToS through API is an arduous process.
But theoretically, what you are asking can be done.
https://usethinkscript.com/threads/automated-python-based-trading-bot.5955/#post-1735
Actually it appears that a number of instructional video's are available that lead you through just about everything concerning the usage of the APIs. For example starting with this one:

Everything I've seen so far revolves around the development and the running code on a local PC. Once perfected then the process of moving it into a cloud computing service (like the Google example I gave) might be advantageous to some but the few steps needed to get it there are hard for me to find.
 
Hey everyone,
It's been some time since this was posted, so I thought I would "bump" it in a sense so newer people can see this.

Python trading bot with thinkorswim: https://github.com/TreyThomas93/python-trading-bot-with-thinkorswim

The bot scrapes alerts in a gmail account that were sent from Thinkorswim.
Once the information is obtained, trades are then place accordingly through the TDA api.
There is also a Discord group with a link at the bottom of the README in the repo.
The group is there for assistance with the program.

Thanks.
 
Hello Everyone,

During trading hours, I want to download 5 minutes price data (OHLC and Volume) for all stocks for current day (or for the last few hours).

I use TOS API code below and it only download Yesterday instead of today.

Can you please help?

Thank you so much,

PYPL

endpoint = 'https://api.tdameritrade.com/v1/marketdata/{stock_ticker}/pricehistory?periodType={periodType}&period={period}&frequencyType={frequencyType}&frequency={frequency}'

# yesterday 1 minute
full_url = endpoint.format(stock_ticker='AAPL',periodType='day',period=1,frequencyType='minute',frequency=1)
 
Hello,

I have a question concerning API's. I like the TOS platform to view charts and level II. However, sometimes, I like to trade on another platform (reduce or no fees on options). Is it possible to use an API, while on the TOS platform, to send a trade to another platform? If yes, I will get back to this later to see if someone can create an API or code.



Thanks in advance!
 
Hi,
I use Excel file to download LAST, OPEN, HIGH, LOW, CLOSE and VOL for a list of ticker.
(The command is:=RTD("tos.rtd", , "LAST", $A1) for LAST.)

During trading hours, data is updated fine.
However, for PRE- and POST-trading hour, Prices data is not updated but VOL does update.
Am I doing anything wrong or what can we do to get Pre and Post data?
Thanks,
HC
 
Hi,
I use Excel file to download LAST, OPEN, HIGH, LOW, CLOSE and VOL for a list of ticker.
(The command is:=RTD("tos.rtd", , "LAST", $A1) for LAST.)

During trading hours, data is updated fine.
However, for PRE- and POST-trading hour, Prices data is not updated but VOL does update.
Am I doing anything wrong or what can we do to get Pre and Post data?
Thanks,
HC
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
431 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top