Esempio n. 1
0
        public override void UpdatePredictors()
        {
            TickerChangedEventArgs[] tickers = Data.Store.GetTickerData(pair);
            if (tickers == null)
            {
                throw new Exception("Data store returned NULL tickers for pair " + pair);
            }

            predictorMACD.Recalculate(tickers);

            Data.ResultSet.Variable tempVar;
            if (predictorMACD.GetLastResult().variables.TryGetValue("macd", out tempVar))
            {
                USDT_BTC_Trend = tempVar.value;
            }
        }
Esempio n. 2
0
        public override void Setup(bool simulate = false)
        {
            predictorMACD = new Data.Predictors.MACD(pair);

            TickerChangedEventArgs[] tickers = Data.Store.GetTickerData(pair);
            if (tickers == null)
            {
                throw new Exception("Couldn't build predictor history for " + pair + " - no tickers available");
            }

            predictorMACD.Recalculate(tickers);

            Data.ResultSet.Variable tempVar;
            if (predictorMACD.GetLastResult().variables.TryGetValue("macd", out tempVar))
            {
                USDT_BTC_Trend = tempVar.value;
            }
        }
Esempio n. 3
0
        public override void EvaluateTrade()
        {
            TickerChangedEventArgs lastTicker = Data.Store.GetLastTicker(pair);
            double lastPrice = lastTicker.MarketData.PriceLast;
            double buyPrice  = lastTicker.MarketData.OrderTopBuy;
            double sellPrice = lastTicker.MarketData.OrderTopSell;

            if (lastPrice == lastTradePrice)
            {
                return;
            }
            lastTradePrice = lastPrice;

            double currQuoteAmount = Manager.GetWalletState(pair.QuoteCurrency);
            double currBaseAmount  = Manager.GetWalletState(pair.BaseCurrency);

            double currQuoteOrdersAmount = Manager.GetWalletStateOrders(pair.QuoteCurrency);

            double currTradableBaseAmount = currBaseAmount * 1;

            if (currTradableBaseAmount < RuleMinimumBaseAmount.MinimumTradeAmount)
            {
                currTradableBaseAmount = currBaseAmount;
            }

            double postBaseAmount  = currQuoteAmount * buyPrice;
            double postQuoteAmount = currTradableBaseAmount / sellPrice;

            double minPrice = 0;
            double maxPrice = 0;
            double maShort  = 0;
            double maLong   = 0;
            double rsi      = 0;

            // --------------------------

            Data.ResultSet.Variable tempVar;
            if (predictorRSI.GetLastResult().variables.TryGetValue("rsi", out tempVar))
            {
                rsi = tempVar.value;
            }
            if (predictorExtremes.GetLastResult().variables.TryGetValue("min", out tempVar))
            {
                minPrice = tempVar.value;
            }
            if (predictorExtremes.GetLastResult().variables.TryGetValue("max", out tempVar))
            {
                maxPrice = tempVar.value;
            }
            if (predictorMacd.GetLastResult().variables.TryGetValue("maShort", out tempVar))
            {
                maShort = tempVar.value;
            }
            if (predictorMacd.GetLastResult().variables.TryGetValue("maLong", out tempVar))
            {
                maLong = tempVar.value;
            }

            // -------------------------------------------
            // Compile all the rule variables into a dictionary
            // -------------------------------------------

            Dictionary <string, double> ruleVariables = new Dictionary <string, double>();

            ruleVariables.Add("lastBuyTimestamp", LastBuyTime);
            ruleVariables.Add("lastSellTimestamp", LastSellTime);
            ruleVariables.Add("lastTickerTimestamp", lastTicker.Timestamp);

            ruleVariables.Add("price", lastPrice);
            ruleVariables.Add("buyPrice", buyPrice);
            ruleVariables.Add("sellPrice", sellPrice);

            ruleVariables.Add("openPrice", openPosition);

            ruleVariables.Add("quoteAmountOrders", currQuoteOrdersAmount);

            ruleVariables.Add("quoteAmount", currQuoteAmount);
            ruleVariables.Add("baseAmount", currBaseAmount);

            ruleVariables.Add("baseAmountTradable", currTradableBaseAmount);

            ruleVariables.Add("postQuoteAmount", postQuoteAmount);
            ruleVariables.Add("postBaseAmount", postBaseAmount);

            ruleVariables.Add("maShort", maShort);
            ruleVariables.Add("maLong", maLong);
            ruleVariables.Add("rsi", rsi);

            ruleVariables.Add("minPrice", minPrice);
            ruleVariables.Add("maxPrice", maxPrice);

            ruleVariables.Add("minGUI", lastPrice / minPrice);
            ruleVariables.Add("maxGUI", maxPrice / lastPrice);

            // -----------------------
            // Update the sell band price rise offset
            // -----------------------

            ((RuleSellBand)ruleSellBand).PriceRiseOffset = predictorExtremes.PriceRiseOffset;

            // -----------------------
            // Recalculate all the rules
            // -----------------------

            try {
                for (int i = 0; i < allRules.Length; i++)
                {
                    allRules[i].Recalculate(ruleVariables);
                }
            }
            catch (Exception e) {
                Console.WriteLine("Error: " + e.Message);
                return;
            }

            // ----------------
            // Update GUI
            // ----------------

            GUI.GUIManager.UpdateStrategyScreenPair(this.pair, ruleVariables);

            Utility.TradeTracker.UpdateStopLoss(pair, ruleStopLoss.currTrigger);

            // ----------------
            // Custom rule logic
            // ----------------

            #region Buy Logic Tree
            if (ruleMinBase.Result != RuleResult.BlockBuy && ruleMinQuotePost.Result != RuleResult.BlockBuy)
            {
                // we have enough of base and will have enough of quote (after trade) to satisfy minimum trade amount (0.0001)
                // note: this counts the volatility factor, RuleMinimumBaseAmount uses baseAmount * volatility in verification

                if (ruleForce.Result == RuleResult.Buy)
                {
                    Buy(sellPrice, postQuoteAmount, true);
                    return;
                }

                if (ruleDelayAllTrades.Result != RuleResult.BlockBuySell && ruleDelayBuy.Result != RuleResult.BlockBuy && !RuleBubbleSave.BlockTrade)
                {
                    // enough time has passed since the last trades were made

                    if (ruleMinQuote.Result == RuleResult.BlockSell)
                    {
                        // if it's blocking sell that means we don't own quote, so go ahead with buying

                        if (ruleMacdConverge.Result == RuleResult.Buy && ruleRSI.Result == RuleResult.Buy)
                        {
                            Buy(sellPrice, postQuoteAmount, true);
                            return;
                        }
                    }
                }
            }
            #endregion

            #region Sell Logic Tree
            if (ruleMinQuote.Result != RuleResult.BlockSell && ruleMinBasePost.Result != RuleResult.BlockSell)
            {
                // we have enough of quote and will have enough of base (after trade) to satisfy minimum trade amount (0.0001)

                if (ruleForce.Result == RuleResult.Sell)
                {
                    Sell(buyPrice, currQuoteAmount, true);
                    return;
                }

                if (ruleDelayAllTrades.Result != RuleResult.BlockBuySell)
                {
                    // enough time has passed since the last trades were made

                    if (ruleStopLoss.Result == RuleResult.Sell)
                    {
                        // price has dropped below stop-loss

                        Sell(buyPrice, currQuoteAmount, true);
                        return;
                    }

                    if (ruleMinSellprice.Result != RuleResult.BlockSell)
                    {
                        // current price is profitable (2%)

                        if (ruleSellBand.Result == RuleResult.Sell)
                        {
                            // price is below the sell band

                            Sell(buyPrice, currQuoteAmount, false);
                            return;
                        }
                    }
                }
            }
            #endregion
        }
