11#if (defined(_MSVC_LANG) ? _MSVC_LANG : __cplusplus) < 201703L
13 "OpenPine C++ wrapper headers require C++17 (std::optional, std::string_view)."
66 : ptr_(ffi::candlestickStreamCreate(
67 reinterpret_cast<const uint8_t*>(symbol.data()), symbol.size(),
68 reinterpret_cast<const uint8_t*>(timeframe.data()),
69 timeframe.size(), from_time)) {}
79 void push(
const CandlestickItem& item) {
80 std::unique_ptr<ffi::Result,
decltype(&ffi::resultDelete)> result{
81 ffi::candlestickStreamPush(ptr_, &item), ffi::resultDelete};
82 if (!ffi::resultIsSuccess(&*result))
83 throw Error{ffi::resultErrorMessage(&*result),
84 ffi::resultErrorSpan(&*result)};
90 std::unique_ptr<ffi::Result,
decltype(&ffi::resultDelete)> result{
91 ffi::candlestickStreamFinish(ptr_), ffi::resultDelete};
112 ffi::CandlestickStream*
handle()
const {
return ptr_; }
115 ffi::CandlestickStream* ptr_;
162 virtual PartialSymbolInfo
symbolInfo(std::string_view symbol) = 0;
173 std::string_view timeframe,
174 int64_t from_time) = 0;
187 const std::string& symbol,
188 const std::optional<std::string>& path = std::nullopt)
190 timeframe_(timeframe),
193 background_color_(0xff),
194 last_info_(std::nullopt) {}
200 void setLastInfo(
const LastInfo& last_info) { last_info_ = last_info; }
219 std::optional<std::string> path_;
220 std::string timeframe_;
222 uint32_t background_color_;
223 std::optional<LastInfo> last_info_;
224 DataProvider* data_provider_ =
nullptr;
225 ffi::COutputMode output_mode_ = ffi::COutputMode::Chart;
264 return ScriptInfo{ffi::instanceScriptInfo(&*p_)};
283 void run(std::string_view symbol, std::string_view timeframe,
291 explicit Instance(ffi::Instance* p) : p_(p, ffi::instanceDelete) {}
293 std::unique_ptr<ffi::Instance,
decltype(&ffi::instanceDelete)> p_{
294 nullptr, ffi::instanceDelete};
298 struct ProviderTrampoline {
299 DataProvider* provider;
301 static const char* symbol_info_fn(
void* ctx,
const uint8_t* sym,
303 ffi::PartialSymbolInfo* out) {
304 auto* t =
static_cast<ProviderTrampoline*
>(ctx);
305 std::string_view sv(
reinterpret_cast<const char*
>(sym), sym_len);
307 *out = t->provider->symbolInfo(sv);
309 }
catch (
const std::exception& e) {
310 static thread_local std::string buf;
316 static ffi::CandlestickStream* candlesticks_open_fn(
317 void* ctx,
const uint8_t* sym,
size_t sym_len,
const uint8_t* tf,
318 size_t tf_len, int64_t from_time) {
319 auto* t =
static_cast<ProviderTrampoline*
>(ctx);
320 std::string_view sv(
reinterpret_cast<const char*
>(sym), sym_len);
321 std::string_view tfv(
reinterpret_cast<const char*
>(tf), tf_len);
323 return t->provider->candlesticksOpen(sv, tfv, from_time);
330 std::unique_ptr<ProviderTrampoline> trampoline_;
331 std::optional<ffi::DataProvider> ffi_provider_;
339 const ffi::PineStringRef* path_ptr =
nullptr;
340 ffi::PineStringRef ffi_path;
342 ffi_path = {
reinterpret_cast<const uint8_t*
>(options.path_->data()),
343 options.path_->size()};
344 path_ptr = &ffi_path;
346 ffi::PineStringRef source{
347 reinterpret_cast<const uint8_t*
>(options.source_.data()),
348 options.source_.size()};
349 ffi::PineStringRef timeframe{
350 reinterpret_cast<const uint8_t*
>(options.timeframe_.data()),
351 options.timeframe_.size()};
352 ffi::PineStringRef symbol{
353 reinterpret_cast<const uint8_t*
>(options.symbol_.data()),
354 options.symbol_.size()};
358 const ffi::DataProvider* provider_ptr =
nullptr;
359 if (options.data_provider_) {
360 inst.trampoline_ = std::make_unique<ProviderTrampoline>();
361 inst.trampoline_->provider = options.data_provider_;
362 inst.ffi_provider_ = ffi::DataProvider{
363 static_cast<void*
>(inst.trampoline_.get()),
364 ProviderTrampoline::symbol_info_fn,
365 ProviderTrampoline::candlesticks_open_fn,
367 provider_ptr = &*inst.ffi_provider_;
370 ffi::CreateInstanceOptions ffi_opts{source,
374 &options.background_color_,
376 ? &*options.last_info_
379 options.output_mode_};
381 ffi::Instance* raw =
nullptr;
382 std::unique_ptr<ffi::Result,
decltype(&ffi::resultDelete)> result{
383 ffi::instanceCreate(&ffi_opts, &raw), ffi::resultDelete};
384 if (!ffi::resultIsSuccess(&*result))
385 throw Error{ffi::resultErrorMessage(&*result),
386 ffi::resultErrorSpan(&*result)};
396inline void Instance::run(std::string_view symbol, std::string_view timeframe,
405 auto* cbs =
new Callbacks{std::move(callback), std::move(event_callback)};
407 auto c_callback = [](
const ffi::Result* result,
void* ud)
noexcept {
408 auto* cbs =
static_cast<Callbacks*
>(ud);
410 cbs->run(Result{result});
417 ffi::InstanceEventCallback c_event_callback =
nullptr;
419 c_event_callback = [](
const ffi::CEvent* event,
void* ud)
noexcept {
420 auto* cbs =
static_cast<Callbacks*
>(ud);
428 std::unique_ptr<ffi::Result,
decltype(&ffi::resultDelete)> start_result{
431 reinterpret_cast<const uint8_t*
>(symbol.data()), symbol.size(),
432 reinterpret_cast<const uint8_t*
>(timeframe.data()), timeframe.size(),
433 from_time, c_event_callback, c_callback, cbs),
436 if (!ffi::resultIsSuccess(&*start_result)) {
438 throw Error{ffi::resultErrorMessage(&*start_result),
439 ffi::resultErrorSpan(&*start_result)};
C++ wrappers for chart/visual output produced by the VM.
CandlestickStream(const CandlestickStream &)=delete
void finish()
Signals end-of-stream (thread-safe). Idempotent.
CandlestickStream & operator=(const CandlestickStream &)=delete
void push(const CandlestickItem &item)
Pushes one bar into the stream (thread-safe).
ffi::CandlestickStream * release()
Returns the underlying raw handle and releases ownership.
ffi::CandlestickStream * handle() const
Returns the raw handle without releasing ownership.
CandlestickStream(std::string_view symbol, std::string_view timeframe, int64_t from_time)
Creates a new stream.
Chart data produced by the VM.
Options for creating an Instance.
void setBackgroundColor(uint32_t color)
Sets the background color (0xAARRGGBB).
void setDataProvider(DataProvider *provider)
Injects a DataProvider.
void setLastInfo(const LastInfo &last_info)
Sets last-bar info.
CreateInstanceOptions(const std::string &source, const std::string &timeframe, const std::string &symbol, const std::optional< std::string > &path=std::nullopt)
void setOutputMode(ffi::COutputMode mode)
Sets the output mode.
virtual ffi::CandlestickStream * candlesticksOpen(std::string_view symbol, std::string_view timeframe, int64_t from_time)=0
Opens a candlestick stream.
virtual PartialSymbolInfo symbolInfo(std::string_view symbol)=0
Returns symbol metadata. Throw on failure.
virtual ~DataProvider()=default
Represents an error that occurred during script compilation or execution.
static Instance create(const CreateInstanceOptions &options)
Creates a new Instance.
Chart chart() const
Returns the chart produced by the last completed run().
std::function< void(const ffi::CEvent &)> EventCallback
Callback invoked for each event during run().
void run(std::string_view symbol, std::string_view timeframe, int64_t from_time, RunCallback callback, EventCallback event_callback=nullptr)
Starts asynchronous execution and returns immediately.
Instance & operator=(Instance &&)=default
ScriptInfo scriptInfo() const
Returns the script metadata.
Instance(Instance &&)=default
std::function< void(const Result &)> RunCallback
Callback invoked when run() completes.
Script metadata (type, overlay, title, inputs, alerts) produced by the VM.
Error type used by the C++ wrapper API.
C++ wrapper for script metadata (ScriptInfo) from the OpenPine VM.
Re-exports FFI types into the openpine namespace.