Esempio n. 1
0
        protected virtual Thrust GetActiveThrust()
        {
            Thrust thrust = null;
            Signal signal = null;

            signal = SubscriptionCaller.Instance().GetActiveSignalsByType(MACC.Constants.SignalType.Thrust).FirstOrDefault(s => s.Granularity == _chart.Granularity && s.Instrument == _chart.Instrument);

            if (signal != null)
            {
                thrust           = new Thrust();
                thrust.Direction = signal.Side == MACC.Constants.SignalSide.Buy ? EPatternDirection.Up : EPatternDirection.Down;
                thrust.InjectWith(signal);

                if (signal.StrategyTransactionID.HasValue)
                {
                    IEnumerable <StrategyTransaction> transactions;
                    StrategyTransaction orderTransaction, lastOrderUpdate, lastTradeUpdate;

                    // get transaction collection from db
                    int orderTransactionID = signal.StrategyTransactionID.Value;
                    transactions = StrategyCaller.Instance().GetStrategyTransactionsCollectionAsync(orderTransactionID).Result;
                    transactions = transactions.OrderBy(t => t.BrokerTransactionID);

                    orderTransaction = transactions.FirstOrDefault(t => t.StrategyTransactionID == orderTransactionID);
                    lastOrderUpdate  = transactions.LastOrDefault(t => t.Type == MACC.Constants.TransactionTypes.OrderUpdate);

                    // update prices
                    if (lastOrderUpdate != null)
                    {
                        thrust.SetOrUpdatePrices(lastOrderUpdate.Price, lastOrderUpdate.TakeProfit, lastOrderUpdate.StopLoss, null);
                    }
                    else
                    {
                        thrust.SetOrUpdatePrices(orderTransaction.Price, orderTransaction.TakeProfit, orderTransaction.StopLoss, null);
                    }

                    lastTradeUpdate = transactions.LastOrDefault(t => t.Type == MACC.Constants.TransactionTypes.TradeUpdate);
                    if (lastTradeUpdate != null)
                    {
                        thrust.SetOrUpdatePrices(null, lastTradeUpdate.TakeProfit, lastTradeUpdate.StopLoss, null);
                    }

                    // update price times
                    List <StrategyTransaction> updateTransactions = transactions.Where(t => t.Type == MACC.Constants.TransactionTypes.OrderUpdate || t.Type == MACC.Constants.TransactionTypes.TradeUpdate).ToList();

                    StrategyTransaction lastTakeProfitUpdate    = orderTransaction;
                    StrategyTransaction lastStopLossPriceUpdate = orderTransaction;

                    if (updateTransactions.Count() > 0)
                    {
                        updateTransactions.OrderByDescending(t => t.BrokerTransactionID).ToList().ForEach(t =>
                        {
                            if (t.TakeProfit != lastTakeProfitUpdate.TakeProfit)
                            {
                                lastTakeProfitUpdate = t;
                            }
                            if (t.StopLoss != lastStopLossPriceUpdate.StopLoss)
                            {
                                lastStopLossPriceUpdate = t;
                            }
                        });
                    }

                    thrust.SetOrUpdatePriceUpdatedTimes(null, lastTakeProfitUpdate.Time, lastStopLossPriceUpdate.Time);
                }
            }

            return(thrust);
        }