/// <summary>
        /// Convert List of Trades to List of Completed Trades
        /// </summary>
        /// <param name="TradesOnly"></param>
        /// <returns></returns>
        public static List<CompletedTrade> CreateList(List<Trade> TradesOnly)
        {
            var CompList = new List<CompletedTrade>();
            var even = TradesOnly.Count % 2;
            int back = even == 2 ? 0 : 1;

            for (int x = 0; x < TradesOnly.Count - back; x += 2)
            {
                CompletedTrade t = new CompletedTrade();
                t.OpenTrade = TradesOnly[x];
                t.CloseTrade = TradesOnly[x + 1];
                CompList.Add(t);
            }

            if (TradesOnly.Last() != CompList.Last().CloseTrade)
            {
                CompletedTrade last = new CompletedTrade();

                last.OpenTrade = TradesOnly.Last();
                last.CloseTrade.TimeStamp = last.OpenTrade.TimeStamp;
                last.CloseTrade.BuyorSell = Trade.BuySell.None;
                last.CloseTrade.Reason = Trade.Trigger.None;
                last.CloseTrade.CurrentDirection = last.OpenTrade.CurrentDirection;
                last.CloseTrade.TradedPrice = last.OpenTrade.TradedPrice;
                CompList.Add(last);
            }



            return CompList;
        }
Example #2
0
        public static List <CompletedTrade> TakeProfit_Exiguous_GoldenBoil(List <Trade> FullTradeList, int N, double stdev, int triggercount)
        {
            var TakeProfitList = new List <CompletedTrade>();
            var TO             = Trade.TradesOnly(FullTradeList);
            var CompletedList  = CompletedTrade.CreateList(TO);

            var GoldenBoil = Factory_Indicator.createBollingerBand(N, stdev, GlobalObjects.Points, TicTacTec.TA.Library.Core.MAType.Sma);


            foreach (var t in CompletedList)
            {
                var timeframe = from x in FullTradeList
                                where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                                select x;


                var trade = new CompletedTrade();
                trade.OpenTrade = t.OpenTrade;

                int boilTriggerCount = 0;
                foreach (var ts in timeframe)
                {
                    if (t.OpenTrade.Reason == Trade.Trigger.OpenShort)
                    {
                        if (ts.CurrentPrice < GoldenBoil.Where(z => z.TimeStamp == ts.TimeStamp).First().Lower)
                        {
                            boilTriggerCount++;
                        }
                    }
                    if (t.OpenTrade.Reason == Trade.Trigger.OpenLong)
                    {
                        if (ts.CurrentPrice > GoldenBoil.Where(z => z.TimeStamp == ts.TimeStamp).First().Upper)
                        {
                            boilTriggerCount++;
                        }
                    }
                    if (boilTriggerCount == triggercount)
                    {
                        trade.CloseTrade = ts;
                        break;
                    }
                    trade.CloseTrade = ts;
                }

                if (trade.OpenTrade.Reason == Trade.Trigger.OpenLong)
                {
                    trade.CloseTrade.Reason = Trade.Trigger.CloseLong;
                }
                if (trade.OpenTrade.Reason == Trade.Trigger.OpenShort)
                {
                    trade.CloseTrade.Reason = Trade.Trigger.CloseShort;
                }


                TakeProfitList.Add(trade);
            }


            return(TakeProfitList);
        }
Example #3
0
        public static List <CompletedTrade> IntratradeToCandle(List <Trade> FullTradeList)
        {
            var TO = Trade.TradesOnly(FullTradeList);
            var r  = CompletedTrade.CreateList(TO);

            foreach (var to in r)
            {
                var end = to.CloseTrade.TimeStamp;
                if (to.CloseTrade.Reason == Trade.Trigger.None)
                {
                    end = DateTime.Now;
                }

                var pl = from x in FullTradeList
                         where x.TimeStamp >= to.OpenTrade.TimeStamp && x.TimeStamp <= end
                         select x.RunningProfit;

                var minmax = new
                {
                    low   = pl.Min(),
                    high  = pl.Max(),
                    close = pl.Last(),
                    fist  = pl.Skip(1).First(),
                };

                to.CloseTrade.OHLC.Low   = minmax.low;
                to.CloseTrade.OHLC.High  = minmax.high;
                to.CloseTrade.OHLC.Close = minmax.close;
                to.CloseTrade.OHLC.Open  = minmax.fist;
            }

            return(r);
        }
