prvni verze dynamicky ind

This commit is contained in:
David Brazda
2023-10-23 21:36:15 +02:00
parent dd90b03a30
commit 0c4fb20c66
26 changed files with 4812 additions and 580 deletions

View File

@ -3,6 +3,8 @@ from v2realbot.strategy.base import StrategyState
from v2realbot.indicators.indicators import ema, natr, roc
from v2realbot.indicators.oscillators import rsi
from traceback import format_exc
from v2realbot.strategyblocks.indicators.helpers import get_source_series
#RSI INDICATOR
# type = RSI, source = [close, vwap, hlcc4], rsi_length = [14], MA_length = int (optional), on_confirmed_only = [true, false]
# pokud existuje MA, vytvarime i stejnojnojmenny MAcko
@ -19,16 +21,14 @@ def populate_dynamic_RSI_indicator(data, state: StrategyState, name):
#poustet kazdy tick nebo jenom na confirmed baru (on_confirmed_only = true)
on_confirmed_only = safe_get(options, 'on_confirmed_only', False)
req_source = safe_get(options, 'source', 'vwap')
if req_source not in ["close", "vwap","hlcc4"]:
state.ilog(lvl=1,e=f"Unknown source error {req_source} for {name}")
return
rsi_length = int(safe_get(options, "RSI_length",14))
req_source = safe_get(options, 'source', 'vwap')
rsi_length = int(safe_get(options, "length",14))
rsi_MA_length = safe_get(options, "MA_length", None)
if on_confirmed_only is False or (on_confirmed_only is True and data['confirmed']==1):
try:
source = state.bars[req_source]
#source = state.bars[req_source]
source = get_source_series(state, req_source)
#cekame na dostatek dat
if len(source) > rsi_length:
rsi_res = rsi(source, rsi_length)

View File

@ -25,6 +25,8 @@ def upscaledrsi(state, params):
#pokud potrebuju jen close nebo open muzu pouzit toto
# vezme to N-th element z pole
#TODO resample any series
def resample_close_prices(bars, new_resolution):
# Check that the new resolution is a multiple of the old resolution.
if new_resolution % bars['resolution'][-1] != 0:

View File

@ -7,6 +7,7 @@ from traceback import format_exc
from v2realbot.ml.ml import ModelML
import numpy as np
from collections import defaultdict
from scipy.stats import linregress
#vstupem je bud indicator nebo bar parametr
#na tomto vstupu dokaze provest zakladni statisticke funkce pro subpole X hodnot zpatky
@ -61,6 +62,26 @@ def basestats(state, params):
#val = 2 * (angle_deg / 180) - 1
elif func =="stdev":
val = np.std(source_array)
#linregres slope
elif func == "slope":
if len(source_array) < 4:
return -2, "less than 4 elmnts"
try:
np.seterr(all="raise")
val, _, _, _, _ = linregress(np.arange(len(source_array)), source_array)
val = round(val, 4)
except FloatingPointError:
return -2, "FloatingPointError"
#zatim takto, dokud nebudou podporovany indikatory s vice vystupnimi
elif func == "intercept":
if len(source_array) < 4:
return -2, "less than 4 elmnts"
try:
np.seterr(all="raise")
_, val, _, _, _ = linregress(np.arange(len(source_array)), source_array)
val = round(val, 4)
except FloatingPointError:
return -2, "FloatingPointError"
else:
return -2, "wrong function"

View File

@ -2,17 +2,17 @@ from v2realbot.utils.utils import isrising, isfalling,zoneNY, price2dec, print,
from v2realbot.strategy.base import StrategyState
import numpy as np
#allows executing a statement - pozor neni sanitized a zatim se spousti i v globalni scopu
#allows executing a expression - pozor neni sanitized a zatim se spousti i v globalni scopu
#v pripade jineho nez soukromeho uziti zabezpecit
#do budoucna prozkoumat NUMEXPR - ten omezuje jen na operatory a univerzalni funkce
#eval nyni umi i user-defined function, string operation and control statements
#teroeticky se dá pouzit i SYMPY - kde se daji vytvorit jednotlive symboly s urcitou funkcni
def statement(state: StrategyState, params):
funcName = "statement"
def expression(state: StrategyState, params):
funcName = "expression"
#indicator name
operation = safe_get(params, "statement", None)
operation = safe_get(params, "expression", None)
if operation is None :
return -2, "required param missing"
@ -20,7 +20,7 @@ def statement(state: StrategyState, params):
state.ilog(lvl=1,e=f"BEFORE {funcName} {operation=}", **params)
#pro zacatek eval
val = eval(operation, None, state.ind_mapping)
val = eval(operation, {'state': state}, state.ind_mapping)
if not np.isfinite(val):

View File

@ -10,7 +10,7 @@ from collections import defaultdict
from v2realbot.strategyblocks.indicators.helpers import value_or_indicator
#IMPLEMENTS different types of moving averages
#IMPLEMENTS different types of moving averages in package v2realbot.indicators.moving_averages
def ma(state, params):
funcName = "ma"
type = safe_get(params, "type", "ema")