Ejemplo n.º 1
0
        private List <TradingRule> RuleGenerator(Pattern x, Pattern y)
        {
            List <TradingRule> lstTradingRule = new List <TradingRule>();

            if (x.TIDSet[0] != y.TIDSet[0])
            {
                TradingRule trRule1 = new TradingRule();
                TradingRule trRule2 = new TradingRule();
                trRule1.BP          = x;
                trRule1.SP          = y;
                trRule1.topPriority = TP.Both;
                trRule2.BP          = y;
                trRule2.SP          = x;
                trRule2.topPriority = TP.Both;
                lstTradingRule.Add(trRule1);
                lstTradingRule.Add(trRule2);
            }
            else if (ComparePattern(x, y) == 0)
            {
                TradingRule trRule1 = new TradingRule();
                TradingRule trRule2 = new TradingRule();
                trRule1.BP          = x;
                trRule1.SP          = y;
                trRule1.topPriority = TP.BF;
                trRule2.BP          = x;
                trRule2.SP          = y;
                trRule2.topPriority = TP.SF;
                lstTradingRule.Add(trRule1);
                lstTradingRule.Add(trRule2);
            }
            else if (ComparePattern(x, y) == -1)
            {
                TradingRule trRule1 = new TradingRule();
                TradingRule trRule2 = new TradingRule();
                trRule1.BP          = x;
                trRule1.SP          = y;
                trRule1.topPriority = TP.BF;
                trRule2.BP          = x;
                trRule2.SP          = y;
                trRule2.topPriority = TP.SF;
                lstTradingRule.Add(trRule1);
                lstTradingRule.Add(trRule2);
            }

            return(lstTradingRule);
        }