Example #4
0
        /// <summary>
        /// Convert List of Trades to List of Completed Trades
        /// </summary>
        /// <param name="TradesOnly"></param>
        /// <returns></returns>
        public static List <CompletedTrade> CreateList(List <Trade> TradesOnly)
        {
            var CompList = new List <CompletedTrade>();
            var even     = TradesOnly.Count % 2;
            int back     = even == 2 ? 0 : 1;

            for (int x = 0; x < TradesOnly.Count - back; x += 2)
            {
                CompletedTrade t = new CompletedTrade();
                t.OpenTrade  = TradesOnly[x];
                t.CloseTrade = TradesOnly[x + 1];
                CompList.Add(t);
            }

            if (TradesOnly.Last() != CompList.Last().CloseTrade)
            {
                CompletedTrade last = new CompletedTrade();

                last.OpenTrade                   = TradesOnly.Last();
                last.CloseTrade.TimeStamp        = last.OpenTrade.TimeStamp;
                last.CloseTrade.BuyorSell        = Trade.BuySell.None;
                last.CloseTrade.Reason           = Trade.Trigger.None;
                last.CloseTrade.CurrentDirection = last.OpenTrade.CurrentDirection;
                last.CloseTrade.TradedPrice      = last.OpenTrade.TradedPrice;
                CompList.Add(last);
            }



            return(CompList);
        }
Example #5
0
        public static List <CompletedTrade> TakeProfit_Exiguous_AvgProfitPeriod(List <Price> EndOfDay, List <Trade> FullTradeList, int I, int ATR_Range, double ATR_Factor)
        {
            var TakeProfitList = new List <CompletedTrade>();
            var TO             = Trade.TradesOnly(FullTradeList);
            var CompletedList  = CompletedTrade.CreateList(TO);

            var ATR = Factory_Indicator.createATR(ATR_Range, EndOfDay);

            double          pl               = 0;
            double          count            = 0;
            double          index            = 0;
            double          tempPL           = 0;
            double          TotalImprovement = 0;
            List <TestData> TD               = new List <TestData>();

            foreach (var t in CompletedList)
            {
                var timeframe = from x in FullTradeList
                                where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                                select x;


                var TList = timeframe.ToList();


                double A = 0;
                double B = 0;
                for (int x = 0; x < TList.Count; x++)
                {
                    if (x > I && TList.First().TradeVolume != 2)
                    {
                        var adxYesterday = ATR.Where(z => z.TimeStamp.Date.AddDays(-1) == TList[x].TimeStamp.Date);
                        if (adxYesterday.ToList().Count() == 0)
                        {
                            break;
                        }
                        var Z = Math.Round((adxYesterday.First().AvgTrueRange *ATR_Factor), 2);
                        if (TList[x].RunningProfit < Z)
                        {
                            break;
                        }
                        A       = TList[x].RunningProfit;
                        B       = TList.Last().RunningProfit;
                        tempPL += TList[x].RunningProfit;
                        Debug.WriteLine(x + "  " + TList[x].TimeStamp + " vol : " + TList.First().TradeVolume + " ADRxFact " + Z + "         " + A + " VS " + B + " Diff : " + ((A - B) * TList.First().TradeVolume) + " Running " + TotalImprovement);
                        TotalImprovement += ((A - B)); //*TList.First().TradeVolume);
                    }
                }
            }



            Debug.WriteLine("TOTAL IMPROVE I(" + I + ")" + "  ATR_Range(" + ATR_Range + ") ATR_Factor(" + ATR_Factor + ") ====>>  " + TotalImprovement);


            return(CompletedList);
        }
