Пример #1
0
        public void Initialize()
        {
            depth = new MtGoxDepthInfo();
            tradeListInFiveMin = new List<MtGoxTrade>();
            tradeListInOneMin = new List<MtGoxTrade>();
            ticker = new MtGoxTickerItem();

            for (int i = 0; i < 100; i++)
            {
                MtGoxAsk a = new MtGoxAsk();
                a.amount = i + 1;
                a.price = i + 1;
                depth.asks.Add(a);
                MtGoxBid b = new MtGoxBid();
                b.amount = i + 1;
                b.price = i + 1;
                depth.bids.Add(b);
                MtGoxTrade t = new MtGoxTrade();
                t.amount = i + 1;
                t.price = i + 1;
                tradeListInFiveMin.Add(t);
                tradeListInOneMin.Add(t);
            }
            ticker.last = 5;
        }
Пример #2
0
 public bool ShouldExecute(MtGoxDepthInfo depth, List<MtGoxTrade> tradeListInOneMin, List<MtGoxTrade> tradeListInFiveMin, MtGoxTickerItem ticker, double condition)
 {
     if (tradeListInFiveMin != null)
     {
         double amount = 0;
         foreach (MtGoxTrade trade in tradeListInFiveMin)
         {
             amount += trade.amount;
         }
         return amount > condition;
     }
     return false;
 }
Пример #3
0
 public bool ShouldExecute(MtGoxDepthInfo depth, List<MtGoxTrade> tradeListInOneMin, List<MtGoxTrade> tradeListInFiveMin, MtGoxTickerItem ticker, double condition)
 {
     if (depth != null && depth.bids.Count > 10)
     {
         double dAmount = 0;
         int index = 0;
         foreach (MtGoxBid bid in depth.bids)
         {
             if (++index > 10)
                 break;
             dAmount += bid.amount;
         }
         return dAmount > condition;
     }
     return false;
 }
Пример #4
0
 public bool ShouldExecute(MtGoxDepthInfo depth, List<MtGoxTrade> tradeListInOneMin, List<MtGoxTrade> tradeListInFiveMin, MtGoxTickerItem ticker, double condition)
 {
     if (tradeListInFiveMin != null && tradeListInFiveMin.Count >= 2)
     {
         return (tradeListInFiveMin[0].price - ticker.last) * 100 / tradeListInFiveMin[0].price > condition;
     }
     return false;
 }
Пример #5
0
 public bool ShouldExecute(MtGoxDepthInfo depth, List<MtGoxTrade> tradeListInOneMin, List<MtGoxTrade> tradeListInFiveMin, MtGoxTickerItem ticker, double condition)
 {
     return ticker.last < condition;
 }
Пример #6
0
        public bool ComputeNewOrders(MtGoxDepthInfo depth, List<MtGoxTrade> tradeListInOneMin, List<MtGoxTrade> tradeListInFiveMin, MtGoxTickerItem ticker, UserInfo user, MtGoxAPIV0 api, List<AutoTradeSettings> autoTradeSettingsList)
        {
            try
            {
                double buyPrice = depth.asks[0].price + OrderTol;
                double sellPrice = depth.bids[0].price - OrderTol;
                int index = 0;
                foreach (AutoTradeSettings autoTrade in autoTradeSettingsList)
                {
                    index++;
                    if (autoTrade.Status == AutoTradeSettings.OrderStatus.Executed)
                        continue;
                    bool execute = false;

                    foreach (RuleSettings rule in autoTrade.Rules)
                    {
                        IAutoTradeRule tradeRule = AutoTradeRuleFactory.CreateAutoTradeRule(rule.RuleIndex);
                        if (tradeRule != null)
                        {
                            execute = tradeRule.ShouldExecute(depth, tradeListInOneMin, tradeListInFiveMin, ticker, rule.RuleCondition);
                            if (!execute)
                            {
                                break;
                            }
                        }
                    }
                    if (execute)
                    {
                        if (autoTrade.Warn)
                        {
                            try
                            {
                                SoundPlayer player = new SoundPlayer(Path.Combine(SoundFolder, autoTrade.Sound));
                                player.Play();
                            }
                            catch
                            {
                            }
                        }
                        if (autoTrade.Trade)
                        {
                            if (autoTrade.TradeType == AutoTradeSettings.OrderType.Sell)
                            {
                                List<MtGoxOrder> order = api.sellBTC(autoTrade.TradeAmount, user.Currency, sellPrice);
                            }
                            else
                            {
                                List<MtGoxOrder> order = api.buyBTC(autoTrade.TradeAmount, user.Currency, buyPrice);
                            }
                            autoTradeSettingsList[index - 1].ExecuteTime = System.DateTime.Now;
                            autoTradeSettingsList[index - 1].Status = AutoTradeSettings.OrderStatus.Executed;
                        }
                    }
                }

            }
            catch(Exception e)
            {
                throw;
            }
            return true;
        }
