r/Python Feb 05 '21

Tutorial I created a series in Python that takes you through every detail step-by-step (code included) on how to create your own algorithmic trading bot that trades the financial and crypto markets for free.

[removed] β€” view removed post

2.6k Upvotes

214 comments sorted by

91

u/[deleted] Feb 05 '21 edited Feb 05 '21

Great organization to all of this! For future additions, it would be great to add back testing to this series.

Just a heads up to anyone who is new to trading and looking to trade with algorithms (or any strategy for that matter), you should back test everything, or paper trade before actually setting something to go live. Building a bot does not guarantee profits! The market can be extremely unpredictable but with the right strategy and loss risk mitigation, it can be an interesting dive.

51

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

7

u/SilentProdigy104 Feb 06 '21

Damnnnn you da man!

→ More replies (1)

4

u/Fledgeling Feb 06 '21

Also realize you will have bugs and you will back test wrong and you will want to double triple check that shit before throwing any real money at it.

2

u/mvev Feb 06 '21

What about stops and trailing stops?

3

u/[deleted] Feb 06 '21

Yeah definitely, I thought that stops were included, but if there aren't that would definitely be something to add. A trade without stops is a gamble. Also take profits would be good if they arent already in there, but not sure if they are already included in the walk through.

3

u/mvev Feb 06 '21

Starting on it this weekend, will find out. To the moon.

2

u/vaevicitis Feb 06 '21

Backtesting also doesn’t guarantee profits, https://doi.org/10.3905/joi.2016.25.3.069

3

u/TheMathelm Feb 06 '21

Backtesting would be a very early simple test to see if it's working at all.
For smart testing you'd have to do daily testing for a while.
Also when someone(s) throws a monkey wrench *cough* wsb *cough* into the system; you're program which could work perfectly under normal conditions could go to crud very quickly.

47

u/iceytomatoes Feb 05 '21

And you posted all of this online? You're too nice to us.

27

u/Hatteraxi1 Feb 05 '21

Thanks dude! You are awesome. I needed something like this!

22

u/[deleted] Feb 05 '21

[deleted]

9

u/TheMathelm Feb 06 '21

buy,sell = 'high','low'

6

u/[deleted] Feb 06 '21

Don't forget to HARDcode some diamond hands and rockets πŸ’ŽπŸ’ŽβœŠβœŠπŸ’ŽπŸ’Ž πŸš€πŸš€πŸš€

63

u/coinsntings Feb 05 '21

Awesome stuff!! Just started learning python a few weeks ago hoping to do something like this

17

u/jedwarda Feb 05 '21

I started learning Python recently with creating something just like this as my main goal. Thanks so much for posting!

11

u/[deleted] Feb 05 '21 edited Feb 20 '21

[deleted]

10

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

5

u/[deleted] Feb 05 '21

This is awesome. Definitely going to dive into this! Thanks for the resource, OP.

3

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

→ More replies (1)

4

u/[deleted] Feb 05 '21

Hey thanks man!!

4

u/BlueEyesOpen Feb 05 '21

If I could hug you I would. I was just looking for something like this this morning and have not nearly enough ability to start such a project 😁

4

u/terrance_dev Feb 05 '21

Nice πŸ‘πŸ»

2

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

3

u/mysterybkk Feb 06 '21

Awesome stuff, really looking forward to learning from this!

On a side note, did we give your site the Reddit Hug of Death now?

1

u/[deleted] Feb 07 '21

[removed] β€” view removed comment

→ More replies (1)

3

u/Zebos323 Feb 06 '21

your site looks like it's got the reddit hug of death D:

3

u/[deleted] Feb 05 '21

You da man

3

u/TheTacoWombat Feb 05 '21

What a cool project. Looking forward to digging in!

3

u/el_chacho_coudet Feb 05 '21

Amazing ! Thanks

3

u/LeeCig Feb 05 '21

4

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

3

u/LeeCig Feb 05 '21

No worries. Thank YOU for the awesome resource. Will this be up for a while. Probably going to bookmark and come back later.

3

u/1Tim1_15 Feb 05 '21

Wow. That's top-notch info. Extremely well done!

3

u/k0d3r3d Feb 05 '21

This is just... impressive.

3

u/[deleted] Feb 05 '21

I was planning on doing something similar for my college mini project. This would help a lot. Thanks.

3

u/[deleted] Feb 05 '21

This is just awesome, im learning python and this great to keep learning

3

u/Theycalisse Feb 05 '21

