Files
spark/src/client/spark_api/Position/PositionPriceSize.cs
T
John Colvin 505cd37225 chore: Assert that PositionPriceSize.GetHashCode isn't invoked
Impl doesn't guarentee equal objects produce the same hash
2022-08-01 11:14:28 -04:00

52 lines
1.7 KiB
C#

#nullable enable
namespace Spark.Api.Position {
using bts;
using bts.utils;
using System;
public readonly struct PositionPriceSize : IEquatable<PositionPriceSize> {
public static readonly PositionPriceSize empty = new(0, 0, 0, 0);
public readonly double long_pos;
public readonly double short_pos;
public readonly double long_avg;
public readonly double short_avg;
public PositionPriceSize(
double long_pos, double long_avg, double short_pos, double short_avg) {
this.long_pos = long_pos;
this.long_avg = long_avg;
this.short_pos = short_pos;
this.short_avg = short_avg;
}
public double net_pos =>
long_pos.subtract_real(short_pos);
public double net_total_cost =>
(long_pos * long_avg).subtract_real(short_pos * short_avg);
public double net_avg => net_total_cost / net_pos;
public bool has_position => long_pos > 0 || short_pos > 0;
public bool Equals(PositionPriceSize other) =>
long_pos.epsilon_equals(other.long_pos) &&
long_avg.epsilon_equals(other.long_avg) &&
short_pos.epsilon_equals(other.short_pos) &&
short_avg.epsilon_equals(other.short_avg);
public override bool Equals(object? obj) =>
obj is PositionPriceSize other && Equals(other);
public override int GetHashCode() {
Log.assert(false, "GetHashCode doesn't guarentee equal objects produce the same hash");
return HashCode.Combine(long_pos, long_avg, short_pos, short_avg);
}
public static bool operator ==(PositionPriceSize a, PositionPriceSize b) => a.Equals(b);
public static bool operator !=(PositionPriceSize a, PositionPriceSize b) => !a.Equals(b);
}
}