Skip to content

Commit

Permalink
fix traderui
Browse files Browse the repository at this point in the history
  • Loading branch information
51bitquant committed Jun 10, 2023
1 parent 397cb8f commit 8032e66
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 131 deletions.
41 changes: 22 additions & 19 deletions howtrader/trader/dbconnectors/mongo_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def save_bar_data(self, bars: List[BarData]) -> bool:

for bar in bars:
# insert into database.
filter: dict = {
filter_: dict = {
"symbol": bar.symbol,
"exchange": bar.exchange.value,
"datetime": bar.datetime,
Expand All @@ -108,18 +108,21 @@ def save_bar_data(self, bars: List[BarData]) -> bool:
"close_price": bar.close_price,
}

requests.append(ReplaceOne(filter, d, upsert=True))
requests.append(ReplaceOne(filter_, d, upsert=True))

self.bar_collection.bulk_write(requests, ordered=False)

# update overview
filter: dict = {
if len(bars) == 0:
return False

filter_: dict = {
"symbol": bar.symbol,
"exchange": bar.exchange.value,
"interval": bar.interval.value
}

overview: dict = self.overview_collection.find_one(filter)
overview: dict = self.overview_collection.find_one(filter_)

if not overview:
overview = {
Expand All @@ -133,18 +136,18 @@ def save_bar_data(self, bars: List[BarData]) -> bool:
else:
overview["start"] = min(bars[0].datetime, overview["start"])
overview["end"] = max(bars[-1].datetime, overview["end"])
overview["count"] = self.bar_collection.count_documents(filter)
overview["count"] = self.bar_collection.count_documents(filter_)

self.overview_collection.update_one(filter, {"$set": overview}, upsert=True)
self.overview_collection.update_one(filter_, {"$set": overview}, upsert=True)

return True

def save_tick_data(self, ticks: List[TickData]) -> bool:
"""Save tikc data"""
"""Save tick data"""
requests: List[ReplaceOne] = []

for tick in ticks:
filter: dict = {
filter_: dict = {
"symbol": tick.symbol,
"exchange": tick.exchange.value,
"datetime": tick.datetime,
Expand Down Expand Up @@ -189,7 +192,7 @@ def save_tick_data(self, ticks: List[TickData]) -> bool:
"localtime": tick.localtime,
}

requests.append(ReplaceOne(filter, d, upsert=True))
requests.append(ReplaceOne(filter_, d, upsert=True))

self.tick_collection.bulk_write(requests, ordered=False)

Expand All @@ -204,7 +207,7 @@ def load_bar_data(
end: datetime
) -> List[BarData]:
"""Load Kline/Bar data"""
filter: dict = {
filter_: dict = {
"symbol": symbol,
"exchange": exchange.value,
"interval": interval.value,
Expand All @@ -214,7 +217,7 @@ def load_bar_data(
}
}

c: Cursor = self.bar_collection.find(filter)
c: Cursor = self.bar_collection.find(filter_)

bars: List[BarData] = []
for d in c:
Expand All @@ -236,7 +239,7 @@ def load_tick_data(
end: datetime
) -> List[TickData]:
"""load tick data"""
filter: dict = {
filter_: dict = {
"symbol": symbol,
"exchange": exchange.value,
"datetime": {
Expand All @@ -245,7 +248,7 @@ def load_tick_data(
}
}

c: Cursor = self.tick_collection.find(filter)
c: Cursor = self.tick_collection.find(filter_)

ticks: List[TickData] = []
for d in c:
Expand All @@ -265,14 +268,14 @@ def delete_bar_data(
interval: Interval
) -> int:
"""delete bar data"""
filter: dict = {
filter_: dict = {
"symbol": symbol,
"exchange": exchange.value,
"interval": interval.value,
}

result: DeleteResult = self.bar_collection.delete_many(filter)
self.overview_collection.delete_one(filter)
result: DeleteResult = self.bar_collection.delete_many(filter_)
self.overview_collection.delete_one(filter_)

return result.deleted_count

Expand All @@ -282,12 +285,12 @@ def delete_tick_data(
exchange: Exchange
) -> int:
"""Delete tick data"""
filter: dict = {
filter_: dict = {
"symbol": symbol,
"exchange": exchange.value
}

result: DeleteResult = self.tick_collection.delete_many(filter)
result: DeleteResult = self.tick_collection.delete_many(filter_)
return result.deleted_count

def get_bar_overview(self) -> List[BarOverview]:
Expand All @@ -303,4 +306,4 @@ def get_bar_overview(self) -> List[BarOverview]:
overview: BarOverview = BarOverview(**d)
overviews.append(overview)

return overviews
return overviews
4 changes: 2 additions & 2 deletions howtrader/trader/dbconnectors/mysql_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DbBarData(Model):

symbol: str = CharField()
exchange: str = CharField()
datetime: datetime = DateTimeField()
datetime: DateTimeField = DateTimeField()
interval: str = CharField()

volume: float = FloatField()
Expand Down Expand Up @@ -419,4 +419,4 @@ def init_bar_overview(self) -> None:
)
overview.end = end_bar.datetime

overview.save()
overview.save()
4 changes: 2 additions & 2 deletions howtrader/trader/dbconnectors/sqlite_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DbBarData(Model):

symbol: str = CharField()
exchange: str = CharField()
datetime: datetime = DateTimeField()
datetime: DateTimeField = DateTimeField()
interval: str = CharField()

volume: float = FloatField()
Expand Down Expand Up @@ -409,4 +409,4 @@ def init_bar_overview(self) -> None:
)
overview.end = end_bar.datetime

overview.save()
overview.save()
91 changes: 45 additions & 46 deletions howtrader/trader/ui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
from ..engine import MainEngine, BaseApp
from ..utility import get_icon_path, TRADER_DIR, round_to, floor_to, extract_vt_symbol
from ..setting import QUICK_TRADER_SETTINGS
from howtrader.trader.object import ContractData, OrderRequest, Direction, OrderType, Offset, TickData, PositionData, Product
from howtrader.trader.object import ContractData, OrderRequest, Direction, OrderType, Offset, TickData, PositionData, \
Product


class MainWindow(QtWidgets.QMainWindow):
Expand Down Expand Up @@ -61,32 +62,32 @@ def init_ui(self) -> None:
def init_dock(self) -> None:
""""""
self.trading_widget, trading_dock = self.create_dock(
TradingWidget, "Trading", QtCore.Qt.LeftDockWidgetArea
TradingWidget, "Trading", QtCore.Qt.DockWidgetArea.LeftDockWidgetArea
)
tick_widget, tick_dock = self.create_dock(
TickMonitor, "Ticks", QtCore.Qt.RightDockWidgetArea
TickMonitor, "Ticks", QtCore.Qt.DockWidgetArea.RightDockWidgetArea
)
order_widget, order_dock = self.create_dock(
OrderMonitor, "All Orders", QtCore.Qt.RightDockWidgetArea
OrderMonitor, "All Orders", QtCore.Qt.DockWidgetArea.RightDockWidgetArea
)
active_widget, active_dock = self.create_dock(
ActiveOrderMonitor, "Active Orders", QtCore.Qt.RightDockWidgetArea
ActiveOrderMonitor, "Active Orders", QtCore.Qt.DockWidgetArea.RightDockWidgetArea
)
trade_widget, trade_dock = self.create_dock(
TradeMonitor, "Trades", QtCore.Qt.RightDockWidgetArea
TradeMonitor, "Trades", QtCore.Qt.DockWidgetArea.RightDockWidgetArea
)
log_widget, log_dock = self.create_dock(
LogMonitor, "Logs", QtCore.Qt.RightDockWidgetArea
LogMonitor, "Logs", QtCore.Qt.DockWidgetArea.RightDockWidgetArea
)
account_widget, account_dock = self.create_dock(
AccountMonitor, "Accounts", QtCore.Qt.RightDockWidgetArea
AccountMonitor, "Accounts", QtCore.Qt.DockWidgetArea.RightDockWidgetArea
)
position_widget, position_dock = self.create_dock(
PositionMonitor, "Positions", QtCore.Qt.RightDockWidgetArea
PositionMonitor, "Positions", QtCore.Qt.DockWidgetArea.RightDockWidgetArea
)

self.tabifyDockWidget(order_dock, active_dock)
self.tabifyDockWidget(active_dock,trade_dock)
self.tabifyDockWidget(active_dock, trade_dock)

self.tabifyDockWidget(account_dock, position_dock)
self.tabifyDockWidget(position_dock, log_dock)
Expand All @@ -97,9 +98,8 @@ def init_dock(self) -> None:
position_widget.itemDoubleClicked.connect(self.trading_widget.update_with_cell)

def init_menu(self) -> None:
""""""
bar: QtWidgets.QMenuBar = self.menuBar()
bar.setNativeMenuBar(False) # for mac and linux
bar.setNativeMenuBar(False) # for mac and linux

# System menu
sys_menu: QtWidgets.QMenu = bar.addMenu("exchanges")
Expand Down Expand Up @@ -136,7 +136,7 @@ def init_menu(self) -> None:
self.add_action(app_menu, app.display_name, app.icon_name, func, True)

# Global setting editor
action: QtGui.QAction = QtWidgets.QAction("configs", self)
action: QtGui.QAction = QtGui.QAction("configs", self)
action.triggered.connect(self.edit_global_setting)
bar.addAction(action)

Expand Down Expand Up @@ -195,20 +195,20 @@ def init_toolbar(self) -> None:
# Set button spacing
self.toolbar.layout().setSpacing(10)

self.addToolBar(QtCore.Qt.LeftToolBarArea, self.toolbar)
self.addToolBar(QtCore.Qt.ToolBarArea.LeftToolBarArea, self.toolbar)

def add_action(
self,
menu: QtWidgets.QMenu,
action_name: str,
icon_name: str,
func: Callable,
toolbar: bool = False
self,
menu: QtWidgets.QMenu,
action_name: str,
icon_name: str,
func: Callable,
toolbar: bool = False
) -> None:
""""""
icon: QtGui.QIcon = QtGui.QIcon(icon_name)

action: QtGui.QAction = QtWidgets.QAction(action_name, self)
action: QtGui.QAction = QtGui.QAction(action_name, self)
action.triggered.connect(func)
action.setIcon(icon)

Expand All @@ -218,10 +218,10 @@ def add_action(
self.toolbar.addAction(action)

def create_dock(
self,
widget_class: QtWidgets.QWidget,
name: str,
area: int
self,
widget_class: QtWidgets.QWidget.__class__,
name: str,
area: QtCore.Qt.DockWidgetArea
) -> Tuple[QtWidgets.QWidget, QtWidgets.QDockWidget]:
"""
Initialize a dock widget.
Expand All @@ -231,7 +231,7 @@ def create_dock(
dock: QtWidgets.QDockWidget = QtWidgets.QDockWidget(name)
dock.setWidget(widget)
dock.setObjectName(name)
dock.setFeatures(dock.DockWidgetFloatable | dock.DockWidgetMovable)
dock.setFeatures(dock.DockWidgetFeature.DockWidgetFloatable | dock.DockWidgetFeature.DockWidgetMovable)
self.addDockWidget(area, dock)
return widget, dock

Expand All @@ -250,11 +250,11 @@ def closeEvent(self, event: QtGui.QCloseEvent) -> None:
self,
"exit",
"confirm exit?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No,
QtWidgets.QMessageBox.StandardButton.No,
)

if reply == QtWidgets.QMessageBox.Yes:
if reply == QtWidgets.QMessageBox.StandardButton.Yes:
for widget in self.widgets.values():
widget.close()
# self.save_window_setting("custom")
Expand All @@ -265,7 +265,7 @@ def closeEvent(self, event: QtGui.QCloseEvent) -> None:
else:
event.ignore()

def open_widget(self, widget_class: QtWidgets.QWidget, name: str) -> None:
def open_widget(self, widget_class: QtWidgets.QWidget.__class__, name: str) -> None:
"""
Open contract manager.
"""
Expand Down Expand Up @@ -331,7 +331,7 @@ def keyReleaseEvent(self, key_event: QtGui.QKeyEvent):
else:
return None

if key_event.key() == QtCore.Qt.Key_Z:
if key_event.key() == int(QtCore.Qt.Key.Key_Z):
# Z key.
order_list = self.main_engine.get_all_active_orders()
for order in order_list:
Expand All @@ -341,17 +341,16 @@ def keyReleaseEvent(self, key_event: QtGui.QKeyEvent):
self.main_engine.write_log("Press Z key: cancel all orders")
return None

keys = {QtCore.Qt.Key_0: "0",
QtCore.Qt.Key_1: "1",
QtCore.Qt.Key_2: "2",
QtCore.Qt.Key_3: "3",
QtCore.Qt.Key_4: "4",
QtCore.Qt.Key_5: "5",
QtCore.Qt.Key_6: "6",
QtCore.Qt.Key_7: "7",
QtCore.Qt.Key_8: "8",
QtCore.Qt.Key_9: "9"
}
keys = {int(QtCore.Qt.Key.Key_0): "0",
int(QtCore.Qt.Key.Key_1): "1",
int(QtCore.Qt.Key.Key_2): "2",
int(QtCore.Qt.Key.Key_3): "3",
int(QtCore.Qt.Key.Key_4): "4",
int(QtCore.Qt.Key.Key_5): "5",
int(QtCore.Qt.Key.Key_6): "6",
int(QtCore.Qt.Key.Key_7): "7",
int(QtCore.Qt.Key.Key_8): "8",
int(QtCore.Qt.Key.Key_9): "9"}

key = keys.get(key_event.key(), None)
if not key:
Expand Down Expand Up @@ -395,15 +394,15 @@ def keyReleaseEvent(self, key_event: QtGui.QKeyEvent):

else: # percent
if add_minus == '+':
order_price = tick_price * (1 + float(over_price_value)/100)
order_price = tick_price * (1 + float(over_price_value) / 100)
else:
order_price = tick_price * (1 - float(over_price_value)/100)
order_price = tick_price * (1 - float(over_price_value) / 100)

order_price = round_to(Decimal(str(order_price)), contract.pricetick)

if volume_option == "fixed_volume":
volume = Decimal(volume)
else: # position percent
else: # position percent
if contract.product == Product.SPOT:
self.main_engine.write_log(f"Position is not available for Spot market.")
return None
Expand Down Expand Up @@ -435,4 +434,4 @@ def keyReleaseEvent(self, key_event: QtGui.QKeyEvent):
reference="QuickTrader"
)

self.main_engine.send_order(req, contract.gateway_name)
self.main_engine.send_order(req, contract.gateway_name)
Loading

0 comments on commit 8032e66

Please sign in to comment.