Hi
I have a data base with a lot of stocks symbols.
I’m trying to find a way to dynamically update the database with the stock price (API request to some stock price api provider).
In high level, i need to write a script that will run few times a day and will do API request with the stock symbol from my wix database and will update the relevant database row.
I know how to do it with Python or PHP but i can’t find the way to do it in your platform.
How can i create a schedule script?, how can i take values from the database as variable and perform api request with those values?.
For example, query to get Apple stock value: https://financialmodelingprep.com/api/v3/stock/real-time-price/AAPL
The result is a a long Json file, i need to take the last 2 prices and find the percentage change between them
((close1 - close2) / close1) * 100 = x%
((close2 - close1) / close2) * 100 = -x%
Hi
I uploaded the code to my website (after adjustment of course), right now it’s not working.
How can i test the backend script without waiting for the job.config schedule?.
And how can i see some logs about the script execution?, failures etc…
I tried this code out myself by first putting it on a page (frontend code) and it worked fine. Once you try that, move the code to a backend file (.jsw) and call the backend function from the frontend code . You can inspect the results in a console.log() statement. Once you know it’s working, you can set up the job.config scheduling.
I’ve not used scheduled jobs before and when testing this it was awkward to get it to even run. Whether that was because I was scheduling for just a few minutes out or I made a silly mistake in my jobs.config I’m not sure. But it definitely did work in the end.
Thank you guys, it works from the frontend page but not from the backend.
I can’t understand what am i doing wrong.
i created a folder under backend folder “data-fetch” and a script file “stock-price.jsw”.
The time is UTC time https://www.timeanddate.com/worldclock/timezone/utc ?.
this is my jobs.config file:
{
“jobs”: [
{
“functionLocation”: “/data-fetch/stock-price.jsw”,
“functionName”: “recordPriceChange”,
“description”: "stock price change ",
“executionConfig”: {
“time”: “14:54” // mandatory, “hh:mm” format, UTC timezone
// “dayOfWeek”: String, // Optional:
// Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
// “dateInMonth”: Number // Optional: number between 1-31
}
},
{
// More jobs to be added here…
}
]
}
In the end I used this tool. Again, not sure if it even produced output meaningfully different to what I had already, but it did work as soon as I plugged this config in. https://shoonia.github.io/jobs.config/
Lee, Did you managed to query more than 20 symbols?.
It seems like there is a rate limit in Wix or maybe in financialmodelingprep.com. I’m getting errors after 30 symbols
“[”(node:1) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead.“]”
Is there any place in Corvid where I can see the response from the third party web server? I don’t know if the 30 api calls is a wix limitation or api rate limit on the server side.
How can I get a log of the api call outputs?
Could be wrong, but my guess is that it’s the API server, since they seem to be returning a HTML error page (bit odd that in itself). I had a quick look at their documentation and they seem to let you fetch the prices of all stocks at once. There’s definitely a better way than hitting the API a bunch of times in quick succession.
Finally I managed to solve it. Thank you for all the help.
That’s my code:
// /backend/some-folder/new-file.js
import wixData from ‘wix-data’;
import {getJSON} from ‘wix-fetch’;
console.log( “im here”);
// /Define a function
//recordPriceChange(); export async function recordPriceChange()
{
console.info(“Beginning Job”);
// import stocks data base from WIX database and save it as a variable
const table = await wixData.query(‘data collection’)
.limit(1000)
.find({suppressAuth: true });
const apiQueue = ;
const newPrices = {};
const symbols =
//Loop over the table that we imported and extract only symbols
//After the extraction, add the symbols to an array “symbols”
table.items.forEach(stock =>
{
symbols.push(stock.symbol);
});
//Join all the symbols array elements into a string with comma separation in order to add it to the api call
var join_symbols = symbols.join();
console.log(join_symbols);
//create url string
var url = (‘https://financialmodelingprep.com/api/v3/stock/real-time-price/’ + join_symbols);
//API Call
const response = await fetch(url);
const data = await response.json();
//loop over the received JSON file for(let i = 0, l = data.companiesPriceList.length; i < l; i++) {
//extract the symols
var symbol = data.companiesPriceList[i].symbol;
//extract the price
var price = data.companiesPriceList[i].price;
//Add the symbol and price into a dictionay
newPrices[symbol] = price;
}
console.log(newPrices);
//change the table price elemnts and update them with the new data the we recived from the API
table.items.forEach(stock =>
{
stock.change = (newPrices[stock.symbol] - stock.price) / stock.price * 100;
stock.price = newPrices[stock.symbol];
console.log(stock.price);
});
// Update the original database with the new values await wixData.bulkUpdate(‘data collection’, table.items, {suppressAuth: true });
console.info(“Ending Job”); return “Success”;
}