FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
alpaca-cpp/tests/order_request_tests.cpp at main · SlickQuant/alpaca-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SlickQuant
/
alpaca-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
alpaca-cpp
/
tests
/
order_request_tests.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
204 lines (165 loc) · 6.8 KB
Breadcrumbs
alpaca-cpp
/
tests
/
order_request_tests.cpp
Copy path
File metadata and controls
204 lines (165 loc) · 6.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
SPDX-License-Identifier: MIT
//
Copyright (c) 2026 Slick Quant
//
https://github.com/SlickQuant/alpaca-cpp
//
//
Offline tests for order request serialisation. Alpaca requires every numeric field as a
//
decimal string and rejects scientific notation, so these assert the exact wire payload.
#
include
<
gtest/gtest.h
>
#
include
<
alpaca/trading/locate.hpp
>
#
include
<
alpaca/trading/order.hpp
>
#
include
<
alpaca/trading/position.hpp
>
#
include
<
alpaca/trading/watchlist.hpp
>
namespace
alpaca
::tests {
TEST
(OrderRequest, MarketOrder) {
const
auto
j =
order_request::market
(
"
AAPL
"
,
10
, order_side::buy).
to_json
();
EXPECT_EQ
(j[
"
symbol
"
],
"
AAPL
"
);
EXPECT_EQ
(j[
"
side
"
],
"
buy
"
);
EXPECT_EQ
(j[
"
type
"
],
"
market
"
);
EXPECT_EQ
(j[
"
time_in_force
"
],
"
day
"
);
EXPECT_EQ
(j[
"
qty
"
],
"
10
"
);
EXPECT_FALSE
(j.
contains
(
"
limit_price
"
));
EXPECT_FALSE
(j.
contains
(
"
notional
"
));
}
TEST
(OrderRequest, NotionalMarketOrder) {
const
auto
j =
order_request::market_notional
(
"
AAPL
"
,
500.25
, order_side::buy).
to_json
();
EXPECT_EQ
(j[
"
notional
"
],
"
500.25
"
);
EXPECT_FALSE
(j.
contains
(
"
qty
"
));
EXPECT_EQ
(j[
"
time_in_force
"
],
"
day
"
);
}
TEST
(OrderRequest, LimitOrderWithGtc) {
const
auto
j =
order_request::limit
(
"
MSFT
"
,
5
, order_side::sell,
412.5
,
time_in_force::gtc).
to_json
();
EXPECT_EQ
(j[
"
type
"
],
"
limit
"
);
EXPECT_EQ
(j[
"
side
"
],
"
sell
"
);
EXPECT_EQ
(j[
"
limit_price
"
],
"
412.5
"
);
EXPECT_EQ
(j[
"
time_in_force
"
],
"
gtc
"
);
}
TEST
(OrderRequest, StopAndStopLimit) {
const
auto
stop =
order_request::stop
(
"
AAPL
"
,
1
, order_side::sell,
90
).
to_json
();
EXPECT_EQ
(stop[
"
type
"
],
"
stop
"
);
EXPECT_EQ
(stop[
"
stop_price
"
],
"
90
"
);
EXPECT_FALSE
(stop.
contains
(
"
limit_price
"
));
const
auto
stop_limit =
order_request::stop_limit
(
"
AAPL
"
,
1
, order_side::sell,
90
,
89.5
).
to_json
();
EXPECT_EQ
(stop_limit[
"
type
"
],
"
stop_limit
"
);
EXPECT_EQ
(stop_limit[
"
stop_price
"
],
"
90
"
);
EXPECT_EQ
(stop_limit[
"
limit_price
"
],
"
89.5
"
);
}
TEST
(OrderRequest, TrailingStopByPriceAndPercent) {
const
auto
by_price =
order_request::trailing_stop_price
(
"
AAPL
"
,
1
, order_side::sell,
2.5
).
to_json
();
EXPECT_EQ
(by_price[
"
type
"
],
"
trailing_stop
"
);
EXPECT_EQ
(by_price[
"
trail_price
"
],
"
2.5
"
);
EXPECT_FALSE
(by_price.
contains
(
"
trail_percent
"
));
const
auto
by_pct =
order_request::trailing_stop_percent
(
"
AAPL
"
,
1
, order_side::sell,
1.5
).
to_json
();
EXPECT_EQ
(by_pct[
"
trail_percent
"
],
"
1.5
"
);
EXPECT_FALSE
(by_pct.
contains
(
"
trail_price
"
));
}
TEST
(OrderRequest, FractionalQuantitiesKeepTheirPrecision) {
//
Fractional share quantities must survive as decimals, not become 0 or 1e-09.
const
auto
j =
order_request::market
(
"
AAPL
"
,
0.001
, order_side::buy).
to_json
();
EXPECT_EQ
(j[
"
qty
"
],
"
0.001
"
);
const
auto
tiny =
order_request::market
(
"
BTC/USD
"
,
0.000000001
, order_side::buy).
to_json
();
EXPECT_EQ
(tiny[
"
qty
"
],
"
0.000000001
"
);
}
TEST
(OrderRequest, BracketOrderCarriesBothChildLegs) {
const
auto
j =
order_request::bracket
(
order_request::limit
(
"
AAPL
"
,
10
, order_side::buy,
100
, time_in_force::gtc),
110.0
,
//
take profit
90.0
//
stop loss
).
to_json
();
EXPECT_EQ
(j[
"
order_class
"
],
"
bracket
"
);
EXPECT_EQ
(j[
"
limit_price
"
],
"
100
"
);
ASSERT_TRUE
(j.
contains
(
"
take_profit
"
));
EXPECT_EQ
(j[
"
take_profit
"
][
"
limit_price
"
],
"
110
"
);
ASSERT_TRUE
(j.
contains
(
"
stop_loss
"
));
EXPECT_EQ
(j[
"
stop_loss
"
][
"
stop_price
"
],
"
90
"
);
EXPECT_FALSE
(j[
"
stop_loss
"
].
contains
(
"
limit_price
"
));
}
TEST
(OrderRequest, BracketWithStopLimitChild) {
const
auto
j =
order_request::bracket
(
order_request::limit
(
"
AAPL
"
,
10
, order_side::buy,
100
),
110.0
,
90.0
,
89.0
).
to_json
();
EXPECT_EQ
(j[
"
stop_loss
"
][
"
stop_price
"
],
"
90
"
);
EXPECT_EQ
(j[
"
stop_loss
"
][
"
limit_price
"
],
"
89
"
);
}
TEST
(OrderRequest, MultiLegOmitsTopLevelSymbolAndSide) {
//
mleg orders carry symbol/side per leg; sending a top-level symbol makes Alpaca reject them.
const
auto
j =
order_request::multi_leg
({
{
"
AAPL240628C00200000
"
,
1
, order_side::buy, position_intent::buy_to_open},
{
"
AAPL240628C00210000
"
,
1
, order_side::sell, position_intent::sell_to_open},
},
1
).
to_json
();
EXPECT_EQ
(j[
"
order_class
"
],
"
mleg
"
);
EXPECT_FALSE
(j.
contains
(
"
symbol
"
));
EXPECT_FALSE
(j.
contains
(
"
side
"
));
EXPECT_EQ
(j[
"
qty
"
],
"
1
"
);
ASSERT_TRUE
(j.
contains
(
"
legs
"
));
ASSERT_EQ
(j[
"
legs
"
].
size
(),
2u
);
EXPECT_EQ
(j[
"
legs
"
][
0
][
"
symbol
"
],
"
AAPL240628C00200000
"
);
EXPECT_EQ
(j[
"
legs
"
][
0
][
"
ratio_qty
"
],
"
1
"
);
EXPECT_EQ
(j[
"
legs
"
][
0
][
"
side
"
],
"
buy
"
);
EXPECT_EQ
(j[
"
legs
"
][
0
][
"
position_intent
"
],
"
buy_to_open
"
);
EXPECT_EQ
(j[
"
legs
"
][
1
][
"
side
"
],
"
sell
"
);
}
TEST
(OrderRequest, OptionalFlagsOnlyAppearWhenSet) {
order_request r =
order_request::limit
(
"
AAPL
"
,
1
, order_side::buy,
100
);
EXPECT_FALSE
(r.
to_json
().
contains
(
"
extended_hours
"
));
EXPECT_FALSE
(r.
to_json
().
contains
(
"
client_order_id
"
));
EXPECT_FALSE
(r.
to_json
().
contains
(
"
position_intent
"
));
r.
extended_hours
=
true
;
r.
client_order_id
=
"
my-id-123
"
;
r.
position_intent
= position_intent::buy_to_open;
const
auto
j = r.
to_json
();
EXPECT_EQ
(j[
"
extended_hours
"
],
true
);
EXPECT_EQ
(j[
"
client_order_id
"
],
"
my-id-123
"
);
EXPECT_EQ
(j[
"
position_intent
"
],
"
buy_to_open
"
);
}
TEST
(ReplaceOrderRequest, SerialisesOnlySetFields) {
replace_order_request r;
EXPECT_TRUE
(r.
to_json
().
empty
());
r.
qty
=
5
;
r.
limit_price
=
101.25
;
const
auto
j = r.
to_json
();
EXPECT_EQ
(j.
size
(),
2u
);
EXPECT_EQ
(j[
"
qty
"
],
"
5
"
);
EXPECT_EQ
(j[
"
limit_price
"
],
"
101.25
"
);
EXPECT_FALSE
(j.
contains
(
"
stop_price
"
));
}
TEST
(OrderQuery, BuildsFilters) {
order_query q;
q.
status
= order_status_filter::open;
q.
limit
=
100
;
q.
direction
= sort_direction::desc;
q.
nested
=
true
;
q.
symbols
= {
"
AAPL
"
,
"
MSFT
"
};
EXPECT_EQ
(q.
build
().
str
(),
"
?status=open&limit=100&direction=desc&nested=true&symbols=AAPL%2CMSFT
"
);
}
TEST
(OrderQuery, DefaultQueryIsEmpty) {
EXPECT_TRUE
(order_query{}.
build
().
empty
());
}
TEST
(ClosePositionRequest, BuildsQuantitySelector) {
close_position_request r;
EXPECT_TRUE
(r.
build
().
empty
());
r.
qty
=
5
;
EXPECT_EQ
(r.
build
().
str
(),
"
?qty=5
"
);
close_position_request pct;
pct.
percentage
=
50
;
EXPECT_EQ
(pct.
build
().
str
(),
"
?percentage=50
"
);
}
TEST
(WatchlistRequest, Serialises) {
const
watchlist_request r{
"
My List
"
, {
"
AAPL
"
,
"
MSFT
"
}};
const
auto
j = r.
to_json
();
EXPECT_EQ
(j[
"
name
"
],
"
My List
"
);
ASSERT_EQ
(j[
"
symbols
"
].
size
(),
2u
);
EXPECT_EQ
(j[
"
symbols
"
][
0
],
"
AAPL
"
);
}
TEST
(LocateRequest, Serialises) {
locate_request r;
r.
symbol
=
"
GME
"
;
r.
requested_qty
=
100
;
r.
limit_price
=
0.25
;
const
auto
j = r.
to_json
();
EXPECT_EQ
(j[
"
symbol
"
],
"
GME
"
);
EXPECT_EQ
(j[
"
requested_qty
"
],
100
);
EXPECT_EQ
(j[
"
limit_price
"
],
"
0.25
"
);
EXPECT_FALSE
(j.
contains
(
"
all_or_none
"
));
}
}
//
namespace alpaca::tests
Back
|
FazBrowse Home
|
New Git URL