This commit is contained in:
David Brazda
2023-04-21 16:59:04 +02:00
parent a6bf37d7c8
commit 88256b9233
11 changed files with 59 additions and 33 deletions

View File

@ -222,10 +222,13 @@ def next(data, state: StrategyState):
if state.vars.jevylozeno == 1:
##CONSOLIDATION PART kazdy Nty bar dle nastaveni
if int(data["index"])%int(state.vars.consolidation_bar_count) == 0:
print("***Consolidation ENTRY***")
state.ilog(e="***Konzolidujeme")
print("***CONSOLIDATION ENTRY***")
state.ilog(e="CONSOLIDATION ENTRY ***")
orderlist = state.interface.get_open_orders(symbol=state.symbol, side=None)
#pro jistotu jeste dotahneme aktualni pozice
state.avgp, state.positions = state.interface.pos()
#print(orderlist)
pendingbuys_new = {}
limitka_old = state.vars.limitka
@ -254,18 +257,28 @@ def next(data, state: StrategyState):
price=price2dec(float(state.avgp)+state.vars.profit)
state.vars.limitka = asyncio.run(state.interface.sell_l(price=price, size=state.positions))
state.vars.limitka_price = price
state.ilog(e="Vytvořena nová limitka", limitka=str(state.vars.limitka), limtka_price=state.vars.limitka_price)
if state.vars.limitka == -1:
state.ilog(e="Vytvoreni limitky neprobehlo, vracime None", msg=str(state.vars.limitka))
state.vars.limitka = None
state.vars.limitka_price = None
else:
state.ilog(e="Vytvořena nová limitka", limitka=str(state.vars.limitka), limtka_price=state.vars.limitka_price)
if int(state.positions) > 0 and (int(state.positions) != int(limitka_qty)):
elif state.vars.limitka is not None and int(state.positions) > 0 and (int(state.positions) != int(limitka_qty)):
#limitka existuje, ale spatne mnostvi - updatujeme
state.ilog(e="Limitka existuje, ale spatne mnozstvi - updatujeme", msg="POS"+str(state.positions)+" lim_qty:"+str(limitka_qty), pos=state.positions, limitka_qty=limitka_qty)
#snad to nespadne, kdyztak pridat exception handling
puvodni = state.vars.limitka
state.vars.limitka = asyncio.run(state.interface.repl(price=state.vars.limitka_price, orderid=state.vars.limitka, size=int(state.positions)))
limitka_qty = int(state.positions)
state.ilog(e="Změněna limitka", limitka=str(state.vars.limitka), limitka_price=state.vars.limitka_price, limitka_qty=limitka_qty)
#dodelat limitka existuje, ale spatne
if state.vars.limitka == -1:
state.ilog(e="Replace limitky neprobehl, vracime puvodni", msg=str(state.vars.limitka), puvodni=puvodni)
state.vars.limitka = puvodni
else:
limitka_qty = int(state.positions)
state.ilog(e="Změněna limitka", limitka=str(state.vars.limitka), limitka_price=state.vars.limitka_price, limitka_qty=limitka_qty)
#tbd pokud se bude vyskytovat pak pridat ještě konzolidaci ceny limitky
if pendingbuys_new != state.vars.pendingbuys:
state.ilog(e="Rozdilna PB prepsana", pb_new=pendingbuys_new, pb_old = state.vars.pendingbuys)
@ -291,15 +304,14 @@ def next(data, state: StrategyState):
#print(len(pendingbuys_new))
#print(jevylozeno)
print("***CONSOLIDATION EXIT***")
state.ilog(e="***Konzolidace konec")
state.ilog(e="CONSOLIDATION EXIT ***")
else:
state.ilog(e="No time for consolidation", msg=data["index"])
print("no time for consolidation", data["index"])
#HLAVNI ITERACNI LOG JESTE PRED AKCI - obsahuje aktualni hodnoty vetsiny parametru
lp = state.interface.get_last_price(symbol=state.symbol)
state.ilog(e="ENTRY", msg="AVGP:"+str(state.avgp)+ "POS:" +str(state.positions), last_price=lp, stratvars=state.vars)
state.ilog(e="ENTRY", msg="AVGP:"+str(round(state.avgp,2))+ "POS:" +str(state.positions)+" PROFIT:"+str(round(state.profit,2)), last_price=lp, stratvars=state.vars)
#maxSlopeMA = -0.03
#SLOPE ANGLE PROTECTIONs

View File

@ -2,8 +2,11 @@ from alpaca.data.enums import DataFeed
from v2realbot.enums.enums import Mode, Account
from appdirs import user_data_dir
#no print in console
QUIET_MODE = False
#backend counter of api requests
COUNT_API_REQUESTS = False
#stratvars that cannot be changed in gui
STRATVARS_UNCHANGEABLES = ['pendingbuys', 'blockbuy', 'jevylozeno', 'limitka']
DATA_DIR = user_data_dir("v2realbot")
#BT DELAYS

View File