Ejemplo n.º 2
0
        private TradingResult Simulate(TradingRule tradingRule)
        {
            TradingResult             tradingResult = new TradingResult();
            List <SimulatitonPartern> simulateds    = new List <SimulatitonPartern>();
            POS        pOS   = new POS();
            int        index = 1;
            bool       isBuyTradingCommand = false;
            List <int> tidBP = new List <int>();

            if (tradingRule.BP?.TIDSet.Count() > 0)
            {
                tidBP.AddRange(tradingRule.BP?.TIDSet);
            }
            List <int> tidSP = new List <int>();

            if (tradingRule.SP?.TIDSet.Count() > 0)
            {
                tidSP.AddRange(tradingRule.SP?.TIDSet);
            }
            //

            // check if exist at least one complete trading order || Sell trading Command and existing sell transactions when buy transaction end.
            while ((tidBP?.Count > 0 && tidSP?.Count > 0) || (isBuyTradingCommand && tidSP?.Count > 0))
            {
                // generate all Trading order and Hold position
                SimulatitonPartern sm = new SimulatitonPartern();
                sm.TradingOrder  = new TradingOrder();
                sm.TradingResult = new TradingResult();
                sm.HPOS          = new POS();
                if (index == 1)
                {
                    sm.No = index;
                    sm.TradingOrder.tc = tradingRule.topPriority == TP.SF ? TradingCommands.Sell : TradingCommands.Buy;

                    sm.TradingOrder.qty = 1;
                    //transaction start at 0-1-2, tid start at 1-2-3
                    sm.TradingOrder.price = sm.TradingOrder.tc == TradingCommands.Buy ? transactions[tidBP[0] - 1].Price : transactions[tidSP[0] - 1].Price;
                    sm.TID         = sm.TradingOrder.tc == TradingCommands.Buy ? tidBP[0] : tidSP[0];
                    sm.HPOS.mp     = sm.TradingOrder.tc == TradingCommands.Buy ? MarketPosition.Long : MarketPosition.Short;
                    sm.HPOS.hqty   = 1;
                    sm.HPOS.hprice = sm.TradingOrder.price;
                    //
                    isBuyTradingCommand = sm.TradingOrder.tc == TradingCommands.Buy ? true : false;
                }
                else
                {
                    if (isBuyTradingCommand)
                    {
                        //simutaled trading is sell
                        sm.No = index;
                        sm.TradingOrder.tc    = TradingCommands.Sell;
                        sm.TradingOrder.qty   = 1;
                        sm.TradingOrder.price = transactions[tidSP[0] - 1].Price;
                        sm.TID              = tidSP[0];
                        sm.HPOS.mp          = MarketPosition.Short;
                        sm.HPOS.hqty        = 1;
                        sm.HPOS.hprice      = sm.TradingOrder.price;
                        isBuyTradingCommand = false;
                    }
                    else
                    {
                        //simutaled trading is buy
                        sm.No = index;
                        sm.TradingOrder.tc    = TradingCommands.Buy;
                        sm.TradingOrder.qty   = 1;
                        sm.TradingOrder.price = transactions[tidBP[0] - 1].Price;
                        sm.TID              = tidBP[0];
                        sm.HPOS.mp          = MarketPosition.Long;
                        sm.HPOS.hqty        = 1;
                        sm.HPOS.hprice      = sm.TradingOrder.price;
                        isBuyTradingCommand = true;
                    }
                }

                if (sm.TradingOrder.tc == TradingCommands.Buy)
                {
                    if (tidSP[0] == tidBP[0])
                    {
                        tidSP.RemoveAt(0);
                    }
                    tidBP.RemoveAt(0);
                }
                else
                {
                    // avoid empty buy pattern
                    if (tidBP.Count() > 0 && tidBP[0] == tidSP[0])
                    {
                        tidBP.RemoveAt(0);
                    }
                    tidSP.RemoveAt(0);
                }

                index++;
                simulateds.Add(sm);
            }

            for (int i = 0; i < simulateds.Count; i++)
            {
                // set Netprofit
                // Last record don't have Netprofit
                simulateds[i].NP = i == (simulateds.Count - 1) ? 0 : simulateds[i].HPOS.mp == MarketPosition.Long ?
                                   simulateds[i + 1].HPOS.hprice - simulateds[i].HPOS.hprice - MAX_SPAN * FEE :
                                   simulateds[i].HPOS.hprice - simulateds[i + 1].HPOS.hprice - MAX_SPAN * FEE;

                // set Consecutive loss
                //Last record don't have Netprofit
                simulateds[i].CLoss = i == 0 ? simulateds[i].NP : simulateds[i].NP + simulateds[i - 1].CLoss;

                // Closs could not be positive && Last record don't have Netprofit
                simulateds[i].CLoss = (simulateds[i].CLoss > 0) || i == (simulateds.Count - 1) ? 0 : simulateds[i].CLoss;


                // Set Run UP && Draw Down
                if (i == simulateds.Count - 1)
                {
                    if (isExistsTID(simulateds[i].TID))
                    {
                        simulateds[i].DD = simulateds[i].HPOS.mp == MarketPosition.Short ? 0 :
                                           i == 0 ? 0 + Math.Min(simulateds[i].HPOS.hprice, GetTransactionPrice(simulateds[i].TID + 1)) - simulateds[i].HPOS.hprice :
                                           simulateds[i - 1].CLoss + Math.Min(simulateds[i].HPOS.hprice, GetTransactionPrice(simulateds[i].TID + 1)) - simulateds[i].HPOS.hprice;

                        simulateds[i].RU = simulateds[i].HPOS.mp == MarketPosition.Long ? 0 :
                                           i == 0 ? 0 - Math.Max(simulateds[i].HPOS.hprice, GetTransactionPrice(simulateds[i].TID + 1)) + simulateds[i].HPOS.hprice :
                                           simulateds[i - 1].CLoss - Math.Max(simulateds[i].HPOS.hprice, GetTransactionPrice(simulateds[i].TID + 1)) + simulateds[i].HPOS.hprice;
                    }
                }
                else
                {
                    simulateds[i].DD = simulateds[i].HPOS.mp == MarketPosition.Short ? 0 :
                                       i == 0 ? 0 + Math.Min(simulateds[i].HPOS.hprice, simulateds[i + 1].HPOS.hprice) - simulateds[i].HPOS.hprice :
                                       simulateds[i - 1].CLoss + Math.Min(simulateds[i].HPOS.hprice, simulateds[i + 1].HPOS.hprice) - simulateds[i].HPOS.hprice;
                    simulateds[i].RU = simulateds[i].HPOS.mp == MarketPosition.Long ? 0 :
                                       i == 0 ? 0 - Math.Max(simulateds[i].HPOS.hprice, simulateds[i + 1].HPOS.hprice) + simulateds[i].HPOS.hprice :
                                       simulateds[i - 1].CLoss - Math.Max(simulateds[i].HPOS.hprice, simulateds[i + 1].HPOS.hprice) + simulateds[i].HPOS.hprice;
                }

                // Set Profit
                simulateds[i].TradingResult.Profit = i == (simulateds.Count - 1) ? 0 : simulateds.Sum(o => o.NP);

                //Set Risk
                simulateds[i].TradingResult.Risk = i == 0 ? new List <double>()
                {
                    Absolute(simulateds[i].CLoss), Absolute(simulateds[i].DD), Absolute(simulateds[i].RU)
                }.Max() :
                new List <double>()
                {
                    Absolute(simulateds[i].CLoss), Absolute(simulateds[i].DD), Absolute(simulateds[i].RU), Absolute(simulateds[i - 1].TradingResult.Risk)
                }.Max();

                //Set WinRate
                simulateds[i].TradingResult.WinRate = i == (simulateds.Count - 1) ? 0 : Math.Round(simulateds.Where(o => o.NP > 0).Count() * 1.0 / simulateds[i].No * 100, 0);
            }

            tradingResult.Profit  = simulateds[simulateds.Count - 2].TradingResult.Profit;
            tradingResult.Risk    = isExistsTID(simulateds[simulateds.Count - 1].TID) ? simulateds[simulateds.Count - 1].TradingResult.Risk : simulateds[simulateds.Count - 2].TradingResult.Risk;
            tradingResult.WinRate = simulateds[simulateds.Count - 2].TradingResult.WinRate;

            return(tradingResult);
        }