Skip to content
TORNLIFE More

Stock Market [script]

Started by Deglor [3536982] on in Tools & Userscripts.

9 replies · 1.93k views · thread synced · 6 days ago · View on torn.com
About this thread

Posts archived: 10 / 10 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
10
Discussion span
→
People posting
7
Likes on archived posts
13
Authority score
38 / 100
Historical score
28 / 100
Story score
45 / 100
Engagement score
69 / 100
Deglor [3536982]

Hey guys i wrote my first Script for torn. Its for the Stock Market and shows which Stock is close to the minumum prize. Feel free to use it if you like. I just wanted to share my first success. Cheers! Also be Kind please :) 

 

 

 

// ==UserScript==
// @name         Torn - Stock Auto Tracker
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Adds a stock tracker button next to the City button (or in the container if City is missing) and highlights buying opportunities
// @author       Deglor
// @match        https://www.torn.com/page.php?sid=stocks*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let stockElements = [];
    let currentStockIndex = 0;
    let processing = false;

    function addTrackerButton() {
        const checkExist = setInterval(() => {
            const cityButtonContainer = document.querySelector('.linksContainer___LiOTN');

            if (cityButtonContainer) {
                clearInterval(checkExist);

                if (document.querySelector('#stockTrackerButton')) return;

                let trackerButton = document.createElement('a');
                trackerButton.innerText = "Stock Tracker";
                trackerButton.href = "#";
                trackerButton.className = "stock-tracker-button";
                trackerButton.id = "stockTrackerButton";
                trackerButton.style.marginRight = "10px";
                trackerButton.style.color = "inherit";
                trackerButton.style.textDecoration = "none";
                const cityButton = cityButtonContainer.querySelector('a[href="/city.php"]');

                if (cityButton) {
                    cityButtonContainer.insertBefore(trackerButton, cityButton);
                } else {
                    cityButtonContainer.appendChild(trackerButton);
                }

                trackerButton.addEventListener('click', (e) => {
                    e.preventDefault();
                    startStockTracking();
                });
            }
        }, 500);
    }

    function startStockTracking() {
        stockElements = Array.from(document.querySelectorAll("ul.stock___ElSDB"));
        if (stockElements.length === 0) return;

        currentStockIndex = 0;
        processing = true;
        processNextStock();
    }

    function processNextStock() {
        if (currentStockIndex >= stockElements.length) {
            processing = false;
            return;
        }

        let stockElement = stockElements[currentStockIndex];
        let dropdownButton = stockElement.querySelector("[data-name='priceTab']");

        if (dropdownButton) {
            dropdownButton.click();
            waitForDropdown(stockElement, () => {
                let dropdown = document.querySelector(".stockDropdown___Y2X_v");
                if (dropdown) {
                    waitForPrices(dropdown, () => extractStockData(dropdown, stockElement));
                } else {
                    currentStockIndex++;
                    processNextStock();
                }
            });
        } else {
            currentStockIndex++;
            processNextStock();
        }
    }

    function extractStockData(dropdown, stockElement) {
        let priceElements = dropdown.querySelectorAll(".value___S4Cjf");
        let lowPriceElement = dropdown.querySelector(".value___S4Cjf.low___r3cFG");
        let highPriceElement = dropdown.querySelector(".value___S4Cjf.up___ZyXiA");

        if (priceElements.length >= 2 && lowPriceElement && highPriceElement) {
            let currentPrice = parseFloat(priceElements[0].innerText.replace('$', '').replace(',', ''));
            let minPrice = parseFloat(lowPriceElement.innerText.replace('$', '').replace(',', ''));
            let maxPrice = parseFloat(highPriceElement.innerText.replace('$', '').replace(',', ''));

            let percentage = ((currentPrice - minPrice) / (maxPrice - minPrice)) * 100;
            let shouldBuy = percentage <= 15;

            let stockNameElement = stockElement.querySelector("[data-name='nameTab']");
            if (stockNameElement) {
                stockNameElement.style.setProperty("background-color", shouldBuy ? 'rgba(0, 255, 0, 0.3)' : 'rgba(255, 0, 0, 0.3)', "important");
                waitForHighlight(stockNameElement);
            }
        } else {
            currentStockIndex++;
            processNextStock();
        }
    }

    function waitForDropdown(stockElement, callback, attempts = 0) {
        if (attempts > 20) {
            callback();
            return;
        }

        let dropdown = document.querySelector(".stockDropdown___Y2X_v");
        if (dropdown) {
            (callback, 100);
        } else {
            (() => waitForDropdown(stockElement, callback, attempts + 1), 50);
        }
    }

    function waitForPrices(dropdown, callback, attempts = 0) {
        if (attempts > 30) {
            currentStockIndex++;
            processNextStock();
            return;
        }

        let priceElements = dropdown.querySelectorAll(".value___S4Cjf");
        let lowPriceElement = dropdown.querySelector(".value___S4Cjf.low___r3cFG");
        let highPriceElement = dropdown.querySelector(".value___S4Cjf.up___ZyXiA");

        if (priceElements.length >= 2 && lowPriceElement && highPriceElement) {
            (callback, 100);
        } else {
            (() => waitForPrices(dropdown, callback, attempts + 1), 50);
        }
    }

    function waitForHighlight(stockElement, attempts = 0) {
        if (attempts > 30) {
            currentStockIndex++;
            processNextStock();
            return;
        }

        let bgColor = window.getComputedStyle(stockElement).backgroundColor;
        if (bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent') {
            currentStockIndex++;
            processNextStock();
        } else {
            (() => waitForHighlight(stockElement, attempts + 1), 50);
        }
    }

    addTrackerButton();
})();

BritishBenji [2257611]

What is it with people posting scripts directly to a forum lately?

Uploading to the likes of GreasyFork or Github makes it easier to update, read, and edit. It also allows for one click installing

 

This looks mostly decent at least. The use of emojis is great for those that don't speak German. However, sending data to the console instead of having something up on the page is likely going to be a pain for a lot of people

 

 

 

Regarding @Scooper54's question

It appears to pull the "minimum" price by the "Last X Low" field. Which isn't gonna be great if you have "hour" selected. But is decent for some of the other values. 

Deglor [3536982]

Thanks for the hint @BritishBenji I adjusted the language and minimized the ammount of lines. 

 

Also 100% correct it just takes the Info thats written under minimum price. 

Deglor [3536982]

Hey @pimpypoo I just updated this script there should be a Button on the top next to the city Button just hit it and itwill start this script

Amthelos [3600523]

I'm not sure if its just me, as I'm just getting into scripts, but it seems the button on the top may have broke or does not function properly? I just get the top stock open the drop down on the price history graph, and then nothing after that.

Threxen [3606841]

Hey Deglor, looking at this and put it into tampermonkey.  When pressing the Stock Tracker the only thing it is doing is opening the top stock. 
As if it clicked on the Price.

 

When I sort my list, click the button again, it only opens the top stock.  What is the script supposed to do?

 

It does show this on several lines.

LakiZo [3866096]

Bump. What's up with the script @Deglor? Is it working mate? I install it but I don't see any button. It seems very promising though!