TLDR; Trying to update the tradingview settings for a strategy automatically and simple element.value doesn't work.
Hi everyone,
I have created a Strategy that works successfully through Pinescript however there is quite a few options in the settings window (E.g RSI Period etc), with a large variety of combinations. I would like to automate the settings for it to run through the backtesting for each setting and record the details of each change (e.g. record RSI Period 4,5,6,7) (edit: not worried about the recording part, that will come easy once the data is in a variable in javascript).
I am a long time trader and relatively proficient with Javascript and Jquery so I thought i'd give it a crack at creating a Chrome Extension to do this. I have created the manifest/content.js, and have managed to make it read the Settings window when it is opened, and manipulate the values however here lies my problem. I am able to get it to change the values however the Strategy doesn't seem to update/it doesn't seem to submit the changes. I have tried to change the .value, then .focus() and .blur() but that doesn't seem to work.
I understand there may be some backend code/hidden fields where the real value is kept but there HAS to be a way to be able to trick the form that a person is doing it (i.e .click() ).
Here's my code I have so far that just changes the value for an element to 1 when CTRL+ALT+SHIFT+D is pressed(but the form does nothing after this):
document.addEventListener('keydown', function (event) {
if (event.ctrlKey && event.altKey && event.shiftKey && event.key == 'D' ) {
loadForm();
}
});
function loadForm(){
var alertElement = document.getElementsByClassName('input-3bEGcMc9 with-end-slot-S5RrC8PC');
console.log(alertElement);
alertElement[6].focus();
alertElement[6].value = '1';
alertElement[6].blur();
var event = new Event('input', {
bubbles: true,
cancelable: true,
});
alertElement[6].dispatchEvent(event);
$("input").blur();
}
For people that are looking to do something similar to this, simple DOM variations by code is not allowed by Tradingview as their elements look for whether the event isTrusted or not before continuing (all actions done via code in a browser appear as !isTrusted due to standard browser securities).
To work around this and complete what I wanted, I have created the actions I wanted to do using puppeteer which reenacts actual user actions.
See my github for a working example: https://github.com/Snazzikiel/TradingViewPuppeteer/blob/main/strategyanalyzer.js
Please note that this was a hacky-first example for what I wanted to do so. I am posting this to help others in the future that are looking to do a similar thing.