Skip to content
This repository has been archived by the owner on Oct 16, 2019. It is now read-only.

Commit

Permalink
Add UnitCostAveragingCalculator.IsLastTrade
Browse files Browse the repository at this point in the history
References #47
  • Loading branch information
andreashuber69 committed Dec 24, 2017
1 parent efc8db6 commit 3969816
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Bitstamp/UnitCostAveragingCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public sealed class UnitCostAveragingCalculator
}
else
{
if ((startBalance - tradeAmount) < this.minOptimalTradeAmount)
if (this.IsLastTrade(startBalance, tradeAmount))
{
tradeAmount = startBalance;
}
Expand All @@ -90,6 +90,11 @@ public sealed class UnitCostAveragingCalculator
return tradeAmount;
}

/// <summary>Gets a value indicating whether a trade with <paramref name="tradeAmount"/> would be the last
/// trade.</summary>
public bool IsLastTrade(decimal startBalance, decimal tradeAmount) =>
(startBalance - tradeAmount) < this.minOptimalTradeAmount;

/// <summary>Gets the trading fee for <paramref name="tradeAmount"/>.</summary>
public decimal GetFee(decimal tradeAmount) => Ceiling(tradeAmount * this.feeStepsPerUnit) * this.feeStep;

Expand Down
6 changes: 3 additions & 3 deletions SmartTrade/TradeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ private DateTime GetStart(List<ITransaction> transactions)

if (buy)
{
// If we're going to spend the whole second balance, we need to subtract the fee first, as the
// exchange will do the same.
if (secondAmount.Value == secondBalance)
// If this is the last trade, we need to subtract the fee first, as the exchange will do the
// same.
if (calculator.IsLastTrade(secondBalance, secondAmountToTrade))
{
secondAmountToTrade -= calculator.GetFee(secondAmountToTrade);
}
Expand Down

0 comments on commit 3969816

Please sign in to comment.