diff --git a/v2realbot/main.py b/v2realbot/main.py index ff8a444..87ade08 100644 --- a/v2realbot/main.py +++ b/v2realbot/main.py @@ -1,9 +1,9 @@ import os,sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +os.environ["KERAS_BACKEND"] = "jax" from v2realbot.config import WEB_API_KEY, DATA_DIR, MEDIA_DIRECTORY, LOG_FILE, MODEL_DIR from alpaca.data.timeframe import TimeFrame, TimeFrameUnit from datetime import datetime -import os from rich import print from fastapi import FastAPI, Depends, HTTPException, status, File, UploadFile, Response from fastapi.security import APIKeyHeader diff --git a/v2realbot/strategyblocks/indicators/custom/barparams.py b/v2realbot/strategyblocks/indicators/custom/barparams.py index 0e44fb2..a991c82 100644 --- a/v2realbot/strategyblocks/indicators/custom/barparams.py +++ b/v2realbot/strategyblocks/indicators/custom/barparams.py @@ -6,6 +6,7 @@ from v2realbot.strategyblocks.indicators.helpers import get_source_series from rich import print as printanyway from traceback import format_exc import numpy as np +import math from collections import defaultdict #indicator allowing to be based on any bar parameter (index, high,open,close,trades,volume, etc.) @@ -15,10 +16,19 @@ def barparams(state, params, name): return -2, "params required" source = safe_get(params, "source", None) lookback = safe_get(params, "lookback", 1) + mod = safe_get(params, "mod", "no") if source is None: return -2, "source required" + try: - return 0, get_source_series(state, source)[-lookback] + source_series = get_source_series(state, source) + match mod: + case "logreturn": + val = math.log(source_series[-lookback]/source_series[-lookback-1]) + case _: + val = source_series[-lookback] + + return 0, val #return 0, state.bars[source][-1] except Exception as e: return -2, str(e)+format_exc() diff --git a/v2realbot/strategyblocks/indicators/custom/expression.py b/v2realbot/strategyblocks/indicators/custom/expression.py index ef20d18..6c8caba 100644 --- a/v2realbot/strategyblocks/indicators/custom/expression.py +++ b/v2realbot/strategyblocks/indicators/custom/expression.py @@ -1,5 +1,6 @@ from v2realbot.strategy.base import StrategyState import numpy as np +import math from rich import print as printanyway from traceback import format_exc import v2realbot.utils.utils as utls @@ -52,7 +53,7 @@ def expression(state: StrategyState, params, name): # temp_ind_mapping = state.ind_mapping #pro zacatek eval - val = eval(operation, {'state': state, 'np': np, 'utls': utls}, temp_ind_mapping) + val = eval(operation, {'state': state, 'np': np, 'utls': utls, 'math' : math}, temp_ind_mapping) #printanyway(val) val = 0 if not np.isfinite(val) else val diff --git a/v2realbot/strategyblocks/indicators/custom/model.py b/v2realbot/strategyblocks/indicators/custom/model.py index da58612..cde093e 100644 --- a/v2realbot/strategyblocks/indicators/custom/model.py +++ b/v2realbot/strategyblocks/indicators/custom/model.py @@ -16,6 +16,7 @@ def model(state, params, ind_name): return -2, "params required" name = safe_get(params, "name", None) version = safe_get(params, "version", None) + output = safe_get(params, "output", "single") #TBD co s temito, kdyz se budou brat z uloženého modelu? #mozna jen na TRAIN? @@ -54,6 +55,13 @@ def model(state, params, ind_name): value = mdl.predict(state) + #zatim podpora podle shapu (tzn. single value a categorical) - v budoucna pracovat s parametrem output + if value.shape == (1,1): + value = float(value) + else: + #printanyway(value) + value = int(np.argmax(value, axis=1)) + return 0, value except Exception as e: printanyway(str(e)+format_exc()) diff --git a/v2realbot/strategyblocks/indicators/custom/targetema.py b/v2realbot/strategyblocks/indicators/custom/targetema.py index 0202bf4..57d837e 100644 --- a/v2realbot/strategyblocks/indicators/custom/targetema.py +++ b/v2realbot/strategyblocks/indicators/custom/targetema.py @@ -22,6 +22,7 @@ def targetema(state, params, name): window_length_value = safe_get(params, "window_length_value", None) window_length_unit= safe_get(params, "window_length_unit", "position") + downtrend, notrend, uptrend = safe_get(params, "output_vals", [-1,0,1]) source = safe_get(params, "source", None) source_series = get_source_series(state, source, True) ema_slow = safe_get(params, "ema_slow", None) @@ -33,7 +34,8 @@ def targetema(state, params, name): #mezi start a end price musi byt tento threshold req_min_pct_chng = float(safe_get(params, "req_min_pct_chng", 0.04)) #required PCT chng - if div_pos_threshold is not None and ema_div_series[-1] > div_pos_threshold: + #kvalifikuji divergence s cenou jen vyssi nez posledni (pozor pri reverse trendu musime resetovat) + if div_pos_threshold is not None and ema_div_series[-1] > div_pos_threshold and float(source_series[-1])>params.get("last_pos",0): # Finding first index where vwap is smaller than ema_slow (last cross) idx = np.where(source_series < ema_slow_series)[0] @@ -45,9 +47,10 @@ def targetema(state, params, name): first_idx = -len(source_series) + idx[-1] #fill target list with 1 from crossed point until last target_list = get_source_series(state, name) - target_list[first_idx:] = [1] * abs(first_idx) - return 0, 0 - elif div_neg_threshold is not None and ema_div_series[-1] < div_neg_threshold: + target_list[first_idx:] = [uptrend] * abs(first_idx) + params["last_pos"] = float(source_series[-1]) + return 0, notrend + elif div_neg_threshold is not None and ema_div_series[-1] < div_neg_threshold and float(source_series[-1]) ema_slow_series)[0] @@ -59,10 +62,21 @@ def targetema(state, params, name): first_idx = -len(source_series) + idx[-1] #fill target list with 1 from crossed point until last target_list = get_source_series(state, name) - target_list[first_idx:] = [-1] * abs(first_idx) - return 0, 0 + target_list[first_idx:] = [downtrend] * abs(first_idx) + params["last_neg"] = float(source_series[-1]) + return 0, notrend + + #test resetujeme nejvyssi body po uplynuti 20 pozic od trendu (tim konci ochranne okno) + # Finding the first 1 from backwards and its position + target_numpy = get_source_series(state, name, True) + position = np.where(target_numpy[::-1] == uptrend)[0][0] + 1 + if position % 20 == 0: + params["last_pos"] = 0 + position = np.where(target_numpy[::-1] == downtrend)[0][0] + 1 + if position % 20 == 0: + params["last_neg"] = 99999999 - return 0, 0 + return 0, notrend def add_pct(pct, value): """ diff --git a/v2realbot/strategyblocks/inits/init_indicators.py b/v2realbot/strategyblocks/inits/init_indicators.py index 9c59506..0897115 100644 --- a/v2realbot/strategyblocks/inits/init_indicators.py +++ b/v2realbot/strategyblocks/inits/init_indicators.py @@ -1,5 +1,3 @@ -import os -os.environ["KERAS_BACKEND"] = "jax" from v2realbot.strategy.base import StrategyState from v2realbot.strategy.StrategyOrderLimitVykladaciNormalizedMYSELL import StrategyOrderLimitVykladaciNormalizedMYSELL from v2realbot.enums.enums import RecordType, StartBarAlign, Mode, Account, Followup