-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
229 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// TheoPrice.swift | ||
// DXFeedFramework | ||
// | ||
// Created by Aleksey Kosylo on 06.10.23. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Theo price is a snapshot of the theoretical option price computation that is | ||
/// periodically performed by http://www.devexperts.com/en/products/price.html | ||
/// | ||
/// model-free computation. | ||
/// It represents the most recent information that is available about the corresponding | ||
/// values at any given moment of time. | ||
/// The values include first and second order derivative of the price curve by price, so that | ||
/// the real-time theoretical option price can be estimated on real-time changes of the underlying | ||
/// price in the vicinity. | ||
/// | ||
/// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/option/TheoPrice.html) | ||
public class TheoPrice: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStringConvertible { | ||
public var type: EventCode = .theoPrice | ||
|
||
public var eventSymbol: String | ||
|
||
public var eventTime: Int64 = 0 | ||
|
||
public var eventSource: IndexedEventSource = .defaultSource | ||
|
||
public var eventFlags: Int32 = 0 | ||
|
||
public var index: Long = 0 | ||
|
||
/* | ||
* EventFlags property has several significant bits that are packed into an integer in the following way: | ||
* 31..7 6 5 4 3 2 1 0 | ||
* +---------+----+----+----+----+----+----+----+ | ||
* | | SM | | SS | SE | SB | RE | TX | | ||
* +---------+----+----+----+----+----+----+----+ | ||
*/ | ||
|
||
/// Gets or sets theoretical option price. | ||
public var price: Double = .nan | ||
/// Gets or sets underlying price at the time of theo price computation. | ||
public var underlyingPrice: Double = .nan | ||
/// Gets or sets delta of the theoretical price. | ||
/// Delta is the first derivative of an option price by an underlying price. | ||
public var delta: Double = .nan | ||
/// Gets or sets gamma of the theoretical price. | ||
/// Gamma is the second derivative of an option price by an underlying price. | ||
public var gamma: Double = .nan | ||
/// Gets or sets implied simple dividend return of the corresponding option series. | ||
public var dividend: Double = .nan | ||
/// Gets or sets implied simple interest return of the corresponding option series. | ||
public var interest: Double = .nan | ||
|
||
public init(_ eventSymbol: String) { | ||
self.eventSymbol = eventSymbol | ||
} | ||
|
||
public var description: String { | ||
""" | ||
DXFG_THEO_PRICE_T \ | ||
eventSymbol: \(eventSymbol) \ | ||
eventTime: \(eventTime) \ | ||
eventFlags: \(eventFlags), \ | ||
index: \(index), \ | ||
price: \(price), \ | ||
underlyingPrice: \(underlyingPrice), \ | ||
delta: \(delta), \ | ||
gamma: \(gamma), \ | ||
dividend: \(dividend), \ | ||
interest: \(interest) | ||
""" | ||
} | ||
} | ||
|
||
extension TheoPrice { | ||
/// Gets or sets timestamp of the event in milliseconds. | ||
/// Time is measured in milliseconds between the current time and midnight, January 1, 1970 UTC. | ||
public var time: Long { | ||
get { | ||
((index >> 32) * 1000) + ((index >> 22) & 0x3ff) | ||
} | ||
set { | ||
index = Long(TimeUtil.getSecondsFromTime(newValue) << 32) | | ||
(Long(TimeUtil.getMillisFromTime(newValue)) << 22) | | ||
Int64(getSequence()) | ||
} | ||
} | ||
/// Gets sequence number of this quote to distinguish events that have the same ``time``. | ||
/// This sequence number does not have to be unique and | ||
/// does not need to be sequential. Sequence can range from 0 to ``MarketEventConst/maxSequence``. | ||
public func getSequence() -> Int { | ||
return Int(index) & Int(MarketEventConst.maxSequence) | ||
} | ||
/// Sets sequence number of this quote to distinguish quotes that have the same ``time``. | ||
/// This sequence number does not have to be unique and | ||
/// does not need to be sequential. Sequence can range from 0 to ``MarketEventConst/maxSequence``. | ||
/// - Throws: ``ArgumentException/exception(_:)`` | ||
public func setSequence(_ sequence: Int) throws { | ||
if sequence < 0 || sequence > MarketEventConst.maxSequence { | ||
throw ArgumentException.exception( | ||
"Sequence(\(sequence) is < 0 or > MaxSequence(\(MarketEventConst.maxSequence)" | ||
) | ||
} | ||
index = Long(index & ~Long(MarketEventConst.maxSequence)) | Long(sequence) | ||
} | ||
/// Returns string representation of this order fields. | ||
func toString() -> String { | ||
return """ | ||
TheoPrice{\(eventSymbol) \ | ||
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \ | ||
eventFlags=0x\(String(format: "%02X", eventFlags)), \ | ||
time=\(TimeUtil.toLocalDateString(millis: time)), \ | ||
sequence=\(self.getSequence()), \ | ||
price=\(price) \ | ||
underlyingPrice=\(underlyingPrice), \ | ||
delta=\(delta), \ | ||
gamma=\(gamma), \ | ||
dividend=\(dividend), \ | ||
interest=\(interest), \ | ||
} | ||
""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// TheoPrice+Ext.swift | ||
// DXFeedFramework | ||
// | ||
// Created by Aleksey Kosylo on 06.10.23. | ||
// | ||
|
||
import Foundation | ||
@_implementationOnly import graal_api | ||
|
||
extension TheoPrice { | ||
convenience init(native: dxfg_theo_price_t) { | ||
self.init(String(pointee: native.market_event.event_symbol)) | ||
|
||
self.eventTime = native.market_event.event_time | ||
self.eventFlags = native.event_flags | ||
self.index = native.index | ||
|
||
self.price = native.price | ||
self.underlyingPrice = native.underlying_price | ||
self.delta = native.delta | ||
self.gamma = native.gamma | ||
self.dividend = native.dividend | ||
self.interest = native.interest | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
DXFeedFramework/Native/Events/Markets/TheoPriceMapper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// TheoPriceMapper.swift | ||
// DXFeedFramework | ||
// | ||
// Created by Aleksey Kosylo on 06.10.23. | ||
// | ||
|
||
import Foundation | ||
@_implementationOnly import graal_api | ||
|
||
class TheoPriceMapper: Mapper { | ||
let type = dxfg_theo_price_t.self | ||
|
||
func fromNative(native: UnsafeMutablePointer<dxfg_event_type_t>) -> MarketEvent? { | ||
let event = native.withMemoryRebound(to: type, capacity: 1) { native in | ||
return TheoPrice(native: native.pointee) | ||
} | ||
return event | ||
} | ||
|
||
func toNative(event: MarketEvent) -> UnsafeMutablePointer<dxfg_event_type_t>? { | ||
let pointer = UnsafeMutablePointer<dxfg_theo_price_t>.allocate(capacity: 1) | ||
var pointee = pointer.pointee | ||
pointee.market_event.event_symbol = event.eventSymbol.toCStringRef() | ||
pointee.market_event.event_time = event.eventTime | ||
|
||
let theoPrice = event.theoPrice | ||
|
||
pointee.event_flags = theoPrice.eventFlags | ||
pointee.index = theoPrice.index | ||
pointee.price = theoPrice.price | ||
pointee.underlying_price = theoPrice.underlyingPrice | ||
pointee.delta = theoPrice.delta | ||
pointee.gamma = theoPrice.gamma | ||
pointee.dividend = theoPrice.dividend | ||
pointee.interest = theoPrice.interest | ||
|
||
let eventType = pointer.withMemoryRebound(to: dxfg_event_type_t.self, capacity: 1) { pointer in | ||
pointer.pointee.clazz = DXFG_EVENT_THEO_PRICE | ||
return pointer | ||
} | ||
return eventType | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters