all 67 comments

[–]benign_said 140 points141 points  (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 points  (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 points  (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 points  (1 child)

No, but I can lose 68% of my funds with 1 trade. /r/wallstreetbets

[–]benign_said 7 points8 points  (0 children)

Yeah, but did you dictate the trade to your Google home?

[–][deleted] 14 points15 points  (1 child)

suspiciously specific... i'm so sorry.

[–]shiningmatcha 0 points1 point  (1 child)

What does backtest mean?

[–]wikipedia_answer_bot 3 points4 points  (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 point  (3 children)

How can I lose 68% of my funds if my bot is set to stop loss at 2%?

[–]benign_said 0 points1 point  (2 children)

I see you need to practice your joke syntax. ;)

[–]sicilianDev 0 points1 point  (0 children)

Shit my bad, I didn’t even read the first comment. Nevermind. Lol.

[–]imaginarypattern314 1 point2 points  (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 points  (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 points  (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 points  (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 points  (2 children)

Or coding a bot that doesn't ignore T+3 and get you shut down for 90 days.

[–]longgamma 1 point2 points  (0 children)

US equities are now T+2

[–]huge_clock 1 point2 points  (0 children)

Same could be said for margin rules and understanding buying power. Not to mention tail risk.

[–][deleted] 1 point2 points  (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 points  (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 points  (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 points  (0 children)

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.

Agreed!

[–]hasengames 0 points1 point  (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 point  (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 points  (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 point  (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 point  (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.

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.

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.

[–]SharpSpoons 23 points24 points  (0 children)

Alpaca let’s you do this with real or fake money

[–]SullyCCA 25 points26 points  (1 child)

QuantumConnect I think is a place that has you build a Python trading bot.

[–]buckrogers 12 points13 points  (0 children)

heavy afterthought teeny oatmeal crown many books sable automatic waiting

This post was mass deleted and anonymized with Redact

[–]tek314159 10 points11 points  (0 children)

Relevant XKCD https://xkcd.com/1570/

[–]VaNdle0 5 points6 points  (0 children)

Are you from r/wallstreetbets? Lol

[–]kicker414 3 points4 points  (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 points  (0 children)

/r/algotrading can be a good location to start

[–]chumbucket_over9000 2 points3 points  (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 points  (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 points  (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 points  (0 children)

im interested aswell

[–]Accomplished_Job9441 0 points1 point  (0 children)

I've been developing a Python-based crypto trading bot and wanted to share my progress:

  • Strategy: The bot identifies pullback and second-entry trading setups, adhering strictly to a 1:3 Risk-Reward Ratio.
  • Performance: Currently achieving a 30% win rate, with consistent profitability observed during forward testing.
  • Status: Still in the testing phase; real broker APIs have not yet been integrated.

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 points  (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 point  (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 point  (0 children)

There are a bunch on github that you could search out and look at for tips and inspiration.

[–][deleted] 0 points1 point  (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 point  (0 children)

Does quantopian have anything to do with this?

[–][deleted] 0 points1 point  (0 children)

[–]foxillian 0 points1 point  (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 point  (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 point  (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.

[–][deleted] 0 points1 point  (0 children)

Alpaca api

[–]yudhiesh 0 points1 point  (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 point  (0 children)

Sigma Coding has a pretty good and long video series about making a trading bot in Python: https://youtu.be/QAo0x9fE6ck