aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy/trading
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/economy/trading')
-rw-r--r--src/openvic-simulation/economy/trading/BuyResult.cpp11
-rw-r--r--src/openvic-simulation/economy/trading/BuyResult.hpp16
-rw-r--r--src/openvic-simulation/economy/trading/BuyUpToOrder.cpp14
-rw-r--r--src/openvic-simulation/economy/trading/BuyUpToOrder.hpp24
-rw-r--r--src/openvic-simulation/economy/trading/MarketInstance.cpp25
-rw-r--r--src/openvic-simulation/economy/trading/MarketInstance.hpp2
6 files changed, 92 insertions, 0 deletions
diff --git a/src/openvic-simulation/economy/trading/BuyResult.cpp b/src/openvic-simulation/economy/trading/BuyResult.cpp
new file mode 100644
index 0000000..452f3b9
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/BuyResult.cpp
@@ -0,0 +1,11 @@
+#include "BuyResult.hpp"
+
+using namespace OpenVic;
+
+BuyResult::BuyResult(
+ const fixed_point_t new_quantity_bought,
+ const fixed_point_t new_money_left
+) :
+ quantity_bought { new_quantity_bought },
+ money_left { new_money_left }
+ {} \ No newline at end of file
diff --git a/src/openvic-simulation/economy/trading/BuyResult.hpp b/src/openvic-simulation/economy/trading/BuyResult.hpp
new file mode 100644
index 0000000..9eabff7
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/BuyResult.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
+
+namespace OpenVic {
+ struct BuyResult {
+ private:
+ fixed_point_t PROPERTY(quantity_bought);
+ fixed_point_t PROPERTY(money_left);
+ public:
+ BuyResult(
+ const fixed_point_t new_quantity_bought,
+ const fixed_point_t new_money_left
+ );
+ };
+} \ No newline at end of file
diff --git a/src/openvic-simulation/economy/trading/BuyUpToOrder.cpp b/src/openvic-simulation/economy/trading/BuyUpToOrder.cpp
new file mode 100644
index 0000000..6994e18
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/BuyUpToOrder.cpp
@@ -0,0 +1,14 @@
+#include "BuyUpToOrder.hpp"
+
+using namespace OpenVic;
+
+BuyUpToOrder::BuyUpToOrder(
+ GoodDefinition const& new_good,
+ const fixed_point_t new_max_quantity,
+ const fixed_point_t new_money_to_spend,
+ const std::function<void(const BuyResult)> new_after_trade
+) : good { &new_good },
+ max_quantity { new_max_quantity },
+ money_to_spend { new_money_to_spend },
+ after_trade { new_after_trade }
+ {} \ No newline at end of file
diff --git a/src/openvic-simulation/economy/trading/BuyUpToOrder.hpp b/src/openvic-simulation/economy/trading/BuyUpToOrder.hpp
new file mode 100644
index 0000000..b19cf36
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/BuyUpToOrder.hpp
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "openvic-simulation/economy/GoodDefinition.hpp"
+#include "openvic-simulation/economy/trading/BuyResult.hpp"
+#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
+#include "openvic-simulation/utility/Getters.hpp"
+
+namespace OpenVic {
+ struct BuyUpToOrder {
+ private:
+ GoodDefinition const* const PROPERTY(good);
+ const fixed_point_t PROPERTY(max_quantity);
+ const fixed_point_t PROPERTY(money_to_spend);
+ const std::function<void(const BuyResult)> PROPERTY(after_trade);
+
+ public:
+ BuyUpToOrder(
+ GoodDefinition const& new_good,
+ const fixed_point_t new_max_quantity,
+ const fixed_point_t new_money_to_spend,
+ const std::function<void(const BuyResult)> new_after_trade
+ );
+ };
+} \ No newline at end of file
diff --git a/src/openvic-simulation/economy/trading/MarketInstance.cpp b/src/openvic-simulation/economy/trading/MarketInstance.cpp
index 5539cdb..fc0c215 100644
--- a/src/openvic-simulation/economy/trading/MarketInstance.cpp
+++ b/src/openvic-simulation/economy/trading/MarketInstance.cpp
@@ -3,6 +3,8 @@
#include "openvic-simulation/economy/GoodDefinition.hpp"
#include "openvic-simulation/economy/GoodInstance.hpp"
#include "openvic-simulation/economy/trading/MarketSellOrder.hpp"
+#include "economy/trading/BuyResult.hpp"
+#include "types/fixed_point/FixedPoint.hpp"
using namespace OpenVic;
@@ -11,6 +13,16 @@ bool MarketInstance::setup(GoodInstanceManager& new_good_instance_manager) {
return true;
}
+void MarketInstance::place_buy_up_to_order(const BuyUpToOrder buy_up_to_order) {
+ GoodDefinition const* const good = buy_up_to_order.get_good();
+ GoodInstance* const good_instance = good_instance_manager->get_good_instance_by_identifier(good->get_identifier());
+ good_instance->add_buy_up_to_order(
+ buy_up_to_order.get_max_quantity(),
+ buy_up_to_order.get_money_to_spend(),
+ buy_up_to_order.get_after_trade()
+ );
+}
+
void MarketInstance::place_market_sell_order(const MarketSellOrder market_sell_order) {
GoodDefinition const* const good = market_sell_order.get_good();
GoodInstance* const good_instance = good_instance_manager->get_good_instance_by_identifier(good->get_identifier());
@@ -31,6 +43,19 @@ void MarketInstance::execute_orders() {
market_sell_quantity * price
});
}
+
+ std::vector<fixed_point_t> const& buy_up_to_max_quantities = good_instance.get_buy_up_to_order_max_quantities();
+ std::vector<fixed_point_t> const& buy_up_to_money_to_spend = good_instance.get_buy_up_to_order_money_to_spend();
+ std::vector<std::function<void(const BuyResult)>> const& buy_up_to_max_callbacks = good_instance.get_buy_up_to_order_callbacks();
+
+ for(int i = 0; i < buy_up_to_max_quantities.size(); i++) {
+ const fixed_point_t money_to_spend = buy_up_to_money_to_spend[i];
+ const fixed_point_t quantity_bought = std::min(buy_up_to_max_quantities[i], money_to_spend / price);
+ buy_up_to_max_callbacks[i]({
+ quantity_bought,
+ money_to_spend - quantity_bought * price
+ });
+ }
good_instance.clear_orders();
}
diff --git a/src/openvic-simulation/economy/trading/MarketInstance.hpp b/src/openvic-simulation/economy/trading/MarketInstance.hpp
index 2d8a651..51b25b4 100644
--- a/src/openvic-simulation/economy/trading/MarketInstance.hpp
+++ b/src/openvic-simulation/economy/trading/MarketInstance.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "openvic-simulation/economy/GoodInstance.hpp"
+#include "openvic-simulation/economy/trading/BuyUpToOrder.hpp"
#include "openvic-simulation/economy/trading/MarketSellOrder.hpp"
namespace OpenVic {
@@ -9,6 +10,7 @@ namespace OpenVic {
GoodInstanceManager* PROPERTY(good_instance_manager);
public:
bool setup(GoodInstanceManager& new_good_instance_manager);
+ void place_buy_up_to_order(const BuyUpToOrder buy_up_to_order);
void place_market_sell_order(const MarketSellOrder market_sell_order);
void execute_orders();
};