Stocks and the API | API Development | TORN

Stocks and the API

    • Retroscope [3245241]
    • Role: Civilian
    • Level: 17
    • Posts: 41
    • Karma: 4
    • Last Action: 27 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Thread created on 11:42:05 - 27/03/24 (5 months ago)
    |
    Last replied 18:02:13 - 01/04/24 (5 months ago)
    I want to learn user-scripting for Torn, and I picked for my 'hello world' project - a simple stock ticker to show where all your investments be at.

    My abstract is; Pull the users investments. Look up the Company ID. Compare the bought price to the current price. Display the results nicely.

    I thought it would be a lot easier. Have a couple of questions.


    • How do I correlate the company ID's I get from [ https://api.torn.com/user/?selections=stocks&key=${apiKey} ] with company names?
    - So I can take a list of the users investments and reference them aganst the current companies' data.

    • How does the date and time system work?
    - For example, it says [ time_bought : 1711535015 ]. What even is that?




    Any insights or advice would be much appreciated.
    Last edited by Retroscope on 11:46:28 - 27/03/24

    ~~~~~ Retroscope ~~~~~

    • DeKleineKobini [2114440]
    • Role: Committee
    • Level: 100
    • Posts: 3,746
    • Karma: 5,371
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 11:52:40 - 27/03/24 (5 months ago)
    Post link copied to clipboard Copy post link
    Stocks do not relate to any company ID, but to a stock ID. You can get the information about all stocks with the torn/stock selection. While you can get data for all stocks with a single call, providing the id (and thus having to do multiple call to get more stocks) will provide you with more information about that stock.

    Torn's API uses unix time to transfer dates. Unix time is the seconds since January 1st 1970. Most languages use that system as well, but sometimes with milliseconds instead of seconds. Javascript for example uses milliseconds for it's date system.

    Some good sources for working with the API are:
    Last edited by DeKleineKobini on 11:55:38 - 27/03/24
    • Retroscope [3245241]
    • Role: Civilian
    • Level: 17
    • Posts: 41
    • Karma: 4
    • Last Action: 27 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 12:06:17 - 27/03/24 (5 months ago)
    Post link copied to clipboard Copy post link
    Thank you Kobini, that's literally exactly all the information I needed! You are an absolute star.

    I appreciate the links also. The API documentation seemed a little on the sparse side and I coudn't find much on Google. So that's a big bonus.

    Cheers!

    ~~~~~ Retroscope ~~~~~

    • Omanpx [1906686]
    • Role: Civilian
    • Level: 100
    • Posts: 1,934
    • Karma: 11,352
    • Last Action: 2 hours
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 12:19:12 - 27/03/24 (5 months ago)
    Post link copied to clipboard Copy post link
    I know DKK is not a huge fan of my guide, but i think for someone with little coding experience, it might be a bit easier to get into Torn's API and it answers some common questions like the ones you had about timestamps etc.

    Toolbox - a collection of my tools and spreadsheets made for Torn.

    Guidebook - a collection of guides i made for Torn.

    • Retroscope [3245241]
    • Role: Civilian
    • Level: 17
    • Posts: 41
    • Karma: 4
    • Last Action: 27 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 12:22:46 - 27/03/24 (5 months ago)
    Post link copied to clipboard Copy post link
    Cheers, Omanpx.

    Right now all sources of knowledge are very useful to me. I appreciate that.

    ~~~~~ Retroscope ~~~~~

    • DeKleineKobini [2114440]
    • Role: Committee
    • Level: 100
    • Posts: 3,746
    • Karma: 5,371
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 13:33:04 - 27/03/24 (5 months ago)
    Post link copied to clipboard Copy post link

    Omanpx [1906686]

    I know DKK is not a huge fan of my guide, but i think for someone with little coding experience, it might be a bit easier to get into Torn's API and it answers some common questions like the ones you had about timestamps etc.
    While there is indeed an existing API guide that I don't really like iirc, it seems that it's not yours. Never looked at yours (that I remember), but it's actually well laid out with all the different language examples which are tailored to the environment the language should be used in.
    Last edited by DeKleineKobini on 13:34:39 - 27/03/24
    • SwiftJustice [1964832]
    • Role: Civilian
    • Level: 26
    • Posts: 152
    • Karma: 23
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 15:27:35 - 30/03/24 (5 months ago)
    Post link copied to clipboard Copy post link
    I have been learning torns api and if you wish to pull info i have been doing this with Javascript

    pulling stocks just via torns api test system
    https://www.torn.com/api.html#

    :Edit - This doesn't even require a key.. only need a key if you wanted to pull stocks related to yourself.

    https://api.torn.com/torn/?selections=stocks&key=YOURKEYHERE


    this is the display in json format ( I only used the first 2 stocks for example as its huge lol)

    {
    "stocks": {
    "1": {
    "stock_id": 1,
    "name": "Torn & Shanghai Banking",
    "acronym": "TSB",
    "current_price": 1075.74,
    "market_cap": 16912786511085,
    "total_shares": 15722002074,
    "investors": 12423,
    "benefit": {
    "type": "active",
    "frequency": 31,
    "requirement": 3000000,
    "description": "$50,000,000",
    },
    },
    "2": {
    "stock_id": 2,
    "name": "Torn City Investments",
    "acronym": "TCI",
    "current_price": 1071.6,
    "market_cap": 15836496734485,
    "total_shares": 14778365747,
    "investors": 18823,
    "benefit": {
    "type": "passive",
    "frequency": 7,
    "requirement": 1500000,
    "description": "a 10% bank interest bonus",
    },
    }

    "1": { = 1 hence being the ID container can also be the id of the stocks but the real result for id would be stock_id

    how to get the data and return a result.


    const fetchPromise = fetch(`https://api.torn.com/torn/?selections=stocks&key=APIKEY`);

    fetchPromise.then((response) => { const jsonPromise = response.json(); jsonPromise.then((data) => { console.log(data[0].stock_id); });});


    this should return the stock id in the console if you wish to learn more then you can check out the tutorial i took this from for quickness.
    https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises

    If you wanted to dive even deeper into it and ever wondered how people can make new div tags and put them into torns html document code like the crime chain which was added into crimes 2.0 every successful crime grants 1 like an war attacking ups the chain one but if you get a brown result your chain will half so if you did 10 successful crimes and chain of 10 and get a brown result your chain will drop to 5 until 0 or result = 0 (this chain when built increases your skill exp and crime exp) resource linked below

    EDIT AGAIN: Link to the crime chain if you wish to look at the code or use it.
    https://greasyfork.org/en/scripts/481776-torn-display-crime-chain

    for the crime chain to work you need to install the required extension (Violent Monkey (i find this the best), TamperMonkey) etc then you can install torn extensions that people have coded https://greasyfork.org/en/scripts/

    https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector
    Last edited by SwiftJustice on 16:05:51 - 30/03/24

                                                              ╭∩╮(︶︿︶)╭∩╮

    • Kwack [2190604]
    • Role: Civilian
    • Level: 15
    • Posts: 1,865
    • Karma: 2,838
    • Last Action: 4 hours
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 17:46:57 - 30/03/24 (5 months ago)
    Post link copied to clipboard Copy post link

    SwiftJustice [1964832]

    I have been learning torns api and if you wish to pull info i have been doing this with Javascript

    pulling stocks just via torns api test system
    https://www.torn.com/api.html#

    :Edit - This doesn't even require a key.. only need a key if you wanted to pull stocks related to yourself.

    https://api.torn.com/torn/?selections=stocks&key=YOURKEYHERE


    this is the display in json format ( I only used the first 2 stocks for example as its huge lol)

    {
    "stocks": {
    "1": {
    "stock_id": 1,
    "name": "Torn & Shanghai Banking",
    "acronym": "TSB",
    "current_price": 1075.74,
    "market_cap": 16912786511085,
    "total_shares": 15722002074,
    "investors": 12423,
    "benefit": {
    "type": "active",
    "frequency": 31,
    "requirement": 3000000,
    "description": "$50,000,000",
    },
    },
    "2": {
    "stock_id": 2,
    "name": "Torn City Investments",
    "acronym": "TCI",
    "current_price": 1071.6,
    "market_cap": 15836496734485,
    "total_shares": 14778365747,
    "investors": 18823,
    "benefit": {
    "type": "passive",
    "frequency": 7,
    "requirement": 1500000,
    "description": "a 10% bank interest bonus",
    },
    }

    "1": { = 1 hence being the ID container can also be the id of the stocks but the real result for id would be stock_id

    how to get the data and return a result.


    const fetchPromise = fetch(`https://api.torn.com/torn/?selections=stocks&key=APIKEY`);

    fetchPromise.then((response) => { const jsonPromise = response.json(); jsonPromise.then((data) => { console.log(data[0].stock_id); });});


    this should return the stock id in the console if you wish to learn more then you can check out the tutorial i took this from for quickness.
    https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises

    If you wanted to dive even deeper into it and ever wondered how people can make new div tags and put them into torns html document code like the crime chain which was added into crimes 2.0 every successful crime grants 1 like an war attacking ups the chain one but if you get a brown result your chain will half so if you did 10 successful crimes and chain of 10 and get a brown result your chain will drop to 5 until 0 or result = 0 (this chain when built increases your skill exp and crime exp) resource linked below

    EDIT AGAIN: Link to the crime chain if you wish to look at the code or use it.
    https://greasyfork.org/en/scripts/481776-torn-display-crime-chain

    for the crime chain to work you need to install the required extension (Violent Monkey (i find this the best), TamperMonkey) etc then you can install torn extensions that people have coded https://greasyfork.org/en/scripts/

    https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector
    Rather than nesting the .then callbacks, you can chain them. `.then()` returns the result of the given promise, you don't have to redeclare it.


    note: `(r) => r.json()` is the same as `(r) => { return r.json(); }`
    note 2: the line breaks are just there so the line isn't incredibly long, you can pretend they're not there

    Using promises -> Chaining | MDN

    I'd also recommend you learnt about using async / await
    • Retroscope [3245241]
    • Role: Civilian
    • Level: 17
    • Posts: 41
    • Karma: 4
    • Last Action: 27 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 09:30:24 - 31/03/24 (5 months ago)
    Post link copied to clipboard Copy post link

    SwiftJustice [1964832]

    I have been learning torns api and if you wish to pull info i have been doing this with Javascript

    pulling stocks just via torns api test system
    https://www.torn.com/api.html#

    :Edit - This doesn't even require a key.. only need a key if you wanted to pull stocks related to yourself.

    https://api.torn.com/torn/?selections=stocks&key=YOURKEYHERE


    this is the display in json format ( I only used the first 2 stocks for example as its huge lol)

    {
    "stocks": {
    "1": {
    "stock_id": 1,
    "name": "Torn & Shanghai Banking",
    "acronym": "TSB",
    "current_price": 1075.74,
    "market_cap": 16912786511085,
    "total_shares": 15722002074,
    "investors": 12423,
    "benefit": {
    "type": "active",
    "frequency": 31,
    "requirement": 3000000,
    "description": "$50,000,000",
    },
    },
    "2": {
    "stock_id": 2,
    "name": "Torn City Investments",
    "acronym": "TCI",
    "current_price": 1071.6,
    "market_cap": 15836496734485,
    "total_shares": 14778365747,
    "investors": 18823,
    "benefit": {
    "type": "passive",
    "frequency": 7,
    "requirement": 1500000,
    "description": "a 10% bank interest bonus",
    },
    }

    "1": { = 1 hence being the ID container can also be the id of the stocks but the real result for id would be stock_id

    how to get the data and return a result.


    const fetchPromise = fetch(`https://api.torn.com/torn/?selections=stocks&key=APIKEY`);

    fetchPromise.then((response) => { const jsonPromise = response.json(); jsonPromise.then((data) => { console.log(data[0].stock_id); });});


    this should return the stock id in the console if you wish to learn more then you can check out the tutorial i took this from for quickness.
    https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises

    If you wanted to dive even deeper into it and ever wondered how people can make new div tags and put them into torns html document code like the crime chain which was added into crimes 2.0 every successful crime grants 1 like an war attacking ups the chain one but if you get a brown result your chain will half so if you did 10 successful crimes and chain of 10 and get a brown result your chain will drop to 5 until 0 or result = 0 (this chain when built increases your skill exp and crime exp) resource linked below

    EDIT AGAIN: Link to the crime chain if you wish to look at the code or use it.
    https://greasyfork.org/en/scripts/481776-torn-display-crime-chain

    for the crime chain to work you need to install the required extension (Violent Monkey (i find this the best), TamperMonkey) etc then you can install torn extensions that people have coded https://greasyfork.org/en/scripts/

    https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector
    Thank you, SwiftJustice!

    There's a lot of really uselful information there. I was just using the key because it's needed to get the /user/ stock id's. And I've been playing around with divs and styling too. Making topbars and things with .innerHTML

    I'll go through your examples later today. Cheers again, mate.

    ~~~~~ Retroscope ~~~~~

    • Retroscope [3245241]
    • Role: Civilian
    • Level: 17
    • Posts: 41
    • Karma: 4
    • Last Action: 27 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 09:34:03 - 31/03/24 (5 months ago)
    Post link copied to clipboard Copy post link

    SwiftJustice [1964832]

    I have been learning torns api and if you wish to pull info i have been doing this with Javascript

    pulling stocks just via torns api test system
    https://www.torn.com/api.html#

    :Edit - This doesn't even require a key.. only need a key if you wanted to pull stocks related to yourself.

    https://api.torn.com/torn/?selections=stocks&key=YOURKEYHERE


    this is the display in json format ( I only used the first 2 stocks for example as its huge lol)

    {
    "stocks": {
    "1": {
    "stock_id": 1,
    "name": "Torn & Shanghai Banking",
    "acronym": "TSB",
    "current_price": 1075.74,
    "market_cap": 16912786511085,
    "total_shares": 15722002074,
    "investors": 12423,
    "benefit": {
    "type": "active",
    "frequency": 31,
    "requirement": 3000000,
    "description": "$50,000,000",
    },
    },
    "2": {
    "stock_id": 2,
    "name": "Torn City Investments",
    "acronym": "TCI",
    "current_price": 1071.6,
    "market_cap": 15836496734485,
    "total_shares": 14778365747,
    "investors": 18823,
    "benefit": {
    "type": "passive",
    "frequency": 7,
    "requirement": 1500000,
    "description": "a 10% bank interest bonus",
    },
    }

    "1": { = 1 hence being the ID container can also be the id of the stocks but the real result for id would be stock_id

    how to get the data and return a result.


    const fetchPromise = fetch(`https://api.torn.com/torn/?selections=stocks&key=APIKEY`);

    fetchPromise.then((response) => { const jsonPromise = response.json(); jsonPromise.then((data) => { console.log(data[0].stock_id); });});


    this should return the stock id in the console if you wish to learn more then you can check out the tutorial i took this from for quickness.
    https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises

    If you wanted to dive even deeper into it and ever wondered how people can make new div tags and put them into torns html document code like the crime chain which was added into crimes 2.0 every successful crime grants 1 like an war attacking ups the chain one but if you get a brown result your chain will half so if you did 10 successful crimes and chain of 10 and get a brown result your chain will drop to 5 until 0 or result = 0 (this chain when built increases your skill exp and crime exp) resource linked below

    EDIT AGAIN: Link to the crime chain if you wish to look at the code or use it.
    https://greasyfork.org/en/scripts/481776-torn-display-crime-chain

    for the crime chain to work you need to install the required extension (Violent Monkey (i find this the best), TamperMonkey) etc then you can install torn extensions that people have coded https://greasyfork.org/en/scripts/

    https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector

    Kwack [2190604]

    Rather than nesting the .then callbacks, you can chain them. `.then()` returns the result of the given promise, you don't have to redeclare it.


    note: `(r) => r.json()` is the same as `(r) => { return r.json(); }`
    note 2: the line breaks are just there so the line isn't incredibly long, you can pretend they're not there

    Using promises -> Chaining | MDN

    I'd also recommend you learnt about using async / await
    Thanks, Kwack. For such a simple project I'm learning a hella lot of new stuff here! I'll take look at the usage of async and await too. ?

    ~~~~~ Retroscope ~~~~~

    • SwiftJustice [1964832]
    • Role: Civilian
    • Level: 26
    • Posts: 152
    • Karma: 23
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 18:02:13 - 01/04/24 (5 months ago)
    Post link copied to clipboard Copy post link

    SwiftJustice [1964832]

    I have been learning torns api and if you wish to pull info i have been doing this with Javascript

    pulling stocks just via torns api test system
    https://www.torn.com/api.html#

    :Edit - This doesn't even require a key.. only need a key if you wanted to pull stocks related to yourself.

    https://api.torn.com/torn/?selections=stocks&key=YOURKEYHERE


    this is the display in json format ( I only used the first 2 stocks for example as its huge lol)

    {
    "stocks": {
    "1": {
    "stock_id": 1,
    "name": "Torn & Shanghai Banking",
    "acronym": "TSB",
    "current_price": 1075.74,
    "market_cap": 16912786511085,
    "total_shares": 15722002074,
    "investors": 12423,
    "benefit": {
    "type": "active",
    "frequency": 31,
    "requirement": 3000000,
    "description": "$50,000,000",
    },
    },
    "2": {
    "stock_id": 2,
    "name": "Torn City Investments",
    "acronym": "TCI",
    "current_price": 1071.6,
    "market_cap": 15836496734485,
    "total_shares": 14778365747,
    "investors": 18823,
    "benefit": {
    "type": "passive",
    "frequency": 7,
    "requirement": 1500000,
    "description": "a 10% bank interest bonus",
    },
    }

    "1": { = 1 hence being the ID container can also be the id of the stocks but the real result for id would be stock_id

    how to get the data and return a result.


    const fetchPromise = fetch(`https://api.torn.com/torn/?selections=stocks&key=APIKEY`);

    fetchPromise.then((response) => { const jsonPromise = response.json(); jsonPromise.then((data) => { console.log(data[0].stock_id); });});


    this should return the stock id in the console if you wish to learn more then you can check out the tutorial i took this from for quickness.
    https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises

    If you wanted to dive even deeper into it and ever wondered how people can make new div tags and put them into torns html document code like the crime chain which was added into crimes 2.0 every successful crime grants 1 like an war attacking ups the chain one but if you get a brown result your chain will half so if you did 10 successful crimes and chain of 10 and get a brown result your chain will drop to 5 until 0 or result = 0 (this chain when built increases your skill exp and crime exp) resource linked below

    EDIT AGAIN: Link to the crime chain if you wish to look at the code or use it.
    https://greasyfork.org/en/scripts/481776-torn-display-crime-chain

    for the crime chain to work you need to install the required extension (Violent Monkey (i find this the best), TamperMonkey) etc then you can install torn extensions that people have coded https://greasyfork.org/en/scripts/

    https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector

    Retroscope [3245241]

    Thank you, SwiftJustice!

    There's a lot of really uselful information there. I was just using the key because it's needed to get the /user/ stock id's. And I've been playing around with divs and styling too. Making topbars and things with .innerHTML

    I'll go through your examples later today. Cheers again, mate.
    Np my example returns undefined but if you adjust the query to data.stocks it will return stocks better formatted sorry my example was off but glad it was useful

                                                              ╭∩╮(︶︿︶)╭∩╮

Reply
Thread Title: