Skip to content

Commit

Permalink
Merge pull request #68 from AsyncAlgoTrading/synthetic_timefix
Browse files Browse the repository at this point in the history
fix issue with synthetic exchange stop order time
  • Loading branch information
timkpaine committed Aug 4, 2020
2 parents 73de7de + b8f7a9a commit 91d7d22
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion aat/core/models/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def filled(self, filled: float) -> None:
self.__filled = filled

def __repr__(self) -> str:
return f'Order( instrument={self.instrument}, {self.volume}@{self.price}, side={self.side}, exchange={self.exchange})'
return f'Order( instrument={self.instrument}, timestamp={self.timestamp}, {self.volume}@{self.price}, side={self.side}, exchange={self.exchange})'

def __eq__(self, other) -> bool:
assert isinstance(other, Order)
Expand Down
2 changes: 1 addition & 1 deletion aat/core/models/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def my_order(self, order: Order) -> None:
self.__my_order = order

def __repr__(self) -> str:
return f'Trade( id={self.id}, timestamp{self.timestamp}, {self.volume}@{self.price}, \n\ttaker_order={self.taker_order},\n\tmaker_orders={self.maker_orders}, )'
return f'Trade( id={self.id}, timestamp={self.timestamp}, {self.volume}@{self.price}, \n\ttaker_order={self.taker_order},\n\tmaker_orders={self.maker_orders}, )'

def __eq__(self, other) -> bool:
assert isinstance(other, Trade)
Expand Down
7 changes: 7 additions & 0 deletions aat/core/order_book/order_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def add(self, order):

# execute secondaries
for secondary in secondaries:
secondary.timestamp = order.timestamp # adjust trigger time
self.add(secondary)

else:
Expand Down Expand Up @@ -204,6 +205,7 @@ def add(self, order):

# execute secondaries
for secondary in secondaries:
secondary.timestamp = order.timestamp # adjust trigger time
self.add(secondary)

elif order.flag == OrderFlag.ALL_OR_NONE:
Expand Down Expand Up @@ -233,6 +235,7 @@ def add(self, order):

# execute secondaries
for secondary in secondaries:
secondary.timestamp = order.timestamp # adjust trigger time
self.add(secondary)

elif order.flag == OrderFlag.IMMEDIATE_OR_CANCEL:
Expand All @@ -248,6 +251,7 @@ def add(self, order):

# execute secondaries
for secondary in secondaries:
secondary.timestamp = order.timestamp # adjust trigger time
self.add(secondary)

else:
Expand All @@ -264,6 +268,7 @@ def add(self, order):

# execute secondaries
for secondary in secondaries:
secondary.timestamp = order.timestamp # adjust trigger time
self.add(secondary)

else:
Expand All @@ -283,6 +288,7 @@ def add(self, order):

# execute secondaries
for secondary in secondaries:
secondary.timestamp = order.timestamp # adjust trigger time
self.add(secondary)
else:
if order.filled > order.volume:
Expand All @@ -297,6 +303,7 @@ def add(self, order):

# execute secondaries
for secondary in secondaries:
secondary.timestamp = order.timestamp # adjust trigger time
self.add(secondary)

# clear the collector
Expand Down
28 changes: 21 additions & 7 deletions aat/cpp/src/core/order_book/order_book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ namespace core {
collector.commit();

// execute secondaries
for (std::shared_ptr<Order> secondary : secondaries)
for (std::shared_ptr<Order> secondary : secondaries) {
secondary->timestamp = order->timestamp; // adjust trigger time
add(secondary);
}
}
} else {
// Limit Orders
Expand All @@ -143,8 +145,10 @@ namespace core {
prices[order->price]->add(order);

// execute secondaries
for (std::shared_ptr<Order> secondary : secondaries)
for (std::shared_ptr<Order> secondary : secondaries) {
secondary->timestamp = order->timestamp; // adjust trigger time
add(secondary);
}
}
} else if (order->flag == OrderFlag::ALL_OR_NONE) {
if (order->filled > 0) {
Expand All @@ -168,8 +172,10 @@ namespace core {
prices[order->price]->add(order);

// execute secondaries
for (std::shared_ptr<Order> secondary : secondaries)
for (std::shared_ptr<Order> secondary : secondaries) {
secondary->timestamp = order->timestamp; // adjust trigger time
add(secondary);
}
}
} else if (order->flag == OrderFlag::IMMEDIATE_OR_CANCEL) {
if (order->filled > 0) {
Expand All @@ -181,8 +187,10 @@ namespace core {
collector.commit();

// execute secondaries
for (std::shared_ptr<Order> secondary : secondaries)
for (std::shared_ptr<Order> secondary : secondaries) {
secondary->timestamp = order->timestamp; // adjust trigger time
add(secondary);
}

} else {
// add to book
Expand All @@ -197,8 +205,10 @@ namespace core {
prices[order->price]->add(order);

// execute secondaries
for (std::shared_ptr<Order> secondary : secondaries)
for (std::shared_ptr<Order> secondary : secondaries) {
secondary->timestamp = order->timestamp; // adjust trigger time
add(secondary);
}
}
} else {
// clear levels
Expand All @@ -217,8 +227,10 @@ namespace core {
prices[order->price]->add(order);

// execute secondaries
for (std::shared_ptr<Order> secondary : secondaries)
for (std::shared_ptr<Order> secondary : secondaries) {
secondary->timestamp = order->timestamp; // adjust trigger time
add(secondary);
}
}
}
} else {
Expand All @@ -231,8 +243,10 @@ namespace core {
collector.commit();

// execute secondaries
for (std::shared_ptr<Order> secondary : secondaries)
for (std::shared_ptr<Order> secondary : secondaries) {
secondary->timestamp = order->timestamp; // adjust trigger time
add(secondary);
}
}

// clear the collector
Expand Down
11 changes: 10 additions & 1 deletion aat/exchange/synthetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ async def tick(self, snapshot=False):

while self._pending_orders:
order = self._pending_orders.popleft()
self._jumptime(order)
self._orderbooks[order.instrument].add(order)
await asyncio.sleep(self._sleep)

Expand Down Expand Up @@ -322,9 +323,17 @@ async def tick(self, snapshot=False):
# Order Entry Methods #
# ******************* #
async def newOrder(self, order: Order):
# assign id
order.id = self._id
self._id += 1

# adjust time if backtesting

self._jumptime(order)
# fix exchange if messed up
# if order.exchange != self.exchange():
# order.exchange = self.exchange()

self._id += 1
self._pending_orders.append(order)
self._omit_cancel.add(order.id) # don't cancel user orders
return order
Expand Down

0 comments on commit 91d7d22

Please sign in to comment.