-
-
Notifications
You must be signed in to change notification settings - Fork 170
WAP support on tickfilter bars #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gnzsnz
wants to merge
2
commits into
ib-api-reloaded:main
Choose a base branch
from
gnzsnz:vwap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
nan = float("nan") | ||
|
||
|
||
@dataclass | ||
@dataclass(slots=True) | ||
class Ticker: | ||
""" | ||
Current market data such as bid, ask, last price, etc. for a contract. | ||
|
@@ -123,6 +123,7 @@ class Ticker: | |
regulatoryImbalance: float = nan | ||
bboExchange: str = "" | ||
snapshotPermissions: int = 0 | ||
updateEvent: Event = field(init=False) | ||
|
||
def __post_init__(self): | ||
self.updateEvent = TickerUpdateEvent("updateEvent") | ||
|
@@ -258,14 +259,15 @@ def on_source(self, ticker): | |
self.emit(ticker.time, ticker.midpoint(), 0) | ||
|
||
|
||
@dataclass | ||
@dataclass(slots=True) | ||
class Bar: | ||
time: Optional[datetime] | ||
open: float = nan | ||
high: float = nan | ||
low: float = nan | ||
close: float = nan | ||
volume: int = 0 | ||
wap: float = 0 | ||
count: int = 0 | ||
|
||
|
||
|
@@ -302,6 +304,11 @@ def on_source(self, time, price, size): | |
bar.high = max(bar.high, price) | ||
bar.low = min(bar.low, price) | ||
bar.close = price | ||
# wap | ||
if (bar.volume + size) == 0: | ||
|
||
bar.wap = bar.wap | ||
else: | ||
bar.wap = ((bar.wap * bar.volume) + (price * size)) / (bar.volume + size) | ||
bar.volume += size | ||
bar.count += 1 | ||
self.bars.updateEvent.emit(self.bars, False) | ||
|
@@ -310,7 +317,9 @@ def _on_timer(self, time): | |
if self.bars: | ||
bar = self.bars[-1] | ||
if isNan(bar.close) and len(self.bars) > 1: | ||
bar.open = bar.high = bar.low = bar.close = self.bars[-2].close | ||
bar.open = bar.high = bar.low = bar.close = bar.wap = self.bars[ | ||
-2 | ||
].close | ||
self.bars.updateEvent.emit(self.bars, True) | ||
self.emit(bar) | ||
self.bars.append(Bar(time)) | ||
|
@@ -333,16 +342,25 @@ def __init__(self, count, source=None): | |
|
||
def on_source(self, time, price, size): | ||
if not self.bars or self.bars[-1].count == self._count: | ||
bar = Bar(time, price, price, price, price, size, 1) | ||
bar = Bar(time, price, price, price, price, size, price, 1) | ||
self.bars.append(bar) | ||
else: | ||
bar = self.bars[-1] | ||
bar.high = max(bar.high, price) | ||
bar.low = min(bar.low, price) | ||
bar.close = price | ||
# wap | ||
if (bar.volume + size) == 0: | ||
bar.wap = bar.wap | ||
else: | ||
bar.wap = ((bar.wap * bar.volume) + (price * size)) / ( | ||
bar.volume + size | ||
) | ||
bar.volume += size | ||
bar.count += 1 | ||
if bar.count == self._count: | ||
if bar.wap == 0: | ||
bar.wap = bar.close | ||
self.bars.updateEvent.emit(self.bars, True) | ||
self.emit(self.bars) | ||
|
||
|
@@ -360,15 +378,24 @@ def __init__(self, volume, source=None): | |
|
||
def on_source(self, time, price, size): | ||
if not self.bars or self.bars[-1].volume >= self._volume: | ||
bar = Bar(time, price, price, price, price, size, 1) | ||
bar = Bar(time, price, price, price, price, size, price, 1) | ||
self.bars.append(bar) | ||
else: | ||
bar = self.bars[-1] | ||
bar.high = max(bar.high, price) | ||
bar.low = min(bar.low, price) | ||
# wap | ||
bar.close = price | ||
if (bar.volume + size) == 0: | ||
bar.wap = bar.wap | ||
else: | ||
bar.wap = ((bar.wap * bar.volume) + (price * size)) / ( | ||
bar.volume + size | ||
) | ||
bar.volume += size | ||
bar.count += 1 | ||
if bar.volume >= self._volume: | ||
if bar.wap == 0: | ||
bar.wap = bar.close | ||
self.bars.updateEvent.emit(self.bars, True) | ||
self.emit(self.bars) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems lika "nan" is the convention for default. If you decide so, also conditions on ln 362 and 398 will need update to
isNan(bar.wap)
.Although looks like wap can only ever be nan/0 in TimeBar when bar gets created on timer without any input data, so maybe these conditions in Tick and Volume bars are unnecessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll do a few tests with
nan
, I just realize that usingwap = 0
is running my plots