Skip to main content

Signal

Struct Signal 

Source
#[non_exhaustive]
pub struct Signal {
Show 13 fields pub direction: SignalDirection, pub strength: SignalStrength, pub timestamp: i64, pub price: f64, pub reason: Option<String>, pub metadata: Option<SignalMetadata>, pub tags: Vec<String>, pub scale_fraction: Option<f64>, pub order_type: OrderType, pub expires_in_bars: Option<usize>, pub bracket_stop_loss_pct: Option<f64>, pub bracket_take_profit_pct: Option<f64>, pub bracket_trailing_stop_pct: Option<f64>,
}
Expand description

A trading signal generated by a strategy

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§direction: SignalDirection

Signal direction

§strength: SignalStrength

Signal strength/confidence

§timestamp: i64

Timestamp when signal was generated

§price: f64

Price at signal generation

§reason: Option<String>

Optional reason/description

§metadata: Option<SignalMetadata>

Strategy-specific metadata (indicator values, etc.)

§tags: Vec<String>

User-defined tags for post-hoc trade subgroup analysis.

Tags are propagated to [Trade::tags] when a position closes, enabling BacktestResult::trades_by_tag and metrics_by_tag queries. Use the .tag() builder to attach tags at signal creation time.

§scale_fraction: Option<f64>

Fraction for SignalDirection::ScaleIn and SignalDirection::ScaleOut signals.

  • ScaleIn: fraction of current portfolio equity to add to the position (0.0..=1.0).
  • ScaleOut: fraction of current position quantity to close (0.0..=1.0).

None for all other signal directions. Set via Signal::scale_in / Signal::scale_out constructors.

§order_type: OrderType

Order type controlling how this signal’s entry is executed.

OrderType::Market (default) fills at next bar’s open. Limit and stop types queue the order as a PendingOrder and fill when the bar’s high/low reaches the specified price level.

Only meaningful for SignalDirection::Long and SignalDirection::Short signals; ignored for Exit / ScaleIn / ScaleOut / Hold.

§expires_in_bars: Option<usize>

Expiry for pending limit/stop orders, measured in bars.

When set, the pending order is cancelled if not filled within this many bars after it is placed. None means Good-Till-Cancelled.

Only relevant when Signal::order_type is not OrderType::Market.

§bracket_stop_loss_pct: Option<f64>

Per-trade stop-loss percentage override (0.0 – 1.0).

When set on an entry signal (SignalDirection::Long or SignalDirection::Short), this value is stored on the resulting Position and takes precedence over BacktestConfig::stop_loss_pct for the lifetime of that position.

Set via the .stop_loss(pct) builder method.

§bracket_take_profit_pct: Option<f64>

Per-trade take-profit percentage override (0.0 – 1.0).

When set on an entry signal, stored on the resulting Position and takes precedence over BacktestConfig::take_profit_pct.

Set via the .take_profit(pct) builder method.

§bracket_trailing_stop_pct: Option<f64>

Per-trade trailing stop percentage override (0.0 – 1.0).

When set on an entry signal, stored on the resulting Position and takes precedence over BacktestConfig::trailing_stop_pct.

Set via the .trailing_stop(pct) builder method.

Implementations§

Source§

impl Signal

Source

pub fn long(timestamp: i64, price: f64) -> Self

Create a long signal

Source

pub fn short(timestamp: i64, price: f64) -> Self

Create a short signal

Source

pub fn exit(timestamp: i64, price: f64) -> Self

Create an exit signal

Source

pub fn hold() -> Self

Create a hold signal (no action)

Source

pub fn is_hold(&self) -> bool

Check if this is a hold signal

Source

pub fn is_entry(&self) -> bool

Check if this is an entry signal (Long or Short)

Source

pub fn is_exit(&self) -> bool

Check if this is an exit signal

Source

pub fn is_scaling(&self) -> bool

Check if this is a scaling signal (scale-in or scale-out)

Source

pub fn scale_in(fraction: f64, timestamp: i64, price: f64) -> Self

Create a scale-in signal — add to an existing position.

fraction is the portion of current portfolio equity to allocate to the additional shares. Must be in 0.0..=1.0; values outside this range are clamped by the engine. Has no effect if no position is currently open.

§Example
use finance_query::backtesting::Signal;

// In a custom Strategy::on_candle implementation:
// Add 10% of current equity to the existing long position.
let signal = Signal::scale_in(0.10, ctx_timestamp, ctx_price);
Source

pub fn scale_out(fraction: f64, timestamp: i64, price: f64) -> Self

Create a scale-out signal — partially exit an existing position.

fraction is the portion of the current position quantity to close. Must be in 0.0..=1.0; values outside this range are clamped. A fraction of 1.0 closes the entire position (equivalent to Signal::exit). Has no effect if no position is currently open.

§Example
use finance_query::backtesting::Signal;

// In a custom Strategy::on_candle implementation:
// Close half the current position to lock in partial profits.
let signal = Signal::scale_out(0.50, ctx_timestamp, ctx_price);
Source

pub fn buy_limit(timestamp: i64, price: f64, limit_price: f64) -> Self

Create a limit buy order — enter long when price pulls back to limit_price.

The order is queued as a PendingOrder and fills on the first subsequent bar where candle.low ≤ limit_price. Fill price is limit_price, or the bar’s open if a gap-down open is already below the limit (realistic gap fill).

§Example
use finance_query::backtesting::Signal;

