This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
147 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
from dataclasses import dataclass | ||
from core.models.order import Order | ||
|
||
from core.models.position import Position | ||
from core.queries.base import Query | ||
|
||
|
||
@dataclass(frozen=True) | ||
class GetOpenPosition(Query[Position]): | ||
class GetOpenPosition(Query[Order]): | ||
position: Position | ||
|
||
|
||
@dataclass(frozen=True) | ||
class GetClosePosition(Query[Position]): | ||
class GetClosePosition(Query[Order]): | ||
position: Position | ||
exit_price: float |
This file contains 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
This file contains 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
Empty file.
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import time | ||
import numpy as np | ||
|
||
from core.interfaces.abstract_config import AbstractConfig | ||
from core.interfaces.abstract_exchange import AbstractExchange | ||
from core.models.symbol import Symbol | ||
|
||
|
||
class TWAP: | ||
def __init__(self, config_service: AbstractConfig): | ||
self.config = config_service.get("position") | ||
|
||
def calculate(self, symbol: Symbol, exchange: AbstractExchange): | ||
current_time = 0 | ||
timepoints = [] | ||
twap_duration = self.config["twap_duration"] | ||
|
||
while current_time < twap_duration: | ||
order_book = self._fetch_book(symbol, exchange) | ||
timepoints.append(order_book) | ||
|
||
time_interval = twap_duration / len(order_book) | ||
current_time += time_interval | ||
|
||
time.sleep(time_interval) | ||
|
||
twap_value = self._twap(timepoints) | ||
|
||
yield round(twap_value, symbol.price_precision) | ||
|
||
def _fetch_book(self, symbol: Symbol, exchange: AbstractExchange): | ||
bids, asks = exchange.fetch_order_book(symbol) | ||
|
||
bids = np.array([[price, size] for price, size in bids]) | ||
asks = np.array([[price, size] for price, size in asks]) | ||
|
||
return np.vstack([bids, asks]) | ||
|
||
@staticmethod | ||
def _twap(order_book): | ||
timepoints = np.vstack(order_book) | ||
prices = timepoints[:, 0] | ||
total_timepoints = len(prices) | ||
return np.sum(prices) / total_timepoints |