Skip to content

Commit

Permalink
Merge when zero volume.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Jul 11, 2021
1 parent 287133c commit 048650e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
31 changes: 31 additions & 0 deletions Sideways.Tests/Model/CandleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ public static void Merge()
Assert.AreEqual(expected, c2.Merge(c1));
}

[Test]
public static void MergeWhenZeroVolume()
{
var c1 = new Candle(
new DateTimeOffset(2021, 05, 22, 09, 30, 00, 0, TimeSpan.Zero),
open: 0f,
high: 0f,
low: 0f,
close: 0f,
volume: 0);

var c2 = new Candle(
new DateTimeOffset(2021, 05, 22, 09, 31, 00, 0, TimeSpan.Zero),
open: 1.3f,
high: 1.4f,
low: 1.1f,
close: 1.2f,
volume: 1);

var expected = new Candle(
new DateTimeOffset(2021, 05, 22, 09, 31, 00, 0, TimeSpan.Zero),
open: 1.3f,
high: 1.4f,
low: 1.1f,
close: 1.2f,
volume: 1);

Assert.AreEqual(expected, c1.Merge(c2));
Assert.AreEqual(expected, c2.Merge(c1));
}

[Test]
public static void Adjust()
{
Expand Down
8 changes: 4 additions & 4 deletions Sideways/Chart/Candles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public Candles(SortedCandles days, SortedCandles minutes)
public IEnumerable<Candle> DescendingDays(DateTimeOffset end)
{
if (TradingDay.IsRegularHours(end) &&
this.DayRegularMinutes(end, this.minuteIndex) is { Length: > 0 } minutes)
this.DayRegularMinutes(end, this.minuteIndex) is { Length: > 0 } dayMinutes)
{
if (minutes.Length == 1)
if (dayMinutes.Length == 1)
{
yield return minutes[0];
yield return dayMinutes[0];
}
else
{
yield return Candle.Merge(minutes);
yield return Candle.Merge(dayMinutes);
}

end = end.AddDays(-1);
Expand Down
21 changes: 17 additions & 4 deletions Sideways/Model/Candle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,28 @@ public static bool ShouldMergeFiveMinutes(DateTimeOffset x, DateTimeOffset y) =>

public Candle Merge(Candle other)
{
return other.Time <= this.Time
? new(
if (other.Time <= this.Time)
{
if (this.Volume == 0)
{
return other.WithTime(this.Time);
}

if (other.Volume == 0)
{
return this;
}

return new(
time: this.Time,
open: other.Open,
high: Math.Max(this.High, other.High),
low: Math.Min(this.Low, other.Low),
close: this.Close,
volume: this.Volume + other.Volume)
: other.Merge(this);
volume: this.Volume + other.Volume);
}

return other.Merge(this);
}

public bool Equals(Candle other)
Expand Down

0 comments on commit 048650e

Please sign in to comment.