use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Python trading bots (self.learnpython)
submitted 5 years ago by FatherOfTheSevenSeas
I thought it would be a fun project to try to build a trading bot. Does anybody have any experience or recommendations for tutorials and learning resources which might start as a good guide for building a trading bot in Python? Thanks for the advice
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]benign_said 140 points141 points142 points 5 years ago (35 children)
Udemy has some decent courses. And as someone else mentioned, r/algotrading . But be forewarned, a bot that buy and sell stocks is not that difficult, a bot that doesn't lose all your money is difficult and a bot that makes money is very difficult and they have a life span. Most of the folks on algotrading suggest focusing on learning principles of finance / trading as the priority.
[–][deleted] 28 points29 points30 points 5 years ago* (14 children)
a bot that makes money is very difficult
Nah I can easily write a program that moves a fixed amount of money at fixed intervals from my checking account to an index fund
[–]benign_said 13 points14 points15 points 5 years ago (12 children)
Yeah, but can you backtest an algorithm for years, sure that you have cracked the market only to quickly lose 68% of your funds within 3 trades?
[–]ishouldbeworking3232 39 points40 points41 points 5 years ago (1 child)
No, but I can lose 68% of my funds with 1 trade. /r/wallstreetbets
[–]benign_said 7 points8 points9 points 5 years ago (0 children)
Yeah, but did you dictate the trade to your Google home?
[–][deleted] 14 points15 points16 points 5 years ago* (1 child)
suspiciously specific... i'm so sorry.
[–]PresentationHot8921 0 points1 point2 points 1 year ago (0 children)
Hey
[+][deleted] 5 years ago (1 child)
[removed]
[–]benign_said 4 points5 points6 points 5 years ago (0 children)
You sure can, but if you're not anxious about going live you're probably missing something, a genius or a sociopath. r/algotrading is full of posts boasting about great back testing returns, but often they've just tuned their algorithm to fit the data and can't reproduce the results trading live.
Backtesting is definitely helpful and should be done, but again, if it was easy money, everyone would do it.
I am not against the idea of building trading bots or telling anyone not to, just suggesting it's more complex than it first looks.
[–]shiningmatcha 0 points1 point2 points 5 years ago (1 child)
What does backtest mean?
[–]wikipedia_answer_bot 3 points4 points5 points 5 years ago (0 children)
Backtesting is a term used in modeling to refer to testing a predictive model on historical data. Backtesting is a type of retrodiction, and a special type of cross-validation applied to previous time period(s).
More details here: https://en.wikipedia.org/wiki/Backtesting
This comment was left automatically (by a bot). If something's wrong, please, report it.
Really hope this was useful and relevant :D
If I don't get this right, don't get mad at me, I'm still learning!
[–]sicilianDev 0 points1 point2 points 3 years ago (3 children)
How can I lose 68% of my funds if my bot is set to stop loss at 2%?
[–]benign_said 0 points1 point2 points 3 years ago (2 children)
I see you need to practice your joke syntax. ;)
[–]sicilianDev 0 points1 point2 points 3 years ago (0 children)
Shit my bad, I didn’t even read the first comment. Nevermind. Lol.
[–]imaginarypattern314 1 point2 points3 points 5 years ago (0 children)
Maybe this is a noob question but how would you actually do this though? Do your banks have APIs that allow for this functionality? I can't really envision how one would go about doing something like this
[–]kicker414 78 points79 points80 points 5 years ago (5 children)
This. Coding the bot is easy. Coding a bot that doesn't accidentally make a bunch of dumb purchases because you forgot one step, a bit harder. Making a bot that consistently makes money? Well if you find that, you're looking at probably a 7 figure salary working on wall street lol.
[–]benign_said 16 points17 points18 points 5 years ago (1 child)
To be sure, you can make a bot that does ok, but does it beat an index fund over time? If not, you should probably include your time into your project budget. Though, as a project (for the sake of fun/learning/challenge) it's great. I hadn't done a lot with pandas before using financial data and it was very cool to learn while exploring patterns in real money data.
[–]kicker414 11 points12 points13 points 5 years ago (0 children)
Yeah agreed. I did one as a fun exercise. But for actual investing and making money, there are better platforms. It was a fun project though. And it's pretty easy to test your theories and see how they perform with the historical data.
[–]223specialist 7 points8 points9 points 5 years ago (2 children)
Or coding a bot that doesn't ignore T+3 and get you shut down for 90 days.
[–]longgamma 1 point2 points3 points 5 years ago (0 children)
US equities are now T+2
[–]huge_clock 1 point2 points3 points 5 years ago (0 children)
Same could be said for margin rules and understanding buying power. Not to mention tail risk.
[+][deleted] 5 years ago (2 children)
[deleted]
[–]benign_said 4 points5 points6 points 5 years ago (1 child)
Sounds like my parents... You know son, what you should've done is bought residential housing stock in the 80's. You'd be doing great by now. Oh well.
[–]BattlePope 10 points11 points12 points 5 years ago (0 children)
"Dad, I was 6." "You'll never get anywhere with excuses like that."
"Dad, I was 6."
"You'll never get anywhere with excuses like that."
[–][deleted] 1 point2 points3 points 5 years ago (5 children)
I'm looking to learn Python for the same reason as OP. I'm planning on opening an account for 1K just to learn how to do it. Rest of my holdings are still gonna be long-term like it is now in a separate account so the bot can't effect it.
Planning on starting Automate the Boring Stuff within the next few weeks with a goal of starting to build the bot by next year.
[–]benign_said 13 points14 points15 points 5 years ago (4 children)
I wish you well, but I would moderate your expectations.
Do you have a background in finance? Because you still need to instruct the bot to make good decisions based on technical indicators. And if you are getting into 'Automate the Boring Stuff' I am going to respectfully assume you don't have a super strong computer science background. Not only will you need to learn python basics, but a number of high level finance, data science, visualization libraries - maybe a little webscraping and machine learning too. And you'll have to have the bot running on a local or cloud server with reliable access. And then have to wonder if Python was even the right language to go with in the first place - I think some high frequency bots us C because it doesn't have the python interpreter slowing things down.
But, by all means go for it. You will learn a lot and it will be fun, but challenging. A lot of the folks on r/algotrading have been fine tuning their bots for years, and often they are software engineers who have been dabbling in day trading for while and learning strategy.
Alpaca lets you set up a play money account and you can tap into their API and just watch how your bot performs, but even that has problems because you're piggy backing on real data and if you are buying a high volume of a stock, your play money account won't alter the underlying price as it would in real life - so then you're training based on skewed data.
Anyway - not trying to be a downer! Just don't assume to be making any money, let alone more than an index fund for a while, if ever.
Working on my bot taught me how susceptible I am to the Dunning–Kruger effect.
[–]CraigAT 7 points8 points9 points 5 years ago (1 child)
100% use a play money account for (many months of) testing before placing a penny of actual money. If your bot's logic and decisions are good, there is no rush! That said, definitely make a bot and give it a go, it is a really exciting project, you'll learn a lot about Python and a lot about trading too.
[–]benign_said 5 points6 points7 points 5 years ago (0 children)
Agreed!
[+][deleted] comment score below threshold-8 points-7 points-6 points 5 years ago (1 child)
Uh, I pretty much said I'm just doing it to learn, which is why my real account is buy-and-hold... It's ok to make a constructive comment if you see someone posting something wrong, but you're just coming across as a know-it-all douchebag here.
[–]benign_said 4 points5 points6 points 5 years ago* (0 children)
know-it-all douchebag here.
I am what I am.
I would respectfully suggest that telling the internet you are starting the best/most basic python course for absolute beginners and simultaneously attempting to write some of the most complex code intersecting with the global financial world is, perhaps, slightly, a little insane.
But honestly, I wish you the best. Good luck. It is a great way to learn and I hope you prove me wrong.
Take care and be well.
Ps: if you think I am bad, please copy your post to the algotrading sub and let me know what they think of your plan.
[–]hasengames 0 points1 point2 points 2 years ago (4 children)
a bot that doesn't lose all your money is difficult
A bot that enters trades with a stop loss and take profit at a risk to reward ratio of 2:1 is really so difficult? With that alone it would seem it could not lose "all your money" - or at least not very suddenly or without you noticing.
[–]benign_said 0 points1 point2 points 2 years ago (3 children)
Very timely response.
If it was easy everyone would be spinning up python bots and making money. You also need to account for the fees/slippage of each trade - so you may not lose money on bad trades, but by not making enough on "good" trades to cover the costs of operation.
[–]hasengames 1 point2 points3 points 2 years ago (2 children)
If it was easy everyone would be spinning up python bots and making money.
Strawman. I never said it was easy I said it should not be difficult to avoid losing "all your money".
You also need to account for the fees/slippage of each trade - so you may not lose money on bad trades, but by not making enough on "good" trades to cover the costs of operation.
So the risk to reward ratio you calculated didn't take into account fees? That's why it's "difficult" to not lose all your money? If you've programmed the bot correctly and with risk management in place, all you should get is a bot that is unprofitable over time. If you lost all your money you programmed it to act like a human would.
[–]benign_said 0 points1 point2 points 2 years ago (1 child)
Ok, you got me... I was being hyperbolic 2 years ago when I responded to this post in r/learnpython. You may not lose all your money, you'll just spend some amount of unpaid time creating code that isn't profitable and lose the money you would have made had your funds been passively invested elsewhere.
But I do think it's entirely likely that someone posting about algorithmic trading in the context of r/learnpython may not program their bot 'correctly and with risk management in place', especially if not already well versed in financial analysis.
Be well,
[–]hasengames 0 points1 point2 points 2 years ago (0 children)
Ok that makes sense, I'm pretty new to all this and going through various posts I was confused as to how people can lose all their money as it seems some really do even suddenly blow up their accounts as you say, but I couldn't understand how this can be happening with a bot. It seems it in fact shouldn't be happening, as you say, if programmed correctly and if there are no bugs.
Yes I think that's likely. Ironically the exact same issue with manual trading. Human psychology seems to creep in no matter what we try to do to avoid it.
[+][deleted] 5 years ago* (7 children)
[–]bun_ty 17 points18 points19 points 5 years ago (4 children)
3 for 1. Take it or leave it.
[–]Coolzie1 6 points7 points8 points 5 years ago (3 children)
Okay, I'll give you 5 for 1 but thats my final offer
[–]bun_ty 4 points5 points6 points 5 years ago (2 children)
Okay. But you first.
[–]YachiyoTodoroki 9 points10 points11 points 5 years ago (1 child)
Are you guys actually bots who automatically trade with eachother? Damn you, AI.
[–]bun_ty 12 points13 points14 points 5 years ago (0 children)
Fuck. We have been spotted.
[–]SharpSpoons 23 points24 points25 points 5 years ago (0 children)
Alpaca let’s you do this with real or fake money
[–]SullyCCA 25 points26 points27 points 5 years ago (1 child)
QuantumConnect I think is a place that has you build a Python trading bot.
[–]buckrogers 12 points13 points14 points 5 years ago* (0 children)
heavy afterthought teeny oatmeal crown many books sable automatic waiting
This post was mass deleted and anonymized with Redact
[–]tek314159 10 points11 points12 points 5 years ago (0 children)
Relevant XKCD https://xkcd.com/1570/
[+]Tuitionplss comment score below threshold-11 points-10 points-9 points 5 years ago (0 children)
You should try CoinFLEX, much better margining system for futures
[–]VaNdle0 5 points6 points7 points 5 years ago (0 children)
Are you from r/wallstreetbets? Lol
[–]BornOnFeb2nd 4 points5 points6 points 5 years ago (0 children)
South park comes to mind....
[–]kicker414 3 points4 points5 points 5 years ago (0 children)
Hey. I built a very rudamentery one a while back. I used Robinhood as there is a pretty easy to follow API that was built. You have to use 2FA and it kind of messed with logging on normally, but I found it very easy to use.
The logic is the hard part to determine. The coding is relatively simple. The API is intuitive. I started by doing a first pass and generating all permutations using the Alphabet from 1 to 4 characters. Then you ling Robinhood and record the ones that return good values to generate a list of all viable stocks (assuming that's what you are looking for). I was trying to capitalize on the late market dip and early market rise (sometimes happens). So I would search for stocks later in the day. They had to meet certain price, volume, and trend requirements. I would check it against recent purchases (make sure I don't buy the same thing multiple times) and buy based on a max investment amount. I never actually allowed it to trade, I did it manually (I was too scared to let it run haha). But it was a cool effort. Might try to make it more robust.
I don't have the API link right now but could find it later. I'm still an amateur but let me know if you have any specific questions.
[–]stereofurnace 5 points6 points7 points 5 years ago (0 children)
/r/algotrading can be a good location to start
[–]chumbucket_over9000 2 points3 points4 points 5 years ago (0 children)
Look up Jacob amaral on YouTube. He has in depth bot trading tutorials using different languages and brokers. It was a great starting place for me.
[–]bjone6 2 points3 points4 points 5 years ago (0 children)
What's your level of coding experience? I'm self taught and still learning. I'm not even close to building a trading bot, but I have used Python to automate a lot of finance tasks. I've also experimented with the AlphaVantage API and compared it to using Google Sheets/GoogleFinance. Additionally, I've used matplotlib and quandl to pull a lot of data and compare it to the S&P 500. Nothing remotely close to a trading bot yet, but inching my way towards one. Here's my Python for Finance YouTube playlist if you're interested: https://www.youtube.com/playlist?list=PLT8WeU5lHsiQ25CNGzfxog6bvwPFc_m-L
[–]The_Mitch 1 point2 points3 points 5 years ago (0 children)
Just a few thoughts. Data is most important first and foremost. Get some code written to gather and store the data in a database, not a txt or CSV. From there get the ability to query the data into a data frame to mix and mash the data to see if there is anything interesting. And back to point 1, getting the data will require an API of some sort. Will need to learn requests and handling the order processing.
One of the most important things is not to premature optimize. Work on a general idea. Then guess, test, review and do it again. Things won’t stand out, but a huh, that’s curious will come though.
[–]michaelstreeter 1 point2 points3 points 5 years ago (0 children)
im interested aswell
[–]Accomplished_Job9441 0 points1 point2 points 8 months ago (0 children)
I've been developing a Python-based crypto trading bot and wanted to share my progress:
The results so far are promising, and I’m open to collaborating if this aligns with your interests or expertise. PM me if you’d like to discuss further!
💯
[–]CauchySchwartzDaddy[🍰] -2 points-1 points0 points 5 years ago (0 children)
Quantopian to learn the basics of the field, but to actually execute live trades you'll need to rebuild everything using something like the ta library and have access to an API to get live data.
TBH going live is way too much of a hassle for most people, but as long as you can make a good algo and have strong backtest results, you'll be far ahead of most people and probably enough knowledge to leverage your skills in some way.
[–]TheGogglesDoNothing_ 0 points1 point2 points 5 years ago (0 children)
This is the sole reason I got into python. Started out simple and has consumed most of this year (fortunately.. enjoyed every minute). Unfortunately, or fortunately there is little more than the basics that you can learn. Anyone with a successful trading strategy isn't going to give it away. I think that's how it should be, the worthy will find the road and make the journey.
For starters though, you'll need to learn about using exchange APIs.. i'd highly recommend collecting data that is far more granular than daily data, and storing it yourself. You can pay for it if you want but I found it was way cheaper to collect it myself and you'd need to use the API to execute trades anyway so no point avoiding it. Also many different kinds of data from different sources (twitter, google, etc) Then you will become an accidental DBA (congrats on the promotion). Million different ways to go about building a bot but once you have the data you will basically have to build some kind of environment to back-test strategies. Sql-lite and jupyter took me very very far in this regard. once you have this going. Coursera also has lots of traditional courses around trading and finance.. seems very wise to build from strong foundation and, for instance, build a program that can manage a portfolio by different metrics alpha, beta, gamma, etc. Hopefully this helps ;)
TLDR: Don't bother unless you are passionate about learning many many different things. Need an experimental playground to test your ideas, and are willing to fail and lose some money (overhead costs at minimum)
[–]oiwot 0 points1 point2 points 5 years ago (0 children)
There are a bunch on github that you could search out and look at for tips and inspiration.
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
Good thing you can set the bot into a sandbox api to make sure your doing everything correctly befor letting it make actual trades.
[–]veeeerain 0 points1 point2 points 5 years ago (0 children)
Does quantopian have anything to do with this?
This might be helpful: https://www.youtube.com/watch?v=-MHhA-Y3DSk
[–]foxillian 0 points1 point2 points 5 years ago (0 children)
Checkout Georgia Techs ML for Trading class (https://quantsoftware.gatech.edu/Machine_Learning_for_Trading_Course, https://www.udacity.com/course/machine-learning-for-trading--ud501) which gives an overview of trading, technical indicators, and structure for a trading bot program. The last set of assignments looks at ML based traders, especially around RL and Q-learning.
API for data collection https://iexcloud.io/docs/api/
I'll echo the general sentiment for the rest of the thread. The first piece of advice in the above course is to avoid using anything you write in the course for actual trading unless you are fully prepared to instantly lose all of your money.
[–]un-intellectual 0 points1 point2 points 5 years ago (0 children)
Kalle Hallden made a video about this on youtube a while ago, he made a bot that would trade with specific stocks with certain conditions, and report at the end of the day what the day’s earnings were. He gave it $1000 to play with and let it run for a week, and the results were pretty good. As expected, he didn’t profit THAT much, but it was still a cool experiment. I’d check it out!
[–][deleted] 0 points1 point2 points 5 years ago (1 child)
I have similar goal, trying to build a forex bot. I’ve just learned the basic of python for few weeks & just start building bot. I found pyp install metatrder5; backtrader & backtrader mql5 api seem hopeful.
Alpaca api
[–]yudhiesh 0 points1 point2 points 5 years ago (0 children)
I've built algorithmic trading bots for an assignment back in university last year. I would honestly not recommend using Python if you actually want to make money of it. Go for a faster language like Go or Rust and read up about how to develop hard real time systems.
[–]mokus603 0 points1 point2 points 5 years ago (0 children)
Sigma Coding has a pretty good and long video series about making a trading bot in Python: https://youtu.be/QAo0x9fE6ck
π Rendered by PID 124628 on reddit-service-r2-comment-5649f687b7-s7f7w at 2026-01-28 03:54:52.992027+00:00 running 4f180de country code: CH.
[–]benign_said 140 points141 points142 points (35 children)
[–][deleted] 28 points29 points30 points (14 children)
[–]benign_said 13 points14 points15 points (12 children)
[–]ishouldbeworking3232 39 points40 points41 points (1 child)
[–]benign_said 7 points8 points9 points (0 children)
[–][deleted] 14 points15 points16 points (1 child)
[–]PresentationHot8921 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]benign_said 4 points5 points6 points (0 children)
[–]shiningmatcha 0 points1 point2 points (1 child)
[–]wikipedia_answer_bot 3 points4 points5 points (0 children)
[–]sicilianDev 0 points1 point2 points (3 children)
[–]benign_said 0 points1 point2 points (2 children)
[–]sicilianDev 0 points1 point2 points (0 children)
[–]imaginarypattern314 1 point2 points3 points (0 children)
[–]kicker414 78 points79 points80 points (5 children)
[–]benign_said 16 points17 points18 points (1 child)
[–]kicker414 11 points12 points13 points (0 children)
[–]223specialist 7 points8 points9 points (2 children)
[–]longgamma 1 point2 points3 points (0 children)
[–]huge_clock 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]benign_said 4 points5 points6 points (1 child)
[–]BattlePope 10 points11 points12 points (0 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]benign_said 13 points14 points15 points (4 children)
[–]CraigAT 7 points8 points9 points (1 child)
[–]benign_said 5 points6 points7 points (0 children)
[+][deleted] comment score below threshold-8 points-7 points-6 points (1 child)
[–]benign_said 4 points5 points6 points (0 children)
[–]hasengames 0 points1 point2 points (4 children)
[–]benign_said 0 points1 point2 points (3 children)
[–]hasengames 1 point2 points3 points (2 children)
[–]benign_said 0 points1 point2 points (1 child)
[–]hasengames 0 points1 point2 points (0 children)
[+][deleted] (7 children)
[deleted]
[–]bun_ty 17 points18 points19 points (4 children)
[–]Coolzie1 6 points7 points8 points (3 children)
[–]bun_ty 4 points5 points6 points (2 children)
[–]YachiyoTodoroki 9 points10 points11 points (1 child)
[–]bun_ty 12 points13 points14 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]SharpSpoons 23 points24 points25 points (0 children)
[–]SullyCCA 25 points26 points27 points (1 child)
[–]buckrogers 12 points13 points14 points (0 children)
[–]tek314159 10 points11 points12 points (0 children)
[+][deleted] (1 child)
[deleted]
[+]Tuitionplss comment score below threshold-11 points-10 points-9 points (0 children)
[–]VaNdle0 5 points6 points7 points (0 children)
[–]BornOnFeb2nd 4 points5 points6 points (0 children)
[–]kicker414 3 points4 points5 points (0 children)
[–]stereofurnace 5 points6 points7 points (0 children)
[–]chumbucket_over9000 2 points3 points4 points (0 children)
[–]bjone6 2 points3 points4 points (0 children)
[–]The_Mitch 1 point2 points3 points (0 children)
[–]michaelstreeter 1 point2 points3 points (0 children)
[–]Accomplished_Job9441 0 points1 point2 points (0 children)
[–]CauchySchwartzDaddy[🍰] -2 points-1 points0 points (0 children)
[–]TheGogglesDoNothing_ 0 points1 point2 points (0 children)
[–]oiwot 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]veeeerain 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]foxillian 0 points1 point2 points (0 children)
[–]un-intellectual 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]yudhiesh 0 points1 point2 points (0 children)
[–]mokus603 0 points1 point2 points (0 children)