Пример #1
0
        static void Main(string[] args)
        {
            MtGoxAPIV0 v0 = new MtGoxAPIV0();

            v0.apiKey    = "";
            v0.apiSecret = "";


            v0.buyBTC(0.01, MtGoxCurrencySymbol.USD, (double)5);
        }
        private void mtgoxTrade(Advice advice)
        {
            if (advice.Group == GroupBy.hour)
            {
                MtGoxAPIV0 v0 = new MtGoxAPIV0();
                v0.apiKey    = ConfigurationManager.AppSettings["mtgoxAPIKey"];
                v0.apiSecret = ConfigurationManager.AppSettings["mtgoxAPISecret"];

                List <MtGoxOrder> orders = v0.getOrders(MtGoxOrderType.Buy, MtGoxOrderStatus.Invalid);
                orders.AddRange(v0.getOrders(MtGoxOrderType.Buy, MtGoxOrderStatus.Active));
                orders.AddRange(v0.getOrders(MtGoxOrderType.Buy, MtGoxOrderStatus.NotSufficientFunds));
                orders.AddRange(v0.getOrders(MtGoxOrderType.Buy, MtGoxOrderStatus.Pending));
                orders.AddRange(v0.getOrders(MtGoxOrderType.Sell, MtGoxOrderStatus.Active));
                orders.AddRange(v0.getOrders(MtGoxOrderType.Sell, MtGoxOrderStatus.NotSufficientFunds));
                orders.AddRange(v0.getOrders(MtGoxOrderType.Sell, MtGoxOrderStatus.Pending));
                orders.AddRange(v0.getOrders(MtGoxOrderType.Sell, MtGoxOrderStatus.Invalid));

                foreach (MtGoxOrder order in orders)
                {
                    v0.cancelOrder(order.oid, order.type);
                }

                decimal adjustedPrice = decimal.MaxValue;
                if (advice.Action == TradeAction.Buy)
                {
                    MtGoxHistoryItem funds      = v0.info();
                    double           cashOnHand = 0;
                    foreach (MtGoxWallet wall in funds.Wallets)
                    {
                        if (wall.name == MtGoxCurrencySymbol.USD)
                        {
                            cashOnHand = wall.balance.value;
                        }
                    }
                    adjustedPrice = advice.Price * (decimal)(1 - funds.Trade_Fee);
                    v0.buyBTC(cashOnHand, MtGoxCurrencySymbol.USD, (double)adjustedPrice);
                }
                else if (advice.Action == TradeAction.Sell)
                {
                    MtGoxHistoryItem funds      = v0.info();
                    double           cashOnHand = 0;
                    foreach (MtGoxWallet wall in funds.Wallets)
                    {
                        if (wall.name == MtGoxCurrencySymbol.BTC)
                        {
                            cashOnHand = wall.balance.value;
                        }
                    }
                    adjustedPrice = advice.Price * (decimal)(1 + funds.Trade_Fee);
                    v0.sellBTC(21, MtGoxCurrencySymbol.USD, (double)adjustedPrice);
                }
            }
        }
        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);
        }