Example #6
0
        public static List <CompletedTrade> TakeProfit_Exiguous_SlowStoch_D(List <Trade> FullTradeList, int FK, int SK, int D, double H, double L)
        {
            var TakeProfitList = new List <CompletedTrade>();
            var TO             = Trade.TradesOnly(FullTradeList);
            var CompletedList  = CompletedTrade.CreateList(TO);

            var SlowStoch = Factory_Indicator.createSlowStochastic(FK, SK, D, GlobalObjects.Points);


            foreach (var t in CompletedList)
            {
                var timeframe = from x in FullTradeList
                                where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                                select x;

                var period = from x in SlowStoch
                             where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                             select x;

                // foreach (var v in period) Debug.WriteLine(v.TimeStamp + "   " + v.D);

                var trade = new CompletedTrade();
                trade.OpenTrade = t.OpenTrade;


                bool     TookProfitLong  = false;
                bool     TookProfitShort = false;
                DateTime lowJumpUp       = t.CloseTrade.TimeStamp;
                DateTime highDipDown     = t.CloseTrade.TimeStamp;

                if (t.OpenTrade.Reason == Trade.Trigger.OpenLong)
                {
                    bool     touchedHigh = period.Where(z => z.D > H).Any();
                    DateTime highTime;

                    bool dipped;

                    if (touchedHigh)
                    {
                        highTime = period.Where(z => z.D > H).Select(a => a).First().TimeStamp;
                        var x = period.Where(z => z.TimeStamp > highTime && z.D < H);
                        dipped = x.Any();
                        if (dipped)
                        {
                            highDipDown    = x.First().TimeStamp;
                            TookProfitLong = true;
                        }
                        else
                        {
                            TookProfitLong = false;
                        }
                    }
                }

                if (t.OpenTrade.Reason == Trade.Trigger.OpenShort)
                {
                    // foreach (var v in period) Debug.WriteLine(v.TimeStamp + "   " + v.D);
                    var      touchedLow = period.Where(z => z.D < L).Any();
                    DateTime lowTime;

                    bool jumped;
                    if (touchedLow)
                    {
                        lowTime = (period.Where(z => z.D < L)).Select(a => a).First().TimeStamp;
                        var x = period.Where(z => z.TimeStamp > lowTime && z.D > L);
                        jumped = x.Any();
                        if (jumped)
                        {
                            lowJumpUp       = x.First().TimeStamp;
                            TookProfitShort = true;
                        }
                        else
                        {
                            TookProfitShort = false;
                        }
                    }
                }

                if (TookProfitLong)
                {
                    trade.CloseTrade.TimeStamp     = highDipDown;
                    trade.CloseTrade.RunningProfit = timeframe.Where(z => z.TimeStamp == highDipDown).First().RunningProfit;
                }
                else
                if (TookProfitShort)
                {
                    trade.CloseTrade.TimeStamp     = lowJumpUp;
                    trade.CloseTrade.RunningProfit = timeframe.Where(z => z.TimeStamp == lowJumpUp).First().RunningProfit;
                }
                else
                {
                    trade.CloseTrade = t.CloseTrade;
                }
                //if (t.OpenTrade.Reason == Trade.Trigger.OpenShort)
                //    if (ts.CurrentPrice < GoldenBoil.Where(z => z.TimeStamp == ts.TimeStamp).First().Lower) boilTriggerCount++;
                //if (t.OpenTrade.Reason == Trade.Trigger.OpenLong)
                //    if (ts.CurrentPrice > GoldenBoil.Where(z => z.TimeStamp == ts.TimeStamp).First().Upper) boilTriggerCount++;
                //if (boilTriggerCount == triggercount)
                //{
                //    trade.CloseTrade = ts;
                //    break;
                //}
                //trade.CloseTrade = ts;


                if (trade.OpenTrade.Reason == Trade.Trigger.OpenLong)
                {
                    trade.CloseTrade.Reason = Trade.Trigger.CloseLong;
                }
                if (trade.OpenTrade.Reason == Trade.Trigger.OpenShort)
                {
                    trade.CloseTrade.Reason = Trade.Trigger.CloseShort;
                }


                TakeProfitList.Add(trade);
            }


            return(TakeProfitList);
        }
