Exemplo n.º 1
0
        public void UpdateMinMax()
        {
            // Update Min Max
            DerivativeSymbolQuote maxDi = MaxTick.Di;
            DerivativeSymbolQuote minDi = MinTick.Di;

            PercChangeFromMax = 100 * ((StockUtils.GetPriceForAnalysis(CurrTick.Di) - StockUtils.GetPriceForAnalysis(maxDi)) / StockUtils.GetPriceForAnalysis(maxDi));
            PercChangeFromMin = 100 * ((StockUtils.GetPriceForAnalysis(CurrTick.Di) - StockUtils.GetPriceForAnalysis(minDi)) / StockUtils.GetPriceForAnalysis(minDi));

            if (PercChangeFromMax >= 0)
            {
                // New Max
                MaxTick = CurrTick;
            }
            else if (PercChangeFromMin <= 0)
            {
                // New Min
                MinTick = CurrTick;
            }

            // Get trend
            if (Math.Abs(CurrTick.Di.PercChangeFromPrevious) > 1.5)
            {
                DominantDirection = CurrTick.Di.PercChangeFromPrevious > 0 ? MarketDirection.UP : MarketDirection.DOWN;
            }
        }
Exemplo n.º 2
0
        private static void SyncHistory()
        {
            Console.WriteLine("Getting Symbols...");
            var symbols = StockUtils.GetSymbols();

            Console.WriteLine("");
            Console.WriteLine("Found {0} symbols", symbols.Count());
            List <Historic> historics = null;

            foreach (var symbol in symbols)
            {
                IHistoryService _historyService = new HistoryService();

                //Delete stale data (more than a year old)
                Console.WriteLine("{0} -- Deleting stale data...", symbol);

                _historyService.DeleteStaleHistory(symbol);

                var dbHistories = _historyService.GetHistories(symbol);

                // If there's no history for the symbol on the new list, add a year of history
                if (dbHistories.Count() == 0)
                {
                    Console.WriteLine("{0} -- No history found. Getting a years worth...", symbol);

                    historics = GetHistory(symbol, DateTime.Now.AddYears(-1), DateTime.Now);

                    if (historics != null && historics.Count < 210)
                    {
                        Console.WriteLine("{0} -- Not enough historic records found on yahoo ({1}). [SKIPPED]...", symbol, historics.Count);

                        continue;
                    }
                }
                else
                {
                    //if (dbHistories.OrderByDescending(t => t.Date).First().Date == DateTime.Now.Date)
                    //{
                    //    continue;
                    //}

                    // Add history beginning from it's latest record
                    //var latestHistoryDate = dbHistories.OrderByDescending(t => t.Date).First().Date;

                    //Going a month back (re-calibrating)
                    var latestHistoryDate = dbHistories.OrderByDescending(t => t.Date).First().Date.AddMonths(-1);

                    Console.WriteLine("{0} -- DB History found ({1}). Getting Yahoo history starting at {2}...", symbol, dbHistories.Count(), latestHistoryDate.AddDays(1).ToString());
                    historics = GetHistory(symbol, latestHistoryDate.AddDays(1), DateTime.Now);
                }

                // If we were able to pull records from yahoo, add/update database
                if (historics != null)
                {
                    Console.WriteLine("{0} -- Yahoo History found. Adding {1} records...", symbol, historics.Count());

                    _historyService.AddOrUpdateHistorics(historics);
                }

                _historyService.Dispose();
                Console.WriteLine("~~~ Sleeping ~~~");
                Thread.Sleep(2000);
            }
        }