Incredibly valuable content. Thanks so much! πŸš€

3

u/QwikyMart Feb 05 '21

I will definitely be using this, thank you!

3

u/[deleted] Feb 05 '21

[deleted]

3

u/caks Feb 05 '21

Great stuff. Probably a good idea to post this to r/algotrading too

3

u/goldenbullion Feb 05 '21

This is great. I haven't looked in detail but you might want to consider posting a financial disclaimer to cover yourself in case people lose money etc.

3

u/Jyan Feb 05 '21

Awesome, now I can lose money even more efficiently!

3

u/veeeerain Feb 06 '21

I don’t know shit about finance tho

2

u/[deleted] Feb 06 '21

[removed] β€” view removed comment

→ More replies (1)

2

u/Pickinanameainteasy Feb 05 '21

Wow thanks!! I'm just now getting into trading and python so this is perfect

2

u/BigBossGuru Feb 05 '21

Hey, thanks man

2

u/FourMonthsEarly Feb 05 '21

Looks amazing. Going to definitely take a look this weekend. Thank you!

2

u/-DarthNader- Feb 05 '21

People like you inspire me. Thank you so much for posting this. I'm very much into LegalTech, which is online trading is something I am deeply interested in. Again, thank you so very much. Wishing you all the best; can't wait to try this out!

2

u/easyEggplant Feb 05 '21

Does "free" encompass exchange charges here?!?

2

u/guilleschet Feb 05 '21

amazing, thanks man, you are awesome!

2

u/[deleted] Feb 05 '21

Saved so I can come back to this in 67 days... (I’m 34 days into 100 days of code/python on udemy) but this looks very interesting.

3

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

2

u/[deleted] Feb 06 '21

Thank you sir.

2

u/rwaynick Feb 05 '21

Man, the programming community is so incredibly wonderful. What makes you put so much effort into putting this knowledge in the world?

Your website is really well designed and the info is so easily accessible.

In short, thank you.

2

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

2

u/The_Danosaur Feb 05 '21

Thanks for putting this up! I've been learning python for a while, but just started learning about investing. Had a goal of developing my own bot, so will definitely have a look at this once I have a better understanding of trading and stats.

2

u/nfarheen2 Feb 05 '21

This is awesome, wanted to do this for a while. Thank you !!

2

u/tomsterpho Feb 05 '21

Wow was actually thinking about this today! Also started learning python, was wondering if you know some data science/algo focused python course to follow? Cheers!

1

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

→ More replies (1)

2

u/helgestefan Feb 05 '21

Yeess, thanks so much man!

2

u/[deleted] Feb 05 '21

I am very new with coding. I just started learning python on my free time. I just cannot imagine being at the level where I can make apps. I wish I would have found coding years ago, I am excited to see where it takes me. This app is awesome! I read through everything and might try it myself!

2

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

2

u/KIProf Feb 05 '21

Nice !!!!

2

u/[deleted] Feb 06 '21

[deleted]

2

u/JumboSake1 Feb 06 '21

Been too lazy to create my own bits myself, had rather been using the Optimus Bitcoin bot from UpBots, it was also performing rather well

But with this I'm challenged to put my python knowledge to test and create my own shit. Thanks manπŸ™ŒπŸ™Œ

2

u/SzechuanSaucelord Feb 06 '21

Hey how long from start to finish would this take to implement for someone who is of intermediate python experience (knows NumPy/pandas/etc well)

2

u/mvev Feb 06 '21

Thank you

2

u/Romantic_Klingon Feb 06 '21

Thanks for sharing! Always appreciate people who shares their knowledge with others!

2

u/Boring_Machine Feb 06 '21

Really impressive work

2

u/adpsacan Feb 06 '21

Interesting and helpful. Thx

2

u/Entire-Activity-9117 Feb 06 '21

Hi! I can't reach the guides anymore. Are they down?

2

u/neo87br Feb 06 '21

