Skip to content

Commit

Permalink
Gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Nov 7, 2024
1 parent 86e7290 commit 396213a
Show file tree
Hide file tree
Showing 21 changed files with 688 additions and 163 deletions.
13 changes: 7 additions & 6 deletions Core/Models/Instruments/OptionScreenerModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Terminal.Core.Domains;
using Terminal.Core.Enums;

namespace Terminal.Core.Models
Expand All @@ -10,11 +11,6 @@ public class OptionScreenerModel
/// </summary>
public virtual int? Count { get; set; }

/// <summary>
/// Symbol name
/// </summary>
public virtual string Name { get; set; }

/// <summary>
/// Min strike
/// </summary>
Expand All @@ -41,8 +37,13 @@ public class OptionScreenerModel
public virtual OptionSideEnum? Side { get; set; }

/// <summary>
/// Symbol data
/// Current quote
/// </summary>
public virtual PointModel Point { get; set; }

/// <summary>
/// Instrument
/// </summary>
public virtual InstrumentModel Instrument { get; set; }
}
}
42 changes: 24 additions & 18 deletions Gateway/Alpaca/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Distribution.Stream.Extensions;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand All @@ -19,6 +18,7 @@
using Terminal.Core.Enums;
using Terminal.Core.Extensions;
using Terminal.Core.Models;
using A = Alpaca.Markets;

namespace Alpaca
{
Expand Down Expand Up @@ -87,13 +87,6 @@ public override async Task<ResponseModel<StatusEnum>> Connect()
_sender = new Service();
_streamer = await GetConnection(ws, scheduler);

await SendStream(_streamer, new AuthenticationMessage
{
Action = "auth",
SecretKey = ConsumerSecret,
KeyId = ConsumerKey
});

await GetAccount([]);

_connections.Add(_sender);
Expand Down Expand Up @@ -289,13 +282,13 @@ public override async Task<ResponseModel<IList<InstrumentModel>>> GetOptions(Opt
{
var props = new Hashtable
{
["underlying_symbol"] = screener.Name,
["expiration_date_gte"] = screener.MinDate,
["expiration_date_lte"] = screener.MaxDate
["expiration_date_lte"] = screener.MaxDate,
["underlying_symbols"] = screener.Instrument.Name

}.Merge(criteria);

var optionResponse = await SendData<DataMessage<OptionSnapshotMessage>>($"/v1beta1/options/snapshots/{props["underlying_symbol"]}?{props}");
var optionResponse = await SendData<DataMessage<OptionContractMessage>>($"/v2/options/contracts?{props}");

response.Data = optionResponse
.Data
Expand Down Expand Up @@ -340,8 +333,13 @@ public override async Task<ResponseModel<DomModel>> GetDom(DomScreenerModel scre
}

var pointResponse = await SendData<DataMessage<SnapshotMessage>>(source);
var point = InternalMap.GetPrice(pointResponse.Data.Quotes[props["symbols"]]);

response.Data = InternalMap.GetDom(pointResponse.Data.Quotes[props["symbols"]]);
response.Data = new DomModel
{
Asks = [point],
Bids = [point],
};
}
catch (Exception e)
{
Expand Down Expand Up @@ -460,17 +458,23 @@ protected virtual async Task<ClientWebSocket> GetConnection(ClientWebSocket ws,
var cancellation = new CancellationTokenSource();

await ws.ConnectAsync(source.Uri, cancellation.Token);
await SendStream(ws, new AuthenticationMessage
{
Action = "auth",
SecretKey = ConsumerSecret,
KeyId = ConsumerKey
});

scheduler.Send(async () =>
{
while (ws.State is WebSocketState.Open)
{
var data = new byte[byte.MaxValue];
var data = new ArraySegment<byte>(new byte[byte.MaxValue]);

await ws.ReceiveAsync(data, cancellation.Token).ContinueWith(async o =>
{
var response = await o;
var content = $"[{Encoding.ASCII.GetString(data).Trim(['\0', '[', ']'])}]";
var content = $"[{Encoding.ASCII.GetString(data.Array).Trim(['[', ']'])}]";
var message = JsonNode
.Parse(content)
?.AsArray()
Expand Down Expand Up @@ -601,13 +605,15 @@ protected virtual async Task<ResponseModel<OrderModel>> CreateOrder(OrderModel o
var exResponse = await SendData<OrderMessage>("/v2/orders", HttpMethod.Post, exOrder);

inResponse.Data = order;
inResponse.Data.Transaction.Id = exResponse.Data.OrderId;
inResponse.Data.Transaction.Status = InternalMap.GetStatus(exResponse.Data.OrderStatus);

if ((int)exResponse.Message.StatusCode < 400)
if (string.IsNullOrEmpty(exResponse.Data.OrderId))
{
Account.Orders[order.Id] = order;
inResponse.Errors.Add(new ErrorModel { ErrorMessage = $"{exResponse.Message.StatusCode}" });
return inResponse;
}

inResponse.Data.Transaction.Id = exResponse.Data.OrderId;
inResponse.Data.Transaction.Status = InternalMap.GetStatus(exResponse.Data.OrderStatus);
}
catch (Exception e)
{
Expand Down
1 change: 1 addition & 0 deletions Gateway/Alpaca/Libs/Alpaca.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Alpaca.Markets" Version="7.1.2" />
<PackageReference Include="Distribution.Stream" Version="1.2.2" />
</ItemGroup>

Expand Down
23 changes: 10 additions & 13 deletions Gateway/Alpaca/Libs/Maps/InternalMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class InternalMap
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static DomModel GetDom(QuoteMessage message)
public static PointModel GetPrice(QuoteMessage message)
{
var point = new PointModel
{
Expand All @@ -24,32 +24,29 @@ public static DomModel GetDom(QuoteMessage message)
Last = message.AskPrice ?? message.BidPrice,
};

return new DomModel
{
Asks = [point],
Bids = [point],
};
return point;
}

/// <summary>
/// Get option contract
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static InstrumentModel GetOption(OptionSnapshotMessage message)
public static InstrumentModel GetOption(OptionContractMessage message)
{
var point = new PointModel
{
Ask = message.Quote.AskPrice,
Bid = message.Quote.BidPrice,
AskSize = message.Quote.AskSize,
BidSize = message.Quote.BidSize,
Last = message.Quote.AskPrice ?? message.Quote.BidPrice
Ask = message.ClosePrice,
Bid = message.ClosePrice,
Last = message.ClosePrice,
AskSize = 0,
BidSize = 0
};

var derivative = new DerivativeModel
{
Expiration = message.Quote.TimestampUtc
Strike = message.StrikePrice,
Expiration = message.ExpirationDate?.ToDateTime(TimeOnly.MinValue)
};

var option = new InstrumentModel
Expand Down
Loading

0 comments on commit 396213a

Please sign in to comment.