@ -41,7 +41,7 @@ function connect(event) {
}
if (parsed_data.hasOwnProperty("bars")) {
console.log("mame bary")
// console.log("mame bary")
var bar = parsed_data.bars
candlestickSeries.update(bar);
volumeSeries.update({
@ -173,24 +173,24 @@ function connect(event) {
}
if (parsed_data.hasOwnProperty("statinds")) {
console.log("got static indicators")
// console.log("got static indicators")
var statinds = parsed_data.statinds
if (Object.keys(statinds).length > 0) {
console.log("got static indicators")
console.log(JSON.stringify(statinds))
// console.log("got static indicators")
// console.log(JSON.stringify(statinds))
for (const [klic, hodnota] of Object.entries(statinds)) {
console.log(JSON.stringify(klic))
console.log(JSON.stringify(hodnota))
// console.log(JSON.stringify(klic))
// console.log(JSON.stringify(hodnota))
//TODO predelat na configuracni klice vizualizacni dotahovane z backendu, ktere namapuji vybrane stratvars na typ vizualizace
if (klic === "angle") {
//nejsou vsechny hodnoty
if (Object.keys(hodnota).length > 0) {
console.log("angle nalezen");
console.log(JSON.stringify(hodnota));
// console.log("angle nalezen");
// console.log(JSON.stringify(hodnota));
if (angleSeries !== 1) {
console.log("angle neni jedna" + toString(angleSeries))
// console.log("angle neni jedna" + toString(angleSeries))
chart.removeSeries(angleSeries)
}
@ -200,11 +200,13 @@ function connect(event) {
lineStyle: 2,
color: "#d432e6",
lastValueVisible: false,
priceLineVisible: false
priceLineVisible: false,
priceLineWidth: 0,
priceLineStyle: 3
})
dataPoints = [{time: hodnota.lookbacktime, value: hodnota.lookbackprice},{ time: hodnota.time, value: hodnota.price}]
console.log("pridano")
console.log(toString(dataPoints))
// console.log("pridano")
// console.log(toString(dataPoints))
angleSeries.setData(dataPoints)
}
}
@ -222,7 +224,7 @@ function connect(event) {
}
if (parsed_data.hasOwnProperty("indicators")) {
console.log("jsme uvnitr indikatoru")
// console.log("jsme uvnitr indikatoru")
var indicators = parsed_data.indicators
//if there are indicators it means there must be at least two keys (except time which is always present)
if (Object.keys(indicators).length > 1) {

View File

@ -34,7 +34,7 @@
}
#msgContainer {
display: inline-block;
/* display: inline-block; */
overflow: auto;
height: 568px;
}

View File

@ -26,7 +26,7 @@ class StrategyOrderLimitVykladaci(Strategy):
self.state.ilog(e="Příchozí BUY notifikace - mazeme ji z pb", msg="order status:"+o.status, orderid=str(o.id), pb=self.state.vars.pendingbuys)
print("limit buy filled or cancelled. Vyhazujeme z pendingbuys.")
ic(self.state.vars.pendingbuys)
if data.event == TradeEvent.FILL or data.event == TradeEvent.PARTIAL_FILL:
ic("vstupujeme do orderupdatebuy")
print(data)
@ -67,6 +67,18 @@ class StrategyOrderLimitVykladaci(Strategy):
raise Exception(e)
async def orderUpdateSell(self, data: TradeUpdate):
#PROFIT
#profit pocitame z TradeUpdate.price a TradeUpdate.qty - aktualne provedene mnozstvi a cena
#naklady vypocteme z prumerne ceny, kterou mame v pozicich
if data.event == TradeEvent.FILL or data.event == TradeEvent.PARTIAL_FILL:
sold_amount = data.qty * data.price
#podle prumerne ceny, kolik stalo toto mnozstvi
avg_costs = self.state.avgp * data.qty
trade_profit = (sold_amount - avg_costs)
self.state.profit += trade_profit
self.state.ilog(e="SELL not - PROFIT: "+str(round(trade_profit,3))+" celkem: "+str(round(self.state.profit,3)), msg=str(data.event), sold_amount=sold_amount, avg_costs=avg_costs, trade_qty=data.qty, trade_price=data.price, orderid=str(data.order.id))
if data.event == TradeEvent.PARTIAL_FILL:
self.state.ilog(e="SELL notifikace - Partial fill", msg="pouze update pozic", orderid=str(data.order.id))
ic("partial fill jen udpatujeme pozice")
@ -77,10 +89,6 @@ class StrategyOrderLimitVykladaci(Strategy):
#muzeme znovu nakupovat, mazeme limitku, blockbuy a pendingbuys
#self.state.blockbuy = 0
#ADDPROFIT - datd o funkce, zatim vraci chybu
#prodej = data.order.filled_qty * data.order.filled_avg_price
#nakup = self.state.positions
ic("notifikace sell mazeme limitku a update pozic")
#updatujeme pozice
self.state.avgp, self.state.positions = self.interface.pos()

View File

@ -5,7 +5,7 @@ from datetime import datetime
from v2realbot.utils.utils import AttributeDict, zoneNY, is_open_rush, is_close_rush, json_serial, print
from v2realbot.utils.tlog import tlog
from v2realbot.enums.enums import RecordType, StartBarAlign, Mode, Order, Account
from v2realbot.config import BT_DELAYS, get_key, HEARTBEAT_TIMEOUT
from v2realbot.config import BT_DELAYS, get_key, HEARTBEAT_TIMEOUT, QUIET_MODE
import queue
#from rich import print
from v2realbot.loader.aggregator import TradeAggregator2Queue, TradeAggregator2List, TradeAggregator
@ -48,7 +48,6 @@ class Strategy:
self.account = account
self.key = get_key(mode=self.mode, account=self.account)
self.rtqueue = None
self.profit = 0
#TODO predelat na dynamické queues
@ -495,10 +494,12 @@ class StrategyState:
self.sell_l = self.interface.sell_l
self.cancel_pending_buys = None
self.iter_log_list = []
self.profit = 0
def ilog(self, e: str = None, msg: str = None, **kwargs):
if e is None:
row = dict(time=self.time, message=msg, details=kwargs)
else:
row = dict(time=self.time, event=e, message=msg, details=kwargs)
self.iter_log_list.append(row)
self.iter_log_list.append(row)
print(row)