-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount.cpp
93 lines (86 loc) · 2.59 KB
/
account.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "account.hpp"
#include <cmath>
account::account(std::string stock_name, std::shared_ptr<spdlog::logger> logger):
stock_name_(stock_name),
logger_(logger),
money_(0),
stock_number_(0)
{}
void account::order_lots(double price, int64_t amount)
{
if(amount == 0)
{
return;
}
else if(amount > 0)
{
// buy the stock
double total_price = price * amount * 100;
if(total_price > money_)
{
logger_->warn("The money is not enough for the order buy {}:{} and will be discarded", stock_name_, amount);
}
else
{
// the money is enough
money_ -= total_price;
stock_number_ += amount;
logger_->info("Buy {} amount:{} the remain money:{} the remain stock lot:{}", stock_name_, amount, money_, stock_number_);
}
}
else
{
// sell the stock
amount = -amount;
if(amount>stock_number_)
{
logger_->warn("The stock is not enough for the order sell {}:{} and will be discarded", stock_name_, amount);
}
else
{
// the stock is enough
stock_number_ -= amount;
money_ += price * amount * 100;
logger_->info("Sell {} amount:{} the remain money:{} the remain stock lot:{}",stock_name_, amount, money_, stock_number_);
}
}
}
void account::order_target_percent(double price, double percent)
{
if(percent < 0 || percent > 1)
{
logger_->warn("The percent is not in 0 to 1, the order_target_percent will be discarded");
return;
}
double stock_value = price * stock_number_ * 100;
double total_value = money_ + stock_value;
double current_position = stock_value / total_value;
double position_to_adjust = percent - current_position;
int64_t amount_to_adjust = (int64_t)std::floor(total_value*std::abs(position_to_adjust)/(price *100));
if(amount_to_adjust == 0)
{
logger_->info("The diff is less then a lot and this order will be discarded");
return;
}
if(position_to_adjust > 0)
{
// buy more stock
order_lots(price, amount_to_adjust);
}
else
{
order_lots(price, -amount_to_adjust);
}
}
void account::order_percent(double price, double percent)
{
if(percent >= 0)// buy stock
{
int lots =(int)std::floor(money_*percent/(price*100));
order_lots(price,lots);
}else // sell stock
{
int lots = std::floor(stock_number_*(-percent));
order_lots(price, -lots);
}
}