add tick_interval to get_ticker_history as an optional parameter

This commit is contained in:
gcarq 2017-11-07 18:41:48 +01:00
parent ddc7c94a1d
commit a1388ef296
3 changed files with 19 additions and 14 deletions

View File

@ -1,7 +1,7 @@
import enum
import logging
from random import randint
from typing import List, Dict, Any
from typing import List, Dict, Any, Optional
import arrow
@ -122,8 +122,8 @@ def get_ticker(pair: str) -> dict:
return _API.get_ticker(pair)
def get_ticker_history(pair: str) -> List:
return _API.get_ticker_history(pair)
def get_ticker_history(pair: str, tick_interval: Optional[int] = 5) -> List:
return _API.get_ticker_history(pair, tick_interval)
def cancel_order(order_id: str) -> None:

View File

@ -1,5 +1,5 @@
import logging
from typing import List, Dict
from typing import List, Dict, Optional
import requests
from bittrex.bittrex import Bittrex as _Bittrex
@ -20,14 +20,11 @@ class Bittrex(Exchange):
BASE_URL: str = 'https://www.bittrex.com'
TICKER_METHOD: str = BASE_URL + '/Api/v2.0/pub/market/GetTicks'
PAIR_DETAIL_METHOD: str = BASE_URL + '/Market/Index'
# Ticker inveral
TICKER_INTERVAL: str = 'fiveMin'
# Sleep time to avoid rate limits, used in the main loop
SLEEP_TIME: float = 25
@property
def sleep_time(self) -> float:
return self.SLEEP_TIME
""" Sleep time to avoid rate limits, used in the main loop """
return 25
def __init__(self, config: dict) -> None:
global _API, _EXCHANGE_CONF
@ -86,17 +83,24 @@ class Bittrex(Exchange):
'last': float(data['result']['Last']),
}
def get_ticker_history(self, pair: str):
url = self.TICKER_METHOD
def get_ticker_history(self, pair: str, tick_interval: int):
headers = {
# TODO: Set as global setting
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
}
if tick_interval == 1:
interval = 'oneMin'
elif tick_interval == 5:
interval = 'fiveMin'
else:
raise ValueError('Cannot parse tick_interval: {}'.format(tick_interval))
params = {
'marketName': pair.replace('_', '-'),
'tickInterval': self.TICKER_INTERVAL,
'tickInterval': interval,
}
data = requests.get(url, params=params, headers=headers).json()
data = requests.get(self.TICKER_METHOD, params=params, headers=headers).json()
if not data['success']:
raise RuntimeError('{message} params=({pair})'.format(
message=data['message'],

View File

@ -83,10 +83,11 @@ class Exchange(ABC):
"""
@abstractmethod
def get_ticker_history(self, pair: str) -> List:
def get_ticker_history(self, pair: str, tick_interval: int) -> List:
"""
Gets ticker history for given pair.
:param pair: Pair as str, format: BTC_ETC
:param tick_interval: ticker interval in minutes
:return: list, format: [
{
'O': float, (Open)