Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Isolate integration, external tests #1156

Draft
wants to merge 6 commits into
base: v3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-indicators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: Indicators

on:
push:
branches: ["main"]
branches: ["main","v3"]

pull_request:
branches: ["main"]
branches: ["main","v3"]

jobs:
test:
Expand Down
7 changes: 6 additions & 1 deletion Stock.Indicators.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Observe.Streaming", "tests\
{8D0F1781-EDA3-4C51-B05D-D33FF1156E49} = {8D0F1781-EDA3-4C51-B05D-D33FF1156E49}
EndProjectSection
EndProject

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Integration", "tests\Tests.Integration\Tests.Integration.csproj", "{16B980BD-8ACF-4E71-929B-120ED4BAF998}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -52,6 +53,10 @@ Global
{14DEC3AF-9AF2-4A66-8BEE-C342C6CC4307}.Debug|Any CPU.Build.0 = Debug|Any CPU
{14DEC3AF-9AF2-4A66-8BEE-C342C6CC4307}.Release|Any CPU.ActiveCfg = Release|Any CPU
{14DEC3AF-9AF2-4A66-8BEE-C342C6CC4307}.Release|Any CPU.Build.0 = Release|Any CPU
{16B980BD-8ACF-4E71-929B-120ED4BAF998}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{16B980BD-8ACF-4E71-929B-120ED4BAF998}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16B980BD-8ACF-4E71-929B-120ED4BAF998}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16B980BD-8ACF-4E71-929B-120ED4BAF998}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions tests/Tests.Integration/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
21 changes: 21 additions & 0 deletions tests/Tests.Integration/Tests.Integration.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Alpaca.Markets" Version="7.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Skender.Stock.Indicators" Version="3.0.0-preview1014-0025" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions tests/Tests.Integration/WilliamsR.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Skender.Stock.Indicators;

namespace Tests.Indicators;

[TestClass]
public class WilliamsRTests
{
[TestMethod]
public async Task Issue1127()
{
// initialize
IEnumerable<Quote> quotes = await FeedData // live quotes
.GetQuotes("A", 365 * 3)
.ConfigureAwait(false);

List<Quote> quotesList = quotes.ToList();
int length = quotesList.Count;

// get indicators
List<WilliamsResult> resultsList = quotes
.GetWilliamsR(14)
.ToList();

Console.WriteLine($"%R from {length} quotes.");

// analyze boundary
for (int i = 0; i < length; i++)
{
Quote q = quotesList[i];
WilliamsResult r = resultsList[i];

Console.WriteLine($"{q.Date:s} {r.WilliamsR}");

if (r.WilliamsR is not null)
{
Assert.IsTrue(r.WilliamsR <= 0);
Assert.IsTrue(r.WilliamsR >= -100);
}
}
}
}
74 changes: 74 additions & 0 deletions tests/Tests.Integration/_common/Helper.LiveQuotes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Alpaca.Markets;
using Skender.Stock.Indicators;

namespace Tests.Indicators;

internal class FeedData
{
internal static async Task<IEnumerable<Quote>> GetQuotes(string symbol)
=> await GetQuotes(symbol, 365 * 2)
.ConfigureAwait(false);

internal static async Task<IEnumerable<Quote>> GetQuotes(string symbol, int days)
{
/* This won't run if environment variables not set.
Use FeedData.InconclusiveIfNotSetup() in tests.

(1) get your API keys
https://alpaca.markets/docs/market-data/getting-started/

(2) manually install in your environment (replace value)

setx ALPACA_KEY "y0ur_Alp@ca_K3Y_v@lue"
setx ALPACA_SECRET "y0ur_Alp@ca_S3cret_v@lue"

****************************************************/

// get and validate keys
string? alpacaApiKey = Environment.GetEnvironmentVariable("ALPACA_KEY");
string? alpacaSecret = Environment.GetEnvironmentVariable("ALPACA_SECRET");

if (string.IsNullOrEmpty(alpacaApiKey) || string.IsNullOrEmpty(alpacaSecret))
{
Assert.Inconclusive("Data feed unusable. Environment variables missing.");
}

ArgumentException.ThrowIfNullOrEmpty(nameof(alpacaApiKey));
ArgumentException.ThrowIfNullOrEmpty(nameof(alpacaSecret));

// connect to Alpaca REST API
SecretKey secretKey = new(alpacaApiKey, alpacaSecret);

IAlpacaDataClient client = Environments
.Paper
.GetAlpacaDataClient(secretKey);

// compose request
// (excludes last 15 minutes for free delayed quotes)
DateTime into = DateTime.Now.Subtract(TimeSpan.FromMinutes(16));
DateTime from = into.Subtract(TimeSpan.FromDays(days));

HistoricalBarsRequest request = new(symbol, from, into, BarTimeFrame.Day);

// fetch minute-bar quotes in Alpaca's format
IPage<IBar> barSet = await client
.ListHistoricalBarsAsync(request)
.ConfigureAwait(false);

// convert library compatible quotes
IEnumerable<Quote> quotes = barSet
.Items
.Select(bar => new Quote
{
Date = bar.TimeUtc,
Open = bar.Open,
High = bar.High,
Low = bar.Low,
Close = bar.Close,
Volume = bar.Volume
})
.OrderBy(x => x.Date);

return quotes;
}
}