Skip to content

Instantly share code, notes, and snippets.

@sthewissen
Last active April 22, 2022 03:01
Show Gist options
  • Save sthewissen/b49b4af187c49967af3ef4d4d73a8181 to your computer and use it in GitHub Desktop.
Save sthewissen/b49b4af187c49967af3ef4d4d73a8181 to your computer and use it in GitHub Desktop.
public async Task Process()
{
// Get our current trades.
var activeTrades = GetCurrentTrades();
foreach (var trade in activeTrades)
{
// Check the exchange to see if we have a buy/sell order open for this trade.
var orders = GetOpenOrders(trade.Market);
// If there is already an order open for it, skip this.
if (orders.Any(x => x.OrderUuid.ToString() == trade.OpenOrderId))
{
// There's already an open order for this trade.
// This means we're still buying it.
_log($"Already an open order for trade {trade.OpenOrderId}");
}
else
{
// There's no order, so we can check if this trade can be closed
if (!CloseTradeIfFulfilled(trade))
{
// If it wasn't closed, we can check if it is sellable.
HandleTrade(trade);
}
}
}
// As long as we have slots available...
while (activeTrades.Count < Constants.MaxNumberOfConcurrentTrades)
{
// Check if we can find a suitable trade candidate
var trade = StartTrade(activeTrades);
// We found a trade!
if (trade != null)
{
// Add this to activeTrades so we don't trigger the same trade again.
activeTrades.Add(trade);
}
else
{
// No more trade to be found, let's end the loop.
break;
}
}
// Update our trades in the database.
UpdateTrades(activeTrades);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment