Pinescript Trading Automation
This project aims to demonstrate algoritmic trading using pinescript and cloudflare workers. In this project we will:
- Create trading strategy
- Writing pinescript and do backtest.
- Create alert
- Deploy backend as event listener and trade router.
Repository | Description |
---|---|
algo-pinescript-libs | Repository of various algo trading strategy using pinescript |
algo-trade-hono (opens in a new tab) | Event processor for Tradingview Alert for order execution |
Pinescript Tradingview
Pine Script is a programming language specifically designed for creating custom indicators, strategies, and alerts on the TradingView platform. It allows us to develop technical analysis tools, backtest trading strategies, and create automated trading systems. Pine Script provides a wide range of built-in functions and operators to perform complex calculations, plot charts and visualizations, and generate trade entry and exit signals. In this project, we will use Pinescript to generate trading signals for the backend.
Strategy
For this project we will use mean reversion strategy,
basically we try to predict short term low for entry and exit at short term high relatively to mean.
To learn more about mean reversion visit here (Investopedia) (opens in a new tab)
Entry
We will use dual moving average to calculate the price movement given within short period. By subtracting Long period 60 EMA value by Short period 5 EMA value we will get the short term price change. Using this value, we can add threshold to find sudden big price movement. Once signal is detected the system will generate alert and long position.
Below is the syntax for the entry signal:
threshold_pct = 3 # 3 percent deviation
delta = (EMA60 - EMA5) * 100 / close # float closing price to percentage
buy_signal = delta > threshold_pct
if buy_signal:
generate_alert()
Exit
The exit strategy is much more simpler, using ATR (average true range) bands. In this indicator, there are 3 bands:
- Middle bands:
EMA60
- Upper bands:
EMA60 + (ATR * multiplier)
- Lower bands:
EMA60 - (ATR * multiplier)
The exit signal is generated when the price closes above the upper bands. Using this simple strategy, enable us to exit at short term high. Below is the syntax for the exit signal:
upper_band = EMA60 + (ATR * multiplier)
sell_signal = close > upper_band
if sell_signal:
generate_alert()
Backtest
Based on the 6 month backtest result on PEPE/USDT (crypto pair). This is the backtest result:
Backtest settings
Parameters | Value |
---|---|
Symbol | PEPE-USDT 5m |
Broker | BingX |
Initial balance | 1000 |
Order size | 10% equity |
Flash crash order size | 20% equity |
Slow EMA length | 60 |
Fast EMA length | 5 |
Volatility threshold | 1.2% |
Flash crash threshold | 20% of initial entry price |
ATR Period | 30 |
ATR multiplier | 1 |
Backtest result
Using above parameters, we able to reach 200% ROI with end balance of 3129 USDT.
However this backtest is far from complete as this project aims only to display
technical process to automate Tradingview Pinescript. The result may differ according
to symbol, backtest date, parameters, and risk.
Alert and Setup
Once the strategy is saved, we can create alert based on the create alert function calls on the strategy.
The alert message can be sent to webhook as body. This body will contain instruction in format of json string which
enable us to communicate with trade router in backend.
Trade Router
The event listener for this project is set up using Hono. Hono is a lightweight JavaScript framework for serverless services. Once the broker API key is added and the service is deployed, the backend will listen to signals from trading view alerts to execute trades. The logs are saved in the D1 database.