Пример #7
0
 private void GetRealtimeTrade(object state)
 {
     try
     {
         tickerTimer.Change(int.MaxValue, int.MaxValue);
         currentInfo = mtgoxV0.getDepth(Currency);
         currentTicker = mtgoxV0.ticker();
         DateTime fiveMinuteAgo = System.DateTime.Now - TimeSpan.FromMinutes(5);
         string tidFive = getJasonDate(fiveMinuteAgo) + "000000";
         currenttradeListInFiveMin = mtgoxV0.getTrades(tidFive);
         DateTime oneMinuteAgo = System.DateTime.Now - TimeSpan.FromMinutes(1);
         string tidOne = getJasonDate(oneMinuteAgo) + "000000";
         currenttradeListInOneMin = mtgoxV0.getTrades(tidOne);
         executeAutoTrade();
     }
     catch(Exception ex)
     {
         Trace.WriteLine(string.Format("{0} \r\n stack:{1}", Utils.GetDetailedException(ex), Utils.GetStackTrace(ex)));
     }
     finally
     {
         tickerTimer.Change(lastRefreshTime * 1000, lastRefreshTime * 1000);
         formEvent.Set();
     }
 }
Пример #8
0
        public static MtGoxDepthInfo getObjects(string jsonDataStr)
        {
            MtGoxDepthInfo depthInfo = new MtGoxDepthInfo();
            string json = jsonDataStr;
            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
            dynamic obj = serializer.Deserialize(json, typeof(object));
            double itemPrice = 0, itemAmount = 0, lastPrice = 0, amount = 0, btcCount = 0, fundCount = 0;
            int itemNO = 0;
            for (int i = 0; i < obj.asks.Count; i++)
            {
                MtGoxAsk aa = new MtGoxAsk();
                aa.price = double.Parse(obj.asks[i][0].ToString());
                aa.amount = double.Parse(obj.asks[i][1].ToString());
                depthInfo.asks.Add(aa);
                itemPrice = aa.price;
                itemAmount = aa.amount;
                itemPrice = Math.Round(itemPrice, 2);
                if ( i == 0 || Math.Abs(lastPrice - itemPrice) < epsilon)
                {
                    amount += itemAmount;
                    itemNO++;
                    btcCount += itemAmount;
                    fundCount += itemPrice * itemAmount;
                }
                else
                {
                    DepthInfoItem a = new DepthInfoItem();
                    a.Price = lastPrice;
                    a.ItemNO = itemNO;
                    a.FundCount = fundCount;
                    a.BtcCount = btcCount;
                    a.Amount = amount;
                    depthInfo.askGroup.Add(a);
                    itemNO = 0;
                    amount = 0;
                }
                lastPrice = itemPrice;
                if (i > 1500)
                    break;
            }

            itemPrice = 0;
            itemAmount = 0; lastPrice = 0; amount = 0; btcCount = 0; fundCount = 0;
            itemNO = 0;
            for (int i = obj.bids.Count - 1, j = 0; i >= 0; i--, j++)
            {
                MtGoxBid bb = new MtGoxBid();
                bb.price = double.Parse(obj.bids[i][0].ToString());
                bb.amount = double.Parse(obj.bids[i][1].ToString());
                depthInfo.bids.Add(bb);
                itemPrice = bb.price;
                itemAmount = bb.amount;
                itemPrice = Math.Round(itemPrice, 2);
                if (i == obj.bids.Count - 1 || Math.Abs(lastPrice - itemPrice) < epsilon)
                {
                    amount += itemAmount;
                    itemNO++;
                    btcCount += itemAmount;
                    fundCount += itemPrice * itemAmount;
                }
                else
                {
                    DepthInfoItem a = new DepthInfoItem();
                    a.Price = lastPrice;
                    a.ItemNO = itemNO;
                    a.FundCount = fundCount;
                    a.BtcCount = btcCount;
                    a.Amount = amount;
                    depthInfo.bidGroup.Add(a);
                    itemNO = 0;
                    amount = 0;
                }
                lastPrice = itemPrice;
                if (j > 1500)
                    break;
            }
            return depthInfo;
        }
        public static MtGoxDepthInfo getObjects(string jsonDataStr)
        {
            MtGoxDepthInfo depthInfo  = new MtGoxDepthInfo();
            string         json       = jsonDataStr;
            var            serializer = new JavaScriptSerializer();

            serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
            dynamic obj = serializer.Deserialize(json, typeof(object));
            double  itemPrice = 0, itemAmount = 0, lastPrice = 0, amount = 0, btcCount = 0, fundCount = 0;
            int     itemNO = 0;

            for (int i = 0; i < obj.asks.Count; i++)
            {
                MtGoxAsk aa = new MtGoxAsk();
                aa.price  = double.Parse(obj.asks[i][0].ToString());
                aa.amount = double.Parse(obj.asks[i][1].ToString());
                depthInfo.asks.Add(aa);
                itemPrice  = aa.price;
                itemAmount = aa.amount;
                itemPrice  = Math.Round(itemPrice, 2);
                if (i == 0 || Math.Abs(lastPrice - itemPrice) < epsilon)
                {
                    amount += itemAmount;
                    itemNO++;
                    btcCount  += itemAmount;
                    fundCount += itemPrice * itemAmount;
                }
                else
                {
                    DepthInfoItem a = new DepthInfoItem();
                    a.Price     = lastPrice;
                    a.ItemNO    = itemNO;
                    a.FundCount = fundCount;
                    a.BtcCount  = btcCount;
                    a.Amount    = amount;
                    depthInfo.askGroup.Add(a);
                    itemNO = 0;
                    amount = 0;
                }
                lastPrice = itemPrice;
                if (i > 1500)
                {
                    break;
                }
            }

            itemPrice  = 0;
            itemAmount = 0; lastPrice = 0; amount = 0; btcCount = 0; fundCount = 0;
            itemNO     = 0;
            for (int i = obj.bids.Count - 1, j = 0; i >= 0; i--, j++)
            {
                MtGoxBid bb = new MtGoxBid();
                bb.price  = double.Parse(obj.bids[i][0].ToString());
                bb.amount = double.Parse(obj.bids[i][1].ToString());
                depthInfo.bids.Add(bb);
                itemPrice  = bb.price;
                itemAmount = bb.amount;
                itemPrice  = Math.Round(itemPrice, 2);
                if (i == obj.bids.Count - 1 || Math.Abs(lastPrice - itemPrice) < epsilon)
                {
                    amount += itemAmount;
                    itemNO++;
                    btcCount  += itemAmount;
                    fundCount += itemPrice * itemAmount;
                }
                else
                {
                    DepthInfoItem a = new DepthInfoItem();
                    a.Price     = lastPrice;
                    a.ItemNO    = itemNO;
                    a.FundCount = fundCount;
                    a.BtcCount  = btcCount;
                    a.Amount    = amount;
                    depthInfo.bidGroup.Add(a);
                    itemNO = 0;
                    amount = 0;
                }
                lastPrice = itemPrice;
                if (j > 1500)
                {
                    break;
                }
            }
            return(depthInfo);
        }