Python Trading With Machine Learning Predictions

Sdílet
Vložit
  • čas přidán 30. 06. 2024
  • Want to learn how to use Python and machine learning to analyze price trends? In this video, we'll show you how to make realistic predictions on USD CHF exchange prices using K-nearest neighbors classifier and XGBOOST. We've already acquired and loaded the data in our previous video and added technical indicators to our Pandas DataFrame. Now, we'll focus on the model fitting process and validation. We'll also address a common mistake of considering actual price values for machine learning fitting and provide tips for correct use of machine learning models. Plus, we'll include a trading strategy and combine it with the fitted models. Don't miss out on this insightful video!
    🍓 If you want to follow structured courses with more details and practice exercises check my "About" page for Discount Coupons on my Udemy courses covering: Python basics, Object Oriented Programming and Data Analysis with NumPy and Pandas, ... more courses are on the way drop me a message if you have a particular interesting topic! Good luck!
    You may download the Jupyter notebook using this link:
    drive.google.com/file/d/1JTm6...
    00:00 Introduction
    03:20 K Nearest Neighbors Algorithm
    04:52 preparing Data for analysis
    09:00 Fitting and evaluating the KNN Machine Learning model
    16:30 Fitting and evaluating the XGBoost model
    17:42 Correcting a common mistake of Data sampling
    20:15 retesting Machine Learning models
    21:48 XGBoost feature importance plot
    #algotrading #technicalindicators #pythonprogramming