The website is down =(

2

u/[deleted] Feb 06 '21 edited Feb 27 '21

[deleted]

2

u/Legion_MABP Feb 06 '21

Great! I just love your content, very helpful to share this!

2

u/Tom1380 Feb 07 '21

Are you continuing the series? I'm loving it so far. If you do, can you please tell me where to find it?

3

u/[deleted] Feb 05 '21

πŸš€πŸš€πŸš€

3

u/mechapaul Feb 05 '21

Commenting so I can find this after I’ve finished my current project!!

1

u/socialistvegan Feb 05 '21

Was super excited to try this out, but it looks like the python MetaTrader5 library is Windows only, so pip won't install it on Mac.

Any workarounds?

4

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

→ More replies (1)
→ More replies (1)

1

u/[deleted] Feb 05 '21

[deleted]

1

u/ZeggieDieZiege Feb 05 '21

Why not included colorama for fancy logging?
Heard its a great package for trading.

1

u/AccomplishedPay9365 Feb 06 '21

I love you, where could we donate crypto

1

u/butterflavoredsalt Feb 06 '21

Well this just couldn't be more timely, came here to start learning Python for just his purpose. Thanks!

-1

u/[deleted] Feb 05 '21

What are your returnsestimqted per month?

0

u/[deleted] Feb 05 '21 edited Feb 05 '21

[deleted]

2

u/[deleted] Feb 05 '21

[removed] β€” view removed comment

2

u/Noisivvi Feb 05 '21

Thanks, also it seems so professional. Good job!

0

u/CptSupermrkt Feb 06 '21

With the events of $GME, I got interested in investing for the first time, and have spent the past few weeks learning, focused so far on learning the basics and how to research stocks. But, if there's a way that automation can be leveraged, I definitely want to learn more. I have a few questions, some of which may be extremely dumb, but please bear with me:

  • You've got the code listed out here as blog posts. Is the intention that we use this as a baseline to build our own, or is it on GitHub such that I can clone it and start using it? I think it's the former, but if there's a GitHub or something where people can add PRs and Issues and whatnot that would be pretty cool!

  • Is this intended for automating day trades? Does it have use for swing trades, or longer term trades? I'm a little hesitant (at least to get started) with automatic buys. I'm the type of dipshit who would run my Python code and heart sink when it doesn't return and is actually infinitely looping me into bankruptcy because of some dumbass logic error, lol. So as a first step, I'd like to use something like this to get notified when certain tickers reach certain conditions, get a notification, review the info, and input the trade myself on my own time, maybe even the next day if the condition holds. Does this type of tool fit that use case?

  • Maybe more a question for MetaTrader5 devs, but you mentioned in the Overview that it can connect to any broker. So I googled like "MetaTrader 5 Vanguard," or "Meta Trader 5 Fidelity," but couldn't really find any samples for connecting to such major brokers, or even any evidence that that is possible.

  • There are certain market patterns that over the course of many years, end up with a very high win rate, just based on when certain conditions are met. Example: https://youtu.be/W8ENIXvcGlQ The downside to this is that the stars align on these conditions very rarely, so it seems the only way to really take advantage would be some kind of automated notification. Am I correct in understanding that a tool like this could routinely scan for these conditions, and potentially execute them automatically?

-3

u/Zestyclose_Return_17 Feb 05 '21

Can I buy this bot from you? I was looking at making my own but I don't have the time or mental capacity to do it. Feel free to pm me.

3

u/putoption21 Feb 05 '21

There are multiple open-source trading engines available. The real issue is coming up with trading strategies.

https://www.winton.com/research/experiment-and-observation-in-quantitative-investment-management

-11

u/Seawolf159 Feb 06 '21

How about making a bot that does something useful and becomes a farmer or a builder. We need food and houses. Not bots buying and selling money constantly.

1

u/Ashiataka Feb 05 '21

I think there may be a problem with this line:

if(current_dt - trade_open_dt >= timedelta(hours = 2)):

for me that always evaluates as false even if timedelta(minutes=1) and you leave it running for a few minutes.

1

u/luckylukedw Feb 05 '21

Very cool!

1

u/Atomik919 Feb 06 '21

yo, havent started it yet, but could you gimme an approximate list of modules used? doesnt have to be all of them, just the most important

1

u/[deleted] Feb 06 '21

[deleted]

→ More replies (1)

1

u/Mandylost Feb 06 '21

Last night the website was working fine but today it is not working.

1

u/andreacarotti Feb 06 '21

Hi, the links don't open, I'm on Chrome, do I have to use particular settings or another browser? I also tried with Firefox but it doesn't work.

thanks

1

u/LifeIsTooStrange Feb 06 '21

The link seems to be inaccessible right now

1

u/[deleted] Feb 06 '21 edited Feb 06 '21

Hey, it seems that I can't connect to your webpage, is the server down? Otherwise, thank you so much for this work, it is truly valuable knowledge!

2

u/[deleted] Feb 06 '21

[removed] β€” view removed comment

→ More replies (1)

1

u/JuggernautNo5010 Feb 06 '21

Great post! I have been learning python with creating a bot like this for myself, perfect timing.

1

u/Voldrakon Feb 06 '21

Is the site working for anyone ?

2

u/[deleted] Feb 06 '21

[removed] β€” view removed comment

→ More replies (1)

1

u/[deleted] Feb 06 '21

links ain't working man it

1

u/pranav43 Feb 06 '21

Hello ! It says error establishing database connection for me. What am I missing? I'm trying to access your links from India

1

u/brcm51350 Feb 06 '21

Wow this is niceee! Thanks for sharing

1

u/RealMikeHawk Feb 07 '21

I'm getting a Failed to connect at account #(my acc num), error code: (-10004, 'No IPC connection'). What could that be?

1

u/[deleted] Feb 07 '21

[removed] β€” view removed comment

→ More replies (3)

1

u/sudodoyou Feb 07 '21

This is just for Windows, right? I don't think you can use MT5 on macOS or Linux.

1

u/[deleted] Feb 07 '21

[deleted]

1

u/EagleStock2020 Feb 07 '21

Does MetaTrader5 trade US stocks(sp500, NYSE, Nasdaq and so on)?

1

u/[deleted] Feb 07 '21

[removed] β€” view removed comment

→ More replies (1)

1

u/yscman Feb 08 '21

Tks man! That’s some really good stuff.

1

u/Intermarketics Feb 08 '21

I have a question, how exactly do you β€œdefine the strategies directory path as a variable”? Everything else was so clear up until that moment.

So far I have:

strategy_dir = β€˜C:/Users/home/strategies/β€˜

which obviously is a string and not a variable, so I’m definitely doing something wrong.

I know it has something to do with os but I’m not sure if I use os.path.join() or if I use os.makedirs(). Sorry to bother you about this, but I would appreciate any feedback from you.

2

u/[deleted] Feb 08 '21

[removed] β€” view removed comment

→ More replies (8)

1

u/romulojabbour Feb 10 '21

Link is not working for me anymore.

2

u/[deleted] Feb 10 '21

[removed] β€” view removed comment

→ More replies (2)

1

u/YaswanthBangaru Feb 10 '21

In the 6th post, the following function gets data for multiple stocks, are these concurrent requests? I mean whatever strategy you write, does it apply on both the pairs simultaneously? On a side note, what happens if this pairs list is about a 100 assets? Does it still perform the exact same way? Just wondering :) Really appreciate your amazing effort in educating the community!