Esempio n. 4
0
        public override void EvaluateTrade()
        {
            TickerChangedEventArgs lastTicker = Data.Store.GetLastTicker(pair);
            double lastPrice = lastTicker.MarketData.PriceLast;
            double buyPrice  = lastTicker.MarketData.OrderTopBuy;
            double sellPrice = lastTicker.MarketData.OrderTopSell;

            double currQuoteAmount = Manager.GetWalletState(pair.QuoteCurrency);
            double currBaseAmount  = Manager.GetWalletState(pair.BaseCurrency);

            double currTradableBaseAmount = currBaseAmount * 0.3; // VolatilityScore;

            double postBaseAmount  = currQuoteAmount * buyPrice;
            double postQuoteAmount = currTradableBaseAmount / sellPrice;

            double meanRev  = 0;
            double minPrice = 0;
            double maxPrice = 0;
            double macd     = 0;

            Data.ResultSet.Variable tempVar;
            if (predictorMeanRev.GetLastResult().variables.TryGetValue("score", out tempVar))
            {
                meanRev = tempVar.value;
            }
            if (predictorExtremes.GetLastResult().variables.TryGetValue("min", out tempVar))
            {
                minPrice = tempVar.value;
            }
            if (predictorExtremes.GetLastResult().variables.TryGetValue("max", out tempVar))
            {
                maxPrice = tempVar.value;
            }
            if (predictorMacd.GetLastResult().variables.TryGetValue("macd", out tempVar))
            {
                macd = tempVar.value;
            }

            // -------------------------------------------
            // Compile all the rule variables into a dictionary
            // -------------------------------------------

            Dictionary <string, double> ruleVariables = new Dictionary <string, double>();

            ruleVariables.Add("lastBuyTimestamp", LastBuyTime);
            ruleVariables.Add("lastSellTimestamp", LastSellTime);
            ruleVariables.Add("lastTickerTimestamp", lastTicker.Timestamp);

            ruleVariables.Add("price", lastPrice);
            ruleVariables.Add("buyPrice", buyPrice);
            ruleVariables.Add("sellPrice", sellPrice);

            ruleVariables.Add("openPrice", openPosition);

            ruleVariables.Add("quoteAmount", currQuoteAmount);
            ruleVariables.Add("baseAmount", currBaseAmount);

            ruleVariables.Add("baseAmountTradable", currTradableBaseAmount);

            ruleVariables.Add("postQuoteAmount", postQuoteAmount);
            ruleVariables.Add("postBaseAmount", postBaseAmount);

            ruleVariables.Add("meanRev", meanRev);

            ruleVariables.Add("minPrice", minPrice);
            ruleVariables.Add("maxPrice", maxPrice);

            ruleVariables.Add("minGUI", lastPrice / minPrice);
            ruleVariables.Add("maxGUI", maxPrice / lastPrice);

            ruleVariables.Add("macd", macd);

            // -----------------------
            // Recalculate all the rules
            // -----------------------

            try {
                for (int i = 0; i < allRules.Length; i++)
                {
                    allRules[i].Recalculate(ruleVariables);
                }
            }
            catch (Exception e) {
                Console.WriteLine("Error: " + e.Message);
                return;
            }

            // ----------------
            // Update GUI
            // ----------------

            GUI.GUIManager.UpdateStrategyScreenPair(this.pair, ruleVariables);

            // ----------------
            // Custom rule logic
            // ----------------

            #region Buy Logic Tree
            if (ruleMinBase.Result != RuleResult.BlockBuy && ruleMinQuotePost.Result != RuleResult.BlockBuy)
            {
                // we have enough of base and will have enough of quote (after trade) to satisfy minimum trade amount (0.0001)
                // note: this counts the volatility factor, RuleMinimumBaseAmount uses baseAmount * volatility in verification

                if (ruleForce.Result == RuleResult.Buy)
                {
                    Buy(sellPrice, postQuoteAmount);
                    return;
                }

                if (ruleDelayAllTrades.Result != RuleResult.BlockBuySell && ruleDelayBuy.Result != RuleResult.BlockBuy)
                {
                    // enough time has passed since the last trades were made

                    if (ruleMinQuote.Result == RuleResult.BlockSell)
                    {
                        // if it's blocking sell that means we don't own quote, so go ahead with buying

                        if (ruleMacd.currentResult == RuleResult.Buy && ruleMeanRev.currentResult == RuleResult.Buy)
                        {
                            // MACD has stopped falling and price is below average

                            debugMacd    = macd;
                            debugMeanRev = meanRev;

                            Buy(sellPrice, postQuoteAmount);
                            return;
                        }
                    }
                }
            }
            #endregion

            #region Sell Logic Tree
            if (ruleMinQuote.Result != RuleResult.BlockSell && ruleMinBasePost.Result != RuleResult.BlockSell)
            {
                // we have enough of quote and will have enough of base (after trade) to satisfy minimum trade amount (0.0001)

                if (ruleForce.Result == RuleResult.Sell)
                {
                    Sell(buyPrice, currQuoteAmount);
                    return;
                }

                if (ruleDelayAllTrades.Result != RuleResult.BlockBuySell)
                {
                    // enough time has passed since the last trades were made

                    if (ruleMinSellprice.Result != RuleResult.BlockSell)
                    {
                        // current price is profitable

                        if (ruleSellBand.Result == RuleResult.Sell)
                        {
                            // price is below the sell band

                            Sell(buyPrice, currQuoteAmount);

                            SaveTradeData(true, LastSellTime - LastBuyTime);

                            return;
                        }
                    }

                    /*
                     * if (ruleMacd.currentResult == RuleResult.Sell) {
                     *  // price is currently dropping
                     *
                     *  Sell(buyPrice, currQuoteAmount);
                     *
                     *  SaveTradeData(false, LastSellTime - LastBuyTime);
                     *
                     *  return;
                     * }
                     */
                    if (ruleStopLoss.Result == RuleResult.Sell)
                    {
                        // price has dropped below stop-loss

                        Sell(buyPrice, currQuoteAmount);

                        SaveTradeData(false, LastSellTime - LastBuyTime);

                        return;
                    }
                }
            }
            #endregion
        }