Add position line updating to order mode
parent
e91e7bea1f
commit
ea8120156f
|
@ -15,7 +15,7 @@
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Chart trading at it's finest.
|
Chart trading, the only way to scalp.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
@ -27,8 +27,9 @@ import uuid
|
||||||
|
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
import trio
|
import trio
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from ._graphics._lines import LevelLine
|
from ._graphics._lines import LevelLine, position_line
|
||||||
from ._interaction import LineEditor, ArrowEditor, _order_lines
|
from ._interaction import LineEditor, ArrowEditor, _order_lines
|
||||||
from ..exchange._client import open_ems, OrderBook
|
from ..exchange._client import open_ems, OrderBook
|
||||||
from ..data._source import Symbol
|
from ..data._source import Symbol
|
||||||
|
@ -38,6 +39,14 @@ from ..log import get_logger
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Position(BaseModel):
|
||||||
|
symbol: Symbol
|
||||||
|
size: float
|
||||||
|
avg_price: float
|
||||||
|
fills: Dict[str, Any] = {}
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class OrderMode:
|
class OrderMode:
|
||||||
"""Major mode for placing orders on a chart view.
|
"""Major mode for placing orders on a chart view.
|
||||||
|
@ -60,9 +69,32 @@ class OrderMode:
|
||||||
_action: str = 'alert'
|
_action: str = 'alert'
|
||||||
_exec_mode: str = 'dark'
|
_exec_mode: str = 'dark'
|
||||||
_size: float = 100.0
|
_size: float = 100.0
|
||||||
|
_position: Dict[str, Any] = field(default_factory=dict)
|
||||||
|
_position_line: dict = None
|
||||||
|
|
||||||
key_map: Dict[str, Callable] = field(default_factory=dict)
|
key_map: Dict[str, Callable] = field(default_factory=dict)
|
||||||
|
|
||||||
|
def on_position_update(
|
||||||
|
self,
|
||||||
|
msg: dict,
|
||||||
|
) -> None:
|
||||||
|
print(f'Position update {msg}')
|
||||||
|
|
||||||
|
sym = self.chart._lc._symbol
|
||||||
|
if msg['symbol'].lower() not in sym.key:
|
||||||
|
return
|
||||||
|
|
||||||
|
self._position.update(msg)
|
||||||
|
if self._position_line:
|
||||||
|
self._position_line.delete()
|
||||||
|
|
||||||
|
line = self._position_line = position_line(
|
||||||
|
self.chart,
|
||||||
|
level=msg['avg_price'],
|
||||||
|
size=msg['size'],
|
||||||
|
)
|
||||||
|
line.show()
|
||||||
|
|
||||||
def uuid(self) -> str:
|
def uuid(self) -> str:
|
||||||
return str(uuid.uuid4())
|
return str(uuid.uuid4())
|
||||||
|
|
||||||
|
@ -224,6 +256,14 @@ class OrderMode:
|
||||||
price=line.value(),
|
price=line.value(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# def on_key_press(
|
||||||
|
# self,
|
||||||
|
# key:
|
||||||
|
# mods:
|
||||||
|
# text: str,
|
||||||
|
# ) -> None:
|
||||||
|
# pass
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def open_order_mode(
|
async def open_order_mode(
|
||||||
|
@ -280,8 +320,10 @@ async def start_order_mode(
|
||||||
) as order_mode:
|
) as order_mode:
|
||||||
|
|
||||||
def get_index(time: float):
|
def get_index(time: float):
|
||||||
|
|
||||||
# XXX: not sure why the time is so off here
|
# XXX: not sure why the time is so off here
|
||||||
# looks like we're gonna have to do some fixing..
|
# looks like we're gonna have to do some fixing..
|
||||||
|
|
||||||
ohlc = chart._shm.array
|
ohlc = chart._shm.array
|
||||||
indexes = ohlc['time'] >= time
|
indexes = ohlc['time'] >= time
|
||||||
|
|
||||||
|
@ -299,9 +341,17 @@ async def start_order_mode(
|
||||||
fmsg = pformat(msg)
|
fmsg = pformat(msg)
|
||||||
log.info(f'Received order msg:\n{fmsg}')
|
log.info(f'Received order msg:\n{fmsg}')
|
||||||
|
|
||||||
|
resp = msg['resp']
|
||||||
|
|
||||||
|
if resp in (
|
||||||
|
'position',
|
||||||
|
):
|
||||||
|
# show line label once order is live
|
||||||
|
order_mode.on_position_update(msg)
|
||||||
|
continue
|
||||||
|
|
||||||
# delete the line from view
|
# delete the line from view
|
||||||
oid = msg['oid']
|
oid = msg['oid']
|
||||||
resp = msg['resp']
|
|
||||||
|
|
||||||
# response to 'action' request (buy/sell)
|
# response to 'action' request (buy/sell)
|
||||||
if resp in (
|
if resp in (
|
||||||
|
|
Loading…
Reference in New Issue