From 30044dc4ea9a63661d2bb7d2e4ef48192d7fe9e5 Mon Sep 17 00:00:00 2001 From: David Brazda Date: Sat, 24 Aug 2024 06:57:53 +0200 Subject: [PATCH] druhy commit --- v2realbot/backtesting/backtester.py | 9 ++++----- v2realbot/controller/services.py | 2 +- v2realbot/strategy/StrategyClassicSL.py | 4 ++-- v2realbot/strategyblocks/activetrade/helpers.py | 8 ++++---- v2realbot/strategyblocks/activetrade/sl/optimsl.py | 10 +++++----- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/v2realbot/backtesting/backtester.py b/v2realbot/backtesting/backtester.py index 47baba1..1d8320c 100644 --- a/v2realbot/backtesting/backtester.py +++ b/v2realbot/backtesting/backtester.py @@ -105,8 +105,7 @@ class Backtester: self.cash = cash self.cash_reserved_for_shorting = 0 self.trades = [] - def_acc_value = {self.symbol: [0, 0]} - self.internal_account = { account.name:def_acc_value for account in accounts } + self.internal_account = { account.name:{self.symbol: [0, 0]} for account in accounts } # { "ACCOUNT1": {}"BAC": [avgp, size]}, .... } self.open_orders =[] #open orders shared for all accounts, account being an attribute @@ -377,12 +376,12 @@ class Backtester: event = TradeEvent.FILL, execution_id = str(uuid4()), timestamp = datetime.fromtimestamp(fill_time), - position_qty= self.internal_account[o.symbol][0], + position_qty= self.internal_account[o.account.name][o.symbol][0], price=float(fill_price), qty = o.qty, value = float(o.qty*fill_price), cash = self.cash, - pos_avg_price = self.internal_account[o.symbol][1]) + pos_avg_price = self.internal_account[o.account.name][o.symbol][1]) self.trades.append(trade) @@ -418,7 +417,7 @@ class Backtester: elif newsize < 0: newavgp = self.internal_account[o.account.name][o.symbol][1] #JDE O LONG (avgp nove) else: - newavgp = ((self.internal_account[o.account.name][o.symbol][0] * self.internal_account[o.account.name][o.symbol][1]) + (o.qty * o.filled_avg_price)) / (self.internal_account[account.name][o.symbol][0] + o.qty) + newavgp = ((self.internal_account[o.account.name][o.symbol][0] * self.internal_account[o.account.name][o.symbol][1]) + (o.qty * o.filled_avg_price)) / (self.internal_account[o.account.name][o.symbol][0] + o.qty) self.internal_account[o.account.name][o.symbol] = [newsize, newavgp] self.cash = self.cash - (o.qty * o.filled_avg_price) diff --git a/v2realbot/controller/services.py b/v2realbot/controller/services.py index f5e17d5..25b1fe6 100644 --- a/v2realbot/controller/services.py +++ b/v2realbot/controller/services.py @@ -1267,7 +1267,7 @@ def insert_archive_header(archeader: RunArchive): try: c = conn.cursor() #json_string = orjson.dumps(archeader, default=json_serial, option=orjson.OPT_PASSTHROUGH_DATETIME) - print(archeader) + #print(archeader) res = c.execute(""" INSERT INTO runner_header diff --git a/v2realbot/strategy/StrategyClassicSL.py b/v2realbot/strategy/StrategyClassicSL.py index 433271f..477e658 100644 --- a/v2realbot/strategy/StrategyClassicSL.py +++ b/v2realbot/strategy/StrategyClassicSL.py @@ -266,7 +266,7 @@ class StrategyClassicSL(Strategy): print(data) #dostavame zde i celkové akutální množství - ukládáme self.state.account_variables[account.name].positions = data.position_qty - self.state.account_variables[account.name].avgp, self.state.account_variables[account.name].positions = self.state.interface.pos() + self.state.account_variables[account.name].avgp, self.state.account_variables[account.name].positions = self.state.interface[account.name].pos() if o.status == OrderStatus.FILLED or o.status == OrderStatus.CANCELED: #davame pryc pending @@ -431,7 +431,7 @@ class StrategyClassicSL(Strategy): if data.event == TradeEvent.FILL or data.event == TradeEvent.CANCELED: print("Příchozí SELL notifikace - complete FILL nebo CANCEL", data.event) self.state.account_variables[account.name].pending = None - a,p = self.interface.pos() + a,p = self.interface[account.name].pos() #pri chybe api nechavame puvodni hodnoty if a != -1: self.state.account_variables[account.name].avgp, self.state.account_variables[account.name].positions = a,p diff --git a/v2realbot/strategyblocks/activetrade/helpers.py b/v2realbot/strategyblocks/activetrade/helpers.py index fefc6d6..fa994c5 100644 --- a/v2realbot/strategyblocks/activetrade/helpers.py +++ b/v2realbot/strategyblocks/activetrade/helpers.py @@ -154,7 +154,7 @@ def get_profit_target_price(state, data, activeTrade, direction: TradeDirection) smer = "short" directive_name = "profit" - def_profit_both_directions = get_signal_section_directive(state, activeTrade=activeTrade, directive_name=directive_name, default_value=safe_get(state.vars, directive_name, 0.50)) + def_profit_both_directions = get_signal_section_directive(state, signal_name=activeTrade.generated_by, directive_name=directive_name, default_value=safe_get(state.vars, directive_name, 0.50)) #profit pro dany smer directive_name = 'profit_'+str(smer) @@ -162,7 +162,7 @@ def get_profit_target_price(state, data, activeTrade, direction: TradeDirection) #mame v direktivve ticky if isinstance(def_profit, (float, int)): - to_return = get_normalized_profitprice_from_tick(state, data, activeTrade.account, def_profit, direction) + to_return = get_normalized_profitprice_from_tick(state, data, def_profit, activeTrade.account, direction) #mame v direktive indikator elif isinstance(def_profit, str): to_return = float(value_or_indicator(state, def_profit)) @@ -170,7 +170,7 @@ def get_profit_target_price(state, data, activeTrade, direction: TradeDirection) #min profit (ochrana extremnich hodnot indikatoru) directive_name = 'profit_min_ind_tick_value' profit_min_ind_tick_value = get_signal_section_directive(state, signal_name=activeTrade.generated_by, directive_name=directive_name, default_value=def_profit_both_directions) - profit_min_ind_price_value = get_normalized_profitprice_from_tick(state, data, activeTrade.account, profit_min_ind_tick_value, direction) + profit_min_ind_price_value = get_normalized_profitprice_from_tick(state, data, profit_min_ind_tick_value, activeTrade.account, direction) #ochrana pri nastaveni profitu prilis nizko if direction == TradeDirection.LONG and to_return < profit_min_ind_price_value or direction == TradeDirection.SHORT and to_return > profit_min_ind_price_value: @@ -181,7 +181,7 @@ def get_profit_target_price(state, data, activeTrade, direction: TradeDirection) return to_return ##based on tick a direction, returns normalized prfoit price (LONG = avgp(nebo currprice)+norm.tick, SHORT=avgp(or currprice)-norm.tick) -def get_normalized_profitprice_from_tick(state, data, tick, account, direction: TradeDirection): +def get_normalized_profitprice_from_tick(state, data, tick, account: Account, direction: TradeDirection): avgp = state.account_variables[account.name].avgp normalized_tick = normalize_tick(state, data, float(tick)) base_price = avgp if avgp != 0 else data["close"] diff --git a/v2realbot/strategyblocks/activetrade/sl/optimsl.py b/v2realbot/strategyblocks/activetrade/sl/optimsl.py index f386fea..7e7919f 100644 --- a/v2realbot/strategyblocks/activetrade/sl/optimsl.py +++ b/v2realbot/strategyblocks/activetrade/sl/optimsl.py @@ -54,18 +54,18 @@ class SLOptimizer: #jde o novy trade - resetujeme levely if trade.id != self.last_trade: #inicializujeme a vymazeme pripadne puvodni - if self.initialize_levels(state) is False: + if self.initialize_levels(state, activeTrade) is False: return None, None self.last_trade = trade.id #return cost_price, sl_price return state.account_variables[trade.account.name].avgp, trade.stoploss_value - def initialize_levels(self, state): + def initialize_levels(self, state, activeTrade): directive_name = 'SL_opt_exit_levels_'+str(self.direction.value) - SL_opt_exit_levels = get_signal_section_directive(state=state, directive_name=directive_name, default_value=safe_get(state.vars, directive_name, None)) + SL_opt_exit_levels = get_signal_section_directive(state=state, signal_name=activeTrade.generated_by, directive_name=directive_name, default_value=safe_get(state.vars, directive_name, None)) directive_name = 'SL_opt_exit_sizes_'+str(self.direction.value) - SL_opt_exit_sizes = get_signal_section_directive(state=state, directive_name=directive_name, default_value=safe_get(state.vars, directive_name, None)) + SL_opt_exit_sizes = get_signal_section_directive(state=state, signal_name=activeTrade.generated_by, directive_name=directive_name, default_value=safe_get(state.vars, directive_name, None)) if SL_opt_exit_levels is None or SL_opt_exit_sizes is None: #print("no directives found: SL_opt_exit_levels/SL_opt_exit_sizes") @@ -113,7 +113,7 @@ class SLOptimizer: """Evaluates optimalization for current position and returns if the given level was met and how to adjust exit position. """ - cost_price, sl_price = self.get_trade_details(state) + cost_price, sl_price = self.get_trade_details(state, activeTrade) if cost_price is None or sl_price is None: #print("no settings found") return (None, None)