Automated Price Action Trading Strategy In Python
HTML-код
- Опубликовано: 17 май 2024
- This video presents a simple trading strategy using candlestick patterns and support and resistance values. The strategy can be automated for price action detection. The strategy was automated and tested in Python on historical data of Euro versus US Dollar.
Check my previous video on support and resistance levels for more details:
• Automated Support and ...
🍓 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!
The Python code is available for download from the link:
drive.google.com/file/d/1Hd34...
The data file:
drive.google.com/file/d/1v2Bf...
#python #forex #algorithmictrading
00:00 Support And Resistance Trading Introduction
03:40 Support Resistance Trading Strategy
06:50 Algorithmic Trading Code In Python
22:40 Trading Strategy Predictions Results
happy to find your channel, I've been looking to start in python and find this very helpful
Thank you for your comment, check my playlists there is one on learning python it might be helpful, and good luck, it will be fun you will see!
Very thorough explanation!! Great content!
Thanks a lot for your support.
Congrats on not having a clickbait title like, “Make Millions Overnight” or suchlike as used by so many Tubers. Instead a professional honest approach.
Thank you for your appreciation, I am glad you enjoyed the content. In fact I am happy with the coding part most of the time so no need to oversell any indicator, they all have some pros and cons.
Oooh geez! This channel is amazing!
Thanks for your jog. It's very nice.
Glad you enjoy it! thank you for your support.
Good afternoon, you do not need to worry, such videos cannot be too long))) in everything related to money, there is always not enough information and thanks for the strong video
Lol well it reminds me years ago trying to learn trading all I was stumbling on was the moving average strategy in like 6 min vids...plenty of them, nothing that helpful... Times have changed and the field is evolving quickly, today's techniques are really powerful.
Maybe I will try to automate this strategy in another video.
@CodeTrading, you are a gem.💎
Thank you so much for sharing these amazing videos.
Thank you for your support
Thanks, amazing video, I learned a lot!
Thank you for your support, glad the videos is helpful
Thank you so much for sharing 🙂
Thank you for your support, and good luck.
Great work! Excellent!
Thank you :)
Great content.. Thanks a lot !
I have one query. Why are you not using TAlib to detect candlestick patterns?
Hi by the time I started doing these codes candles were still freshly introduced in TAlib. Another reason is I like to control the ratios of the patterns like the wick versus candle body and size of the engulfing candle etc... but I don't mind using TAlib if you know what's happening under the hood.
Thank you much for sharing videos from novice programmer from thailand :-)
Thank you for your support!
on 26.07 you are talking about the up trend is a very smart idea, but you can implement this condition not to trade against the market by setting in the code if the last 3-4 candles are bullish, then we trade, if bearish, then we do not, for example. It is necessary in the code to indicate the NUMBER of PREDING candles, this is the MOST IMPORTANT condition in addition
Hi, sorry for the delay, I had to check in detail, adding the filtering method is good but we will be decreasing the sensitivity of our model therefore yielding no more than ~3 trades per year, maybe this is good to implement for lower timeframes (H4 or H1)
Thanks Again!
...and for your support 😊
Really great video, thanks for putting this out and giving us the source code too. Thanks bro you saved me a ton of pain
Thank you for your support, finally some of my work is being discovered 🙂 happy these codes are of use
Como siempre son excelentes videos los que tienes.
Gracias amigo :) thank you for your support!
Very Good Tutorial !
Thank you for your support, much appreciated.
How would we optimize the automatic finding of the exact stop loss in pips, so that backtesting itself on historical data checks how much optimal in pips the stop would be on the found candles to enter a trade, of course, count the indent from the resistance line
I can see you are deep into this type of analysis! some day it will pay!
@@CodeTradingCafe thank you
Great!
Thank you, I will try to show a bot using this
how to carry out the process of automatic optimization of the stop loss and take profit parameters, as solved in the metatrader in backtesting, and how would you decide to INDENT in the code from the resistance and support lines in order to sell at the distance from the resistance line found in the backtest, for example ?
I think I understand what you mean, we will have to include most of the code in this video in a big for loop that checks different SL values or distances to price, then you keep the SL distance yielding highest income, in machine learning this is close to overfiting in the sense that it would work fine on previous data where you did your adjustments but it doesn't necessarily work well on future data.
Anyway to optimise any parameter the code can be included in a for loop changing the parameter and checking the outcome.
@@CodeTradingCafe I think this is a very good idea for a new video, automatic optimization and finding exactly in pips the price of the stop loss and take profit, as well as the trailing stop and the step of following the price, display the clarified parameters in the table. This will improve profits and help reduce losses. The first rule of the trader))) cut losses
I saw your prev SR level video pls mention it in the description of video, I think prev video should be seen prior this video
Thank you I agree with you I will add a link.
Sir what is prerequisite for starting your course I know python besides that what else you need to know
Hi, python then more python and again python. Later you need to get familiar with trading strategies and some algorithmic trading books. Good luck!
This is a great video and strategy! I think there is look forward bias in the signal calculation though. When iterating through subrow to calculate the support and resistances the subrow finishes on 'row' which is the present day. This is fed in to the support and resistance functions which look n1 candles behind the row input and n2 candles in front so you would have to know what n2 candles are in to the future. Just food for thought. Correct me if I'm wrong because I've been staring at this code all day!
Hi, thank you for your comment, the right boundary in Python is excluded meaning if I write [row-5:row] in fact it goes from row-5 up to row-1
I hope it makes sense
@@CodeTradingCafe
I see, but the current row is included in the iteration:
For subrow in range(row-backcandles+n1, row+1):
if support (df, subrow, n1, n2):
SS.append(df.low[subrow])
So if the code is looking forward n2 candles when subrow = row ?
@@MrHamish1998 actually you're correct, the subrow should stop at row+1-n2 I think it wouldn't affect our results much because it's an error only triggered when the current candle is support or resistance along with previous 45 candles (we simply got lucky with this) this reminded me of the same type of mistake I was commenting on an article years ago.
Thank you for pointing this out
@@CodeTradingCafe I backtested it with the look forward bias using backtesting module on 15min and 5min timeframes on 5 cryptos using ATR as TP. Results were incredible (+1000% APY). After fixing the look forward bias returns went to +10-20%. So still profitable. I reckon using volume and large time frame SRs as well as your wedge breaks and head and shoulders in your other videos this could be a serious trading bot! Either way you’ve helped my coding a lot with these videos and inspired some ideas!
@@CodeTradingCafe also because we’re iterating through each row then every candle backwards within BackCandles every support/resistance found will become the current row at some point. Easily fixed though. Bit of tweaking and this strategy is a weapon
Hi, may I ask if I will test this strategy for example to data of BTC/USD. The column "amount" will represent pips, but how many lots/units am I trading actually?
Hi, good question, actually this is not included in the code nor the video, so we are assuming one full bitcoin in trade, now if you want to make things easier as a full backtest, in my most recent videos I am using backtesting.py for backtests and you can include the trade size in it. Check this one for example ruclips.net/video/C3bh6Y4LpGs/видео.html
Hey man great content learning alot from your coding skills could u do a video on connecting to mt5 api with python to implement some of these strategies in the back testing terminal on mt5 binace connection would be cool aswell
Hi, thank you for your comment, I understand what you mean, I have already thought about it but dropped the idea since we can backtest in python. However, note that these particular strategies can be written in mql language relatively easily because there is no Machine Learning involved (main reason I chose Python was being able to include ML models).
Thank you for your work. I'm probably being an idiot, but I can't get the algo to find the data file. Do I need to save it at a particular location so it can be found by the algo please?
Thank you for your support! The easiest is to keep the data file in the same folder just next to the jupyter notebook python file. Let me know if it worked.
@@CodeTradingCafe Thanks, works now!
where do u get the data csv
Yfinance, my broker and Dukascopy. Good luck!
at 28.40 there will be no false positive triggering))) if you indicate what I just wrote about, do not enter the deal in short, if the previous candles are 3-4, SPECIFY IN CODE !!! There were Bulls
Yes but at the same time we are looking for engulfing signals around resistance/supports so we expect a reversal, I am not sure if I add one more condition we would still have any signals at all. We can investigate this anyway, the code is ready so I can add the condition and see what it does...
hello, what is the barsupfront variable its gives an error thats its not defined
These are the bars to look for in the future, shouldn't run at the end of the dataframe.
@@CodeTradingCafe does it have to be defined before the loop?
I assume so because it's used inside the loop.
how can we put this into tradingview pinescript as strategy indicator?
Hi honestly I am not familiar with tradingview coding language but it shouldn't be very different from any. I mainly prefer python because it has machine learning and neural networks libraries.
how can i get quotex otc candlestick data such as open, hight,low... in python ?
Hi, you might try dukascopy they might have data published.
I know the main mistake of the trailing stop. IT DOESN'T WORK UNTIL THE PRICE TOUCHES IT IN THE BACKWARD DIRECTION, which is very inconvenient! What is your opinion? does the trailing start from the moment you place an order in your code? and it immediately follows the price at a certain distance of 10-15 pips?
In my opinion the trailing distance shouldn't be a constant like 10 or 15 or something else pips, mainly because the market is different every day, if I have to test trailing stop I would use the distance of the stop loss as described in the video as a start but also have to think about modifying the trail distance (making it smaller) as you get closer from a "fictive" take profit value, make like a function where the trailing distance shrinks linearly until a certain point which is the "TP" then the distance remains constant for the rest of the time. (I know it's complex/annoying but we can't use linear or constant thinking on nonlinear market, and you have to give enough room for your trade to move especially at the beginning).
Another way to fix the distance of a trailing stop is using ATR values takes into account the market volatility.
Symmetrical exit maybe 😊 according to the strategy
There is only one way to find out :)
What platform can we use this code on? Tradingview, mt4, mt5 ? Thanks
Hi this code is in Python language so you need python installed to run it, it doesn't work on mt nor tradingview. However the algorithm can be translated to mt or tradingview
@@CodeTradingCafe OK thanks
is it possible to implement python code in MT4 station ?
Hi, yes using mql4 language you can translate any Python code to run in mt4.
Could you please help me how to implement the strategy in indian market on index options
You need to backtest first to make sure it's profitable
at 26.45, you found a 585 candle well suited to our conditions, but why did the scanner miss the next candle right behind it? she perfectly suited all conditions! Is this an ERROR IN THE CODE? SCAN SKIP MAYBE MORE THAN ONCE, I DIDN'T NOTICE BUT IT'S WORTH CHECKING I THINK! AND YOU?
Well spotted ! in fact it might not be an error but I suspect that it wasn't taken as a signal because in the function isstar() we have a minimum candle body difference to consider (the variable is called bodydiffmin check video 8:28) so maybe the candle is "too small" to make a signal, ... there is one way to find out decrease the value of the bodydiffmin or simply check the body length of this particular candle.
thank you so much! But can you add English subtitle pls ?
Are the auto generated captions working?
May I privately share an idea for consideration?
Hi, sure you can send me an email, the address is in the about section of this channel.
@@CodeTradingCafe Sent.
any idea why its now getting multiple errors ?
Hi, it shouldn't get errors I think the code is still the same. Can you mention the type of errors you are getting?
@@CodeTradingCafe high it was my fault, its all good....awesome code. I didnt put the csv in the correct file location...school boy error....
It's good it's working! good luck for your experiments.
@@CodeTradingCafe hi its all good, great channel... I was wondering if you coded something a bit different like HTF say 2H indicators using data from 30mins? For S&R try Gann SQ of 9... a strategy I am looking at is 30min data Stochast(5,3,3) & BB(20) & Gann SQ of 9 S & R, (use this for close to S & R) isStar or isDoji, then HTF indicators: 72 EMA 144 EMA , ADX 56. For BUY 72EMA>144EMA, ADX D+>D-, isStar or isDoji, below BB low, Stoch70. Its just something a bit different from using single TF with single TF indicators and using Gann for S & R.
Why don't you go from static, in real time, it is much more informative to see which candle on the chart is false positive, if you mark all the found candles with arrows showing the direction of entering a deal, a green arrow up long, or a red short down, then you will not need to manually drive in the numbers of the found candles
It's a good idea but there's still a problem where you can fit only a certain number of candles into a visible frame, but yeah I see what you mean I will look into it
@@CodeTradingCafe no problem, you can implement horizontal scrolling scrolling from the first candle in our dataset, for example 10 years and 3000 candles, the graph is drawn in Cartesian coordinates, a new candle will be drawn along the X-axis every time, and the previous ones will disappear behind the visible field to the left, how in any real-time platform charts with new candles are drawn, let's leave, for example, beautiful large, convenient size 30 visible candles, the rest behind the canvas, just scrolling to view
This strategy is good compare to other strategy that use indicator like rsi, macd and so on, because it is lagging and i think i can throw rsi , macd into rubbish.🤣🤣🤣🤣
actually you can still use RSI or other indicators for exit strategies but not for the entry. And you are right support resistance and price action are better for entry signals. Good luck (check also my videos on VWAP and Bollinger Bands they provide good hints as well)
@@CodeTradingCafe because i have my friend that are programmer (data scientist) , he does not use rsi, macd and standard indicator on tradingview for strategies, he use mean reversion, multi time frame analysis and price movement or volatility analysis in his trading strategy.
Multi timeframe is powerful reversals are less noisy on higher timeframes and entry points are clearer on lower timeframes.