コード例 #1
0
        public void TransferInsertTest()
        {
            BaseExchange sellExchange = new Kraken();
            BaseExchange buyExchange  = new Bitstamp();
            int          MaxId        = 0;

            sellExchange.AvailableBtc = 1.0234234234m;
            buyExchange.AvailableBtc  = 2.4534534m;
            decimal transferAmount = 0.6319841998m;

            //First need to create an arbitration run and opportunity
            ArbitrationRun         testRun         = TestsHelper.CreateArbitrationRun();
            ArbitrationOpportunity testOpportunity = TestsHelper.CreateArbitrationOpportunity(buyExchange, sellExchange, testRun.Id.Value);

            //Before the test can continue, ensure the arbitraiton opportunity was properly inserted
            Assert.IsTrue(testOpportunity.Id != null);

            Transfer testTransfer = new Transfer(buyExchange, sellExchange, transferAmount);

            //Get the max id from TRANSFER table
            Object result = DatabaseManager.ExecuteQuery("select MaxId = max(id) from TRANSFER").Rows[0]["MaxId"];

            if (result != DBNull.Value)
            {
                MaxId = (int)result;
            }

            //Now save the TRANSFER to the db
            testTransfer.PersistToDb();

            //Select the transfer from the db
            result = DatabaseManager.ExecuteQuery(string.Format("select id from TRANSFER where id = {0} AND ORIGIN_EXCHANGE = '{1}' and DESTINATION_EXCHANGE = '{2}' AND AMOUNT = {3}", testTransfer.Id, buyExchange.Name, sellExchange.Name, transferAmount)).Rows[0][0];

            //Clean up before testing result
            if (result != DBNull.Value)
            {
                //Remove the test transfer
                DatabaseManager.ExecuteNonQuery(String.Format("delete from TRANSFER where id = {0}", testTransfer.Id));
            }

            TestsHelper.DeleteArbitrationTrade(testOpportunity.Id.Value);
            TestsHelper.DeleteArbitrationRun(testRun.Id.Value);

            //The above query should return the transfer that was created.
            Assert.IsTrue(result != DBNull.Value);
        }
