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

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 พ.ค. 2024
  • 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/CouponAlgorithmicTrading
    💲 Data Analysis with Numpy and Pandas: bit.ly/CouponDataAnalysis
    💲 Machine Learning Methods In Algorithmic Trading: bit.ly/CouponMachineLearningT...
    The python file used in this video:
    drive.google.com/file/d/1WZDz...
    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 :)

ความคิดเห็น • 71

  • @quickmoment12221
    @quickmoment12221 20 วันที่ผ่านมา +1

    Good work as usual

    • @CodeTradingCafe
      @CodeTradingCafe  20 วันที่ผ่านมา

      Appreciate that, thank you!

  • @m.agussantoso
    @m.agussantoso 21 วันที่ผ่านมา +1

    Great work

  • @vassiliscapsis5828
    @vassiliscapsis5828 21 วันที่ผ่านมา +1

    Great !!! Thank you

  • @ShimoriUta77
    @ShimoriUta77 21 วันที่ผ่านมา +2

    Lovely video! I dream of building something like this.

    • @CodeTradingCafe
      @CodeTradingCafe  21 วันที่ผ่านมา +1

      Go for it! you can do it, one line at a time!

    • @gonzalodiazamor5494
      @gonzalodiazamor5494 9 วันที่ผ่านมา

      Love it too!! I think this is one of the best video I've ever see about optimization in trading! :)

  • @eitan71
    @eitan71 21 วันที่ผ่านมา +1

    great idea

    • @CodeTradingCafe
      @CodeTradingCafe  21 วันที่ผ่านมา +1

      Thank you for your support.

  • @rverm1000
    @rverm1000 20 วันที่ผ่านมา +1

    nice effort.

  • @SomeoneElsesSomeoneElse
    @SomeoneElsesSomeoneElse 21 วันที่ผ่านมา

    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  21 วันที่ผ่านมา +1

      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.

  • @abdsh422
    @abdsh422 21 วันที่ผ่านมา

    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  21 วันที่ผ่านมา

      Hey, thank you for your efforts, did you mean on the same strategy? because yes there is a gap between backtests and live trading.

  • @adamscott217
    @adamscott217 21 วันที่ผ่านมา +5

    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 21 วันที่ผ่านมา +3

      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 21 วันที่ผ่านมา

      @@someoneelse3740 yes i was about saying the same.

    • @CodeTradingCafe
      @CodeTradingCafe  21 วันที่ผ่านมา +1

      Hi check this video for forward testing th-cam.com/video/cOptF4hcYhs/w-d-xo.html
      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.

  • @ptfa3212
    @ptfa3212 20 วันที่ผ่านมา +1

    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  20 วันที่ผ่านมา

      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.

  • @poisonza
    @poisonza 21 วันที่ผ่านมา +1

    i would use state space model to choose the parameters. insample optimization kinda lag for some asset class

    • @CodeTradingCafe
      @CodeTradingCafe  21 วันที่ผ่านมา

      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.

  • @alexandergnu4961
    @alexandergnu4961 11 วันที่ผ่านมา

    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  11 วันที่ผ่านมา +1

      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 10 วันที่ผ่านมา

      @@CodeTradingCafe Again, very good work!

  • @hacklab6757
    @hacklab6757 21 วันที่ผ่านมา

    Nice

  • @andrewbisiriyu7393
    @andrewbisiriyu7393 9 วันที่ผ่านมา +1

    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  9 วันที่ผ่านมา

      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  9 วันที่ผ่านมา

      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!

  • @EvgenyDev
    @EvgenyDev 16 วันที่ผ่านมา +1

    Hey how long have you been doing all of this? Can you suggest any good books to start learning? :) thanks in advance

    • @CodeTradingCafe
      @CodeTradingCafe  16 วันที่ผ่านมา

      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.

  • @Botttl
    @Botttl 9 วันที่ผ่านมา

    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  9 วันที่ผ่านมา

      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 8 วันที่ผ่านมา

      @@CodeTradingCafe and what about which libraries to use ?

    • @CodeTradingCafe
      @CodeTradingCafe  8 วันที่ผ่านมา

      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 7 วันที่ผ่านมา

      @@CodeTradingCafe and which is beginner friendly backtrader or backtesting. Py

    • @CodeTradingCafe
      @CodeTradingCafe  7 วันที่ผ่านมา

      Both are good but I find backtesting.py maybe slightly easier with the documentation they have

  • @JulienWaxweiler
    @JulienWaxweiler 4 วันที่ผ่านมา +2

    Hello i couldn't manage to generate access token with an oanda demo account is a real account necessary ?

    • @CodeTradingCafe
      @CodeTradingCafe  4 วันที่ผ่านมา +1

      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)

  • @abdsh422
    @abdsh422 21 วันที่ผ่านมา +4

    What was the result of testing the bot so far

    • @CodeTradingCafe
      @CodeTradingCafe  21 วันที่ผ่านมา +1

      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 21 วันที่ผ่านมา

      @@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 21 วันที่ผ่านมา

      @@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 16 วันที่ผ่านมา

      @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 .

  • @simulateworld3198
    @simulateworld3198 20 วันที่ผ่านมา +1

    May I know why you are not using freqtrade for algorithmmic trading?

    • @CodeTradingCafe
      @CodeTradingCafe  20 วันที่ผ่านมา +2

      I work with python every day I find it easier for me

  • @ehabmamdouh9519
    @ehabmamdouh9519 14 ชั่วโมงที่ผ่านมา +1

    May i ask what platform the bot trade on? Thanks 🙏🏼

    • @CodeTradingCafe
      @CodeTradingCafe  4 ชั่วโมงที่ผ่านมา

      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.

  • @sebaamohammedreda459
    @sebaamohammedreda459 16 วันที่ผ่านมา +1

    Could you please do ICT strategy

    • @CodeTradingCafe
      @CodeTradingCafe  16 วันที่ผ่านมา +1

      This is a good idea, it's already on my list haven't had the time to code it yet...

    • @sebaamohammedreda459
      @sebaamohammedreda459 12 วันที่ผ่านมา

      @@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

  • @OsirisHorscow
    @OsirisHorscow 21 วันที่ผ่านมา

    Does your current paper account strategy here really not have any RSI requirement on the signal?

    • @CodeTradingCafe
      @CodeTradingCafe  21 วันที่ผ่านมา

      you could use the RSI to detect momentum/trend or 2 moving averages also work well, for this strategy I think it's sufficient.

  • @CodeNeural
    @CodeNeural 15 วันที่ผ่านมา

    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  14 วันที่ผ่านมา +1

      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 13 วันที่ผ่านมา

      @@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  13 วันที่ผ่านมา

      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.

  • @shoaibshahbaz6384
    @shoaibshahbaz6384 20 วันที่ผ่านมา +1

    Price of bot ?

    • @CodeTradingCafe
      @CodeTradingCafe  20 วันที่ผ่านมา

      If it's not too much time coding depending on the strategy you need to implement.

  • @legends_talk1
    @legends_talk1 21 วันที่ผ่านมา

    Show us the results

    • @CodeTradingCafe
      @CodeTradingCafe  21 วันที่ผ่านมา

      I killed it few months ago, check the links in the description all the details are in there, from birth to death ( I am refining the strategy I will launch the bot live again later when I am done)

  • @hacklab6757
    @hacklab6757 21 วันที่ผ่านมา

    Nice

    • @CodeTradingCafe
      @CodeTradingCafe  21 วันที่ผ่านมา

      Thank you for your support!