Skip to content

Commit

Permalink
split stockstats candles retrieve and indicator computing
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLep committed May 17, 2022
1 parent 0ad6ae7 commit c2b06b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion strategies/cryptofeed_strategy/cryptofeed_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ def open_position(self, pair: str, side: SideEnum) -> None:
if opened_position_number >= MAX_SIMULTANEOUSLY_OPENED_POSITIONS:
return

atr_14 = StockUtils.get_atr_14(self.ftx_rest_api, pair)
stockstats_candles = StockUtils.get_last_x_stockstats_candles(self.ftx_rest_api, pair, 20)
atr_14 = StockUtils.get_atr_14(stockstats_candles)
logging.info(f"{pair} - atr_14: {atr_14.iloc[-1]}")

# Getting current price of pair
Expand Down
39 changes: 31 additions & 8 deletions strategies/cryptofeed_strategy/stock_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
import time
from typing import List, Tuple

import pandas as pd
import stockstats
Expand All @@ -26,26 +27,27 @@ def get_market_price(ftx_rest_api: FtxRestApi, pair: str) -> float:
return market_data.get("price")

@staticmethod
def get_atr_14(ftx_rest_api: FtxRestApi, pair: str) -> pd.DataFrame:
def get_last_x_stockstats_candles(ftx_rest_api: FtxRestApi, pair: str, x: int) -> List:
"""
Get the atr 14 stockstat indicator
Get the last x stockstats candles indicator
:param ftx_rest_api: a FTX rest api instance
:param pair: The pair to get the atr 14 stockstat indicator for
:return: The atr 14 stockstat indicator
:param pair: The pair to get the candles for
:param x: The number of candles to get
:return: The last x stockstats candles
"""
# Retrieve 20 last candles
# Retrieve x last candles
candles = ftx_rest_api.get(f"markets/{pair}-PERP/candles", {
"resolution": 60,
"limit": 20,
"start_time": math.floor(time.time() - 60 * 20)
"limit": x,
"start_time": math.floor(time.time() - 60 * x)
})

candles = [format_ohlcv_raw_data(candle, 60) for candle in candles]
candles = [Candle(candle["id"], candle["time"], candle["open_price"], candle["high_price"], candle["low_price"],
candle["close_price"], candle["volume"]) for candle in candles]

stock_stat_candles = [{
return [{
"date": candle.time,
"open": candle.open_price,
"high": candle.high_price,
Expand All @@ -54,9 +56,30 @@ def get_atr_14(ftx_rest_api: FtxRestApi, pair: str) -> pd.DataFrame:
"volume": candle.volume
} for candle in candles]

@staticmethod
def get_atr_14(stock_stat_candles: List) -> pd.DataFrame:
"""
Get the atr 14 stockstats indicator
:param stock_stat_candles: The candles to compute atr for
:return: The atr 14 stockstats indicator
"""

stock_indicators = stockstats.StockDataFrame.retype(pd.DataFrame(stock_stat_candles))
return stock_indicators["atr_14"]

@staticmethod
def get_bollinger_bands(stock_stat_candles: List) -> Tuple[pd.DataFrame, pd.DataFrame]:
"""
Get the Bollinger bands (up, down) stockstats indicator
:param stock_stat_candles: The candles to compute atr for
:return: The Bollinger bands (up, down) stockstats indicators
"""

stock_indicators = stockstats.StockDataFrame.retype(pd.DataFrame(stock_stat_candles))
return stock_indicators["boll_ub"], stock_indicators["boll_lb"]

@staticmethod
def get_available_balance_without_borrow(ftx_rest_api: FtxRestApi) -> float:
"""
Expand Down

0 comments on commit c2b06b9

Please sign in to comment.