コード例 #2
0
        public void PersistArbitrationOpportunity()
        {
            //Arbitraty test values
            decimal  buyAmount         = 351.654316m;
            decimal  sellAmount        = 351.654316m;
            decimal  buyPrice          = 409.21m;
            decimal  sellPrice         = 410.87m;
            decimal  totalBuyCost      = 1201.14m;
            decimal  totalSellCost     = 1301.54m;
            decimal  profit            = totalSellCost - totalBuyCost;
            string   buyOrderId        = "Abc-123";
            string   sellOrderId       = "123-Abc";
            DateTime executionDateTime = DateTime.Now;

            Bitstamp               buyExchange          = new Bitstamp();
            Btce                   sellExchange         = new Btce();
            ArbitrationRun         testRun              = null;
            ArbitrationRun         updateTestRun        = null;
            ArbitrationOpportunity testArbitrationTrade = null;

            try
            {
                testRun              = TestsHelper.CreateArbitrationRun();
                updateTestRun        = TestsHelper.CreateArbitrationRun();
                testArbitrationTrade = new ArbitrationOpportunity(buyExchange, sellExchange);

                testArbitrationTrade.BuyAmount         = buyAmount;
                testArbitrationTrade.SellAmount        = sellAmount;
                testArbitrationTrade.ArbitrationRunId  = testRun.Id.Value;
                testArbitrationTrade.BuyPrice          = buyPrice;
                testArbitrationTrade.ExecutionDateTime = executionDateTime;
                testArbitrationTrade.Profit            = profit;
                testArbitrationTrade.SellPrice         = sellPrice;
                testArbitrationTrade.TotalBuyCost      = totalBuyCost;
                testArbitrationTrade.TotalSellCost     = totalSellCost;
                testArbitrationTrade.BuyOrderId        = buyOrderId;
                testArbitrationTrade.SellOrderId       = sellOrderId;

                //Now that the arbitration trade has been set up, save it to the db.
                testArbitrationTrade.PersistToDb();

                //Ensure an id was set after the initial insert
                Assert.IsTrue(testArbitrationTrade.Id != null);

                string insertFetchSql = string.Format("" +
                                                      "select " +
                                                      "   ID, " +
                                                      "   CREATE_DATETIME, " +
                                                      "   LAST_MODIFIED_DATETIME " +
                                                      "from " +
                                                      "   ARBITRATION_TRADE " +
                                                      "where " +
                                                      "   ID = {0} AND " +
                                                      "   BUY_AMOUNT = {1} AND " +
                                                      "   BUY_PRICE = {2} AND " +
                                                      "   SELL_PRICE = {3} AND" +
                                                      "   TOTAL_BUY_COST = {4} AND " +
                                                      "   TOTAL_SELL_COST = {5} AND " +
                                                      "   PROFIT = {6} AND " +
                                                      "   EXECUTION_DATETIME = '{7}' AND " +
                                                      "   SELL_EXCHANGE = '{8}' AND " +
                                                      "   BUY_EXCHANGE = '{9}' AND " +
                                                      "   BUY_ORDER_ID = '{10}' AND " +
                                                      "   SELL_ORDER_ID = '{11}' AND " +
                                                      "   ARBITRATION_RUN_ID = {12} AND " +
                                                      "   SELL_AMOUNT = {13}", testArbitrationTrade.Id.Value, buyAmount, buyPrice, sellPrice, totalBuyCost, totalSellCost, profit, executionDateTime.ToString("yyy-MM-dd HH:mm:ss"), sellExchange.Name, buyExchange.Name, buyOrderId, sellOrderId, testRun.Id.Value, sellAmount);

                //Ensure all the values were properly inserted.
                DataTable result = DatabaseManager.ExecuteQuery(insertFetchSql);
                Assert.IsTrue(result != null && Convert.ToInt32(result.Rows[0]["ID"]) == testArbitrationTrade.Id.Value);

                //Create some bogus dates for CREATE_DATETIME and LAST_MODIFIED_DATETIME
                DateTime createDateTime       = new DateTime(2014, 1, 1, 13, 25, 05);
                DateTime lastModifiedDateTime = new DateTime(2014, 1, 1, 13, 25, 05);

                //In order to test that CREATE_DATETIME and LAST_MODIFIED_DATETIME behave properly on updates, need to put some bogus data in there:
                DatabaseManager.ExecuteNonQuery(string.Format("update ARBITRATION_TRADE set CREATE_DATETIME = '{0}', LAST_MODIFIED_DATETIME = '{1}' where ID = {2}", createDateTime.ToString("yyy-MM-dd HH:mm:ss"), lastModifiedDateTime.ToString("yyy-MM-dd HH:mm:ss"), testArbitrationTrade.Id.Value));

                //Update properties with arbitraty test values.
                buyAmount         = 103264798.175785743216m;
                sellAmount        = 103264798.175785743216m;
                buyPrice          = 1.02m;
                sellPrice         = 10.02m;
                totalBuyCost      = 2.01m;
                totalSellCost     = 18.01m;
                profit            = totalSellCost - totalBuyCost;
                executionDateTime = DateTime.Now;
                buyOrderId        = "Choctaw-47";
                sellOrderId       = "Apache-48";

                //Update arbitration trade to have new values
                testArbitrationTrade.BuyAmount         = buyAmount;
                testArbitrationTrade.SellAmount        = sellAmount;
                testArbitrationTrade.ArbitrationRunId  = updateTestRun.Id.Value;
                testArbitrationTrade.BuyPrice          = buyPrice;
                testArbitrationTrade.ExecutionDateTime = executionDateTime;
                testArbitrationTrade.Profit            = profit;
                testArbitrationTrade.SellPrice         = sellPrice;
                testArbitrationTrade.TotalBuyCost      = totalBuyCost;
                testArbitrationTrade.TotalSellCost     = totalSellCost;
                testArbitrationTrade.BuyOrderId        = buyOrderId;
                testArbitrationTrade.SellOrderId       = sellOrderId;

                testArbitrationTrade.PersistToDb();

                string updateFetchSql = string.Format("" +
                                                      "select " +
                                                      "   ID, " +
                                                      "   CREATE_DATETIME, " +
                                                      "   LAST_MODIFIED_DATETIME " +
                                                      "from " +
                                                      "   ARBITRATION_TRADE " +
                                                      "where " +
                                                      "   ID = {0} AND " +
                                                      "   BUY_AMOUNT = {1} AND " +
                                                      "   BUY_PRICE = {2} AND " +
                                                      "   SELL_PRICE = {3} AND" +
                                                      "   TOTAL_BUY_COST = {4} AND " +
                                                      "   TOTAL_SELL_COST = {5} AND " +
                                                      "   PROFIT = {6} AND " +
                                                      "   EXECUTION_DATETIME = '{7}' AND " +
                                                      "   SELL_EXCHANGE = '{8}' AND " +
                                                      "   BUY_EXCHANGE = '{9}' AND " +
                                                      "   BUY_ORDER_ID = '{10}' AND " +
                                                      "   SELL_ORDER_ID = '{11}' AND " +
                                                      "   ARBITRATION_RUN_ID = {12} AND " +
                                                      "   SELL_AMOUNT = {13}", testArbitrationTrade.Id.Value, buyAmount, buyPrice, sellPrice, totalBuyCost, totalSellCost, profit, executionDateTime.ToString("yyy-MM-dd HH:mm:ss"), sellExchange.Name, buyExchange.Name, buyOrderId, sellOrderId, updateTestRun.Id.Value, sellAmount);

                result = DatabaseManager.ExecuteQuery(updateFetchSql);

                //Ensure a record was found with all the updated values
                Assert.IsTrue(result != null);

                //Ensure the CREATE_DATETIME is the same, but the LAST_MODIFIED_DATETIME is different
                Assert.IsTrue(createDateTime == (DateTime)result.Rows[0]["CREATE_DATETIME"]);
                Assert.IsTrue(lastModifiedDateTime != (DateTime)result.Rows[0]["LAST_MODIFIED_DATETIME"]);
            }
            finally
            {
                //Remove test data from the database
                if (testArbitrationTrade.Id != null)
                {
                    TestsHelper.DeleteArbitrationTrade(testArbitrationTrade.Id.Value);
                }

                if (testRun.Id != null)
                {
                    TestsHelper.DeleteArbitrationRun(testRun.Id.Value);
                }

                if (updateTestRun.Id != null)
                {
                    TestsHelper.DeleteArbitrationRun(updateTestRun.Id.Value);
                }
            }
        }
