|
| 1 | +using Core.Enums; |
| 2 | +using Core.Grains; |
| 3 | +using Core.Models; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Tradier.Messages.MarketData; |
| 8 | +using Tradier.Models; |
| 9 | +using Tradier.Queries.MarketData; |
| 10 | + |
| 11 | +namespace Tradier.Grains |
| 12 | +{ |
| 13 | + public interface ITradierOptionsGrain : IOptionsGrain |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Connect |
| 17 | + /// </summary> |
| 18 | + /// <param name="connection"></param> |
| 19 | + Task<StatusResponse> Setup(Connection connection); |
| 20 | + } |
| 21 | + |
| 22 | + public class TradierOptionsGrain : OptionsGrain, ITradierOptionsGrain |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// State |
| 26 | + /// </summary> |
| 27 | + protected Connection state; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Connector |
| 31 | + /// </summary> |
| 32 | + protected TradierBroker connector; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Connect |
| 36 | + /// </summary> |
| 37 | + /// <param name="connection"></param> |
| 38 | + public virtual async Task<StatusResponse> Setup(Connection connection) |
| 39 | + { |
| 40 | + var cleaner = new CancellationTokenSource(connection.Timeout); |
| 41 | + |
| 42 | + state = connection; |
| 43 | + connector = new() |
| 44 | + { |
| 45 | + Token = connection.AccessToken, |
| 46 | + SessionToken = connection.SessionToken, |
| 47 | + }; |
| 48 | + |
| 49 | + await connector.Connect(cleaner.Token); |
| 50 | + |
| 51 | + return new() |
| 52 | + { |
| 53 | + Data = StatusEnum.Active |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Option chain |
| 59 | + /// </summary> |
| 60 | + /// <param name="criteria"></param> |
| 61 | + public override async Task<InstrumentsResponse> Options(Criteria criteria) |
| 62 | + { |
| 63 | + var query = new OptionChainRequest |
| 64 | + { |
| 65 | + Symbol = criteria.Instrument.Name, |
| 66 | + Expiration = criteria.MaxDate ?? criteria.MinDate |
| 67 | + }; |
| 68 | + |
| 69 | + var cleaner = new CancellationTokenSource(state.Timeout); |
| 70 | + var chain = await connector.GetOptionChain(query, cleaner.Token); |
| 71 | + var options = chain |
| 72 | + .Options |
| 73 | + ?.Select(MapOption) |
| 74 | + ?.OrderBy(o => o.Derivative.ExpirationDate) |
| 75 | + ?.ThenBy(o => o.Derivative.Strike) |
| 76 | + ?.ThenBy(o => o.Derivative.Side) |
| 77 | + ?.ToList() ?? []; |
| 78 | + |
| 79 | + return new() |
| 80 | + { |
| 81 | + Data = options |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Get internal option |
| 87 | + /// </summary> |
| 88 | + /// <param name="message"></param> |
| 89 | + protected virtual Instrument MapOption(OptionMessage message) |
| 90 | + { |
| 91 | + var instrument = new Instrument |
| 92 | + { |
| 93 | + Name = message.Underlying, |
| 94 | + Exchange = message.Exchange |
| 95 | + }; |
| 96 | + |
| 97 | + var optionPoint = new Price |
| 98 | + { |
| 99 | + Ask = message.Ask, |
| 100 | + Bid = message.Bid, |
| 101 | + AskSize = message.AskSize ?? 0, |
| 102 | + BidSize = message.BidSize ?? 0, |
| 103 | + Volume = message.Volume, |
| 104 | + Last = message.Last, |
| 105 | + Bar = new() |
| 106 | + { |
| 107 | + Low = message.Low, |
| 108 | + High = message.High, |
| 109 | + Open = message.Open, |
| 110 | + Close = message.Close |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + var optionInstrument = new Instrument |
| 115 | + { |
| 116 | + Basis = instrument, |
| 117 | + Price = optionPoint, |
| 118 | + Name = message.Symbol, |
| 119 | + Exchange = message.Exchange, |
| 120 | + Leverage = message.ContractSize ?? 100, |
| 121 | + Type = GetInstrumentType(message.Type) |
| 122 | + }; |
| 123 | + |
| 124 | + var side = null as OptionSideEnum?; |
| 125 | + |
| 126 | + switch (message.OptionType.ToUpper()) |
| 127 | + { |
| 128 | + case "PUT": side = OptionSideEnum.Put; break; |
| 129 | + case "CALL": side = OptionSideEnum.Call; break; |
| 130 | + } |
| 131 | + |
| 132 | + var derivative = new Derivative |
| 133 | + { |
| 134 | + Side = side, |
| 135 | + Strike = message.Strike, |
| 136 | + TradeDate = message.ExpirationDate, |
| 137 | + ExpirationDate = message.ExpirationDate, |
| 138 | + OpenInterest = message.OpenInterest ?? 0, |
| 139 | + Volatility = message?.Greeks?.SmvIV ?? 0, |
| 140 | + }; |
| 141 | + |
| 142 | + var greeks = message?.Greeks; |
| 143 | + |
| 144 | + if (greeks is not null) |
| 145 | + { |
| 146 | + derivative = derivative with |
| 147 | + { |
| 148 | + Variance = new Variance |
| 149 | + { |
| 150 | + Rho = greeks.Rho ?? 0, |
| 151 | + Vega = greeks.Vega ?? 0, |
| 152 | + Delta = greeks.Delta ?? 0, |
| 153 | + Gamma = greeks.Gamma ?? 0, |
| 154 | + Theta = greeks.Theta ?? 0 |
| 155 | + } |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + optionInstrument = optionInstrument with |
| 160 | + { |
| 161 | + Price = optionPoint, |
| 162 | + Derivative = derivative |
| 163 | + }; |
| 164 | + |
| 165 | + return optionInstrument; |
| 166 | + } |
| 167 | + |
| 168 | + /// <summary> |
| 169 | + /// Asset type |
| 170 | + /// </summary> |
| 171 | + /// <param name="assetType"></param> |
| 172 | + protected virtual InstrumentEnum? GetInstrumentType(string assetType) |
| 173 | + { |
| 174 | + switch (assetType?.ToUpper()) |
| 175 | + { |
| 176 | + case "EQUITY": return InstrumentEnum.Shares; |
| 177 | + case "INDEX": return InstrumentEnum.Indices; |
| 178 | + case "FUTURE": return InstrumentEnum.Futures; |
| 179 | + case "OPTION": return InstrumentEnum.Options; |
| 180 | + } |
| 181 | + |
| 182 | + return InstrumentEnum.Group; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments