Why Your Indicators Aren’t Working: And How to Optimize Them for Profit

Поділитися
Вставка
  • Опубліковано 30 вер 2024

КОМЕНТАРІ • 95

  • @Adam-fy8xo
    @Adam-fy8xo 3 місяці тому +7

    I just discovered your channel, that's insane content ! Hundred of thanks

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      Glad you enjoy it! Thank you for your support!

  • @gvidalcantavella
    @gvidalcantavella Місяць тому +2

    I have an idea for the next video, I'm currently experimenting to set a dynamic stop-loss using Parabolic SAR without setting a Take Profit, maybe that should help specially for Scalping strategies. Thank you for your video :)

    • @CodeTradingCafe
      @CodeTradingCafe  Місяць тому

      Thank you for your support, I will keep the SAR in mind, haven't done any video about it yet!

    • @vassiliscapsis5828
      @vassiliscapsis5828 Місяць тому

      the idea of using a SAR as a stop loss is valid and is almost like using a moving average as a trailing stop and works on any time frame. Of course there will be cases where you wished you had a take profit because the retracement took all your paper profits away and cases where price moves without looking back and you multiply your profits

    • @gvidalcantavella
      @gvidalcantavella Місяць тому

      @@vassiliscapsis5828 yes, that's it, by just setting this trailing stop loss with no tp, I experimented that some nice profits on super large trends, but only relying on this makes large drawdawns.

  • @GntcFrk
    @GntcFrk 3 місяці тому +7

    Seems like a good way to overfit your algos/ indicators :D

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +4

      I was waiting for someone to comment about this :) I agree overfitting is to be avoided, but I am still convinced, in general, if you overfit on 5 years of data all together what are the chances the indicator will fail just this month? ( I mean theoretically 5 years is a lot of data and it's not one specific case)

    • @GntcFrk
      @GntcFrk 3 місяці тому +1

      @@CodeTradingCafe You're right. 5 years is a lot of data. Unfortunately, past performance is no guarantee of future performance :( I always optimized my strategies and found that after a few weeks/months they were performing worse and worse, so that continuous adjustment was necessary. I no longer work with indicators at all because the algorithms usually have too many degrees of freedom. I prefer to scalp Nasdaq and S&P futures by analyzing the order flow. This works regardless of market phases, whereas indicators sometimes only work under certain conditions :)

    • @Chuckn0ris
      @Chuckn0ris 3 місяці тому +4

      Exactly why you break your data up so you can forward test unseen data.😊

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      order flow is a good alternative, I also found support resistance and price patterns work well if you think of what's happening in the background.

    • @mikilyasmikilyas9222
      @mikilyasmikilyas9222 3 місяці тому

      ​@@CodeTradingCafe Exactly what I did! For 10 coins got 1million hist candles for each and optimized stops, takes and indicators that I use. Results are awesome in real trading. But I also fixed the order enter price from fix Close to random between open/close. And results now are up to 100% accurate as in real life trading

  • @mavricsalvaje
    @mavricsalvaje 2 місяці тому +1

    Have you built a time series pattern algorithm that checks day close to next day close, including premarket and postmarket for ES? This is not a technical pattern trading pattern like head and shoulders, etc. It's like a Stochastic signal with repeated patterns in time-series. There are about 11 repeated day trade time-series patterns. Could you look at the ES to scan repeated time-series patterns? Python would likely catch half of the signals in 60 days of real-time trading with yfinance. Timeframe would be 5 minute and 15 minutes for the time-series pattern signals.

    • @CodeTradingCafe
      @CodeTradingCafe  2 місяці тому

      Hi, I think I understand what you mean, I did try time series before I also tried facebook's algorithm Prophet check this video: ua-cam.com/video/lmoHsgSKBNA/v-deo.html

  • @s36ast1an
    @s36ast1an Місяць тому +1

    Can you please implement Walk Forward Optimization to this strategy.? Simply find best in sample and out sample parameters - instead of finding best indicator param for whole DF, find best indi for in sample, and test on out sample and iterate in walk forward optimkzation.

    • @CodeTradingCafe
      @CodeTradingCafe  Місяць тому +1

      Hi, check this video ua-cam.com/video/cOptF4hcYhs/v-deo.html
      Forward testing is included but for a different indicator, you can merge both codes for a quick start.

  • @poplolyipp
    @poplolyipp 2 місяці тому +1

    I did not know this exist so I builted my own backtester with the optimizer function for indecators LoL

    • @CodeTradingCafe
      @CodeTradingCafe  2 місяці тому

      In Python everything already exists lol welcome aboard

  • @sbqb21
    @sbqb21 Місяць тому +2

    Great stuff. Got case study where im trying to categorize large pip movements in the market and try to see hidden unkown patterns or characteristics of those pip movements to get a mean value of indicators on those movements maybe you help with that. Thanks

    • @CodeTradingCafe
      @CodeTradingCafe  Місяць тому +1

      Hi provide a bit more details here I will check it out.

  • @mohammadnematollahi717
    @mohammadnematollahi717 2 місяці тому +1

    Hello my friend
    Please answer my question to solve my problem
    I'm a Laravel (PHP) programmer, but I need to do a project that treats Python like this, the structure of the project is like this:
    We have a chart and whenever our chart comes to buy and sell patterns it tells us when it's time to buy or sell, patterns like head and shoulder or triangle, etc. Or the patterns that traders use
    I want this course to help me ؟

    • @CodeTradingCafe
      @CodeTradingCafe  2 місяці тому

      Hi, sorry I don't code in PHP but I do have couple of videos around triangle and flag patterns, head and shoulders as well, check them out they might be of help.

    • @mohammadnematollahi717
      @mohammadnematollahi717 2 місяці тому

      @@CodeTradingCafe Wow, thank you very much my friend for your reply

  • @robertbendkowski3385
    @robertbendkowski3385 3 місяці тому +2

    Using a heatmap while optimizing a strategy makes me feel like a badass data scientist. Dunno why :D

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      lol it has colors :D I like visuals too, if you are into data science and visualization check these 2 sites for cool ideas:
      www.data-to-viz.com/
      datavizproject.com/

  • @muhireinnocent2371
    @muhireinnocent2371 3 місяці тому +2

    Actually when i use 1hour eurusd data instead of 30m data , the returns increases from 16 to 61%.

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      Yes if the commissions are 0, usually it goes this way, but if you add a slight commission lower timeframes become more challenging.

  • @sothisremindsmeof.....1179
    @sothisremindsmeof.....1179 Місяць тому +1

    I would be so happy if you could reply this asap but I respect your time and all the effort you put into your videos so I can only hope you do.
    My question is whether you used machine learning in this video, there is no mention of it but I wonder if there is a machine learning model that can be used optimize indicators.
    I am working on a research topic and machine learning learning application in algorithmic trading is an area of interest but I want something simple I can do and complete within limited time, given I am a novice in python
    Thank you

    • @CodeTradingCafe
      @CodeTradingCafe  Місяць тому +1

      In this video no I haven't. Previous videos I tested some ideas nothing really impressive with ML results for trading:
      ua-cam.com/video/lhrCz6t7rmQ/v-deo.html
      ua-cam.com/video/tNFnACpzxVw/v-deo.html
      ua-cam.com/video/m1rY2J8ZlsY/v-deo.html

    • @sothisremindsmeof.....1179
      @sothisremindsmeof.....1179 Місяць тому +1

      @@CodeTradingCafe Thanks for your fast response! In your video comparing technical indicators, do you think using a short number of days instead of a 200-250pip count would yield better results?

    • @CodeTradingCafe
      @CodeTradingCafe  Місяць тому

      There is only one way to find out, by running the simulation, it's very hard to forecast ML it's somehow a black/grey box I would say.

  • @jroche1832
    @jroche1832 3 місяці тому +1

    Is there a way to test for highest return with lowest max drawdown. I have a number of algos that perform extremely well but suffer from high max drawdown. I would like to trade off % return against max drawdown however the optimizer only seems to return a heat map of stop loss and take profit (whatever metric you use) that result in % returns regardless of Max DD. I guess I could roll my own but it would be a painful job

    • @jroche1832
      @jroche1832 3 місяці тому

      Had a fiddle with this and there is no easy way so heres the code to roll your own
      ## DEFINE BACKTEST PARAMS ##
      lower_b = 10
      upper_b = 31
      slcoef_values = [i / 10 for i in range(lower_b, upper_b)] # 1.0 to 3.0 in steps of 0.1
      TPSLRatio_values = [i / 10 for i in range(lower_b, upper_b)] # 1.0 to 3.0 in steps of 0.1
      for slcoef, TPSLRatio in itertools.product(slcoef_values, TPSLRatio_values):
      MyStrat.slcoef = slcoef
      MyStrat.TPSLRatio = TPSLRatio

      bt = Backtest(data, MyStrat, cash=capital)
      stats = bt.run()
      ### REPORT INFO I WANT ###
      algo_ret = capital + capital*(getattr(stats,'Return [%]')/100)
      BH_Ret = capital + capital*(getattr(stats,'Buy & Hold Return [%]')/100)
      out_under_perform = getattr(stats,'Return [%]') - getattr(stats,'Buy & Hold Return [%]')
      getattr(stats,'Max. Drawdown [%]')
      # oup_dollars = getattr(stats,'Equity Final [$]') - ((getattr(stats,'Buy & Hold Return [%]')/100) * capital)
      oup_dollars = algo_ret - BH_Ret
      ### DF FOR EACH RUN IE DATA I WANT ###
      ### NB max_drawdown ROUTINE NOT INCLUDED FOR B&H ###
      df_res = pd.DataFrame({'Target' : ticker,
      'SL/TP' : str(slcoef) + '/' + str(TPSLRatio),
      'Buy Hold %' : getattr(stats,'Buy & Hold Return [%]'),
      'Return %' : getattr(stats,'Return [%]'),
      'B&H Ret$' : '${:,.2f}'.format(BH_Ret),
      'Algo Ret$' : '${:,.2f}'.format(algo_ret),
      'Out/Under Perform %' : out_under_perform,
      'Out/Under Perform $' : '${:,.2f}'.format(oup_dollars),
      'Annual Return %' : getattr(stats,'Return (Ann.) [%]'),
      'Win Rate' : getattr(stats,'Win Rate [%]'),
      'Annual Vol' : getattr(stats,'Volatility (Ann.) [%]'),
      'Max DD' : getattr(stats,'Max. Drawdown [%]'),
      'B&H Max DD': (-1*max_drawdown(data.Close))*100,
      'Avg DD' : getattr(stats,'Avg. Drawdown [%]'),
      'Total Trades' : getattr(stats,'# Trades'),
      'Sharpe Ratio' : getattr(stats,'Sharpe Ratio'),
      'Profit Factor' : getattr(stats,'Profit Factor')},index=[1])

      df_aggr = pd.concat([df_aggr, df_res], ignore_index=True)
      df_aggr = df_aggr.sort_values(by=['Return %', 'Max DD'], ascending=[False, True])
      df_aggr.to_csv('C:\\temp\\Agregated_Results_YEARS.csv', encoding='utf-8', index=False)

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      The easiest is simply to optimize for minimal drawdown then the obtained return percentage is a result of this.

    • @jroche1832
      @jroche1832 2 місяці тому

      @@CodeTradingCafe an alternative: # Define parameter ranges
      lower_b = 10
      upper_b = 31
      slcoef_values = [i / 10 for i in range(lower_b, upper_b)] # 1.0 to 3.0 in steps of 0.1
      TPSLRatio_values = [i / 10 for i in range(lower_b, upper_b)] # 1.0 to 3.0 in steps of 0.1
      ## ALGO CODE HERE ##
      for slcoef, TPSLRatio in itertools.product(slcoef_values, TPSLRatio_values):
      MyStrat.slcoef = slcoef
      MyStrat.TPSLRatio = TPSLRatio

      bt = Backtest(data, MyStrat, cash=capital)
      stats = bt.run()
      algo_ret = capital + capital*(getattr(stats,'Return [%]')/100)
      BH_Ret = capital + capital*(getattr(stats,'Buy & Hold Return [%]')/100)
      out_under_perform = getattr(stats,'Return [%]') - getattr(stats,'Buy & Hold Return [%]')
      getattr(stats,'Max. Drawdown [%]')
      # oup_dollars = getattr(stats,'Equity Final [$]') - ((getattr(stats,'Buy & Hold Return [%]')/100) * capital)
      oup_dollars = algo_ret - BH_Ret

      # What is the average stop loss and the average take profit
      num_recs = len(tp_long)
      tp_average_value = round(np.mean(tp_long[0:num_recs]),2)
      sl_average_value = round(np.mean(sl_long[0:num_recs]),2)

      df_res = pd.DataFrame({'Target' : ticker + "_" + test,
      'SL/TP' : str(slcoef) + '/' + str(TPSLRatio),
      'Avg SL/TP': str(sl_average_value) + ' / ' + str(tp_average_value),
      'Buy Hold %' : getattr(stats,'Buy & Hold Return [%]'),
      'Return %' : getattr(stats,'Return [%]'),
      'B&H Ret$' : '${:,.2f}'.format(BH_Ret),
      'Algo Ret$' : '${:,.2f}'.format(algo_ret),
      'Out/Under Perform %' : out_under_perform,
      'Out/Under Perform $' : '${:,.2f}'.format(oup_dollars),
      'Annual Return %' : getattr(stats,'Return (Ann.) [%]'),
      'Win Rate' : getattr(stats,'Win Rate [%]'),
      'Annual Vol' : getattr(stats,'Volatility (Ann.) [%]'),
      'Max DD' : getattr(stats,'Max. Drawdown [%]'),
      'B&H Max DD': (-1*max_drawdown(data.Close))*100,
      'Avg DD' : getattr(stats,'Avg. Drawdown [%]'),
      'Total Trades' : getattr(stats,'# Trades'),
      'Sharpe Ratio' : getattr(stats,'Sharpe Ratio'),
      'Profit Factor' : getattr(stats,'Profit Factor')},index=[1])

      df_aggr = pd.concat([df_aggr, df_res], ignore_index=True)
      df_aggr = df_aggr.sort_values(by=['Return %', 'Max DD'], ascending=[False, True])
      df_aggr.to_csv('C:\\temp\\Agregated_Results_YEARS.csv', encoding='utf-8', index=False)
      df_aggr
      spit out to csv for analysis in excel

  • @sukantasaha3034
    @sukantasaha3034 3 місяці тому +2

    I'm going through each and every videos of your. Even in udemy as well.
    Thank you so much for sharing all this knowledge

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      Thank you for your support!

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      @FeverYonge I like this book, and yes while being aware that there is a bit of luck hoping we are in a year/month/decade where a trading system still works, sometimes chances are high.

  • @nandoribas
    @nandoribas 3 місяці тому +3

    Great video. Please create more videos about optimizing strategies using other indicators.

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      More to come! thank you for your support.

    • @nagurk9773
      @nagurk9773 3 місяці тому

      True, v can expect more

    • @nagurk9773
      @nagurk9773 3 місяці тому

      ​@@CodeTradingCafe3:13

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      what's at 3:13?

    • @vassiliscapsis5828
      @vassiliscapsis5828 Місяць тому

      what is important is to understand the principle of optimization and how it works.

  • @sanskarmankar1516
    @sanskarmankar1516 2 місяці тому +1

    thanks bro helps a lot

  • @АлександрБондарчук-п8б

    I am pleasantly surprised to see your channel! Thank you a lot for your content, very interesting and informative!

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      So nice of you! Thank you for your support.

  • @sanskarmankar1516
    @sanskarmankar1516 2 місяці тому

    i was thinking og trading with the help of reinforcment learning where we give 5 option to our agent (buy,close buy,sell , close sell,do nothing) but it is hard to impliment as i dont have experince in reinforcment learning

    • @CodeTradingCafe
      @CodeTradingCafe  2 місяці тому

      It doesn't work, tried it before, for reasons that don't for in the comments, the best is if I make a video about it. But still it's interesting to see!

  • @ChrisHamberg-ok2cz
    @ChrisHamberg-ok2cz 3 місяці тому +1

    This will make your backtest look very good, but when you take it to simulation on out of sample data more than likely you'll find that it overfit. The optimization range is too tight, and should be limited to values that are expected to generalize well. For example, if your data is 1-min, you should try fitting maybe 1, 5, 13, 60, and 390. Since those might generalize.

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +2

      Hi, thank you for the detailed comment. I agree overfit is tricky, I usually think that if we fit on let's say 5 years of data it kind of generalizes at the end since 5 years is a lot of scenarios and it has to work for a month or 2 at least... but it's just a theory I still have to prove it :)

    • @ChrisHamberg-ok2cz
      @ChrisHamberg-ok2cz 3 місяці тому +1

      @@CodeTradingCafe Maybe. I was using 1-min data, so there might be something to what you're saying for that reason. On the other hand, even something like 60,000 1-min ticks, and it's still going to overfit on the 1-min.

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      I must try it with iterative out of sampling to verify

    • @ChrisHamberg-ok2cz
      @ChrisHamberg-ok2cz 3 місяці тому

      @@CodeTradingCafe I already did. The backtests are awesome, even with cross validation, but in portfolio simulation, on the exact same datasets, it performs worse than plain cross validated backtests in the same portfolio simulation. The only variable aside from the backtest method is that the portfolio simulation selects that best performers from backtest to construct the trading portfolio, so between fit models and unfit models it is not the case that fitting gave any kind of edge on 1-min time scaled ohlc. It continously over fits. Portfolio candidate selection primarily was based on median return lower 95% confidence interval, and also restricted by average length of time a position was held.

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      Thanks for the feedback I am skeptical also 1 min timeframe that must incur high relative spread and trading fees. I would go more towards 1h TF.

  • @riseup902
    @riseup902 3 місяці тому +1

    This is really awesome stuff. Would be interesting to do a video about going over how to run multiple strategies at once to generate signals and then the order execution part based on these signals.

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      Hi, it's a good idea, thank you. I will be showing multiple assets first, and maybe multiple timeframes , these were requiested a while ago.

    • @riseup902
      @riseup902 3 місяці тому

      @@CodeTradingCafe Thanks that would be really awesome!

  • @nandoribas
    @nandoribas 3 місяці тому

    I would like to make a video request:
    Updated video automating a strategy on Binance API (in Python), using features such as entry, stop loss, take profit, leverage (10x, 50x, etc.), partial profit taking, etc.
    That would be great!

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      Thank you I will put binance on my list, it has been requested many times.

  • @TheEcherriman
    @TheEcherriman 3 місяці тому +1

    Thanks for this. I tried to change the EMAs to Vortex but struggled (i.e. failed). If you get bored it would be great to see Vortex optimised with a volatility indicator !

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      Hi, did you mean the vortex indicator described here www.investopedia.com/terms/v/vortex-indicator-vi.asp ?

    • @TheEcherriman
      @TheEcherriman 3 місяці тому

      @@CodeTradingCafe That's the one ! Here's the code for pandas-ta - import pandas_ta as ta
      length = 14
      vortex = ta.vortex(df['High'], df['Low'], df['Close'], length=length)
      print(vortex)
      # Add Vortex Indicator to the DataFrame
      df['viplus'] = vortex[f'VTXP_{length}']
      df['viminus'] = vortex[f'VTXM_{length}']

  • @kobiyobi629
    @kobiyobi629 3 місяці тому +1

    any role for neural networks to optimize multiple indicators?

    • @martinsandor707
      @martinsandor707 3 місяці тому +1

      I only recently started looking into neural networks for algo trading, but they don't seem too promising. I really want to be proven wrong, though.

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      Hi, at the moment NN are not that impressive in trading at least as far as I know, they might be useful as a one indicator among others, but I find simple algorithms more powerful.

  • @luisfranco5396
    @luisfranco5396 3 місяці тому +1

    To add your codes to the trading platform do l have to have mt4 or mt5?

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      No this is only python no metatrader needed.

    • @luisfranco5396
      @luisfranco5396 3 місяці тому +1

      @@CodeTradingCafe so do l need to download python?

    • @CodeTradingCafe
      @CodeTradingCafe  2 місяці тому +1

      Yes, and install python with some libraries like pandas_ta

  • @fredyhazard6412
    @fredyhazard6412 3 місяці тому

    how do I get the "Gmt time"? I got key error:'Gmt time' plizz heeelpp

    • @muhireinnocent2371
      @muhireinnocent2371 3 місяці тому +1

      key error means that you dont have gmt time as a column in your dataset , check the column names of your dataset and handle them accordingly

  • @cpti8736
    @cpti8736 3 місяці тому

    Be careful, optimizing is often overfitting, be sure to cross validate on not seen data.

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      Yes it's a trap, I don't usually fit indicators like this really, I just choose average parameters and count that the strategy itself is good. But I agree also out of sample validation is a must if we intend to optimize like this.

  • @akam0707
    @akam0707 3 місяці тому

    How to plot support and resistance using python in stocks market?

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      You have been following me closely :) I have few videos about this topic, check this one ua-cam.com/video/xky3ZiCsEDs/v-deo.html

  • @DalazG
    @DalazG 3 місяці тому

    Is this transferable to forex trading?

  • @thembelssengwayo6896
    @thembelssengwayo6896 3 місяці тому

    Why didn't you use MQL5?

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому +1

      Python has more options using ML, neural networks and what not...

  • @rafalsza
    @rafalsza 3 місяці тому

    vectorbt is faster and simplier

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      faster yes, simpler I would argue that, not sure.

  • @DEEPAKCHAUDHARI-d9g
    @DEEPAKCHAUDHARI-d9g 3 місяці тому

    Thank you so much sirji

  • @preetipics
    @preetipics 3 місяці тому

    awesome, thanks alot

    • @CodeTradingCafe
      @CodeTradingCafe  3 місяці тому

      Thank you for your constant support! Glad you find this vid interesting.