Exemplo n.º 1
0
        public void UpdatePositionValue(TradePosition position, Entity.Indicator ind)
        {
            position.CurrentPrice       = ind.Close.Value;
            position.CurrentTradingDate = ind.TradingDate;
            position.LastProcessedDate  = ind.TradingDate;

            double diff = (position.CurrentPrice - position.EntryPrice) * position.Size;

            position.Margin = position.EntryPrice * position.Size * position.Flag * Global.MarginRate + diff;

            Update(position);
        }
        public void PopulateReview(TradeReview review)
        {
            TransactionBLL   tBLL  = new TransactionBLL(_unit);
            TradePositionBLL tpBll = new TradePositionBLL(_unit);
            IndicatorBLL     iBll  = new IndicatorBLL(_unit);
            TickerBLL        tkBll = new TickerBLL(_unit);

            OutPosition pos = tpBll.GetOutPositionById(review.TradePositionId);

            Transaction entryTr = tBLL.GetByID(pos.EntryTransactionId);

            Entity.Indicator entryInd = iBll.GetIndicatorByShareDate(pos.ShareId, entryTr.TradingDate);
            Ticker           entryT   = tkBll.GetTickerByDate(pos.ShareId, entryTr.TradingDate);

            iBll.PopulateIndicatorWithTicker(entryInd, entryT);

            review.IsEntryLong = pos.Size > 0 ? true : false;

            if (entryInd.BB_Low.HasValue && entryInd.BB_High.HasValue)
            {
                review.BBEntryPercent = 100 * (entryTr.Price - entryInd.BB_Low.Value) / (entryInd.BB_High.Value - entryInd.BB_Low.Value);
            }

            review.EntryPercent = 100 * (entryTr.Price - entryInd.Low.Value) / (entryInd.High.Value - entryInd.Low.Value);

            if (pos.ExitTransactionId.HasValue)
            {
                Transaction      exitTr  = new TransactionBLL(_unit).GetByID(pos.ExitTransactionId.Value);
                Entity.Indicator exitInd = new IndicatorBLL(_unit).GetIndicatorByShareDate(pos.ShareId, exitTr.TradingDate);
                Ticker           exitT   = tkBll.GetTickerByDate(pos.ShareId, exitTr.TradingDate);
                iBll.PopulateIndicatorWithTicker(exitInd, exitT);

                if (exitInd.BB_Low.HasValue && exitInd.BB_High.HasValue)
                {
                    review.BBExitPercent = 100 * (exitTr.Price - exitInd.BB_Low.Value) / (exitInd.BB_High.Value - exitInd.BB_Low.Value);
                }

                review.ExitPercent = 100 * (exitTr.Price - exitInd.Low.Value) / (exitInd.High.Value - exitInd.Low.Value);

                review.DaysSpan = pos.Days;
                review.Diff     = pos.Diff;
                review.Diff_Per = pos.Diff_Per;
            }
        }
Exemplo n.º 3
0
        public TradeOrder CreateExitOrder(TradePosition position, Entity.Indicator ind, bool isStop)
        {
            TradeOrder     to     = new TradeOrder();
            Account        acc    = new AccountBLL(_unit).GetByID(position.AccountId);
            Broker         broker = new BrokerBLL(_unit).GetByID(acc.BrokerId);
            AccountBalance ab     = new AccountBalanceBLL(_unit).GetAccountBalanceByAccount(acc.Id);

            to.AccountId = position.AccountId;
            if (position.Size > 0)
            {
                to.Direction = "Short";
            }
            else
            {
                to.Direction = "Long";
            }
            to.Size = Math.Abs(position.Size);

            if (isStop)
            {
                to.OrderPrice = position.Stop.Value;
            }
            else
            {
                to.OrderPrice = position.Limit.Value;
            }

            to.Fee                  = this.GetFee(to.OrderValue, broker);
            to.ShareId              = position.ShareId;
            to.Source               = OrderSource.Stop.ToString();
            to.UpdateDate           = DateTime.Now;
            to.ProcessedTradingDate = ind.TradingDate;
            to.LatestTradingDate    = ind.TradingDate;
            to.Status               = "Fulfilled";
            to.UpdatedBy            = "System";
            to.TradingOrderDate     = ind.TradingDate;
            to.OrderType            = "Exit";

            this.Create(to);

            new AccountBalanceJourneyBLL(_unit).AddJourneyOrder(ab, "O." + to.Direction, to);

            return(to);
        }
