druhy commit

This commit is contained in:
David Brazda
2024-08-24 06:57:53 +02:00
parent 30048364bb
commit 30044dc4ea
5 changed files with 16 additions and 17 deletions

View File

@ -105,8 +105,7 @@ class Backtester:
self.cash = cash self.cash = cash
self.cash_reserved_for_shorting = 0 self.cash_reserved_for_shorting = 0
self.trades = [] self.trades = []
def_acc_value = {self.symbol: [0, 0]} self.internal_account = { account.name:{self.symbol: [0, 0]} for account in accounts }
self.internal_account = { account.name:def_acc_value for account in accounts }
# { "ACCOUNT1": {}"BAC": [avgp, size]}, .... } # { "ACCOUNT1": {}"BAC": [avgp, size]}, .... }
self.open_orders =[] #open orders shared for all accounts, account being an attribute self.open_orders =[] #open orders shared for all accounts, account being an attribute
@ -377,12 +376,12 @@ class Backtester:
event = TradeEvent.FILL, event = TradeEvent.FILL,
execution_id = str(uuid4()), execution_id = str(uuid4()),
timestamp = datetime.fromtimestamp(fill_time), 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), price=float(fill_price),
qty = o.qty, qty = o.qty,
value = float(o.qty*fill_price), value = float(o.qty*fill_price),
cash = self.cash, 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) self.trades.append(trade)
@ -418,7 +417,7 @@ class Backtester:
elif newsize < 0: newavgp = self.internal_account[o.account.name][o.symbol][1] elif newsize < 0: newavgp = self.internal_account[o.account.name][o.symbol][1]
#JDE O LONG (avgp nove) #JDE O LONG (avgp nove)
else: 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.internal_account[o.account.name][o.symbol] = [newsize, newavgp]
self.cash = self.cash - (o.qty * o.filled_avg_price) self.cash = self.cash - (o.qty * o.filled_avg_price)

View File

@ -1267,7 +1267,7 @@ def insert_archive_header(archeader: RunArchive):
try: try:
c = conn.cursor() c = conn.cursor()
#json_string = orjson.dumps(archeader, default=json_serial, option=orjson.OPT_PASSTHROUGH_DATETIME) #json_string = orjson.dumps(archeader, default=json_serial, option=orjson.OPT_PASSTHROUGH_DATETIME)
print(archeader) #print(archeader)
res = c.execute(""" res = c.execute("""
INSERT INTO runner_header INSERT INTO runner_header

View File

@ -266,7 +266,7 @@ class StrategyClassicSL(Strategy):
print(data) print(data)
#dostavame zde i celkové akutální množství - ukládáme #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].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: if o.status == OrderStatus.FILLED or o.status == OrderStatus.CANCELED:
#davame pryc pending #davame pryc pending
@ -431,7 +431,7 @@ class StrategyClassicSL(Strategy):
if data.event == TradeEvent.FILL or data.event == TradeEvent.CANCELED: if data.event == TradeEvent.FILL or data.event == TradeEvent.CANCELED:
print("Příchozí SELL notifikace - complete FILL nebo CANCEL", data.event) print("Příchozí SELL notifikace - complete FILL nebo CANCEL", data.event)
self.state.account_variables[account.name].pending = None 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 #pri chybe api nechavame puvodni hodnoty
if a != -1: if a != -1:
self.state.account_variables[account.name].avgp, self.state.account_variables[account.name].positions = a,p self.state.account_variables[account.name].avgp, self.state.account_variables[account.name].positions = a,p

View File

@ -154,7 +154,7 @@ def get_profit_target_price(state, data, activeTrade, direction: TradeDirection)
smer = "short" smer = "short"
directive_name = "profit" 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 #profit pro dany smer
directive_name = 'profit_'+str(smer) directive_name = 'profit_'+str(smer)
@ -162,7 +162,7 @@ def get_profit_target_price(state, data, activeTrade, direction: TradeDirection)
#mame v direktivve ticky #mame v direktivve ticky
if isinstance(def_profit, (float, int)): 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 #mame v direktive indikator
elif isinstance(def_profit, str): elif isinstance(def_profit, str):
to_return = float(value_or_indicator(state, def_profit)) 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) #min profit (ochrana extremnich hodnot indikatoru)
directive_name = 'profit_min_ind_tick_value' 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_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 #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: 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 return to_return
##based on tick a direction, returns normalized prfoit price (LONG = avgp(nebo currprice)+norm.tick, SHORT=avgp(or currprice)-norm.tick) ##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 avgp = state.account_variables[account.name].avgp
normalized_tick = normalize_tick(state, data, float(tick)) normalized_tick = normalize_tick(state, data, float(tick))
base_price = avgp if avgp != 0 else data["close"] base_price = avgp if avgp != 0 else data["close"]

View File

@ -54,18 +54,18 @@ class SLOptimizer:
#jde o novy trade - resetujeme levely #jde o novy trade - resetujeme levely
if trade.id != self.last_trade: if trade.id != self.last_trade:
#inicializujeme a vymazeme pripadne puvodni #inicializujeme a vymazeme pripadne puvodni
if self.initialize_levels(state) is False: if self.initialize_levels(state, activeTrade) is False:
return None, None return None, None
self.last_trade = trade.id self.last_trade = trade.id
#return cost_price, sl_price #return cost_price, sl_price
return state.account_variables[trade.account.name].avgp, trade.stoploss_value 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) 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) 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: 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") #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 """Evaluates optimalization for current position and returns if the given level was
met and how to adjust exit position. 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: if cost_price is None or sl_price is None:
#print("no settings found") #print("no settings found")
return (None, None) return (None, None)