Skip to content

Commit

Permalink
Arbitrage (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzonaut committed Jul 18, 2018
1 parent 781cb5b commit decf162
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion IntelliTrader.Core/Interfaces/Services/ITradingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface ITradingService : IConfigurableService
bool CanSell(SellOptions options, out string message);
bool CanSwap(SwapOptions options, out string message);
bool CanArbitrage(ArbitrageOptions options, out string message);
decimal GetPrice(string pair, TradePriceType? priceType = null);
decimal GetPrice(string pair, TradePriceType? priceType = null, bool normalize = true);
decimal CalculateOrderFees(IOrderDetails order);
bool IsNormalizedPair(string pair);
string NormalizePair(string pair);
Expand Down
7 changes: 2 additions & 5 deletions IntelliTrader.Exchange.Base/Services/ExchangeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,12 @@ public virtual string GetPairMarket(string pair)

public virtual string ChangeMarket(string pair, string market)
{
if (!pair.EndsWith(market))
if (!pair.EndsWith(market) && !pair.StartsWith(market))
{
string currentMarket = GetPairMarket(pair);
return pair.Substring(0, pair.Length - currentMarket.Length) + market;
}
else
{
return pair;
}
return pair;
}

public virtual decimal ConvertPrice(string pair, decimal price, string market, TradePriceType priceType)
Expand Down
4 changes: 2 additions & 2 deletions IntelliTrader.Trading/Services/OrderingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public IOrderDetails PlaceBuyOrder(BuyOptions options)
{
IPairConfig pairConfig = tradingService.GetPairConfig(options.Pair);
ITradingPair tradingPair = tradingService.Account.GetTradingPair(options.Pair);
decimal buyPrice = tradingService.GetPrice(options.Pair, TradePriceType.Ask);
decimal buyPrice = tradingService.GetPrice(options.Pair, TradePriceType.Ask, normalize: false);
decimal buyAmount = options.Amount ?? (options.MaxCost.Value / (options.Pair.EndsWith(Constants.Markets.USDT) ? 1 : buyPrice));
string signalRule = options.Metadata.SignalRule ?? "N/A";
options.Metadata.TradingRules = pairConfig.Rules.ToList();
options.Metadata.LastBuyMargin = options.Metadata.LastBuyMargin ?? tradingPair?.CurrentMargin ?? 0;
decimal buyAmount = options.Amount ?? (options.MaxCost.Value / (options.Pair.EndsWith(Constants.Markets.USDT) ? 1 : buyPrice));

BuyOrder buyOrder = new BuyOrder
{
Expand Down
11 changes: 9 additions & 2 deletions IntelliTrader.Trading/Services/TradingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public void ArbitrageReverse(ArbitrageOptions options)
decimal buyMarketPairFees = CalculateOrderFees(buyMarketPairOrderDetails);
string arbitragePair = Exchange.ChangeMarket(options.Pair, options.Arbitrage.Market.ToString());
decimal buyArbitragePairAmount = options.Arbitrage.Market == ArbitrageMarket.USDT ?
buyMarketPairOrderDetails.AmountFilled * GetPrice(buyMarketPairOrderDetails.Pair, TradePriceType.Ask) / GetPrice(arbitragePair, TradePriceType.Ask) :
buyMarketPairOrderDetails.AmountFilled * GetPrice(buyMarketPairOrderDetails.Pair, TradePriceType.Ask, normalize: false) / GetPrice(arbitragePair, TradePriceType.Ask) :
buyMarketPairOrderDetails.AmountFilled / GetPrice(arbitragePair, TradePriceType.Ask);

var buyArbitragePairOptions = new BuyOptions(arbitragePair)
Expand Down Expand Up @@ -649,8 +649,15 @@ public bool CanArbitrage(ArbitrageOptions options, out string message)
return true;
}

public decimal GetPrice(string pair, TradePriceType? priceType = null)
public decimal GetPrice(string pair, TradePriceType? priceType = null, bool normalize = true)
{
if (normalize)
{
if (pair == Config.Market + Constants.Markets.USDT)
{
return 1;
}
}
return Exchange.GetPrice(pair, priceType ?? Config.TradePriceType);
}

Expand Down
63 changes: 35 additions & 28 deletions IntelliTrader.Trading/TimedTasks/TradingTimedTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TradingTimedTask : HighResolutionTimedTask
private readonly ConcurrentDictionary<string, BuyTrailingInfo> trailingBuys = new ConcurrentDictionary<string, BuyTrailingInfo>();
private readonly ConcurrentDictionary<string, SellTrailingInfo> trailingSells = new ConcurrentDictionary<string, SellTrailingInfo>();

public TradingTimedTask(ILoggingService loggingService, INotificationService notificationService,
public TradingTimedTask(ILoggingService loggingService, INotificationService notificationService,
IHealthCheckService healthCheckService, ISignalsService signalsService, IOrderingService orderingService, ITradingService tradingService)
{
this.loggingService = loggingService;
Expand Down Expand Up @@ -77,40 +77,47 @@ public void InitiateBuy(BuyOptions options)

public void InitiateSell(SellOptions options)
{
IPairConfig pairConfig = tradingService.GetPairConfig(options.Pair);
if (!options.ManualOrder && pairConfig.SellTrailing != 0)
if (tradingService.Account.HasTradingPair(options.Pair))
{
if (!trailingSells.ContainsKey(options.Pair))
IPairConfig pairConfig = tradingService.GetPairConfig(options.Pair);
if (!options.ManualOrder && pairConfig.SellTrailing != 0)
{
StopTrailingBuy(options.Pair);
ITradingPair tradingPair = tradingService.Account.GetTradingPair(options.Pair);
tradingPair.SetCurrentValues(tradingService.GetPrice(tradingService.NormalizePair(options.Pair)), tradingService.Exchange.GetPriceSpread(options.Pair));

var trailingInfo = new SellTrailingInfo
if (!trailingSells.ContainsKey(options.Pair))
{
SellOptions = options,
SellMargin = pairConfig.SellMargin,
Trailing = pairConfig.SellTrailing,
TrailingStopMargin = pairConfig.SellTrailingStopMargin,
TrailingStopAction = pairConfig.SellTrailingStopAction,
InitialPrice = tradingPair.CurrentPrice,
LastTrailingMargin = tradingPair.CurrentMargin,
BestTrailingMargin = tradingPair.CurrentMargin
};
StopTrailingBuy(options.Pair);
ITradingPair tradingPair = tradingService.Account.GetTradingPair(options.Pair);
tradingPair.SetCurrentValues(tradingService.GetPrice(tradingService.NormalizePair(options.Pair)), tradingService.Exchange.GetPriceSpread(options.Pair));

if (trailingSells.TryAdd(options.Pair, trailingInfo))
{
if (LoggingEnabled)
var trailingInfo = new SellTrailingInfo
{
SellOptions = options,
SellMargin = pairConfig.SellMargin,
Trailing = pairConfig.SellTrailing,
TrailingStopMargin = pairConfig.SellTrailingStopMargin,
TrailingStopAction = pairConfig.SellTrailingStopAction,
InitialPrice = tradingPair.CurrentPrice,
LastTrailingMargin = tradingPair.CurrentMargin,
BestTrailingMargin = tradingPair.CurrentMargin
};

if (trailingSells.TryAdd(options.Pair, trailingInfo))
{
loggingService.Info($"Start trailing sell {tradingPair.FormattedName}. " +
$"Price: {tradingPair.CurrentPrice:0.00000000}, Margin: {tradingPair.CurrentMargin:0.00}");
if (LoggingEnabled)
{
loggingService.Info($"Start trailing sell {tradingPair.FormattedName}. " +
$"Price: {tradingPair.CurrentPrice:0.00000000}, Margin: {tradingPair.CurrentMargin:0.00}");
}
}
}
}
else
{
orderingService.PlaceSellOrder(options);
}
}
else
{
orderingService.PlaceSellOrder(options);
loggingService.Info($"Cancel initiate sell for {options.Pair}. Reason: pair does not exist");
}
}

Expand Down Expand Up @@ -139,7 +146,7 @@ public void ProcessTradingPairs()
}
}

if (tradingPair.CurrentMargin <= sellTrailingInfo.TrailingStopMargin || tradingPair.CurrentMargin <
if (tradingPair.CurrentMargin <= sellTrailingInfo.TrailingStopMargin || tradingPair.CurrentMargin <
(sellTrailingInfo.BestTrailingMargin - sellTrailingInfo.Trailing))
{
StopTrailingSell(tradingPair.Pair);
Expand Down Expand Up @@ -186,8 +193,8 @@ public void ProcessTradingPairs()
{
InitiateSell(new SellOptions(tradingPair.Pair));
}
else if (pairConfig.SellEnabled && pairConfig.SellStopLossEnabled &&
tradingPair.CurrentMargin <= pairConfig.SellStopLossMargin &&
else if (pairConfig.SellEnabled && pairConfig.SellStopLossEnabled &&
tradingPair.CurrentMargin <= pairConfig.SellStopLossMargin &&
tradingPair.CurrentAge >= pairConfig.SellStopLossMinAge &&
(pairConfig.NextDCAMargin == null || !pairConfig.SellStopLossAfterDCA))
{
Expand Down Expand Up @@ -274,7 +281,7 @@ public void ProcessTradingPairs()
}
}

healthCheckService.UpdateHealthCheck(Constants.HealthChecks.TradingPairsProcessed,
healthCheckService.UpdateHealthCheck(Constants.HealthChecks.TradingPairsProcessed,
$"Pairs: {traidingPairsCount}, Trailing buys: {trailingBuys.Count}, Trailing sells: {trailingSells.Count}");
}

Expand Down

0 comments on commit decf162

Please sign in to comment.