Exemplo n.º 4
0
        public Transaction CreateTransaction(TradeOrder order, Entity.Indicator ind)
        {
            Share       s  = new ShareBLL(_unit).GetByID(order.ShareId);
            Transaction tr = new Transaction
            {
                Direction    = order.Direction,
                Message      = string.Format("{0} {1} * {2} @ ${3}", order.Direction, s.Symbol, order.Size, order.OrderPrice),
                ModifiedBy   = order.UpdatedBy,
                ModifiedDate = DateTime.Now,
                Price        = order.OrderPrice,
                Size         = order.Size,
                Fee          = order.Fee,
                TradingDate  = ind.TradingDate,
                TradeOrderId = order.Id
            };

            this.Create(tr);
            return(tr);
        }
        public override bool IsEntryCriteriaMatched(List <StatScanResultItem> resultList, Entity.Indicator[] indArray, Entity.Indicator ind, out double entryPrice)
        {
            bool isMatch = false;
            int  index   = base.GetIndex(indArray, ind);

            entryPrice = -1;

            if (index > 4 && indArray[index - 4].ADX.HasValue &&
                indArray[index].ADX_Plus.Value > indArray[index].ADX_Minus.Value)
            {
                int days       = 0;
                int bottomDays = 0;
                int wrDays     = 0;

                if (Check_ADX_Below_Both_2days(indArray, index))
                {
                    if (((IsAdxFlappedWithinDays(indArray, index, out days) && days <= 3) ||
                         (IsAdxNarrowedWithinDays(indArray, index, out days) && days <= 3)) &&
                        (IsMacdBottomWithinDays(indArray, index, out bottomDays) && bottomDays < 4))
                    {
                        double?latestItem = GetLastMatchDays(resultList, ind.ShareId, ind.TradingDate, 1);

                        Entity.Indicator indexInd = GetIndexIndicator(indArray[index - 1].TradingDate);

                        if (!(latestItem.HasValue && latestItem.Value <= 3) &&
                            (indexInd != null && indexInd.Heikin_Close >= indexInd.Heikin_Open))
                        {
                            if (ind.Low.Value <= ind.EMA20 && ind.High.Value >= ind.EMA20)
                            {
                                isMatch    = true;
                                entryPrice = ind.EMA20.Value;
                            }
                        }
                    }
                }
            }

            return(isMatch);
        }
        public override bool IsDailyScanMatched(List <StatScanResultItem> resultList, Entity.Indicator[] indArray, Entity.Indicator ind, out double entryPrice)
        {
            bool isMatch = false;
            int  index   = base.GetIndex(indArray, ind);

            entryPrice = -1;

            if (index > 4 && indArray[index - 4].ADX.HasValue)
            {
                int days = 0;

                if (IsAdxFlappedWithinDays(indArray, index, out days))
                {
                    int    maxIndex   = 0;
                    double?maxADXDiff = 0;

                    for (int i = 0; i < days; i++)
                    {
                        if ((indArray[index - i].ADX_Plus - indArray[index - i].ADX_Minus) > maxADXDiff)
                        {
                            maxADXDiff = indArray[index - i].ADX_Plus - indArray[index - i].ADX_Minus;
                            maxIndex   = index - i;
                        }
                    }

                    if (maxIndex != index)
                    {
                        double diff = indArray[index].ADX_Plus.Value - indArray[index].ADX_Minus.Value;

                        if (diff > 0 && diff < Narrow_Level)
                        {
                            isMatch = true;
                        }
                    }
                }

                if (!isMatch)
                {
                    if ((indArray[index - 1].ADX_Plus <= indArray[index - 1].ADX_Minus) &&
                        (indArray[index].ADX_Plus >= indArray[index].ADX_Minus))
                    {
                        isMatch = true;
                    }
                }
            }

            return(isMatch);
        }
        public override bool IsEntryCriteriaMatched(List <StatScanResultItem> resultList, Entity.Indicator[] indArray, Entity.Indicator ind, out double entryPrice)
        {
            bool isMatch = false;
            int  index   = base.GetIndex(indArray, ind);

            if (ind.TradingDate == 20120405)
            {
                int i = 0;
            }

            entryPrice = -1;

            if (index > 4 && indArray[index - 4].ADX.HasValue)
            {
                int days     = 0;
                int peekDays = 0;
                int wrDays   = 0;

                if ((IsAdxFlappedWithinDays(indArray, index, out days) && days <= 3) &&
                    (indArray[index].ADX_Minus > indArray[index].ADX_Plus) &&
                    (indArray[index].Heikin_Open > indArray[index].Heikin_Close))
                {
                    double?          entryPr     = indArray[index].EMA20 - (indArray[index].EMA20 - indArray[index].BB_Low) * 0;
                    double?          latestTrade = GetLastMatchDays(resultList, ind.ShareId, ind.TradingDate, -1);
                    Entity.Indicator indexInd    = GetIndexIndicator(indArray[index - 1].TradingDate);

                    if (indexInd.Open > indexInd.Close &&
                        entryPr.HasValue &&
                        entryPr.Value <= indArray[index].High.Value && entryPr.Value >= indArray[index].Low.Value)
                    {
                        isMatch    = true;
                        entryPrice = entryPr.Value;

                        Debug.WriteLine("found match at {0} on {1}", ind.TradingDate, entryPrice);
                    }
                }


                //if (Check_ADX_Below_Both_2days(indArray, index))
                //{
                //    if (((IsAdxFlappedWithinDays(indArray, index, out days) && days <= 3) ||
                //        (IsAdxNarrowedWithinDays(indArray, index, out days) && days <= 3)) &&
                //        (IsMacdPeekWithinDays(indArray, index, out peekDays) && peekDays < 4))
                //    {
                //        double? latestItem = GetLastMatchDays(resultList, ind.ShareId, ind.TradingDate, 1);

                //        Entity.Indicator indexInd = GetIndexIndicator(indArray[index - 1].TradingDate);

                //        if (!(latestItem.HasValue && latestItem.Value <= 3) &&
                //            (indexInd != null && indexInd.Heikin_Close <= indexInd.Heikin_Open))
                //        {
                //            if (ind.Low.Value <= ind.EMA20 && ind.High.Value >= ind.EMA20)
                //            {
                //                isMatch = true;
                //                entryPrice = ind.EMA20.Value;

                //                Debug.WriteLine("found match at {0} on {1}", ind.TradingDate, entryPrice);
                //            }
                //        }
                //    }
                //}
            }

            return(isMatch);
        }
