statement plugin added
This commit is contained in:
@ -1,23 +1,28 @@
|
|||||||
# def my_function():
|
import numpy as np
|
||||||
# return "Hello, World!"
|
|
||||||
|
indicators = dict(time=[1,2,3], rsi=[15,16,17], vwap=[5,6,7])
|
||||||
|
bars = dict(time=[1,2,3], open=[15,16,17], close=[5,6,7])
|
||||||
|
|
||||||
|
#last values
|
||||||
|
operation = "rsi[-1] * (vwap[-1] + 1) + var1 + np.mean(rsi[-2:])"
|
||||||
|
|
||||||
|
#nejdriv naharadit indikatory skutečnými hodnotami
|
||||||
|
|
||||||
|
#muzu iterovat nad indikatory
|
||||||
|
# for key in indicators.keys():
|
||||||
|
# if key != 'time':
|
||||||
|
# operation = operation.replace(key, f"indicators['{key}'][-1]")
|
||||||
|
|
||||||
|
var1 = 13
|
||||||
|
|
||||||
|
#a nebo si jednou pri initiu vytvorim mapovaci dictionary indikatorů a barů
|
||||||
|
local_dict_inds = {key: indicators[key] for key in indicators.keys() if key != "time"}
|
||||||
|
local_dict_bars = {key: bars[key] for key in bars.keys() if key != "time"}
|
||||||
|
|
||||||
|
local_dict = {**local_dict_inds, **local_dict_bars}
|
||||||
|
|
||||||
|
print(local_dict)
|
||||||
|
|
||||||
|
|
||||||
# def call_function_by_name():
|
print(operation)
|
||||||
# func_name = "my_function"
|
print(eval(operation, None, local_dict))
|
||||||
# # Use eval to call the function
|
|
||||||
# result = eval(func_name)()
|
|
||||||
|
|
||||||
def my_function():
|
|
||||||
return "Hello, World!"
|
|
||||||
|
|
||||||
|
|
||||||
def call_function_by_name():
|
|
||||||
# Create a closure to capture the my_function function
|
|
||||||
def inner_function():
|
|
||||||
return eval("my_function")()
|
|
||||||
|
|
||||||
result = inner_function()
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
print(call_function_by_name())
|
|
||||||
@ -117,6 +117,12 @@ def init(state: StrategyState):
|
|||||||
initialize_dynamic_indicators(state)
|
initialize_dynamic_indicators(state)
|
||||||
intialize_directive_conditions(state)
|
intialize_directive_conditions(state)
|
||||||
|
|
||||||
|
#intitialize indicator mapping (for use in operation) - mozna presunout do samostatne funkce prip dat do base kdyz se osvedci
|
||||||
|
local_dict_inds = {key: state.indicators[key] for key in state.indicators.keys() if key != "time"}
|
||||||
|
local_dict_bars = {key: state.bars[key] for key in state.bars.keys() if key != "time"}
|
||||||
|
|
||||||
|
state.ind_mapping = {**local_dict_inds, **local_dict_bars}
|
||||||
|
printanyway("IND MAPPING DONE:", state.ind_mapping)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
name = os.path.basename(__file__)
|
name = os.path.basename(__file__)
|
||||||
|
|||||||
@ -593,7 +593,8 @@ def populate_metrics_output_directory(strat: StrategyInstance, inter_batch_param
|
|||||||
WIP
|
WIP
|
||||||
Spocte zakladni metriky pred ulozenim do archivu
|
Spocte zakladni metriky pred ulozenim do archivu
|
||||||
|
|
||||||
1) zatim jen max pozice
|
Toto cele predelat nejak systemove v ramci systemoveho reportingu. Tato preliminary cast by mela umoznovat pridavat
|
||||||
|
a ukladat zakladni metriky, ktere me zajimaji ihned po skonceni runu.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tradeList = strat.state.tradeList
|
tradeList = strat.state.tradeList
|
||||||
@ -646,11 +647,16 @@ def populate_metrics_output_directory(strat: StrategyInstance, inter_batch_param
|
|||||||
short_wins = 0
|
short_wins = 0
|
||||||
max_profit = 0
|
max_profit = 0
|
||||||
max_profit_time = None
|
max_profit_time = None
|
||||||
|
max_loss = 0
|
||||||
|
max_loss_time = None
|
||||||
long_cnt = 0
|
long_cnt = 0
|
||||||
short_cnt = 0
|
short_cnt = 0
|
||||||
|
|
||||||
if "prescribedTrades" in strat.state.vars:
|
if "prescribedTrades" in strat.state.vars:
|
||||||
for trade in strat.state.vars.prescribedTrades:
|
for trade in strat.state.vars.prescribedTrades:
|
||||||
|
if trade.profit_sum < max_loss:
|
||||||
|
max_loss = trade.profit_sum
|
||||||
|
max_loss_time = trade.last_update
|
||||||
if trade.profit_sum > max_profit:
|
if trade.profit_sum > max_profit:
|
||||||
max_profit = trade.profit_sum
|
max_profit = trade.profit_sum
|
||||||
max_profit_time = trade.last_update
|
max_profit_time = trade.last_update
|
||||||
@ -680,6 +686,8 @@ def populate_metrics_output_directory(strat: StrategyInstance, inter_batch_param
|
|||||||
res["profit"]["short_losses"] = round(short_losses,2)
|
res["profit"]["short_losses"] = round(short_losses,2)
|
||||||
res["profit"]["max_profit"] = round(max_profit,2)
|
res["profit"]["max_profit"] = round(max_profit,2)
|
||||||
res["profit"]["max_profit_time"] = str(max_profit_time)
|
res["profit"]["max_profit_time"] = str(max_profit_time)
|
||||||
|
res["profit"]["max_loss"] = round(max_loss,2)
|
||||||
|
res["profit"]["max_loss_time"] = str(max_loss_time)
|
||||||
#vlozeni celeho listu
|
#vlozeni celeho listu
|
||||||
res["prescr_trades"]=json.loads(json.dumps(strat.state.vars.prescribedTrades, default=json_serial))
|
res["prescr_trades"]=json.loads(json.dumps(strat.state.vars.prescribedTrades, default=json_serial))
|
||||||
|
|
||||||
|
|||||||
@ -226,10 +226,10 @@ def _get_stratin(stratin_id) -> StrategyInstance:
|
|||||||
@app.put("/stratins/{stratin_id}/run", dependencies=[Depends(api_key_auth)], status_code=status.HTTP_200_OK)
|
@app.put("/stratins/{stratin_id}/run", dependencies=[Depends(api_key_auth)], status_code=status.HTTP_200_OK)
|
||||||
def _run_stratin(stratin_id: UUID, runReq: RunRequest):
|
def _run_stratin(stratin_id: UUID, runReq: RunRequest):
|
||||||
#print(runReq)
|
#print(runReq)
|
||||||
if runReq.bt_from is not None:
|
if runReq.bt_from is not None and runReq.bt_from.tzinfo is None:
|
||||||
runReq.bt_from = zonePRG.localize(runReq.bt_from)
|
runReq.bt_from = zonePRG.localize(runReq.bt_from)
|
||||||
|
|
||||||
if runReq.bt_to is not None:
|
if runReq.bt_to is not None and runReq.bt_to.tzinfo is None:
|
||||||
runReq.bt_to = zonePRG.localize(runReq.bt_to)
|
runReq.bt_to = zonePRG.localize(runReq.bt_to)
|
||||||
#pokud jedeme nad test intervaly anebo je požadováno více dní - pouštíme jako batch day by day
|
#pokud jedeme nad test intervaly anebo je požadováno více dní - pouštíme jako batch day by day
|
||||||
#do budoucna dát na FE jako flag
|
#do budoucna dát na FE jako flag
|
||||||
@ -376,6 +376,7 @@ def _get_archived_runner_log_byID(runner_id: UUID, timestamp_from: float, timest
|
|||||||
#get alpaca history bars
|
#get alpaca history bars
|
||||||
@app.get("/history_bars/", dependencies=[Depends(api_key_auth)])
|
@app.get("/history_bars/", dependencies=[Depends(api_key_auth)])
|
||||||
def _get_alpaca_history_bars(symbol: str, datetime_object_from: datetime, datetime_object_to: datetime, timeframe_amount: int, timeframe_unit: TimeFrameUnit) -> list[Bar]:
|
def _get_alpaca_history_bars(symbol: str, datetime_object_from: datetime, datetime_object_to: datetime, timeframe_amount: int, timeframe_unit: TimeFrameUnit) -> list[Bar]:
|
||||||
|
print("Requested dates ",datetime_object_from,datetime_object_to)
|
||||||
res, set =cs.get_alpaca_history_bars(symbol, datetime_object_from, datetime_object_to, TimeFrame(amount=timeframe_amount,unit=timeframe_unit))
|
res, set =cs.get_alpaca_history_bars(symbol, datetime_object_from, datetime_object_to, TimeFrame(amount=timeframe_amount,unit=timeframe_unit))
|
||||||
if res == 0:
|
if res == 0:
|
||||||
return set
|
return set
|
||||||
|
|||||||
@ -692,6 +692,8 @@ class StrategyState:
|
|||||||
self.bars = AttributeDict(bars)
|
self.bars = AttributeDict(bars)
|
||||||
self.trades = AttributeDict(trades)
|
self.trades = AttributeDict(trades)
|
||||||
self.indicators = AttributeDict(time=[])
|
self.indicators = AttributeDict(time=[])
|
||||||
|
#pro mapping indikatoru pro pouziti v operation statementu
|
||||||
|
self.ind_mapping = {}
|
||||||
self.cbar_indicators = AttributeDict(time=[])
|
self.cbar_indicators = AttributeDict(time=[])
|
||||||
#secondary timeframe indicators
|
#secondary timeframe indicators
|
||||||
#self.secondary_indicators = AttributeDict(time=[], sec_price=[])
|
#self.secondary_indicators = AttributeDict(time=[], sec_price=[])
|
||||||
|
|||||||
32
v2realbot/strategyblocks/indicators/custom/statement.py
Normal file
32
v2realbot/strategyblocks/indicators/custom/statement.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from v2realbot.utils.utils import isrising, isfalling,zoneNY, price2dec, print, safe_get, is_still, is_window_open, eval_cond_dict, crossed_down, crossed_up, crossed, is_pivot, json_serial, pct_diff, create_new_bars, slice_dict_lists
|
||||||
|
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
|
||||||
|
#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"
|
||||||
|
#indicator name
|
||||||
|
operation = safe_get(params, "statement", None)
|
||||||
|
|
||||||
|
if operation is None :
|
||||||
|
return -2, "required param missing"
|
||||||
|
|
||||||
|
#pro zacatek eval
|
||||||
|
val = eval(operation, None, state.ind_mapping)
|
||||||
|
|
||||||
|
#val = ne.evaluate(operation, state.ind_mapping)
|
||||||
|
|
||||||
|
state.ilog(lvl=1,e=f"INSIDE {funcName} {operation=} res:{val}", **params)
|
||||||
|
return 0, val
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -5,21 +5,8 @@ from rich import print as printanyway
|
|||||||
from v2realbot.indicators.indicators import ema
|
from v2realbot.indicators.indicators import ema
|
||||||
from traceback import format_exc
|
from traceback import format_exc
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
#TODO TENTO IMPORT VYMYSLET, abych naloadoval package custom a nemusel nic pridat (vymyslet dynamicke volani z cele package ci)
|
|
||||||
#from v2realbot.strategyblocks.indicators.custom._upscaled_rsi_wip import upscaledrsi
|
|
||||||
import v2realbot.strategyblocks.indicators.custom as ci
|
import v2realbot.strategyblocks.indicators.custom as ci
|
||||||
# from v2realbot.strategyblocks.indicators.custom.barparams import barparams
|
|
||||||
# from v2realbot.strategyblocks.indicators.custom.basestats import basestats
|
|
||||||
# from v2realbot.strategyblocks.indicators.custom.delta import delta
|
|
||||||
# from v2realbot.strategyblocks.indicators.custom.divergence import divergence
|
|
||||||
# from v2realbot.strategyblocks.indicators.custom.model import model
|
|
||||||
# from v2realbot.strategyblocks.indicators.custom.opengap import opengap
|
|
||||||
# from v2realbot.strategyblocks.indicators.custom.slope import slope
|
|
||||||
# from v2realbot.strategyblocks.indicators.custom.conditional import conditional
|
|
||||||
# from v2realbot.strategyblocks.indicators.custom.mathop import mathop
|
|
||||||
|
|
||||||
# import v2realbot.strategyblocks.indicators.custom as ci
|
|
||||||
|
|
||||||
def populate_dynamic_custom_indicator(data, state: StrategyState, name):
|
def populate_dynamic_custom_indicator(data, state: StrategyState, name):
|
||||||
ind_type = "custom"
|
ind_type = "custom"
|
||||||
|
|||||||
Reference in New Issue
Block a user