fix
This commit is contained in:
@ -1,9 +1,9 @@
|
|||||||
import os,sys
|
import os,sys
|
||||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
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 v2realbot.config import WEB_API_KEY, DATA_DIR, MEDIA_DIRECTORY, LOG_FILE, MODEL_DIR
|
||||||
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
|
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import os
|
|
||||||
from rich import print
|
from rich import print
|
||||||
from fastapi import FastAPI, Depends, HTTPException, status, File, UploadFile, Response
|
from fastapi import FastAPI, Depends, HTTPException, status, File, UploadFile, Response
|
||||||
from fastapi.security import APIKeyHeader
|
from fastapi.security import APIKeyHeader
|
||||||
|
|||||||
@ -6,6 +6,7 @@ from v2realbot.strategyblocks.indicators.helpers import get_source_series
|
|||||||
from rich import print as printanyway
|
from rich import print as printanyway
|
||||||
from traceback import format_exc
|
from traceback import format_exc
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import math
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
#indicator allowing to be based on any bar parameter (index, high,open,close,trades,volume, etc.)
|
#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"
|
return -2, "params required"
|
||||||
source = safe_get(params, "source", None)
|
source = safe_get(params, "source", None)
|
||||||
lookback = safe_get(params, "lookback", 1)
|
lookback = safe_get(params, "lookback", 1)
|
||||||
|
mod = safe_get(params, "mod", "no")
|
||||||
if source is None:
|
if source is None:
|
||||||
return -2, "source required"
|
return -2, "source required"
|
||||||
|
|
||||||
try:
|
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]
|
#return 0, state.bars[source][-1]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return -2, str(e)+format_exc()
|
return -2, str(e)+format_exc()
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
from v2realbot.strategy.base import StrategyState
|
from v2realbot.strategy.base import StrategyState
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import math
|
||||||
from rich import print as printanyway
|
from rich import print as printanyway
|
||||||
from traceback import format_exc
|
from traceback import format_exc
|
||||||
import v2realbot.utils.utils as utls
|
import v2realbot.utils.utils as utls
|
||||||
@ -52,7 +53,7 @@ def expression(state: StrategyState, params, name):
|
|||||||
# temp_ind_mapping = state.ind_mapping
|
# temp_ind_mapping = state.ind_mapping
|
||||||
|
|
||||||
#pro zacatek eval
|
#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)
|
#printanyway(val)
|
||||||
val = 0 if not np.isfinite(val) else val
|
val = 0 if not np.isfinite(val) else val
|
||||||
|
|||||||
@ -16,6 +16,7 @@ def model(state, params, ind_name):
|
|||||||
return -2, "params required"
|
return -2, "params required"
|
||||||
name = safe_get(params, "name", None)
|
name = safe_get(params, "name", None)
|
||||||
version = safe_get(params, "version", 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?
|
#TBD co s temito, kdyz se budou brat z uloženého modelu?
|
||||||
#mozna jen na TRAIN?
|
#mozna jen na TRAIN?
|
||||||
@ -54,6 +55,13 @@ def model(state, params, ind_name):
|
|||||||
|
|
||||||
value = mdl.predict(state)
|
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
|
return 0, value
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
printanyway(str(e)+format_exc())
|
printanyway(str(e)+format_exc())
|
||||||
|
|||||||
@ -22,6 +22,7 @@ def targetema(state, params, name):
|
|||||||
window_length_value = safe_get(params, "window_length_value", None)
|
window_length_value = safe_get(params, "window_length_value", None)
|
||||||
window_length_unit= safe_get(params, "window_length_unit", "position")
|
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 = safe_get(params, "source", None)
|
||||||
source_series = get_source_series(state, source, True)
|
source_series = get_source_series(state, source, True)
|
||||||
ema_slow = safe_get(params, "ema_slow", None)
|
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
|
#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
|
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)
|
# Finding first index where vwap is smaller than ema_slow (last cross)
|
||||||
idx = np.where(source_series < ema_slow_series)[0]
|
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]
|
first_idx = -len(source_series) + idx[-1]
|
||||||
#fill target list with 1 from crossed point until last
|
#fill target list with 1 from crossed point until last
|
||||||
target_list = get_source_series(state, name)
|
target_list = get_source_series(state, name)
|
||||||
target_list[first_idx:] = [1] * abs(first_idx)
|
target_list[first_idx:] = [uptrend] * abs(first_idx)
|
||||||
return 0, 0
|
params["last_pos"] = float(source_series[-1])
|
||||||
elif div_neg_threshold is not None and ema_div_series[-1] < div_neg_threshold:
|
return 0, notrend
|
||||||
|
elif div_neg_threshold is not None and ema_div_series[-1] < div_neg_threshold and float(source_series[-1])<params.get("last_neg",99999999):
|
||||||
|
|
||||||
# Finding first index where vwap is smaller than ema_slow (last cross) and price at cross must respect min PCT threshold
|
# Finding first index where vwap is smaller than ema_slow (last cross) and price at cross must respect min PCT threshold
|
||||||
idx = np.where(source_series > ema_slow_series)[0]
|
idx = np.where(source_series > ema_slow_series)[0]
|
||||||
@ -59,10 +62,21 @@ def targetema(state, params, name):
|
|||||||
first_idx = -len(source_series) + idx[-1]
|
first_idx = -len(source_series) + idx[-1]
|
||||||
#fill target list with 1 from crossed point until last
|
#fill target list with 1 from crossed point until last
|
||||||
target_list = get_source_series(state, name)
|
target_list = get_source_series(state, name)
|
||||||
target_list[first_idx:] = [-1] * abs(first_idx)
|
target_list[first_idx:] = [downtrend] * abs(first_idx)
|
||||||
return 0, 0
|
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):
|
def add_pct(pct, value):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
import os
|
|
||||||
os.environ["KERAS_BACKEND"] = "jax"
|
|
||||||
from v2realbot.strategy.base import StrategyState
|
from v2realbot.strategy.base import StrategyState
|
||||||
from v2realbot.strategy.StrategyOrderLimitVykladaciNormalizedMYSELL import StrategyOrderLimitVykladaciNormalizedMYSELL
|
from v2realbot.strategy.StrategyOrderLimitVykladaciNormalizedMYSELL import StrategyOrderLimitVykladaciNormalizedMYSELL
|
||||||
from v2realbot.enums.enums import RecordType, StartBarAlign, Mode, Account, Followup
|
from v2realbot.enums.enums import RecordType, StartBarAlign, Mode, Account, Followup
|
||||||
|
|||||||
Reference in New Issue
Block a user