Automate Trading Bot Parameter Optimization with Python: A Comprehensive Guide!

  Рет қаралды 9,988

CodeTrading

CodeTrading

Күн бұрын

In this video, we show you how to automate the optimization of trading bot parameters using Python. Our simple trading strategy, which we previously backtested and deployed live, required weekly updates to the trading parameters to keep the bot optimized on the most recent data.
To automate this process, we developed a Python code that includes the trading bot components and automates the optimization process. With this code, we can create a scheduler that runs the optimization process, finds the best SL and TP distances, and updates these parameters in the trading bot currently running our trades.
This process ensures that the trading bot uses the most recent data for its parameters, which is crucial for maintaining its performance. By automating this process, we save time and effort while also improving the bot's accuracy and efficiency.
In this video, we walk through the Python code and explain how it works.
previous videos detailing the strategy and the bot results:
* the strategy: • Trading with Python: S...
* live trading bot: • Live Trading Bot Strat...
* backtest and forward test: • How To Improve A Live ...
* automated trading results: • The Most Realistic Aut...
°°° Discount Vouchers for my Algorithmic Trading and Python courses:
💲 Algorithmic Trading: bit.ly/CouponA...
💲 Data Analysis with Numpy and Pandas: bit.ly/CouponD...
💲 Machine Learning Methods In Algorithmic Trading: bit.ly/CouponM...
The python file used in this video:
drive.google.c...
Main topics of videos on this channel: trading bot, Python, automation, optimization, parameters, Stop Loss distance, Take Profit, backtesting, heatmap, scheduler, efficiency, accuracy, time-saving, coding, data analysis, machine learning, artificial intelligence, quantitative trading, algorithmic trading, finance, economics, technology, programming.
Enjoy coding and good luck! ... if you are still reading subscribe it's a nice and cozy channel :)

