algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
Loading...
Searching...
No Matches
timed_liquidation.hpp
1
#pragma once
2
3
#include "
../event.hpp
"
4
#include "
../intent.hpp
"
5
6
#include <algotradelib/core>
7
8
#include <cstddef>
9
#include <string>
10
#include <vector>
11
12
namespace
algotradelib
{
13
namespace
strategy
{
14
namespace
vanilla
{
15
28
struct
TimedLiquidation
{
30
struct
Params
{
32
std::vector<MarketPhase>
hold_through_
{
MarketPhase::OPEN
,
MarketPhase::PRE_OPEN
};
34
std::string
tag_
{
"timed-liquidation"
};
35
};
36
38
Params
params_
;
39
41
static
constexpr
std::size_t
warmup_bars
= 0;
42
49
template
<
class
Ctx>
50
void
on_phase
(Ctx& ctx,
MarketPhase
phase) {
51
if
(!holds_through(phase)) {
52
flatten(ctx);
53
}
54
}
55
56
private
:
57
// Whether `phase` is one this strategy may hold through.
58
bool
holds_through(
MarketPhase
phase)
const
{
59
for
(
const
auto
allowed :
params_
.
hold_through_
) {
60
if
(allowed == phase) {
61
return
true
;
62
}
63
}
64
return
false
;
65
}
66
67
// Targets zero in every instrument held, once per instrument (a holding may span several rows,
68
// and duplicate targets would each plan a sell). Working orders are left to reconciliation.
69
template
<
class
Ctx>
70
void
flatten(Ctx& ctx) {
71
std::vector<InstrumentId> targeted;
72
for
(
const
auto
& position : ctx.portfolio().positions_) {
73
if
(position.quantity_.coefficient() == 0) {
74
continue
;
75
}
76
bool
seen =
false
;
77
for
(
const
auto
& already : targeted) {
78
if
(already == position.instrument_) {
79
seen =
true
;
80
break
;
81
}
82
}
83
if
(seen) {
84
continue
;
85
}
86
targeted.push_back(position.instrument_);
87
88
TargetPosition target;
89
target.instrument_ = position.instrument_;
90
target.target_quantity_ =
Decimal64::parse
(
"0"
);
91
target.tag_ =
params_
.
tag_
;
92
ctx.submit(target);
93
}
94
}
95
};
96
97
}
// namespace vanilla
98
}
// namespace strategy
99
}
// namespace algotradelib
algotradelib::Decimal64::parse
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
Definition
types.hpp:323
event.hpp
The events a runner delivers to a strategy's handlers, and the market phase.
intent.hpp
What a strategy asks the runner for: orders (OrderIntent), target positions (TargetPosition) and canc...
algotradelib::strategy::vanilla
The strategies this library ships (buy and hold, crossovers, mean reversion, sizing,...
Definition
buy_and_hold.hpp:13
algotradelib::strategy
What a strategy sees and emits: events, intents, the portfolio, reconciliation and risk limits.
Definition
concept.hpp:16
algotradelib::strategy::MarketPhase
MarketPhase
Where the clock stands relative to the trading session.
Definition
event.hpp:30
algotradelib::strategy::MarketPhase::PRE_OPEN
@ PRE_OPEN
A trading day, before the session opens.
Definition
event.hpp:36
algotradelib::strategy::MarketPhase::OPEN
@ OPEN
Inside the session.
Definition
event.hpp:38
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib::strategy::vanilla::TimedLiquidation::Params
The strategy's settings.
Definition
timed_liquidation.hpp:30
algotradelib::strategy::vanilla::TimedLiquidation::Params::hold_through_
std::vector< MarketPhase > hold_through_
The phases in which positions may be held; entering any other phase flattens.
Definition
timed_liquidation.hpp:32
algotradelib::strategy::vanilla::TimedLiquidation::Params::tag_
std::string tag_
The tag on the targets.
Definition
timed_liquidation.hpp:34
algotradelib::strategy::vanilla::TimedLiquidation
Flattens every position when the market enters a phase it should not hold through.
Definition
timed_liquidation.hpp:28
algotradelib::strategy::vanilla::TimedLiquidation::params_
Params params_
The settings.
Definition
timed_liquidation.hpp:38
algotradelib::strategy::vanilla::TimedLiquidation::on_phase
void on_phase(Ctx &ctx, MarketPhase phase)
Targets zero in every held instrument if phase is not in hold_through_.
Definition
timed_liquidation.hpp:50
algotradelib::strategy::vanilla::TimedLiquidation::warmup_bars
static constexpr std::size_t warmup_bars
No history is needed.
Definition
timed_liquidation.hpp:41
algotradelib
src
strategy
vanilla
timed_liquidation.hpp
Generated by
1.18.0