Exemplo n.º 8
0
        public override bool IsDailyScanMatched(List <StatScanResultItem> resultList, Entity.Indicator[] indArray, Entity.Indicator ind, out double entryPrice)
        {
            bool isMatch = false;
            int  index   = base.GetIndex(indArray, ind);

            entryPrice = -1;

            if (index > 4 && indArray[index - 4].ADX.HasValue &&
                indArray[index].ADX_Plus.Value < indArray[index].ADX_Minus.Value)
            {
                int days = 0;

                if (IsAdxFlappedWithinDays(indArray, index, out days) && days > 3)
                {
                    double?entry = (indArray[index].SMA10 + indArray[index].EMA20) * 0.5;
                    if (entry.HasValue &&
                        entry.Value <= indArray[index].High.Value && entry.Value >= indArray[index].Low.Value)
                    {
                        isMatch    = true;
                        entryPrice = entry.Value;
                    }
                }
            }

            return(isMatch);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates the trade position.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <param name="ts">The ts.</param>
        /// <returns>is position closed </returns>
        public TradePosition CreateTradePosition(Transaction trans, TradeOrder order, TradeSet ts, Entity.Indicator ind, out bool isReverse)
        {
            TradePosition tp    = null;
            TradeOrderBLL toBll = new TradeOrderBLL(_unit);

            //TradeOrder order = toBll.GetByID(trans.TradeOrderId);
            isReverse = false;

            tp = _unit.DataContext.TradePosition.Where(p => p.AccountId == order.AccountId && p.ShareId == order.ShareId).SingleOrDefault();

            if (tp == null)
            {
                tp = new TradePosition
                {
                    ShareId            = order.ShareId,
                    AccountId          = order.AccountId,
                    Size               = trans.Size * order.Flag,
                    EntryPrice         = (trans.Size * trans.Price + trans.Fee * trans.Flag) / trans.Size,
                    UpdateDT           = DateTime.Now,
                    CurrentPrice       = ind.Close.Value,
                    CurrentTradingDate = ind.TradingDate,
                };

                this.Create(tp);
            }
            else
            {
                // update the position size
                if (toBll.IsReverse(order, tp))
                {
                    isReverse = true;
                    tp.Size  += order.Size * order.Flag;

                    if (tp.Size != 0)
                    {
                        tp.EntryPrice = tp.EntryCost / Math.Abs(tp.Size);
                    }

                    tp.UpdateDT           = DateTime.Now;
                    tp.CurrentPrice       = ind.Close.Value;
                    tp.CurrentTradingDate = ind.TradingDate;

                    this.Update(tp);
                }
                else
                {
                    tp.Size += order.Size * order.Flag;

                    if (tp.Size != 0)
                    {
                        tp.EntryPrice = tp.EntryCost / Math.Abs(tp.Size);
                    }

                    tp.UpdateDT           = DateTime.Now;
                    tp.CurrentPrice       = ind.Close.Value;
                    tp.CurrentTradingDate = ind.TradingDate;

                    this.Update(tp);
                }
            }

            return(tp);
        }
Exemplo n.º 10
0
        public TradePosition UpdateExitPosition(TradePosition position, Transaction trans, TradeOrder order, Entity.Indicator ind)
        {
            position.ExistFee          = trans.Fee;
            position.ExistPrice        = trans.Price;
            position.ExitTransactionId = trans.Id;
            position.LastProcessedDate = ind.TradingDate;
            position.UpdateDT          = DateTime.Now;

            double diff = (position.ExistPrice - position.EntryPrice) * trans.Size * position.Flag;

            position.Margin = position.EntryPrice * position.Size * Global.MarginRate * position.Flag + diff;

            Transaction en = new TransactionBLL(_unit).GetByID(position.EntryTransactionId);

            position.days = new TickerBLL(_unit).GetTradesDaySpan(position.ShareId, en.TradingDate, trans.TradingDate);

            this.Update(position);

            return(position);
        }
Exemplo n.º 11
0
        public TradePosition CreateEntryPosition(Transaction trans, TradeOrder order, Entity.Indicator ind)
        {
            TradePosition tp = new TradePosition
            {
                AccountId          = order.AccountId,
                CurrentPrice       = ind.Close.Value,
                CurrentTradingDate = ind.TradingDate,
                EntryPrice         = trans.Price,
                EntryFee           = trans.Fee,
                EntryTransactionId = trans.Id,
                Limit    = order.Limit,
                Stop     = order.Stop,
                ShareId  = order.ShareId,
                Size     = trans.Size * order.Flag,
                Source   = order.Source,
                UpdateDT = DateTime.Now
            };

            // Set margin on position
            double diff = (tp.CurrentPrice - trans.Price) * trans.Size * trans.Flag;

            tp.Margin = tp.EntryPrice * tp.Size * tp.Flag * Global.MarginRate + diff;

            this.Create(tp);

            return(tp);
        }
Exemplo n.º 12
0
        public override bool IsEntryCriteriaMatched(List <StatScanResultItem> resultList, Entity.Indicator[] indArray, Entity.Indicator ind, out double entryPrice)
        {
            bool isMatch = false;
            int  index   = base.GetIndex(indArray, ind);

            entryPrice = -1;

            if (index > 4 && indArray[index - 4].ADX.HasValue &&
                indArray[index - 1].ADX_Plus.Value > indArray[index - 1].ADX_Minus.Value &&
                (indArray[index - 1].ADX.Value > 30 ||
                 indArray[index - 2].ADX < indArray[index - 1].ADX)
                )
            {
                int days     = 0;
                int peekDays = 0;
                int wrDays   = 0;

                if ((IsAdxFlappedWithinDays(indArray, index, out days) && days > 3) &&
                    (indArray[index - 1].MACD > indArray[index - 1].MACD_Signal))
                {
                    double?entry = (indArray[index - 1].SMA10 + indArray[index - 1].EMA20) * 0.498;
                    if (entry.HasValue &&
                        entry.Value <= indArray[index].High.Value && entry.Value >= indArray[index].Low.Value)
                    {
                        isMatch    = true;
                        entryPrice = entry.Value;
                    }
                }
            }

            return(isMatch);
        }
        public override bool IsDailyScanMatched(List <StatScanResultItem> resultList, Entity.Indicator[] indArray, Entity.Indicator ind, out double entryPrice)
        {
            bool isMatch = false;
            int  index   = base.GetIndex(indArray, ind);

            entryPrice = -1;


            if (index > 2 && indArray[index].MACD.HasValue && indArray[index - 1].MACD.HasValue &&
                indArray[index].MACD.HasValue &&
                indArray[index].ADX_Plus < indArray[index].ADX_Minus)
            {
                if (indArray[index - 1].ADX <= indArray[index - 2].ADX &&
                    indArray[index].ADX >= indArray[index - 1].ADX)
                {
                    isMatch = true;
                }

                if (!isMatch)
                {
                    if (((indArray[index - 1].ADX_Plus.Value - indArray[index - 1].ADX.Value) < (indArray[index].ADX_Plus.Value - indArray[index].ADX.Value)) &&
                        Math.Abs(indArray[index].ADX_Plus.Value - indArray[index].ADX.Value) < ADX_Narrow)
                    {
                        isMatch = true;
                    }
                }
            }

            return(isMatch);
        }
        public override bool IsEntryCriteriaMatched(List <StatScanResultItem> resultList, Entity.Indicator[] indArray, Entity.Indicator ind, out double entryPrice)
        {
            bool isMatch = false;
            int  index   = base.GetIndex(indArray, ind);

            entryPrice = -1;

            if (index > 2 && indArray[index].MACD.HasValue && indArray[index - 1].MACD.HasValue &&
                indArray[index - 2].MACD.HasValue
                )
            {
                if (indArray[index].ADX_Plus < indArray[index].ADX_Minus &&
                    indArray[index - 2].ADX_Plus >= indArray[index - 2].ADX &&
                    indArray[index - 1].ADX_Plus <= indArray[index - 1].ADX)
                {
                    isMatch    = true;
                    entryPrice = (indArray[index].Open.Value + indArray[index].Close.Value) * 0.5;
                }
            }

            return(isMatch);
        }
Exemplo n.º 15
0
        public override bool IsDailyScanMatched(List <StatScanResultItem> resultList, Entity.Indicator[] indArray, Entity.Indicator ind, out double entryPrice)
        {
            bool isMatch = false;
            int  index   = base.GetIndex(indArray, ind);

            entryPrice = -1;

            if (index > 2 && indArray[index].MACD.HasValue && indArray[index - 1].MACD.HasValue &&
                indArray[index].MACD.HasValue)
            {
                if ((Math.Abs(indArray[index - 1].WR.Value) >= WR_LEVEL || Math.Abs(indArray[index].WR.Value) >= WR_LEVEL))
                {
                    isMatch    = true;
                    entryPrice = (indArray[index].Open.Value + indArray[index].Close.Value) * 0.5;
                }
            }

            return(isMatch);
        }