MNP01/simulator.cpp
2018-03-18 21:31:48 +01:00

45 lines
1.1 KiB
C++

#include "simulator.h"
simulator::simulator(class decider* decider, double money, unsigned stock)
: decider(decider), money(money), start_money(money), stock(stock), start_stock(stock)
{
decider->start_money = money;
decider->start_stock = stock;
initialized = false;
}
double simulator::proceed(double price)
{
if (!initialized) {
initialized = true;
start_price = price;
decider->reset(price);
}
auto decision = decider->decide(price, money, stock);
auto current = price * stock + money;
auto max_credit = std::max(current * this->credit_ratio, -this->max_credit);
if (decision < 0) {
decision = std::max<int>(decision, -stock); // cannot sell more than we actually have
} else if (decision > 0) {
decision = std::min<int>(floor((money + max_credit) / price), decision);
}
money -= price * decision;
stock += decision;
return decision;
}
std::vector<double> simulator::proceed(std::vector<double> prices)
{
std::vector<double> result;
for(auto price : prices) {
result.push_back(proceed(price));
}
return result;
}