private void TransferOnTime(ArbitrationOpportunity opportunityToExecute) { Transfer executedTransfer = null; switch (_currentRun.ArbitrationMode) { case ArbitrationMode.Live: //No need to keep track of transfers in live mode since they are completed automatically. executedTransfer = TransferManager.OnTimeTransfer_Live(opportunityToExecute.BuyExchange, opportunityToExecute.SellExchange, opportunityToExecute.BuyAmount, log); break; case ArbitrationMode.Simulation: executedTransfer = TransferManager.OnTimeTransfer_Simulate(opportunityToExecute.BuyExchange, opportunityToExecute.SellExchange, opportunityToExecute.BuyAmount, log); //In simulation mode, keep track of transfers so that they can be completed manually later. _transfersInProcess.Add(executedTransfer); break; } //if (executedTransfer != null) { PrintToOutputTextBox((String.Format(LOG_MESSAGE, executedTransfer.Id.Value, executedTransfer.Amount, executedTransfer.OriginExchange.Name, executedTransfer.DestinationExchange.Name))); } }
private void IntervalElapsed(Object source, ElapsedEventArgs e) { ArbitrationOpportunity opportunityToExecute = null; List <ArbitrationOpportunity> opportunityList = null; //Timer needs to chill while arbitration is looked for. _timer.Enabled = false; //Update the balances first, to give time for orders that may have been initiated on the previous round to execute. //Only need to update account balances in live mode. if (_currentRun.ArbitrationMode == ArbitrationMode.Live) { UpdateExchangeAccountInfo(); } DisplayExchangeBalances(); try { //Before looking for another arbitration opportunity, see if the previous one was executed correctly. if (_previousOpportunity != null) { //Verify the arbitration trade executed as expected. try { //See if the orders filled (only necesary in live mode) if (CurrentRun.ArbitrationMode == ArbitrationMode.Live) { _opportunityValidator.ValidateArbitrationTradeOrderExecution(_previousOpportunity); } //Compare the balances - this will detect if realized profit is different than computed profit, which would indicate there is a serious problem somewhere log.Info(_opportunityValidator.ValidateExchangeBalancesAfterTrade(_previousOpportunity)); } catch (ArbitrationTradeValidationException error) { LogAndDisplayString(error.Message, LogLevel.Warning); log.Info(error.ExchangeBalanceDetailMessage); } } //Reset _previousOpportunity, so it is not verified again _previousOpportunity = null; //Prepare the opportunity validator so it can compare balances before and after a trade _opportunityValidator.SetFiatAndBitcoinBalanceBeforeArbitrationTrade(); try { opportunityList = FindArbitrationOpportunities(); if (opportunityList != null && opportunityList.Count > 0) { opportunityToExecute = DetermineOpportunityToExecute(opportunityList); } } catch (Exception exception) { log.Warn("There was a problem looking for arbitration: " + Environment.NewLine + exception.Message); log.Debug(exception.StackTrace); } //If an opportunity was found, execute it if (opportunityToExecute != null) { ExecuteArbitrationTrade(opportunityToExecute); //Save the arbitration opportunity so it can be verified on the next round. _previousOpportunity = opportunityToExecute; } //Do the logging and display after the fact, to reduce lage between order book get and order execution DisplayAndLogArbitrationOpportunityList(opportunityList); //Only need to keep track of transfers in simulation mode. if (_currentRun.ArbitrationMode == ArbitrationMode.Simulation) { //Complete any transfers that are done processing. TransferManager.CompleteTransfers(_transfersInProcess); } //Based on the transfer mode of the current run, process any transfers that are due. try { if (_currentRun.TransferMode == TransferMode.RollupOnTrades) { TransferRollupOnTrades(); } else if (_currentRun.TransferMode == TransferMode.RollupByHour) { TransferRollupByHours(); } //Note, for transfer mode 'None,' don't need to do anything. } catch (Exception exception) { OnError("There was a problem processing the transfers.", exception, "There was a problem processing the transfers: "); } //If this manager hasn't been stopped since the timer fired (which would be the case if there were a fatal error or the stop button was clicked), turn the timer back on. if (!_stopped) { _timer.Enabled = true; } } catch (Exception exception) { OnError("There was an error in this arbitration run, stopping the run.", exception, "Unexpected error with the arbitration run: "); } }