// Buy if price dips to 98 within the next 5 bars.
let signal = Signal::buy_limit(ts, close, 98.0).expires_in_bars(5);
Source

pub fn buy_stop(timestamp: i64, price: f64, stop_price: f64) -> Self

Create a stop buy order (breakout entry) — enter long when price breaks above stop_price.

Fills on the first subsequent bar where candle.high ≥ stop_price. Fill price is stop_price, or the bar’s open if a gap-up open is already above the stop (open price used instead).

§Example
use finance_query::backtesting::Signal;

// Enter long on a breakout above 105.
let signal = Signal::buy_stop(ts, close, 105.0);
Source

pub fn sell_limit(timestamp: i64, price: f64, limit_price: f64) -> Self

Create a limit sell order — enter short when price rallies to limit_price.

Fills on the first subsequent bar where candle.high ≥ limit_price. Fill price is limit_price, or the bar’s open if a gap-up open is already at or above the limit.

§Example
use finance_query::backtesting::Signal;

// Short into a rally reaching 103.
let signal = Signal::sell_limit(ts, close, 103.0).expires_in_bars(10);
Source

pub fn sell_stop(timestamp: i64, price: f64, stop_price: f64) -> Self

Create a stop sell order (breakdown entry) — enter short when price breaks below stop_price.

Fills on the first subsequent bar where candle.low ≤ stop_price. Fill price is stop_price, or the bar’s open if a gap-down open is already below the stop.

§Example
use finance_query::backtesting::Signal;

// Short on a breakdown below 95.
let signal = Signal::sell_stop(ts, close, 95.0);
Source

pub fn buy_stop_limit( timestamp: i64, price: f64, stop_price: f64, limit_price: f64, ) -> Self

Create a stop-limit buy order — triggered by a breakout above stop_price but capped at limit_price.

Fills when candle.high ≥ stop_price and the computed trigger price (bar open or stop_price, whichever is higher) does not exceed limit_price. If the bar gaps up above limit_price, the order cannot fill that bar and remains pending.

§Example
use finance_query::backtesting::Signal;

// Breakout buy above 105, but reject fills above 107.
let signal = Signal::buy_stop_limit(ts, close, 105.0, 107.0);
Source

pub fn expires_in_bars(self, bars: usize) -> Self

Set an expiry (in bars) for this pending limit/stop order.

When the order is not filled within bars bars after being placed, it is automatically cancelled. Has no effect on OrderType::Market signals.

§Example
use finance_query::backtesting::Signal;

// Day order: fill or cancel after 1 bar.
let signal = Signal::buy_limit(ts, close, 98.0).expires_in_bars(1);
Source

pub fn with_strength(self, strength: SignalStrength) -> Self

Set signal strength

Source

pub fn with_reason(self, reason: impl Into<String>) -> Self

Set reason/description

Source

pub fn with_metadata(self, metadata: SignalMetadata) -> Self

Set metadata

Source

pub fn tag(self, name: impl Into<String>) -> Self

Attach a tag to this signal for post-hoc trade subgroup analysis.

Tags are propagated to [Trade::tags] when the position closes, enabling BacktestResult::trades_by_tag and metrics_by_tag queries. Multiple tags can be chained: .tag("breakout").tag("high_volume").

Source

pub fn stop_loss(self, pct: f64) -> Self

Attach a per-trade stop-loss to this entry signal.

pct is the loss fraction relative to the entry price (0.0..=1.0). When set, the resulting position’s stop-loss overrides BacktestConfig::stop_loss_pct for the lifetime of that trade.

§Example
use finance_query::backtesting::Signal;

// Stop out at -5% from entry, regardless of the config default.
let signal = Signal::long(ts, price).stop_loss(0.05);
Source

pub fn take_profit(self, pct: f64) -> Self

Attach a per-trade take-profit to this entry signal.

pct is the profit fraction relative to the entry price (0.0..=1.0). When set, the resulting position’s take-profit overrides BacktestConfig::take_profit_pct for the lifetime of that trade.

§Example
use finance_query::backtesting::Signal;

// Take profit at +15% from entry.
let signal = Signal::long(ts, price).take_profit(0.15);
Source

pub fn trailing_stop(self, pct: f64) -> Self

Attach a per-trade trailing stop to this entry signal.

pct is the trail fraction from the position’s peak/trough price (0.0..=1.0). When set, the resulting position’s trailing stop overrides BacktestConfig::trailing_stop_pct for the lifetime of that trade.

§Example
use finance_query::backtesting::Signal;

// Exit if price drops 3% from its peak since entry.
let signal = Signal::long(ts, price).trailing_stop(0.03);

Trait Implementations§

Source§

impl Clone for Signal

Source§

fn clone(&self) -> Signal

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Signal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Signal

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Signal

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Signal

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Key for T
where T: Clone,

Source§

fn align() -> usize

The alignment necessary for the key. Must return a power of two.
Source§

fn size(&self) -> usize

The size of the key in bytes.
Source§

unsafe fn init(&self, ptr: *mut u8)

Initialize the key in the given memory location. Read more
Source§

unsafe fn get<'a>(ptr: *const u8) -> &'a T

Get a reference to the key from the given memory location. Read more
Source§

unsafe fn drop_in_place(ptr: *mut u8)

Drop the key in place. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> PlanCallbackArgs for T

Source§

impl<T> PlanCallbackOut for T