Beispiel #1
0
        static void MainLoop()
        {
            CancelOrderThread = new Thread(CancelOldOrders);
            CancelOrderThread.IsBackground = true;
            CancelOrderThread.Start();

            BalanceThread = new Thread(BalanceAccount);
            BalanceThread.IsBackground = true;
            BalanceThread.Start();
            for (numTick = 0; ; numTick++)
            {
                //UpdateTrades();
                //UpdateOrderBook();

                bool   bull = false, bear = false;
                double tradeamount = 0;
                double burstPrice  = prices[prices.Count - 1] * BurstThresholdPct;
                //Console.WriteLine("Tick:" + numTick + ",LastPrice:" + prices[prices.Count - 1] + "burstPrice:" + burstPrice.ToString("F12"));

                double maxlastsix = 0, maxlastfive = 0;
                for (int i = 6; i > 1; i--)
                {
                    if (maxlastsix < prices[prices.Count - i])
                    {
                        maxlastsix = prices[prices.Count - i];
                    }
                    if (i > 2)
                    {
                        if (maxlastfive < prices[prices.Count - i])
                        {
                            maxlastfive = prices[prices.Count - i];
                        }
                    }
                }
                double diff     = prices[prices.Count - 1] - maxlastsix;
                double difffive = prices[prices.Count - 1] - maxlastfive;
                if (diff != 0)
                {
                    Console.WriteLine("maxlastsix:{0},maxlastfive{1},Diff{2}", maxlastsix, maxlastfive, diff.ToString("F12"));
                }
                if (numTick > 2 && (diff > burstPrice || (difffive > burstPrice && prices[prices.Count - 1] > prices[prices.Count - 2])))
                {
                    bull        = true;
                    tradeamount = ToSymbolFree / bidPrice * 0.99;
                }
                else if (numTick > 2 && (diff < burstPrice || (difffive < burstPrice && prices[prices.Count - 1] < prices[prices.Count - 2])))
                {
                    bear        = true;
                    tradeamount = FromSymbolFree;
                }
                if (Vol < BurstThresholdVol)
                {
                    tradeamount *= Vol / BurstThresholdVol;
                }

                if (numTick < 5)
                {
                    tradeamount *= 0.8;
                }

                if (numTick < 10)
                {
                    tradeamount *= 0.8;
                }
                if ((!bull && !bear) || tradeamount < MinTradeAmount)
                {
                    Thread.Sleep(300);
                    continue;
                }
                if (IsBalance)
                {
                    Thread.Sleep(300);
                    continue;
                }
                double[] da  = prices.ToArray();
                double[] da1 = new double[14];
                Array.Copy(da, 0, da1, 0, 14);

                if (bull && prices[prices.Count - 1] < da1.Max())
                {
                    tradeamount *= 0.90;
                }
                if (bear && prices[prices.Count - 1] > da1.Min())
                {
                    tradeamount *= 0.90;
                }
                if (Math.Abs(prices[prices.Count - 1] - prices[prices.Count - 2]) > burstPrice * 2)
                {
                    tradeamount *= 0.90;
                }
                if (Math.Abs(prices[prices.Count - 1] - prices[prices.Count - 2]) > burstPrice * 3)
                {
                    tradeamount *= 0.90;
                }
                if (Math.Abs(prices[prices.Count - 1] - prices[prices.Count - 2]) > burstPrice * 4)
                {
                    tradeamount *= 0.90;
                }
                if (OrderBook.Asks[0].Price - OrderBook.Bids[0].Price > burstPrice * 2)
                {
                    tradeamount *= 0.90;
                }
                if (OrderBook.Asks[0].Price - OrderBook.Bids[0].Price > burstPrice * 3)
                {
                    tradeamount *= 0.90;
                }
                if (OrderBook.Asks[0].Price - OrderBook.Bids[0].Price > burstPrice * 4)
                {
                    tradeamount *= 0.90;
                }

                double tradePrice;
                if (bull)
                {
                    tradePrice = bidPrice;
                }
                else
                {
                    tradePrice = askPrice;
                }
                while (tradeamount >= MinTradeAmount)
                {
                    if (IsBalance)
                    {
                        Thread.Sleep(300);
                        continue;
                    }
                    string raw;
                    string orderid = "";
                    try
                    {
                        if (bull && tradeamount < account.Balances[FromSymbol.ToLower()].available)
                        {
                            orderid = exchange.Buy(exchange.GetLocalTradingPairString(tradepair), bidPrice, tradeamount);
                            log.log("Trading:BUY price:" + bidPrice + " amount:" + tradeamount + " orderid:" + orderid);
                            Console.WriteLine("Trading:BUY price:" + bidPrice + " amount:" + tradeamount + " orderid:" + orderid);
                        }
                        else if (tradeamount < account.Balances[ToSymbol.ToLower()].available / askPrice)
                        {
                            orderid = exchange.Sell(exchange.GetLocalTradingPairString(tradepair), askPrice, tradeamount);
                            log.log("Trading:SELL price:" + askPrice + " amount:" + tradeamount + " orderid:" + orderid);
                            Console.WriteLine("Trading:SELL price:" + askPrice + " amount:" + tradeamount + " orderid:" + orderid);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("error:" + e.Message);
                        //log.log("error:" + e.Message);
                    }

                    Thread.Sleep(200);
                    if (orderid.Length > 0)
                    {
                        while (true)
                        {
                            o = exchange.GetOrderStatus(orderid.ToString(), exchange.GetLocalTradingPairString(tradepair), out raw);
                            if (o != null)
                            {
                                if (o.Status == OrderStatus.ORDER_STATE_PENDING)
                                {
                                    exchange.CancelOrder(o.Id.ToString(), exchange.GetLocalTradingPairString(tradepair), out raw);
                                    log.log("OrderCancel: orderid:" + orderid);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        log.log("OrderDetails: orderid:" + orderid + " price:" + o.Price + " amount:" + o.Amount + " dealamount:" + o.DealAmount + " staus:" + o.Status);
                        tradeamount -= o.DealAmount;
                        tradeamount *= 0.9;
                        if (o.Status == OrderStatus.ORDER_STATE_CANCELED)
                        {
                            // UpdateOrderBook();
                            Thread.Sleep(100);
                            while (bull && bidPrice - tradePrice > +0.1)
                            {
                                tradeamount *= 0.99;
                                tradePrice  += 0.1;
                            }
                            while (bear && askPrice - tradePrice < -0.1)
                            {
                                tradeamount *= 0.99;
                                tradePrice  -= 0.1;
                            }
                        }
                    }
                }
                numTick = 0;
            }
        }
Beispiel #2
0
        static void BalanceAccount()
        {
            string   raw;
            DateTime ds = DateTime.Now;

            while (true)
            {
                try

                {
                    account = exchange.GetAccount(out raw);
                    if (startaccount == null)
                    {
                        startaccount = account.Clone();
                    }
                    if (account == null)
                    {
                        return;
                    }
                    if (OrderBook == null)
                    {
                        return;
                    }
                    FromSymbolFree = account.Balances[FromSymbol.ToLower()].balance;
                    ToSymbolFree   = account.Balances[ToSymbol.ToLower()].balance;
                    double p = FromSymbolFree / (ToSymbolFree / prices[prices.Count - 1] + FromSymbolFree);
                    if (p < 0.2)
                    {
                        IsBalance = true;
                        log.log("Balance:" + FromSymbol + " / (" + FromSymbol + " + " + ToSymbol + ") Pencent is " + p.ToString() + " < 0.4 Start BUY.");
                        Console.WriteLine(FromSymbol + "/(" + FromSymbol + "+" + ToSymbol + ") Pencent is" + p.ToString() + "<0.4 Start BUY.");
                        double p1, p2, p3;
                        p1 = (OrderBook.Asks[0].Price * 0.3 + OrderBook.Bids[0].Price * 0.7);
                        p2 = (OrderBook.Asks[0].Price * 0.5 + OrderBook.Bids[0].Price * 0.5);
                        p3 = (OrderBook.Asks[0].Price * 0.7 + OrderBook.Bids[0].Price * 0.3);
                        if (p1 >= OrderBook.Asks[0].Price)
                        {
                            p1 = OrderBook.Bids[0].Price;
                        }
                        if (p2 >= OrderBook.Asks[0].Price)
                        {
                            p2 = OrderBook.Bids[0].Price;
                        }
                        if (p3 >= OrderBook.Asks[0].Price)
                        {
                            p3 = OrderBook.Bids[0].Price;
                        }
                        try
                        {
                            string oid = exchange.Buy(exchange.GetLocalTradingPairString(tradepair), p1, 0.5);
                            log.log("Trading:Balance BUY price:" + p1 + " orderbook sell at:" + OrderBook.Asks[0].Price + " amount:" + 0.01 + " orderid:" + oid);
                            oid = exchange.Buy(exchange.GetLocalTradingPairString(tradepair), p2, 0.5);
                            log.log("Trading:Balance BUY price:" + p2 + " orderbook sell at:" + OrderBook.Asks[0].Price + " amount:" + 0.01 + " orderid:" + oid);
                            oid = exchange.Buy(exchange.GetLocalTradingPairString(tradepair), p3, 0.5);
                            log.log("Trading:Balance BUY price:" + p3 + " orderbook sell at:" + OrderBook.Asks[0].Price + " amount:" + 0.01 + " orderid:" + oid);
                        }
                        catch
                        { }
                    }


                    if (p > 0.8)
                    {
                        IsBalance = true;
                        log.log("Balance:" + FromSymbol + "/(" + FromSymbol + "+" + ToSymbol + ") Pencent is" + p.ToString() + ">0.6 Start SELL.");
                        Console.WriteLine(FromSymbol + "/(" + FromSymbol + "+" + ToSymbol + ") Pencent is" + p.ToString() + ">0.6 Start SELL.");
                        double p1, p2, p3;
                        p3 = (OrderBook.Asks[0].Price * 0.3 + OrderBook.Bids[0].Price * 0.7);
                        p2 = (OrderBook.Asks[0].Price * 0.5 + OrderBook.Bids[0].Price * 0.5);
                        p1 = (OrderBook.Asks[0].Price * 0.7 + OrderBook.Bids[0].Price * 0.3);
                        if (p1 <= OrderBook.Bids[0].Price)
                        {
                            p1 = OrderBook.Asks[0].Price;
                        }
                        if (p2 >= OrderBook.Bids[0].Price)
                        {
                            p2 = OrderBook.Asks[0].Price;
                        }
                        if (p3 >= OrderBook.Bids[0].Price)
                        {
                            p3 = OrderBook.Asks[0].Price;
                        }
                        try
                        {
                            string oid = exchange.Sell(exchange.GetLocalTradingPairString(tradepair), p1, 0.5);
                            log.log("Trading:Balance SELL price:" + p1 + " orderbook buy at:" + OrderBook.Bids[0].Price + " amount:" + 0.01 + " orderid:" + oid);
                            oid = exchange.Sell(exchange.GetLocalTradingPairString(tradepair), p2, 0.5);
                            log.log("Trading:Balance SELL price:" + p2 + " orderbook buy at:" + OrderBook.Bids[0].Price + " amount:" + 0.01 + " orderid:" + oid);
                            oid = exchange.Sell(exchange.GetLocalTradingPairString(tradepair), p3, 0.5);
                            log.log("Trading:Balance SELL price:" + p3 + " orderbook buy at:" + OrderBook.Bids[0].Price + " amount:" + 0.01 + " orderid:" + oid);
                        }
                        catch
                        { }
                    }


                    if (p >= 0.2 && p <= 0.8)
                    {
                        IsBalance = false;
                    }
                    double snet = startaccount.Balances[FromSymbol.ToLower()].balance + startaccount.Balances[ToSymbol.ToLower()].balance / OrderBook.Asks[0].Price;
                    double nnet = account.Balances[FromSymbol.ToLower()].balance + account.Balances[ToSymbol.ToLower()].balance / OrderBook.Asks[0].Price;
                    double cp   = nnet / snet;
                    cp = cp * 100;
                    log.log("Start " + FromSymbol + ": " + startaccount.Balances[FromSymbol.ToLower()].balance + " | " + ToSymbol + ": " + startaccount.Balances[ToSymbol.ToLower()].balance + "Net:" + snet + "Now " + FromSymbol + ": " + account.Balances[FromSymbol.ToLower()].balance + " | " + ToSymbol + ": " + account.Balances[ToSymbol.ToLower()].balance + " Net:" + nnet + " " + cp + "%");
                    Console.WriteLine("Start " + FromSymbol + ": " + startaccount.Balances[FromSymbol.ToLower()].balance + " | " + ToSymbol + ": " + startaccount.Balances[ToSymbol.ToLower()].balance + "Net:" + snet + "Now " + FromSymbol + ": " + account.Balances[FromSymbol.ToLower()].balance + " | " + ToSymbol + ": " + account.Balances[ToSymbol.ToLower()].balance + " Net:" + nnet + " " + cp + "%");
                    //Thread.Sleep(BalanceTimeMinsec);
                    while (true)
                    {
                        if ((DateTime.Now - ds).TotalSeconds < 10)
                        {
                            continue;
                        }
                        else
                        {
                            ds = DateTime.Now; break;
                        }
                    }
                    exchange.CancelAllOrders();
                }
                catch (Exception e)
                {
                    log.log("error:" + e.Message);
                    Console.WriteLine("Balance account error:" + e.Message);
                }
            }
        }