Add timeframe_to_prev_candle

This commit is contained in:
Matthias
2019-08-12 16:11:43 +02:00
parent 933a553dd4
commit dd0ba183f8
2 changed files with 43 additions and 3 deletions

View File

@@ -793,6 +793,20 @@ def timeframe_to_msecs(ticker_interval: str) -> int:
return ccxt.Exchange.parse_timeframe(ticker_interval) * 1000
def timeframe_to_prev_date(timeframe: str, date: datetime = None):
"""
Use Timeframe and determine last possible candle.
"""
if not date:
date = datetime.utcnow()
timeframe_secs = timeframe_to_seconds(timeframe)
# Get offset based on timerame_secs
offset = date.timestamp() % timeframe_secs
# Subtract seconds passed since last offset
new_timestamp = date.timestamp() - offset
return datetime.fromtimestamp(new_timestamp, tz=timezone.utc)
def timeframe_to_next_date(timeframe: str, date: datetime = None):
"""
Use Timeframe and determine next candle.
@@ -800,6 +814,8 @@ def timeframe_to_next_date(timeframe: str, date: datetime = None):
if not date:
date = datetime.utcnow()
timeframe_secs = timeframe_to_seconds(timeframe)
# Get offset to prev timeframe
offset = date.timestamp() % timeframe_secs
# Add remaining seconds to next timeframe
new_timestamp = date.timestamp() + (timeframe_secs - offset)
return datetime.fromtimestamp(new_timestamp, tz=timezone.utc)