Exemplo n.º 3
0
        public override void AddTick(SymbolTick si)
        {
            if (!AlgoUpdateTick(si))
            {
                PostProcAddTick();
                return;
            }

            // Get call-put tick for mock, for live implement later
            if (AlgoParams.IsReplayMode)
            {
                bool isBuy = S.TotalBuyTrades < S.TotalSellTrades;
                if (S.TotalBuyTrades != S.TotalSellTrades)
                {
                    var ordCall = GetPairedSquareOffOrder(isBuy ? Position.BUY : Position.SELL, DerivativePositionType.Call, false);
                    if (ordCall != null)
                    {
                        var strkPrc = (int)ordCall.OrderTick.Di.StrikePriceDouble;
                        // get tick from tick store
                        S.TickCall = StockUtils.GetNearestOptionTick(S.Ticks.CurrTick, S.TicksCall, ref strkPrc, S.TickCall);
                    }
                    else
                    {
                        S.TickCall = null;
                    }

                    var ordPut = GetPairedSquareOffOrder(isBuy ? Position.BUY : Position.SELL, DerivativePositionType.Put, false);
                    if (ordPut != null)
                    {
                        var strkPrc = (int)ordPut.OrderTick.Di.StrikePriceDouble;
                        // get tick from tick store
                        S.TickPut = StockUtils.GetNearestOptionTick(S.Ticks.CurrTick, S.TicksPut, ref strkPrc, S.TickPut);
                    }
                    else
                    {
                        S.TickPut = null;
                    }
                }
            }

            // Remotely called squareoff
            if (S.IsSquareoffAtProfit)
            {
                if (S.TotalBuyTrades != S.TotalSellTrades)
                {
                    var profitPerc = GetSquareOffProfitPerc();
                    if (profitPerc > 0.05)
                    {
                        var orderPlaceCode = TryImmediateSquareOff(false, false, true);
                        if (orderPlaceCode == OrderPlaceAlgoCode.SUCCESS || orderPlaceCode != OrderPlaceAlgoCode.RETRY)
                        {
                            S.IsSquareoffAtProfit = false;
                            if (S.IsPauseAfterSquareOffAtProfit)
                            {
                                Pause(false);
                                S.IsPauseAfterSquareOffAtProfit = false;
                            }
                        }
                    }
                }
            }

            if (AlgoParams.AlgoId >= 150)
            {
                Scalper20(1, 0.5);
            }

            else if (AlgoParams.AlgoId < 20)
            {
                Scalper10();
            }
            else if (AlgoParams.AlgoId < 30)
            {
                Scalper10(true, true);
            }
            else if (AlgoParams.AlgoId < 40)
            {
                Scalper20(1, 0.5);
            }
            else if (AlgoParams.AlgoId < 50)
            {
                Scalper20(0.5, 0.5);
            }
            else if (AlgoParams.AlgoId < 100)
            {
                Scalper20(0.5, 0.5, false, true);
            }
            else if (AlgoParams.AlgoId > 115 && AlgoParams.AlgoId < 120)
            {
                Scalper116();
            }
            else
            {
                Scalper20(0.5, 0.4, true);
            }

            PostProcAddTick();
        }
