BTC Algorithmic Trading — Python vs TradingView ~ for Trading.

John Jairo
Trading Data Analysis
5 min readDec 15, 2022

--

The main difference between Python trading algorithms and TradingView is that Python is a programming language while TradingView is a technical analysis, charting platform with own programming language.

Anyone who has worked on developing a trading strategy from scratch knows the difficulty of getting your logic right. You can spend too much time writing code and this does not guarantee a profitable algorithm, that’s why I wanted to bring this article to help you the differences between Python and TradingView.

Python trading algorithms can be developed using the Python programming language and open source libraries, such as Panda, Numpy, Yfinance, Matplotlib, etc., and can be used to perform automated buy and sell trades in the financial markets. These algorithms are based on certain predetermined rules and conditions that are set in advance and are used to make trading decisions based on market data.

On the other hand, Tradingview is a technical analysis platform and trading tool that allows traders to view real-time charts, perform technical analysis and execute trading operations. It also allows users to share their trading ideas and strategies with the community and to use trading algorithms developed by other users.

However, Tradingview has its own programming language, it’s called Pine Script which was used to create technical indicators and trading strategies on the TradingView platform. It allows traders to customize and automate their analysis and decision-making process by creating custom indicators, signals, alerts, and backtesting capabilities. Pine Script also provides access to various data sources, such as historical prices and market news, and allows for easy integration with other TradingView features, such as alerts and order execution.

Python requires advanced programming knowledge and can be more difficult to use for inexperienced traders, other hand Tradingview could be greater flexibility and control over the trading strategy, as you can be customized to the user’s needs and preferences.

However, there are different trading algorithms in the Tradingview pine script, which can be used to make buy or sell decisions in the financial markets. Some examples of these algorithms are:

  • Technical analysis: uses chart patterns and technical indicators to identify buy and sell signals in the market.
  • Quantitative analysis: uses mathematical and statistical models to analyze market data and make buy or sell decisions.
  • Automated trading: uses pre-programmed trading algorithms to make buy or sell decisions based on certain predetermined parameters.

Strategies for trading for beginners

To develop a strategy for trading is important to do market research to identify potential trading opportunities and understand market trends and conditions.

Our strategy is gonna be based on creating a trend following strategy using Parabolic SAR and Stochastic Oscillator indicators.

Parabolic SAR also known as parabolic stop and reverse is used to determine the price direction of a security. It is displayed as a single parabolic line underneath the price bars in an uptrend, and above the price bars in a downtrend.

A stochastic Oscillator measures the security’s close price relative to its high-low range over a period of time.

Strategy in Python.

In Python using open-source libraries such as Talib, it allows us to calculate with more than 200 functions for the calculation of technical indicators such as ADX, MACD, RSI, Stochastic, Bollinger Bands, etc. Additionally, it has candlestick pattern recognition and a few, but useful, statistical functions, which reduces the programmer’s effort in developing indicators, as long as they already exist in TA-Lib.

Despite the open-source Python libraries, you must calculate the performance metrics of the strategy yourself using data from your DataFrame such as Slippage, Sharpe, Maximum Drawdown, and Stock returns.

# Buy signal
stock_data.loc[(stock_data['SAR'] < stock_data['Close']) & (stock_data['fastk'] > stock_data['slowd']), 'new_signal'] = 1

# Exit signal
stock_data.loc[(stock_data['SAR'] > stock_data['Close']) & (stock_data['fastk'] < stock_data['slowd']), 'new_signal'] = 0

# Fill the missing values with last valid observation
stock_data = stock_data.fillna(method = 'ffill')

Click here to get the Python strategy to implement in your routine.

Performance metrics

| Strategy Returns = 120.279 % |

| Maximum Drawdown = -70.0222 %|

Strategy in Pine Script — TradingView.

Granted, TradingView has a very comprehensive database of data feeds. TradingView has a plethora of data available at your fingertips, ready to access with as little as one line of code. TradingView users have the option to publish their indicators and strategies to the TradingView library, which is a great way to learn from other programmers.

Don’t forget that TradingView set up some values to run the strategy, the amount of funds initially available for the strategy to trade, in units of `currency`. Also, the slippage is set to 5, and a long market order will enter at 5 * 0.01 = 0.05 points above the actual price, however, this setting can also be changed in the strategy’s “Settings/Properties” tab. Optional. The default is 0, and The fund capital 100000.

long_signal = sar < close and fastk > slowd 
strategy.entry ("BUY", strategy.long, when=long_signal)

//close signal
if sar > close and fastk < slowd
strategy.close_all(comment = "close entry")

Click here to get the strategy in TradingView to implement it in your routine.

Performance metrics

| Strategy Returns = 2.73 % |

| Maximum Drawdown = -2.9%%|

This strategies is has the better results when we have a bull market manifests with market prices experiencing higher highs and higher lows over an extended period of time.

Holding crypto thesis.

I talked about the main differences between Python and TradingView using a simple and easy strategy to Long Positions with Parabolic Sar and Stochastic indicator. The result of the strategies is similar in the chart, on the other hand, performance metrics aren’t similar, this is due to the way each one obtains the data to calculate the yields, tradingview predefined values are used whereas in Python they are calculated with the values from the database.

TradingView is a good tool for beginners to start testing their trading hypotheses and get an idea of what might work. Python and its range of libraries broaden the possibilities to get more robust trading algorithms to the constantly changing market conditions by using the Maching learning and AI libraries that TradingView is limited to.

If you have a trading strategy hypothesis that you would like to test, please contact me and I will be happy to help you.

Happy trading.

If you enjoyed this article, make sure you check our Twitter & Linkedin.

All the content is educational, It’s not an investment advisor, make your own research, thank you so much to read.

--

--

1/3 Crypto Investor + 1/3 Trading Programmer + 1/3 DeFi Researcher = Research Engineer