Komentáře • 69

  • @karthikeyannainar9876
    @karthikeyannainar9876 Před 2 lety +2

    Thank you for the nice presentation

  • @neroluke
    @neroluke Před rokem

    Hello, thank you very much for your interesting videos. I wanted to know, if it possible, which ✋version of Python you are using. Thanks a lot, great job.

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      Hey, thank you for your support. I honestly forgot which version I used for that particular video since it's more than a year ago, I would assume 3.9. I hope this helps.

  • @jama4002
    @jama4002 Před rokem +3

    thanks so much for your videos, I am waiting for the next one, this are my results so far
    Accuracy train: 88.35%
    Accuracy test: 88.61%
    0.0 50.538557
    1.0 49.461443
    Name: Res, dtype: float64
    Accuracy Gambler: 33.17%
    I dont understand quite well the overfitting with this metrics and dhow to fix it.

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      Actually these results are good, but are they good enough for trading? We need a backtest to find out! Thank you for sharing 🙂

    • @spidergaming5146
      @spidergaming5146 Před rokem

      On which model you get this accuracy

  • @alexisgraff2049
    @alexisgraff2049 Před rokem +1

    Hello sr, thank you very much for your videos, i have a question, where are you downloading the historical data? i cant find any good place to download it

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      Hey thank you for your support. Data is from dukascopy, also yfinance provides data.

  • @seekingtruth9417
    @seekingtruth9417 Před 2 lety +3

    Can we merge multi strategy while using ML. Can you please make a video on the same. Thanks for your efforts!

    • @CodeTradingCafe
      @CodeTradingCafe  Před 2 lety +1

      It's possible, I was thinking about it but in this case we have to find multiple winning strategies of the same type like all signaling for example continuation of a trend or all signaling trend reversal... Will keep it on my mind and see

  • @ridergami6821
    @ridergami6821 Před 5 měsíci

    Me from Malaysia. Tq for your videos.

  • @Yewbzee
    @Yewbzee Před 10 měsíci

    Great video. The method of splitting the data seems counter intuitive to me. In a time series prediction for trading we are generally relying on the relationships of candles, indicators etc of the ohlc of neighbouring candles in a sequence but here you are randomly taking candles from remote parts of the dataset thereby negating the relationship that those candles had with their close neighbours.
    Maybe I’m missing something, can you explain why this is still effective?

    • @CodeTradingCafe
      @CodeTradingCafe  Před 10 měsíci +1

      You are absolutely right, and you didn't miss the main idea, I think what you are referring to is more LSTM style where we consider a slice of time and try to predict the future price, rather than considering one candle at a time. But this video shows that trading based on simple conditions related to technical indicators shouldn't work (at least theoretically). Thanks again for sharing, see you around :)

  • @viper2413
    @viper2413 Před 3 měsíci

    Great video! I was looking to test the accuracy of 4 indicators on a stock by creating buy and sell signals over a long time period. Would you create a SL and TP for this or Buy and Sell when there is a reversal of the indicator?
    Do you have any video on this in particular? Would be very helpful, Thank you!

    • @CodeTradingCafe
      @CodeTradingCafe  Před 3 měsíci

      Hi, thank you, I wouldn't use machine learning for this and labeling might be challenging since it's a dynamic market.

    • @jaydy71
      @jaydy71 Před 3 měsíci

      In my humble experience, things like stop-loss and take-profit really muddy the waters when using machine learning in a trading bot. Stop-loss is basically being eager to sell at a loss, and take-profit is being eager to sell at a non-optimal profit. It kind of goes against what a machine learning model is supposed to do.
      Of course, things can (and often will) go in unexpected ways so you might want to limit your risks, and SL/TP always seems like an intuitive approach there. But SL/TP is not the way to do it IMHO. At least that always worked against me in back-testing and forward-testing.

    • @viper2413
      @viper2413 Před 3 měsíci

      @@jaydy71 That’s interesting, how else would you limit loss and mitigate risk other than SL/TP?
      By the way, I’m building models to the test the accuracy of different technical indicators on a particular stock for my university dissertation (final year project). Any advice would be cool!

    • @jaydy71
      @jaydy71 Před 3 měsíci

      @@viper2413 Admittedly my remarks about SL/TP might only be valid in my case where the ML model behind the trading bot is aimed at doing quick trades in volatile markets. I've not seen SL/TP being helpful at all during all in my testing in that scenario, but YMMV.
      Pardon me being pedantic, but when you say 'test the accuracy of technical indicators' you probably mean 'test the accuracy of a strategy using technical indicators'? Such indicators are obviously accurate, what matters is how you use them. IMHO such indicators don't predict anything by themselves; indicators can only work because people are using them.
      As for advice for your project, I could say this (FWIW): Make sure you test with enough data spanning a long enough time. For example test data of the last year, training data on everything before that. If you have enough data, doing n-fold validation is even better.
      Also if your project involves testing a trading strategy, make sure it trades enough in your testing data: 100% winning trades says nothing if it were only like 10 trades; personally I want to see at least a 1000 trades (in testing data only) before drawing a conclusion with any confidence.
      Also makes sure the test data properly represents your training data. For example if training data represents a growing market while your test data represents a falling market, I'm not sure what I could conclude from the test results.
      But that's just my 2cts, I might not be telling you anything new 🙂. But anyway, good luck with your project!

  • @qw4316
    @qw4316 Před rokem +1

    Hi Bro、could u pleases show us the complete code for this Vedic ? I don’t know the df model stands for what and how can we download the training datasets? Thanks for ur good vedio;

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      Hi, the code is available from the link in the description of the video, you can open it in a jupyter notebook.

  • @jasonweeks4062
    @jasonweeks4062 Před 2 lety +1

    Can you provide the links to the previous videos, please.

    • @CodeTradingCafe
      @CodeTradingCafe  Před 2 lety +1

      Hi thank you for your comment, I think I meant this video
      czcams.com/video/XK2IU5vRJr0/video.html
      Check the playlist and the most recent videos also information is better presented... At least in my opinion

  • @jaydy71
    @jaydy71 Před 3 měsíci +1

    Very interesting!
    For me things finally started working when I started taking a rather similar sort of classification approach (rather than attempting to predict future prices). However I just used a classic multi-layer perceptron and different indicators/features. RSI never worked for me for example (the model never put any weight to it).
    And yeah, never shuffle your data before splitting it into train/validation examples for time-series predictions! The model will just be trained to 'fill in the gaps' (too easy!), and as such the model will probably not be able to predict anything in the future (and probably will be overfitted).
    I think it's also important to carefully select which markets you're going to train a machine learning model on. In my humble experience, very low market-cap markets can't really be predicted with any kind of usable accuracy for example.
    Anyway, I've developed a bot using a similar machine learning approach and it has been trading real money since a few months now. So far it has been doing quite well after a few hundred real transactions.

    • @CodeTradingCafe
      @CodeTradingCafe  Před 3 měsíci

      Thank you for sharing, I agree classification is much easier and computationally more affordable.

    • @jaydy71
      @jaydy71 Před 3 měsíci

      @@CodeTradingCafe How come is classification more computationally affordable? At least the way I've done it, it doesn't really make a difference in that regard. But my machine learning knowledge is somewhat limited, so maybe I'm not doing the classification as efficiently as it could be.
      But the reason that I switched to classification instead of price predictions is because the latter seemed way too inaccurate to build a workable trading strategy on for a bot. With classification OTOH I sort of trade a price prediction for a 'confidence factor' for a prediction of 'up/down/undecided' (no matter how much up/down). It made things more reliable/robust and easier to work with.

    • @CodeTradingCafe
      @CodeTradingCafe  Před 3 měsíci +1

      That's the correct way, at least from what I saw in academic research papers they all used the classification approach, like you said. Computationally... it depends on the algorithm in fact most of algorithms run regression in the background and then apply a threshold for categorization, so I guess you are right it doesn't differ from this point of view.

  • @danch_ranch6594
    @danch_ranch6594 Před rokem +1

    Hey, I did the same procedure. Only I used data on USD/CHF from 2003 until now and changed the pipdiff to a smaller number: pipdiff = 300 * 1e-5. Why am I getting such a large accuracy?
    This is for the K-neighbors classifier model
    Output from the script:
    Accuracy train: 59.43%
    Accuracy test: 59.31%
    2.0 59.405260
    1.0 30.119733
    0.0 10.475007
    Name: mytarget, dtype: float64
    Accuracy Gambler: 32.96%
    Thank you for a very informative video btw. Such small group of videos online about technical trading in python, really appreciate it!

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      Hey, thank you for your support! Try checking if your categories are balanced before the test, it might be that one category (category 2 or uptrend here) is more frequent and the algorithm is predicting the same every time so it gets high fake accuracy. Maybe this is just my suspicion.

    • @danch_ranch6594
      @danch_ranch6594 Před rokem

      @@CodeTradingCafe This is the distribution of 'mytarget' values:
      2.0 18270
      1.0 9245
      0.0 3212
      Very much overweight of uptrend signals -_-.
      What causes this, do you know?

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      @@danch_ranch6594 try plotting the price on a graph if there has been a large uptrend during a period of time like over the years then it might be it

    • @danch_ranch6594
      @danch_ranch6594 Před rokem +1

      @@CodeTradingCafe I found the problem. Has been a downtrend since 2003, so seemed strange xD.
      I had written valueOpenHigh instead of valueOpenLow, like this:
      elif ( ( valueOpenHigh = pipdiff) ):
      trendcat[line] = 2
      Thank you for your help boss!

  • @ProfitPlaza
    @ProfitPlaza Před 24 dny +1

    thanks for great video I have trained my model and now Accuracy of trained results is 54.7804% accuracy of test Results is 53.6247% which mean my model does is not overfitted. But i have a problem my problem is that winrate of strategy is low for example around 35% for risk reward ration of 2. So my question is that although machine learning can make model to predict results for unseen data but what should we do if its winrate is low? I did many things such as different indicators, changing model of machine learning but non of them works. Can u please tell me how to do?

    • @CodeTradingCafe
      @CodeTradingCafe  Před 24 dny

      Hi, actually 35% for RR Ratio 2 is not as bad, you need only 33% to break even. You can use the Machine Learning model you have as an indicator and confirm it with another set of indicators for example.

    • @ProfitPlaza
      @ProfitPlaza Před 23 dny

      @@CodeTradingCafe Many thanks

  • @nilesh168
    @nilesh168 Před rokem +1

    Hi ,
    Liked your videos always and able to learn new things very quickly.
    I didnt find proper video if you have already covered below topic.
    I need help to print slope of line with output as degree or just +ve or -ve is fine.
    i am using np and mapplotlib
    data is like.
    chris_gayle = np.array([32.44, 67.55, 61.08, 59.00, 21.77, 40.91, 22.70, 22.22, 40.88, 40.83])
    X = np.array([2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019])
    i am able to plot and see slope but want to get in some variable in text format.
    I dont want to display graph.
    Thank you very much in advance.

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem +1

      Hi thank you for your comment, you can use slope = np.polyfit(X,Y, 1)[0] where x and y are your 2 coordinates vectors. You can also use lineregress from scipy...
      I don't know if this answers your question, let me know if it works

    • @nilesh168
      @nilesh168 Před rokem +2

      @@CodeTradingCafe thank you very much, is worked.

  • @PrayingForYourWellBeing
    @PrayingForYourWellBeing Před 8 měsíci +1

    After all these maths and complexity, how much do you really end up making?

    • @CodeTradingCafe
      @CodeTradingCafe  Před 8 měsíci +2

      Not much (~10% per year) but for some reason coding is so appealing I still code every day :)

  • @sharemarketinvestorsgroups5602
    @sharemarketinvestorsgroups5602 Před 9 měsíci +1

    i want to make python programme to log in via login id pwd and api key through oauth 2 can u help

    • @CodeTradingCafe
      @CodeTradingCafe  Před 9 měsíci

      Hi, I have used this in the past I remember this video has it as well: czcams.com/video/WcfKaZL4vpA/video.html
      I hope this helps, good luck!

  • @kudzovu
    @kudzovu Před rokem +1

    Can we use it to predict trends more accurately than current-only reports?

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      It might bring some insights but there are better methods

    • @kudzovu
      @kudzovu Před rokem +1

      @@CodeTradingCafe what is better methods?

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      @@kudzovu candle patterns and support resistance

    • @kudzovu
      @kudzovu Před rokem +1

      @@CodeTradingCafe Can you teach the machine how to predict price based on candlestick patterns and support and resistance?

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      @@kudzovu surely but the result is not guaranteed, there's only one way to find out.

  • @GurvinderSingh-bz9ys
    @GurvinderSingh-bz9ys Před měsícem

    CAN YOU SHARE LINK OF THE VIDEO IN WHICH YOU CALCULATED THE SLOPE.

    • @CodeTradingCafe
      @CodeTradingCafe  Před měsícem

      I am not sure I think it's this one czcams.com/video/XK2IU5vRJr0/video.html

  • @ridergami6821
    @ridergami6821 Před 5 měsíci

    🎉 tq

  • @jerrywong832
    @jerrywong832 Před 10 měsíci +1

    Can I have the URL link of your Udemy course please.

    • @CodeTradingCafe
      @CodeTradingCafe  Před 10 měsíci

      Hi, thank you for reaching out, there are 3 courses of different levels all Python focused, the links are at the bottom of the about page: www.youtube.com/@CodeTradingCafe/about

  • @ridergami6821
    @ridergami6821 Před 5 měsíci

    I cant find where to export model in this video

  • @seguidor777
    @seguidor777 Před 3 měsíci +1

    Hello, do you have any course for machine learning & trading?

    • @CodeTradingCafe
      @CodeTradingCafe  Před 3 měsíci +1

      Hi, yes I do actually, bit.ly/CouponMachineLearningTrading, check my channel page for complete links with discounted prices for all my courses. Good luck!

  • @SubhanSadad109
    @SubhanSadad109 Před 2 lety +2

    Hi would you be intrested in a project ? I will be paying for your time

    • @CodeTradingCafe
      @CodeTradingCafe  Před 2 lety

      Hi thank you for reaching out, to be honest I am always open for ideas the only problem is time so if you are not in a rush just send me details of your strategy by email and I will check how long this might take.

    • @eklanjoki1981
      @eklanjoki1981 Před rokem +1

      @@CodeTradingCafe hi thankyou for the videos I have an idea I would request you to look into and see its success what your email?

    • @CodeTradingCafe
      @CodeTradingCafe  Před rokem

      @@eklanjoki1981 Hi thank you, email is in the about page of the channel, codingntrading gmail