Example #7
0
        public static List <CompletedTrade> TakeProfit_Exiguous(List <Trade> FullTradeList, double TakeProfit, double StopLoss)
        {
            var TakeProfitList = new List <CompletedTrade>();
            var TO             = Trade.TradesOnly(FullTradeList);
            var CompletedList  = CompletedTrade.CreateList(TO);


            foreach (var t in CompletedList)
            {
                var pl = from x in FullTradeList
                         where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                         select x;


                var trade = new CompletedTrade();
                trade.OpenTrade = t.OpenTrade;

                //Check profit
                Trade profitTrade;
                Trade lossTrade;
                if (pl.Any(z => z.RunningProfit > TakeProfit))
                {
                    profitTrade = pl.Where(z => z.RunningProfit > TakeProfit).First();
                }
                else
                {
                    profitTrade = t.CloseTrade;
                }



                // check loss
                if (pl.Any(z => z.RunningProfit < StopLoss))
                {
                    lossTrade = pl.Where(z => z.RunningProfit < StopLoss).First();
                }
                else
                {
                    lossTrade = t.CloseTrade;
                }

                if (lossTrade.TimeStamp > profitTrade.TimeStamp)
                {
                    trade.CloseTrade = profitTrade;
                }
                else
                {
                    trade.CloseTrade = lossTrade;
                }


                if (trade.OpenTrade.Reason == Trade.Trigger.OpenLong)
                {
                    trade.CloseTrade.Reason = Trade.Trigger.CloseLong;
                }
                if (trade.OpenTrade.Reason == Trade.Trigger.OpenShort)
                {
                    trade.CloseTrade.Reason = Trade.Trigger.CloseShort;
                }


                TakeProfitList.Add(trade);
            }


            return(TakeProfitList);
        }
        public static List<CompletedTrade> TakeProfit_Exiguous_SlowStoch_D(List<Trade> FullTradeList, int FK, int SK, int D, double H, double L)
        {
            var TakeProfitList = new List<CompletedTrade>();
            var TO = Trade.TradesOnly(FullTradeList);
            var CompletedList = CompletedTrade.CreateList(TO);

            var SlowStoch = Factory_Indicator.createSlowStochastic(FK, SK, D, GlobalObjects.Points);


            foreach (var t in CompletedList)
            {
                var timeframe = from x in FullTradeList
                                where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                                select x;

                var period = from x in SlowStoch
                             where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                             select x;

                // foreach (var v in period) Debug.WriteLine(v.TimeStamp + "   " + v.D);

                var trade = new CompletedTrade();
                trade.OpenTrade = t.OpenTrade;


                bool TookProfitLong = false;
                bool TookProfitShort = false;
                DateTime lowJumpUp = t.CloseTrade.TimeStamp;
                DateTime highDipDown = t.CloseTrade.TimeStamp;

                if (t.OpenTrade.Reason == Trade.Trigger.OpenLong)
                {
                    bool touchedHigh = period.Where(z => z.D > H).Any();
                    DateTime highTime;

                    bool dipped;

                    if (touchedHigh)
                    {
                        highTime = period.Where(z => z.D > H).Select(a => a).First().TimeStamp;
                        var x = period.Where(z => z.TimeStamp > highTime && z.D < H);
                        dipped = x.Any();
                        if (dipped)
                        {
                            highDipDown = x.First().TimeStamp;
                            TookProfitLong = true;
                        }
                        else
                            TookProfitLong = false;
                    }
                }

                if (t.OpenTrade.Reason == Trade.Trigger.OpenShort)
                {
                    // foreach (var v in period) Debug.WriteLine(v.TimeStamp + "   " + v.D);
                    var touchedLow = period.Where(z => z.D < L).Any();
                    DateTime lowTime;

                    bool jumped;
                    if (touchedLow)
                    {

                        lowTime = (period.Where(z => z.D < L)).Select(a => a).First().TimeStamp;
                        var x = period.Where(z => z.TimeStamp > lowTime && z.D > L);
                        jumped = x.Any();
                        if (jumped)
                        {
                            lowJumpUp = x.First().TimeStamp;
                            TookProfitShort = true;
                        }
                        else
                            TookProfitShort = false;
                    }

                }

                if (TookProfitLong)
                {
                    trade.CloseTrade.TimeStamp = highDipDown;
                    trade.CloseTrade.RunningProfit = timeframe.Where(z => z.TimeStamp == highDipDown).First().RunningProfit;
                }
                else
                    if (TookProfitShort)
                    {
                        trade.CloseTrade.TimeStamp = lowJumpUp;
                        trade.CloseTrade.RunningProfit = timeframe.Where(z => z.TimeStamp == lowJumpUp).First().RunningProfit;
                    }
                    else
                        trade.CloseTrade = t.CloseTrade;
                //if (t.OpenTrade.Reason == Trade.Trigger.OpenShort)
                //    if (ts.CurrentPrice < GoldenBoil.Where(z => z.TimeStamp == ts.TimeStamp).First().Lower) boilTriggerCount++;
                //if (t.OpenTrade.Reason == Trade.Trigger.OpenLong)
                //    if (ts.CurrentPrice > GoldenBoil.Where(z => z.TimeStamp == ts.TimeStamp).First().Upper) boilTriggerCount++;
                //if (boilTriggerCount == triggercount)
                //{
                //    trade.CloseTrade = ts;
                //    break;
                //}
                //trade.CloseTrade = ts;


                if (trade.OpenTrade.Reason == Trade.Trigger.OpenLong) trade.CloseTrade.Reason = Trade.Trigger.CloseLong;
                if (trade.OpenTrade.Reason == Trade.Trigger.OpenShort) trade.CloseTrade.Reason = Trade.Trigger.CloseShort;


                TakeProfitList.Add(trade);

            }


            return TakeProfitList;
        }
        public static List<CompletedTrade> TakeProfit_Exiguous_GoldenBoil(List<Trade> FullTradeList, int N, double stdev, int triggercount)
        {
            var TakeProfitList = new List<CompletedTrade>();
            var TO = Trade.TradesOnly(FullTradeList);
            var CompletedList = CompletedTrade.CreateList(TO);

            var GoldenBoil = Factory_Indicator.createBollingerBand(N, stdev, GlobalObjects.Points, TicTacTec.TA.Library.Core.MAType.Sma);


            foreach (var t in CompletedList)
            {
                var timeframe = from x in FullTradeList
                                where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                                select x;


                var trade = new CompletedTrade();
                trade.OpenTrade = t.OpenTrade;

                int boilTriggerCount = 0;
                foreach (var ts in timeframe)
                {
                    if (t.OpenTrade.Reason == Trade.Trigger.OpenShort)
                        if (ts.CurrentPrice < GoldenBoil.Where(z => z.TimeStamp == ts.TimeStamp).First().Lower) boilTriggerCount++;
                    if (t.OpenTrade.Reason == Trade.Trigger.OpenLong)
                        if (ts.CurrentPrice > GoldenBoil.Where(z => z.TimeStamp == ts.TimeStamp).First().Upper) boilTriggerCount++;
                    if (boilTriggerCount == triggercount)
                    {
                        trade.CloseTrade = ts;
                        break;
                    }
                    trade.CloseTrade = ts;
                }

                if (trade.OpenTrade.Reason == Trade.Trigger.OpenLong) trade.CloseTrade.Reason = Trade.Trigger.CloseLong;
                if (trade.OpenTrade.Reason == Trade.Trigger.OpenShort) trade.CloseTrade.Reason = Trade.Trigger.CloseShort;


                TakeProfitList.Add(trade);

            }


            return TakeProfitList;
        }
        public static List<CompletedTrade> TakeProfit_Exiguous(List<Trade> FullTradeList, double TakeProfit, double StopLoss)
        {
            var TakeProfitList = new List<CompletedTrade>();
            var TO = Trade.TradesOnly(FullTradeList);
            var CompletedList = CompletedTrade.CreateList(TO);


            foreach (var t in CompletedList)
            {
                var pl = from x in FullTradeList
                         where x.TimeStamp >= t.OpenTrade.TimeStamp && x.TimeStamp <= t.CloseTrade.TimeStamp
                         select x;


                var trade = new CompletedTrade();
                trade.OpenTrade = t.OpenTrade;

                //Check profit
                Trade profitTrade;
                Trade lossTrade;
                if (pl.Any(z => z.RunningProfit > TakeProfit))
                    profitTrade = pl.Where(z => z.RunningProfit > TakeProfit).First();
                else
                    profitTrade = t.CloseTrade;



                // check loss
                if (pl.Any(z => z.RunningProfit < StopLoss))
                    lossTrade = pl.Where(z => z.RunningProfit < StopLoss).First();
                else
                    lossTrade = t.CloseTrade;

                if (lossTrade.TimeStamp > profitTrade.TimeStamp)
                    trade.CloseTrade = profitTrade;
                else
                    trade.CloseTrade = lossTrade;


                if (trade.OpenTrade.Reason == Trade.Trigger.OpenLong) trade.CloseTrade.Reason = Trade.Trigger.CloseLong;
                if (trade.OpenTrade.Reason == Trade.Trigger.OpenShort) trade.CloseTrade.Reason = Trade.Trigger.CloseShort;


                TakeProfitList.Add(trade);

            }


            return TakeProfitList;
        }