Пікірлер: 75
@quickmoment12221
@quickmoment12221 4 ай бұрын
Good work as usual
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Appreciate that, thank you!
@PWN-FOREX-vw3gy
@PWN-FOREX-vw3gy 3 ай бұрын
Very interesting Video, thanks!
@CodeTradingCafe
@CodeTradingCafe 3 ай бұрын
Glad you enjoyed it! Thank you for your support.
@ShimoriUta77
@ShimoriUta77 4 ай бұрын
Lovely video! I dream of building something like this.
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Go for it! you can do it, one line at a time!
@gonzalodiazamor5494
@gonzalodiazamor5494 4 ай бұрын
Love it too!! I think this is one of the best video I've ever see about optimization in trading! :)
@abdsh422
@abdsh422 4 ай бұрын
Hi, Yes, I have been through this and I fixed almost 7 bugs that were causing a significant gap between the backtest and reality. If you have any challenging points, I am interested in discussing them with you. If you're interested. Once again, thank you for your great effort.
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hey, thank you for your efforts, did you mean on the same strategy? because yes there is a gap between backtests and live trading.
@SomeoneElsesSomeoneElse
@SomeoneElsesSomeoneElse 4 ай бұрын
I believe the logic you're using for fitting_job will fire continuously like a while loop between the hours of 12am and 7:05am on Monday's. I would constrain the fitting window to a specific time on a specific day.
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hey, actually it will fire once on Monday because the cron scheduler is calling the trading_job function only from 7 to 18 monday to friday (check line 194 in the code) and the condition now.weekday() == 0 and now.hour < 7 and now.minute < 5 will end up calling the fitting once on Monday at 7 or so. I hope it's clear, I do admit the way it was coded is not very clear though.
@adamscott217
@adamscott217 4 ай бұрын
Has anyone ever front/forward tested a strategy and make good returns ? anyone who provided proof online that I could find had like 14% a year gain etc (still good of course) but you always see back tests with like 500% a year lol
@someoneelse3740
@someoneelse3740 4 ай бұрын
Yes i have, i make approx. 100% per year but it only works if i consistently use just around 20%-30% of the initial capital (safety reason and drawdown possability) over 5 years so for example 100$ -> Tradesize 20$ - 30%, gain 100% per market per year approx. 20$-30$ a year. That is 20-30% / Year. But its no way near to use it for a living
@abdsh422
@abdsh422 4 ай бұрын
@@someoneelse3740 yes i was about saying the same.
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hi check this video for forward testing kzbin.info/www/bejne/mYDTpXlqnciMntU yes there are strategies that work live, mainly on the daily dataframe making around 10% yearly (you can increase this by trading multiple assets but you need more money). The challenging part is to automate lower timeframes like 5 min or so, looks too hectic.
@m.agussantoso
@m.agussantoso 4 ай бұрын
Great work
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Thank you! Cheers!
@eitan71
@eitan71 4 ай бұрын
great idea
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Thank you for your support.
@vassiliscapsis5828
@vassiliscapsis5828 4 ай бұрын
Great !!! Thank you
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Thank you too!
@JulienWaxweiler
@JulienWaxweiler 3 ай бұрын
Hello i couldn't manage to generate access token with an oanda demo account is a real account necessary ?
@CodeTradingCafe
@CodeTradingCafe 3 ай бұрын
Hi, no the real account is not mandatory, but I think it depends where you live sometimes the option is not available, try to check the Oanda website again maybe contact support to ask about this (just a short message to know if Python API is available in your region)
@rverm1000
@rverm1000 4 ай бұрын
nice effort.
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Thanks a lot
@jaydy71
@jaydy71 Ай бұрын
Very interesting again! I'm wondering though, considering things are optimized on just 1 week. One one hand it's great that it can quite quickly adapt things to the current state of affairs. On the other hand, how will this behave in unpredictable events? Like a sudden crash (because of bad news about the traded asset or something)?
@CodeTradingCafe
@CodeTradingCafe Ай бұрын
Hi, it's completely random I would say for crash events, you might be on the right side or on wrong side of the market, nothing is coded for this in this model. probably adding a condition where if you get 3 consecutive losses or a certain amount of loss threshold stop the bot and send notification to the human trader to come check and relaunch, this is relatively easy to do and probably the safest.
@jaydy71
@jaydy71 Ай бұрын
@@CodeTradingCafe This is why I tend to prefer using machine learning trained on a lot of history. I think it defends me more against such events because the model has seen it before. And in my personal experience (for what it's worth) it has proven to be quite robust during such events. For example I've seen multiple times that my strategy sometimes loses a lot of money during market crashes but then quite quickly makes it back (and often even get to a net profit surprisingly quickly). But my approach of depending on machine learning still leaves me with a lot of unknowns of what it is actually doing, what my training features actually mean, etc. And that's how I learn a lot from your videos because you give me a lot of insight into things that I didn't consider before.
@CodeTradingCafe
@CodeTradingCafe Ай бұрын
Hi good to know, from my side Machine Learning was much slower than classic strategies, which is why I mostly focus on these. As long as it works for you it's a good sign. But market crashes/high volatility tend to differ in pattern from time to time so keep a safety rule somewhere just in case.
@ptfa3212
@ptfa3212 4 ай бұрын
hii, great work buddy can i have a separate video on how we can optimize on lower time frame and use cloud for automation
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hi, lower timeframe just exactly the same as in this video, for the cloud yes I will make a video how to deploy on the cloud.
@abdsh422
@abdsh422 4 ай бұрын
What was the result of testing the bot so far
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hi, I killed it few months ago, I need to recheck edge cases and see where it is failing exactly for which trades maybe add a filter later... got distracted with other strategies.
@abdsh422
@abdsh422 4 ай бұрын
@@CodeTradingCafe Hi, Yes, I have been through this and I fixed almost 7 bugs that were causing a significant gap between the backtest and reality. If you have any challenging points, I am interested in discussing them with you. If you're interested. Once again, thank you for your great effort.
@abdsh422
@abdsh422 4 ай бұрын
@@CodeTradingCafe Hi, Yes, I have been through this and I fixed almost 7 bugs that were causing a significant gap between the backtest and reality. If you have any challenging points, I am interested in discussing them with you. If you're interested. Once again, thank you for your great effort.
@AI-ART-333
@AI-ART-333 4 ай бұрын
@CodeTradingCafe Your content is good, but I think overfitting is a major issue with any kind of machine learning. Stop-loss and take-profit can also have an overfitting issue. The solution is to take the chosen parameters (the best and the average results from the optimization test) and run them on an untrained data set with an equal or bigger size than the optimized test. So, if you ran a backtest on the last 6 months, then you should run the resulting parameters on the 6 months before it. That's the best way to get an "unbiased opinion" from the data , Thanks again .
@ehabmamdouh9519
@ehabmamdouh9519 3 ай бұрын
May i ask what platform the bot trade on? Thanks 🙏🏼
@CodeTradingCafe
@CodeTradingCafe 3 ай бұрын
Hi, it runs locally on your PC and it connects with Oanda's API you can also use the code and adapt the functions to make it work with Binance or interactive brokers or any broker if they provide Python API.
@alexandergnu4961
@alexandergnu4961 4 ай бұрын
Very good work! One thing that I would add is: after developing the code for automatic optimization, why not backtesting the new workflow with history data (e.g. last year) and then compare with the previous performance?
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
It's a good idea and I am still intending to go back to this strategy, even testing this live again. I just needed a break and bringing new strategies to the channel (it's boring for viewers to keep publishing videos on the same strategy). But yes I will bring it back live with some improvements.
@alexandergnu4961
@alexandergnu4961 4 ай бұрын
@@CodeTradingCafe Again, very good work!
@andrewbisiriyu7393
@andrewbisiriyu7393 4 ай бұрын
Thank you for your work. Am not sure what needs to be done about this. I changes the bollinger band length to 26 and the job failed with error message pointing to bbl 15 1.5 which I think is the field. Also I noticed that when the ema fast and slow length and are changed the signal detected gets cancelled during the job execution. Can you please advice what codes I need to tweak on both scenarios to get the script to work. Thank you
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hi yes if you change the length of BB the name of the produced columns in the dataframe will change so you need to replace with correct name in your code. The second issue is also normal, by changing the EMA lengths the trading conditions need to be verified so the signals will change accordingly. I hope this helps, good luck!
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hi, thank you for your support. The first issue is that when you change the length of the BB the name of the produced columns in dataframe will also change so you need to update the code with the new columns names. The second issue is probably that the conditions are not verified to get the same signals after you change the EMA lengths, try to apply small changes at first to sense the indicator. I hope this helps, good luck!
@simulateworld3198
@simulateworld3198 4 ай бұрын
May I know why you are not using freqtrade for algorithmmic trading?
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
I work with python every day I find it easier for me
@hacklab6757
@hacklab6757 4 ай бұрын
Nice
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Thank you for your support!
@poisonza
@poisonza 4 ай бұрын
i would use state space model to choose the parameters. insample optimization kinda lag for some asset class
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hi, thank you, I think in this case it doesn't matter since it's one or 2 optimizations per week, but you are right if this is to be more frequent it might add some lag.
@EvgenyDev
@EvgenyDev 4 ай бұрын
Hey how long have you been doing all of this? Can you suggest any good books to start learning? :) thanks in advance
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Hi, coding since 2002, coding in trading more than 10 years (not on youtube though) started with mql5 and moved to python almost 5 years ago. Books there are plenty depending on your target, I think "Profitability and Systematic trading" for Michael Harris is a good start for the trading part, but you also need to know python and data analytics to code strategies.
@sebaamohammedreda459
@sebaamohammedreda459 4 ай бұрын
Could you please do ICT strategy
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
This is a good idea, it's already on my list haven't had the time to code it yet...
@sebaamohammedreda459
@sebaamohammedreda459 4 ай бұрын
@@CodeTradingCafe thank you for your effort sir, yes i am thinking of it also it is a good strategy and i would realy like to have it backtested. It would be great
@CodeNeural
@CodeNeural 4 ай бұрын
I know you prefer to build out your strategies without fees, but shouldn't there be a check on the ATR derived TP to ensure that it will cover the fees / average spread, as a minimum?
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
you are absolutely right theoretically we should include this, the reason I got lazy and I didn't is that Oanda doesn't allow a trade to open if the TP or SL are too close to the entry position it will simply return an error message, so yes I counted on errors to correct the gap in my coding (shame :) )
@CodeNeural
@CodeNeural 4 ай бұрын
@@CodeTradingCafe I wasn't thinking about errors or the code itself- the coding and ideas here are excellent and I always look forward to your inspirational output. I was actually thinking about the ATR derived target. Say the ATR value gives a TP of .3%, but the minimum fee / spread is .4%. That trade is a loser before it has even hit the order book. However, it looks like you are saying that Oanda prevents TP/SL that close to the spread, so it isn't an issue in live trading, but I think it would impact the back test model.
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
True, this causes bias in the backtest, now if you add a parameter commission=0.0002 to the backtest function it will account for the average spread in Forex (as per the backtesting documents description), in this case when the TP or SL are too close to entry position the backtest also returns an error and aborts, so it forces a wider TP and SL. Regarding the 0.0002 again it's an estimated spread average for Forex only, and it remains constant during the whole backtest, it's an approximation but it avoids using 2 data sets Bid and Ask prices.
@OsirisHorscow
@OsirisHorscow 4 ай бұрын
Does your current paper account strategy here really not have any RSI requirement on the signal?
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
you could use the RSI to detect momentum/trend or 2 moving averages also work well, for this strategy I think it's sufficient.
@shoaibshahbaz6384
@shoaibshahbaz6384 4 ай бұрын
Price of bot ?
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
If it's not too much time coding depending on the strategy you need to implement.
@Botttl
@Botttl 4 ай бұрын
Hey man amazing work. I need your help as i am just getting started in python for backtesting i have zero knowledge of coding can you suggest some sources that would help a beginner... And also in all you videos do you use the backtrader library? And please guide me through all the pitfalls that you might have gone through in the beginning. Please.
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
The first pitfall don't rush, give python 2 months of learning, trading a year or 2 of learning and code strategies in parallel, slowly, all the mistakes are made when rushing. Resources, plenty of python courses online on YT, later for Quant trading some books might be useful Check those of Robert Carver as a good start.
@Botttl
@Botttl 4 ай бұрын
@@CodeTradingCafe and what about which libraries to use ?
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Libraries there are plenty for backtesting, and for indicators talib and pandas_ta the rest are for mathermatics stats and regression (numpy, pandas, etc...)
@Botttl
@Botttl 4 ай бұрын
@@CodeTradingCafe and which is beginner friendly backtrader or backtesting. Py
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Both are good but I find backtesting.py maybe slightly easier with the documentation they have
@hacklab6757
@hacklab6757 4 ай бұрын
Nice
@CodeTradingCafe
@CodeTradingCafe 4 ай бұрын
Thanks
ПРИКОЛЫ НАД БРАТОМ #shorts
00:23
Паша Осадчий
Рет қаралды 6 МЛН
Шок. Никокадо Авокадо похудел на 110 кг
00:44
Will A Guitar Boat Hold My Weight?
00:20
MrBeast
Рет қаралды 238 МЛН
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 8 МЛН
Trading with Python: Simple Scalping Strategy
13:47
CodeTrading
Рет қаралды 94 М.
3 Essential Methods for Risk Management & Algorithmic Trading
21:27
This is How I Scrape 99% of Sites
18:27
John Watson Rooney
Рет қаралды 74 М.
Live Trading Bot Strategy In Python
14:57
CodeTrading
Рет қаралды 68 М.
Python Options Trading Bot Interactive Brokers
26:51
Jacob Amaral
Рет қаралды 21 М.
How to Code a AI Trading bot (so you can make $$$)
35:09
Nicholas Renotte
Рет қаралды 636 М.
ПРИКОЛЫ НАД БРАТОМ #shorts
00:23
Паша Осадчий
Рет қаралды 6 МЛН