Exemplo n.º 4
0
        // Beyond 0.6 and less than 0.3 does not work good, at least surely in nifty
        // Stoploss 1 is most optimized
        // Min profit 0.5, 0.75 and 1 are optimized. Not needed to dabble with them

        public static TradingAlgo GetAlgo(short algoId, AlgoParams algoParams, bool onlyCheckAlgoExist)//bool doLimitLoss = false)
        {
            TradingAlgo algo        = null;
            bool        isAlgoFound = true;

            //algoParams.AllowInitialTickStabilization = true;


            switch (algoId)
            {
            // -------------------- New Experiments Scalper 150 Algos-------------------------------- //
            case 150:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                break;

            case 151:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                break;

            case 152:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                break;

            case 153:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.3;
                break;

            // --------------------Scalper Algos: Immediate squareoff on min profit. No peak or minmax reset-------------------------------- //
            case 13:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 2;
                break;

            case 14:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 2;
                break;

            case 15:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 2;
                break;

            case 17:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                break;

            case 18:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                break;

            case 19:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                break;

            // --------------------Scalper Algos: Immediate squareoff on min profit. Reset peaktype and minmax-------------------------------- //
            case 20:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                break;

            case 21:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                break;

            case 22:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                break;

            case 23:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.35;
                break;

            // ----------------------Min Profit must. threshold moves (perc-minprofit) ---------------------------------- //
            case 30:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                break;

            case 31:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                break;

            case 32:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                break;

            // ---------------------Min profit + half of over that is must. threshold moves (perc-minprofit)/2------------------------------------------- //
            case 40:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                break;

            case 41:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                break;

            case 42:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                break;

            // -------------------------Same like 40s with reset min-max--------------------------- //
            case 50:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                break;

            case 51:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                break;

            case 52:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                break;

            case 53:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.35;
                break;

            case 60:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 1;
                break;

            case 61:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 2;
                break;

            case 62:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 3;
                break;

            // ------------------ Stop loss threshold moves with profit : lenient------------------------- //
            case 100:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                break;

            case 101:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                break;

            case 102:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                break;

            case 113:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 2;
                break;

            case 114:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 2;
                break;

            case 115:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 2;
                break;

            // ------------------ Stop loss threshold moves with profit :Straight profit/3 lenient------------------------- //
            case 116:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.5;
                break;

            case 117:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 1;
                break;

            case 118:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.75;
                break;

            case 119:
                algoParams.PercSquareOffThreshold = 1;
                algoParams.PercMinProfit          = 0.35;
                algoParams.IsFixedTradesPerDay    = true;
                algoParams.NumTradesStopForDay    = 2;
                break;

            default:
                isAlgoFound = false;
                break;
            }

            // Temporary test
            //if (algoParams.IsReplayMode)
            //{
            //    //algoParams.PercSquareOffThreshold = 1;
            //    //algoParams.AllowInitialTickStabilization = true;
            //    //algoParams.IsFixedTradesPerDay = true;
            //    algoParams.IsLimitLossPerDay = true;
            //    algoParams.NumTradesStopForDay = 2;
            //    algoParams.PercSquareOffThreshold = 1.25;
            //}


            if (!isAlgoFound)
            {
                return(null);
            }
            else if (onlyCheckAlgoExist)
            {
                algoParams.I = new Instrument("dummy", InstrumentType.FutureIndex, 100);
                if (algoParams.IsHedgeAlgo)
                {
                    algo = new AlgoHedge(algoParams);
                }
                else
                {
                    algo = new AlgoScalper(algoParams);
                }

                return(algo);
            }

            algoParams.IsLimitLossPerDay           = true;
            algoParams.PercLossStopForDay          = 2.5;
            algoParams.NumNettLossTradesStopForDay = 5;

            if (algoId >= 10)
            {
                algoParams.IsMinProfitMust = true;
            }

            if (StockUtils.IsInstrumentOptionType(algoParams.I.InstrumentType))
            {
                algoParams.PercMinProfit += 5;
                if (algoParams.PercMarketDirectionChange == 7.05)
                {
                    algoParams.PercSquareOffThreshold += 5;
                }
                else if (algoParams.PercMarketDirectionChange == 7.15)
                {
                    //algoParams.PercMarketDirectionChange += 3.6;
                    algoParams.PercSquareOffThreshold += 15;
                }
                else
                {
                    algoParams.PercSquareOffThreshold += 10;
                }
                algoParams.PercBrokerage = 12000 / (algoParams.I.StrikePrice * algoParams.I.Qty);
                //algoParams.SquareOffBrokerageFactor = 1.3;
            }

            //if (algoId < 10)
            //    algo = new AlgoMinMax(algoParams);
            //else
            if (algoParams.IsHedgeAlgo)
            {
                algo = new AlgoHedge(algoParams);
            }
            else
            {
                algo = new AlgoScalper(algoParams);
            }

            return(algo);
        }