コード例 #3
0
        public void NotEnoughtBtcExceptionAreSwallowedCorrectly()
        {
            ArbitrationRun         testRun      = null;
            ArbitrationOpportunity opportunity1 = null;
            ArbitrationOpportunity opportunity2 = null;
            ArbitrationOpportunity opportunity3 = null;

            try
            {
                //Set up a test run
                testRun = TestsHelper.CreateArbitrationRun();

                //Set up the buy and sell exchanges.
                Bitstamp            bitstamp     = new Bitstamp();
                Kraken              kraken       = new Kraken();
                List <BaseExchange> exchangeList = new List <BaseExchange>();
                exchangeList.Add(bitstamp);
                exchangeList.Add(kraken);

                bitstamp.AvailableBtc  = 0.2m;
                bitstamp.AvailableFiat = 100m;
                kraken.AvailableBtc    = 0.2m;
                kraken.AvailableFiat   = 30m;


                //Initial trad; bitstamp will need to transfer 0.1 btc to kraken for this. It will be found in the rollup later.
                //Bitstamp now has 0.4 btc.
                opportunity1 = TestsHelper.CreateArbitrationOpportunity(bitstamp, kraken, testRun.Id.Value, 0.1m);

                //Made up numbers; they don't really matter for this test
                opportunity1.TotalBuyCost  = 10m;
                opportunity1.TotalSellCost = 10m;

                bitstamp.SimulatedBuy(opportunity1.BuyAmount, opportunity1.TotalBuyCost);
                kraken.SimulatedSell(opportunity1.BuyAmount, opportunity1.TotalSellCost);
                Assert.IsTrue(bitstamp.AvailableBtc == 0.3m);

                //Now a couple a trade where btc is sold off at Bitstamp; enough to make the rollup transfer fail
                //Bitstamp now has 0.1
                opportunity2 = TestsHelper.CreateArbitrationOpportunity(kraken, bitstamp, testRun.Id.Value, 0.3m);

                //Made up numbers; they don't really matter for this test
                opportunity2.TotalBuyCost  = 30m;
                opportunity2.TotalSellCost = 30m;

                kraken.SimulatedBuy(opportunity2.BuyAmount, opportunity2.TotalBuyCost);
                bitstamp.SimulatedSell(opportunity2.BuyAmount, opportunity2.TotalSellCost);
                Assert.IsTrue(bitstamp.AvailableBtc == 0.0m);

                //Now assume this trigger a transfer, so lets say 0.5btc is in transfer to bitstamp from kraken
                bitstamp.BTCInTransfer = 0.5m;

                //Now another buy at bitstamp; this will trigger a transfer rollup from bitstamp to kraken
                opportunity3 = TestsHelper.CreateArbitrationOpportunity(bitstamp, kraken, testRun.Id.Value, 0.15m);

                //Made up numbers; they don't really matter for this test
                opportunity3.TotalBuyCost  = 15m;
                opportunity3.TotalSellCost = 15m;

                bitstamp.SimulatedBuy(opportunity3.BuyAmount, opportunity3.TotalBuyCost);
                kraken.SimulatedSell(opportunity3.BuyAmount, opportunity3.TotalSellCost);
                Assert.IsTrue(bitstamp.AvailableBtc == 0.15m);

                //This should detect a transfer, but not actually create it
                List <Transfer> transferFound = TransferManager.DetectAndExecuteRollupTransfers_Simulate(3, testRun.Id.Value, exchangeList);
                Assert.IsTrue(transferFound == null);

                //Erase btc in transfer and look for transfers again, this time the TransferManager should throw an exception because
                //a bad transfer has been found (it's amount is > bitstamp total btc)
                bool errorFound = false;
                bitstamp.BTCInTransfer = 0m;

                try
                {
                    TransferManager.DetectAndExecuteRollupTransfers_Simulate(2, testRun.Id.Value, exchangeList);
                }

                catch (NotEnoughBtcException e)
                {
                    errorFound = true;
                }

                Assert.IsTrue(errorFound, "TransferManager did not throw NotEnoughBtcException.");
            }

            //Clean up test data
            finally
            {
                if (opportunity1 != null)
                {
                    TestsHelper.DeleteArbitrationTrade(opportunity1.Id.Value);
                }

                if (opportunity2 != null)
                {
                    TestsHelper.DeleteArbitrationTrade(opportunity2.Id.Value);
                }

                if (opportunity3 != null)
                {
                    TestsHelper.DeleteArbitrationTrade(opportunity3.Id.Value);
                }

                if (testRun != null)
                {
                    TestsHelper.DeleteArbitrationTrade(testRun.Id.Value);
                }
            }
        }