Skip to content

Commit

Permalink
Merge pull request #273 from northwesternfintech/float
Browse files Browse the repository at this point in the history
Switched from doubles to floats in templates
  • Loading branch information
stevenewald authored Oct 1, 2024
2 parents a1cf14d + 67a57f6 commit e90d8ad
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LevelQuantityTracker::report_quantity(
// TODO: guarantee size checks
if (levels.size() <= static_cast<uint64_t>(price.get_underlying())) [[unlikely]] {
auto new_size =
static_cast<size_t>(static_cast<double>(price.get_underlying()) * 1.5);
static_cast<size_t>(static_cast<float>(price.get_underlying()) * 1.5f);
bid_levels_.resize(new_size);
ask_levels_.resize(new_size);
}
Expand Down
4 changes: 2 additions & 2 deletions exchange/src/wrapper/messaging/exchange_communicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace nutc::wrapper {
using namespace nutc::common;

using LimitOrderFunction = std::function<order_id_t(
common::Side side, common::Ticker ticker, double quantity, double price, bool ioc
common::Side side, common::Ticker ticker, float quantity, float price, bool ioc
)>;
using MarketOrderFunction =
std::function<bool(common::Side side, common::Ticker ticker, double quantity)>;
std::function<bool(common::Side side, common::Ticker ticker, float quantity)>;
using CancelOrderFunction =
std::function<bool(common::Ticker ticker, order_id_t order_id)>;

Expand Down
12 changes: 6 additions & 6 deletions exchange/src/wrapper/runtime/cpp/cpp_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ CppRuntime::fire_on_trade_update(
) const
{
on_trade_update_func_(
strategy_object_, ticker, side, static_cast<double>(quantity),
static_cast<double>(price)
strategy_object_, ticker, side, static_cast<float>(quantity),
static_cast<float>(price)
);
}

Expand All @@ -115,8 +115,8 @@ CppRuntime::fire_on_orderbook_update(
) const
{
on_orderbook_update_func_(
strategy_object_, ticker, side, static_cast<double>(quantity),
static_cast<double>(price)
strategy_object_, ticker, side, static_cast<float>(quantity),
static_cast<float>(price)
);
}

Expand All @@ -127,8 +127,8 @@ CppRuntime::fire_on_account_update(
) const
{
on_account_update_func_(
strategy_object_, ticker, side, static_cast<double>(quantity),
static_cast<double>(price), static_cast<double>(capital)
strategy_object_, ticker, side, static_cast<float>(quantity),
static_cast<float>(price), static_cast<float>(capital)
);
}

Expand Down
6 changes: 3 additions & 3 deletions exchange/src/wrapper/runtime/cpp/cpp_runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class CppRuntime : public Runtime {
using Strategy = void;
using InitFunc = Strategy* (*)(MarketOrderFunction, LimitOrderFunction,
CancelOrderFunction);
using on_trade_update_func = void (*)(Strategy*, Ticker, Side, double, double);
using on_orderbook_update_func = void (*)(Strategy*, Ticker, Side, double, double);
using on_trade_update_func = void (*)(Strategy*, Ticker, Side, float, float);
using on_orderbook_update_func = void (*)(Strategy*, Ticker, Side, float, float);
using on_account_update_func =
void (*)(Strategy*, Ticker, Side, double, double, double);
void (*)(Strategy*, Ticker, Side, float, float, float);

on_trade_update_func on_trade_update_func_;
on_orderbook_update_func on_orderbook_update_func_;
Expand Down
8 changes: 4 additions & 4 deletions exchange/src/wrapper/runtime/python/python_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PyRuntime::fire_on_trade_update(
{
try {
py::globals()["strategy"].attr("on_trade_update")(
ticker, side, static_cast<double>(quantity), static_cast<double>(price)
ticker, side, static_cast<float>(quantity), static_cast<float>(price)
);
} catch (const py::error_already_set& err) {
std::cerr << err.what() << "\n";
Expand All @@ -32,7 +32,7 @@ PyRuntime::fire_on_orderbook_update(
{
try {
py::globals()["strategy"].attr("on_orderbook_update")(
ticker, side, static_cast<double>(quantity), static_cast<double>(price)
ticker, side, static_cast<float>(quantity), static_cast<float>(price)
);
} catch (const py::error_already_set& err) {
std::cerr << err.what() << "\n";
Expand All @@ -47,8 +47,8 @@ PyRuntime::fire_on_account_update(
{
try {
py::globals()["strategy"].attr("on_account_update")(
ticker, side, static_cast<double>(quantity), static_cast<double>(price),
static_cast<double>(capital)
ticker, side, static_cast<float>(quantity), static_cast<float>(price),
static_cast<float>(capital)
);
} catch (const py::error_already_set& err) {
std::cerr << err.what() << "\n";
Expand Down
16 changes: 8 additions & 8 deletions exchange/template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

#include <functional>

using PlaceMarketOrder = std::function<bool(Side, Ticker, double)>;
using PlaceLimitOrder = std::function<std::int64_t(Side, Ticker, double, double, bool)>;
using PlaceMarketOrder = std::function<bool(Side, Ticker, float)>;
using PlaceLimitOrder = std::function<std::int64_t(Side, Ticker, float, float, bool)>;
using CancelOrder = std::function<bool(Ticker, std::int64_t order_id)>;

static PlaceMarketOrder s_place_market_order;
static PlaceLimitOrder s_place_limit_order;
static CancelOrder s_cancel_order;

bool
place_market_order(Side side, Ticker ticker, double quantity)
place_market_order(Side side, Ticker ticker, float quantity)
{
return s_place_market_order(side, ticker, quantity);
}

std::int64_t
place_limit_order(Side side, Ticker ticker, double quantity, double price, bool ioc)
place_limit_order(Side side, Ticker ticker, float quantity, float price, bool ioc)
{
return s_place_limit_order(side, ticker, quantity, price, ioc);
}
Expand All @@ -47,24 +47,24 @@ init(

void
on_trade_update(
Strategy* strategy, Ticker ticker, Side side, double quantity, double price
Strategy* strategy, Ticker ticker, Side side, float quantity, float price
)
{
strategy->on_trade_update(ticker, side, quantity, price);
}

void
on_orderbook_update(
Strategy* strategy, Ticker ticker, Side side, double quantity, double price
Strategy* strategy, Ticker ticker, Side side, float quantity, float price
)
{
strategy->on_orderbook_update(ticker, side, quantity, price);
}

void
on_account_update(
Strategy* strategy, Ticker ticker, Side side, double price, double quantity,
double capital_remaining
Strategy* strategy, Ticker ticker, Side side, float price, float quantity,
float capital_remaining
)
{
strategy->on_account_update(ticker, side, price, quantity, capital_remaining);
Expand Down
12 changes: 6 additions & 6 deletions exchange/test/src/integration/test_algos/cpp/buy_eth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum class Ticker: std::uint8_t { ETH = 0, BTC = 1, LTC = 2 }; // NOLINT
* @return true if order succeeded, false if order failed due to rate limiting
*/
bool
place_market_order(Side side, Ticker ticker, double quantity);
place_market_order(Side side, Ticker ticker, float quantity);

/**
* Place a limit order
Expand All @@ -37,7 +37,7 @@ place_market_order(Side side, Ticker ticker, double quantity);
* @return true if order succeeded, false if order failed due to rate limiting
*/
std::int64_t place_limit_order(
Side side, Ticker ticker, double quantity, double price,
Side side, Ticker ticker, float quantity, float price,
bool ioc = false
);

Expand All @@ -58,7 +58,7 @@ class Strategy {
* @quantity quantity Volume traded
*/
void
on_trade_update(Ticker ticker, Side side, double quantity, double price)
on_trade_update(Ticker ticker, Side side, float quantity, float price)
{}

/**
Expand All @@ -72,7 +72,7 @@ class Strategy {
*/
void
on_orderbook_update(
Ticker ticker, Side side, double quantity, double price
Ticker ticker, Side side, float quantity, float price
)
{}

Expand All @@ -86,8 +86,8 @@ class Strategy {
*/
void
on_account_update(
Ticker ticker, Side side, double price, double quantity,
double capital_remaining
Ticker ticker, Side side, float price, float quantity,
float capital_remaining
)
{}
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum class Ticker : std::uint8_t { ETH = 0, BTC = 1, LTC = 2 }; // NOLINT
*
* @return true if order succeeded, false if order failed due to rate limiting
*/
bool place_market_order(Side side, Ticker ticker, double quantity);
bool place_market_order(Side side, Ticker ticker, float quantity);

/**
* Place a limit order
Expand All @@ -36,7 +36,7 @@ bool place_market_order(Side side, Ticker ticker, double quantity);
* @return true if order succeeded, false if order failed due to rate limiting
*/
std::int64_t place_limit_order(
Side side, Ticker ticker, double quantity, double price, bool ioc = false
Side side, Ticker ticker, float quantity, float price, bool ioc = false
);

bool cancel_order(Ticker ticker, std::int64_t order_id);
Expand All @@ -56,7 +56,7 @@ class Strategy {
* @quantity quantity Volume traded
*/
void
on_trade_update(Ticker ticker, Side side, double quantity, double price)
on_trade_update(Ticker ticker, Side side, float quantity, float price)
{
if (ticker == Ticker::ETH && price <= 101 && price >= 99 && quantity == 5) {
place_limit_order(Side::buy, Ticker::BTC, 1, 100);
Expand All @@ -74,7 +74,7 @@ class Strategy {
* @param quantity Volume placed into orderbook
*/
void
on_orderbook_update(Ticker ticker, Side side, double quantity, double price)
on_orderbook_update(Ticker ticker, Side side, float quantity, float price)
{}

/**
Expand All @@ -88,8 +88,8 @@ class Strategy {
*/
void
on_account_update(
Ticker ticker, Side side, double price, double quantity,
double capital_remaining
Ticker ticker, Side side, float price, float quantity,
float capital_remaining
)
{}
};
12 changes: 6 additions & 6 deletions exchange/test/src/integration/test_algos/cpp/buy_tsla_at_100.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum class Ticker: std::uint8_t { ETH = 0, BTC = 1, LTC = 2 }; // NOLINT
* @return true if order succeeded, false if order failed due to rate limiting
*/
bool
place_market_order(Side side, Ticker ticker, double quantity);
place_market_order(Side side, Ticker ticker, float quantity);

/**
* Place a limit order
Expand All @@ -37,7 +37,7 @@ place_market_order(Side side, Ticker ticker, double quantity);
* @return true if order succeeded, false if order failed due to rate limiting
*/
std::int64_t place_limit_order(
Side side, Ticker ticker, double quantity, double price,
Side side, Ticker ticker, float quantity, float price,
bool ioc = false
);

Expand All @@ -58,7 +58,7 @@ class Strategy {
* @quantity quantity Volume traded
*/
void
on_trade_update(Ticker ticker, Side side, double quantity, double price)
on_trade_update(Ticker ticker, Side side, float quantity, float price)
{}

/**
Expand All @@ -72,7 +72,7 @@ class Strategy {
*/
void
on_orderbook_update(
Ticker ticker, Side side, double quantity, double price
Ticker ticker, Side side, float quantity, float price
)
{
if (ticker == Ticker::ETH && quantity < 101 && quantity > 99) {
Expand All @@ -90,8 +90,8 @@ class Strategy {
*/
void
on_account_update(
Ticker ticker, Side side, double price, double quantity,
double capital_remaining
Ticker ticker, Side side, float price, float quantity,
float capital_remaining
)
{}
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum class Ticker: std::uint8_t { ETH = 0, BTC = 1, LTC = 2 }; // NOLINT
* @return true if order succeeded, false if order failed due to rate limiting
*/
bool
place_market_order(Side side, Ticker ticker, double quantity);
place_market_order(Side side, Ticker ticker, float quantity);

/**
* Place a limit order
Expand All @@ -37,7 +37,7 @@ place_market_order(Side side, Ticker ticker, double quantity);
* @return true if order succeeded, false if order failed due to rate limiting
*/
std::int64_t place_limit_order(
Side side, Ticker ticker, double quantity, double price,
Side side, Ticker ticker, float quantity, float price,
bool ioc = false
);

Expand All @@ -58,7 +58,7 @@ class Strategy {
* @quantity quantity Volume traded
*/
void
on_trade_update(Ticker ticker, Side side, double quantity, double price)
on_trade_update(Ticker ticker, Side side, float quantity, float price)
{}

/**
Expand All @@ -72,7 +72,7 @@ class Strategy {
*/
void
on_orderbook_update(
Ticker ticker, Side side, double quantity, double price
Ticker ticker, Side side, float quantity, float price
)
{}

Expand All @@ -86,8 +86,8 @@ class Strategy {
*/
void
on_account_update(
Ticker ticker, Side side, double price, double quantity,
double capital_remaining
Ticker ticker, Side side, float price, float quantity,
float capital_remaining
)
{
if (ticker == Ticker::ETH && quantity >= 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum class Ticker: std::uint8_t { ETH = 0, BTC = 1, LTC = 2 }; // NOLINT
* @return true if order succeeded, false if order failed due to rate limiting
*/
bool
place_market_order(Side side, Ticker ticker, double quantity);
place_market_order(Side side, Ticker ticker, float quantity);

/**
* Place a limit order
Expand All @@ -37,7 +37,7 @@ place_market_order(Side side, Ticker ticker, double quantity);
* @return true if order succeeded, false if order failed due to rate limiting
*/
std::int64_t place_limit_order(
Side side, Ticker ticker, double quantity, double price,
Side side, Ticker ticker, float quantity, float price,
bool ioc = false
);

Expand All @@ -58,7 +58,7 @@ class Strategy {
* @quantity quantity Volume traded
*/
void
on_trade_update(Ticker ticker, Side side, double quantity, double price)
on_trade_update(Ticker ticker, Side side, float quantity, float price)
{
if (ticker == Ticker::ETH && quantity >= 10)
place_limit_order(Side::buy, Ticker::BTC, 1, 100);
Expand All @@ -75,7 +75,7 @@ class Strategy {
*/
void
on_orderbook_update(
Ticker ticker, Side side, double quantity, double price
Ticker ticker, Side side, float quantity, float price
)
{}

Expand All @@ -89,8 +89,8 @@ class Strategy {
*/
void
on_account_update(
Ticker ticker, Side side, double price, double quantity,
double capital_remaining
Ticker ticker, Side side, float price, float quantity,
float capital_remaining
)
{}
};
Loading

0 comments on commit e90d8ad

Please sign in to comment.