How to modify Algo-Trading scripts without coding in TradingView?
How to modify our scripts without coding?
A cool feature about Pine script is that we can create custom inputs to easily change the parameters of our strategies and indicators. We have discussed about this in detail in one of the earlier episodes. Let’s take it a step further now
Take a look at any indicator offered in Tradingivew. Note how easy it is to modify the length and even the colors via the Style tab.
We can achieve the same for the studies and strategies created in Pine script by using the input() function.
In this episode, we can take the strategy we created in the previous episode and use the input function to make it an adaptable strategy with custom inputs.
So first step here is to get the user input for long and short trigger or threshold. In the last episode, our script fired reversal trades and when a stock moves up or down by 3%. But we can change this to any % levels easily by adding 2 more line of code.
longTrigger = input(title=”% Change for short entries”, type=input.float, defval=3)
shortTrigger = input(title=”% Change for long entries”, type=input.float, defval=3)
Let’s go through the parameters that are passed through the input() function.
1. title — this is where we specify the text that the user sees when trying to change the value of that particular parameter.
2. type — we have to let TradingView know what kind of value to accept. Since the user could be entering a number such as 4.5, we’ve used float.
3. defval — this is the default value.
But they will be inputting a value such as 5(%). We need to convert this to 1.05 for our if statements. For that we need to add 2 more lines of code.
longTrigger := 1 — longTrigger/100
shortTrigger := 1 + shortTrigger/100
And we need to change our if statements to look at our newly created variables based on user input rather than the previous hard coded values.
if price_change < longTrigger
strategy.entry(“long”, strategy.long, 100)
if price_change > shortTrigger
strategy.entry(“short”, strategy.short, 100)
To access the input options, click on the gear icon next to the name of your strategy in the data window.
Now, let’s take this a step further. What if you want to short a stock if nifty falls a certain %?
Well, that’s simple. We need to use securities function here and replace another 2 lines of code
price_close = security(“NIFTY”, “D”, close)
price_open = security(“NIFTY”, “D”, open)
Now, instead of getting actual price of stock chart which is open right now, we are getting Nifty’s price and using it as base condition to trigger the trades.
Now look at the chart and the trade results. Do you see the difference?
Check Out Script Library to get script used for this episode:
https://marketsecrets.in/script-library/
To know more, checkout:
Related
amibroker tutorial videos Getting started with algo-trading basics how to backtest a trading strategy? how to do algo trading in india? how to do algo trading in Zerodha? how to learn algo trading? how to start with algo trading in india? how to use pine script for algo trading? how to write algo trading scripts? Pine script tutorial videos step-by-step approach to start with algo-trading in India