How to get the last 200 candles from bybit - algorithmic-trading

import bybit
import pprint
api_key_i = "my key"
api_secret_i = "mysecret"
client = bybit.bybit(test=True, api_key="api_key_i", api_secret="api_secret_i")
x = client.LinearKline.LinearKline_get(symbol="DOTUSDT", interval="5", **{'from':1581231260}).result()
print(len(x[0]['result']))
pprint.pprint(x[0]['result'])
I know I have to change this value **{'from':1581231260} but no idea how to change this. I have never seen this kind of syntax before.
https://bybit-exchange.github.io/docs/linear/?python#t-orderbook

What you have to do is get the current UTC datetime, use a floor method that rounds that datetime to 5 min (e.g. 12:07:53 will become 12:05:00) and subtract (200 * 5 min) from it, that datetime (is the oldest candle) needs to be converted to an unix timestamp and set in the from parameter :). Let me know if you get stuck.

Related

how to convert mm:ss.000 to s.ms using Posixct and strptime

One of my first posts, so I'll do my best. I've tried searching this, which is how I got this far..but I could use some help converting some time data in the form mm:ss.000 (that's milliseconds at the end) to seconds with a fraction at the end. For example, 2:15.45 should come out to 135.45.
This works:
t <- "02:15.45" (as.numeric(as.POSIXct(strptime(t, format = "%M:%OS"))) - as.numeric(as.POSIXct(strptime("0", format = "%S"))))
But this one, where I'm trying to use a column of my dataframe (originally in character form) does not work:
starttimesFPsnapjumps <- FPsnapjumps$start (as.numeric(as.POSIXct(strptime(starttimesFPsnapjumps, format = "%M:%OS"))) - as.numeric(as.POSIXct(strptime("0", format = "%S"))))
Perhaps it's because my numbers in the column have an extra 0 - they are mm:ss.000. Any thoughts? Thanks in advance.

How to convert a hex TimeDateStamp DWORD value into human readable format?

Can anyone explain how to convert a Hex TimeDateStamp DWORD value into human readable format?
I'm just curious as to how a value such as 0x62444DB4 is converted into
"Wednesday, 30 March 2022 10:31:48 PM"
I tried googling of course and could not find any explanation. But there are online converters available.
But I'm just interested in converting these values for myself.
Your value is a 32-bit Timestamp.
Your datetime value is a 32-bit Unix Timestamp: The number of seconds since 1/1/1970.
See https://unixtime.org/
In most programming languages you can work with the hexadecimal notation directly.
Implementation should not be done by one person alone, since a lot of engineering goes into it. Leap years, even leap seconds, timezones, daylight savings time, UTC... all these things need to be addressed when working with a timestamp.
I have added my rough calculation below as a demonstration. Definitely use an existing package or library to work with timestamps.
See the JavaScript code below for demonstration.
There I multiply your value by 1000 because JavaScript works in Milliseconds. But otherwise this applies the same to other systems.
let timestamp = 0x62444DB4;
let dateTime = new Date(timestamp * 1000);
console.log('Timestamp in seconds:', timestamp);
console.log('Human-Readable:', dateTime.toDateString() + ' ' + dateTime.toTimeString());
// Rough output, just for the time.
// Year month and day get really messy with timezones, leap years, etc.
let hours = Math.floor(timestamp/3600) % 24;
let minutes = Math.floor(timestamp/60) % 60;
let seconds = Math.floor(timestamp) % 60;
console.log('Using our own time calculation:', hours + ':' + minutes + ':' + seconds);

yfinance not returning todays intraday one minute bars

This code returns 1 minute data for the last seven days, but it only returns a single quote for today. Is there a way to get the 1 minute bar data for today?
import yfinance as yf
from datetime import datetime, timedelta
x=datetime.now()
date_N_days_ago = datetime.now() - timedelta(days=7)
print(date_N_days_ago)
msft = yf.Ticker("MSFT")
data_df = yf.download("MSFT",start=date_N_days_ago.strftime("%Y"+"-"+"%m"+"-"+"%d"), interval="1m", end=x.strftime("%Y"+"-"+"%m"+"-"+"%d"))
data_df.to_csv('ds.csv')
By default yfinance takes period='max' as parameter.
To get only last day or intraday during opening times you have to specify this parameter on your own.
In your case:
data_df = yf.download("MSFT",start=date_N_days_ago.strftime("%Y"+"-"+"%m"+"-"+"%d"), interval="1m", end=x.strftime("%Y"+"-"+"%m"+"-"+"%d"))
Just open the function for more details.
This is possible values for period - always depending on your interval value
period : str
Valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
Either Use period parameter or use start and end
For example period='6m', interval='1d' is the maximum yahoo delivers with 1d candles

Lua convert format "X h X min" to seconds in WoW

So in the game World of Warcraft I have managed to get the remaining time of a mission via their API. The problem for me is that I want to convert this to seconds to then be able to check at which time the mission will finish. If I call the function time() in the game I get a response similar to this 1418569973 which to me, makes no sense. But this is why I need to convert it to seconds, because then I can simply add the amount of seconds I get to the current time and get the end time of the mission.
But my problem is that the when I look into the table that gives me the current time left of a mission it returns a string with the format "X h X min" for example "4 h 34 min". I need to convert that to seconds but I literally have no idea on where to start. I'm thinking of something like removing the "h" and the "min" in a function. But from there I'm not really sure of where to go.
os.time() returns the seconds since epoch. Thus, "1418569973" gives you "12/14/14 03:12:53 PM" UTC. Now, to convert your string to seconds:
local iH, iM = sInput:match "(%d+) h (%d+) min"
iH, iM = tonumber( iH ), tonumber( iM )
local iSec = iH * 3600 + iM * 60

number of days in a period that fall within another period

I have 2 independent but contiguous date ranges. The first range is the start and end date for a project. Lets say start = 3/21/10 and end = 5/16/10. The second range is a month boundary (say 3/1/10 to 3/31/10, 4/1/10 to 4/30/10, etc.) I need to figure out how many days in each month fall into the first range.
The answer to my example above is March = 10, April = 30, May = 16.
I am trying to figure out an excel formula or VBA function that will give me this value.
Any thoughts on an algorithm for this? I feel it should be rather easy but I can't seem to figure it out.
I have a formula which will return TRUE/FALSE if ANY part of the month range is within the project start/end but not the number of days. That function is below.
return month_start <= project_end And month_end >= project_start
Think it figured it out.
=MAX( MIN(project_end, month_end) - MAX(project_start,month_start) + 1 , 0 )

Resources