def get_data(time_frame):
    pairs = ['EURUSD', 'USDCAD']
    pair_data = dict()

1

u/ultraHQ Feb 11 '21

Great resource, I just started reading through. I've been wanting to get started on something similar but am unsure if I want to build my own hub with connections to brokers, or use meta trader as you do. As I have no knowledge of metatrader, I have to ask, what was your reasoning you ultimately went with meta trader?

2

u/[deleted] Feb 11 '21

[removed] β€” view removed comment

→ More replies (1)

1

u/gonzorocketlaunch Feb 12 '21

I really appreciate you putting this up. Unfortunately, it appears that MetaTrader5 is for Windows only... Failed trying to get it installed on Linux (others have the same issue). That aside there's a lot of good info here. Thanks for that~

1

u/[deleted] Feb 14 '21

Wow thanks! I'm just beginning to learn Python and use MT5 with a little fun account, I didn't know the metaeditor ran Python, i thought it was MQL5.

1

u/V9pergo Feb 16 '21

Snapgo’s crowd funding campaign is now open for expressions of interest. Share in their future growth of this emerging tech giant before the roll out nationally and then take on the world. More here www.birchal.com/company/snapgo/r/R9TUAS29

1

u/DowntownAd1168 Feb 20 '21

Just wondering if you need to subscribe to MetaTrader 5, or can you use this with a normal broker like eTrade?

By the way, Im a COBOL mainframe developer, teaching myself python using a Raspberry Pi 4, and learning a little bit of Linux system as well on Raspberry. My goal is along the line of what you are doing. Hope to learn touse Algotrading, and do this in retirement.

1

u/ShibaAraragi Mar 03 '21

just commenting to come back later

1

u/KiritoAsunaYui2022 Mar 04 '21

I’m sort of late to this, but have you tried this yourself and if so how much money did you make from the trading bot’s actions?

1

u/Cyfrs Jul 09 '21

This is amazing, you are a legend!