Beispiel #1
0
        public static void TrySetStrategy(StrategyInterface strategy)
        {
            if (breakMode == BreakMode.None || locationType != LocationType.Platform)
            {
                return;
            }
            BreakPoint bp;

            switch (constraintType)
            {
            case ConstraintType.Strategy:
                if (strategy.Name == constraint)
                {
                    bp          = new BreakPoint();
                    bp.strategy = strategy;
                    strategy.AddInterceptor(bp);
                }
                break;

            case ConstraintType.Symbol:
                if (strategy.SymbolDefault == constraint)
                {
                    bp          = new BreakPoint();
                    bp.strategy = strategy;
                    strategy.AddInterceptor(bp);
                }
                break;

            default:
                bp          = new BreakPoint();
                bp.strategy = strategy;
                strategy.AddInterceptor(bp);
                break;
            }
        }
Beispiel #2
0
        void UpdatePosition(float deltaTime)
        {
            Vector2       vertical = new Vector2();
            float         speed    = controllable.speed;
            TransformBase tr       = controllable.transform;

            // Test strategy pattern
            if (elapsedTime < 3f)
            {
                action = new MoveForward();
                action.DoAction(tr, ref vertical, speed, deltaTime);
            }
            else if (elapsedTime >= 3f && elapsedTime < 4.8f)
            {
                action = new TurnRight();
                action.DoAction(tr, ref vertical, speed, deltaTime);
            }
            else if (elapsedTime >= 4.8f && elapsedTime < 6.6f)
            {
                action = new TurnLeft();
                action.DoAction(tr, ref vertical, speed, deltaTime);
            }
            else if (elapsedTime >= 6.6f && elapsedTime < 9.6f)
            {
                action = new MoveBackwards();
                action.DoAction(tr, ref vertical, speed, deltaTime);
            }
            else if (elapsedTime >= 9.6f)
            {
                elapsedTime = 0;
            }

            tr.position           += Utils.Rotate(vertical, tr.rotation);
            controllable.transform = tr;
        }
Beispiel #3
0
        internal IEnumerable <StrategyInterface> GetStrategyList(Chain chain)
        {
            StrategyInterface currentStrategy = chain.Model as StrategyInterface;

            if (currentStrategy != null)
            {
                yield return(currentStrategy);
            }
            foreach (Chain tempChain in chain.Dependencies)
            {
                if (tempChain == chain)
                {
                    throw new ApplicationException("Found recursive loop. " + tempChain.Model.Name + " cannot be a dependency of itself.");
                }
                foreach (StrategyInterface strategy in GetStrategyList(tempChain.Tail))
                {
                    yield return(strategy);
                }
            }
            if (chain.Previous.Model != null)
            {
                foreach (StrategyInterface strategy in GetStrategyList(chain.Previous))
                {
                    yield return(strategy);
                }
            }
        }
        public virtual void Init()
        {
            this.strategy         = Factory.Utility.Strategy();
            this.strategy.Context = new MockContext();
            string appData = Factory.Settings["AppDataFolder"];
//			File.Delete( Factory.SysLog.LogFolder + @"\" + assemblyName+"Tests.log");
//			File.Delete( Factory.SysLog.LogFolder + @"\" + assemblyName+".log");
        }
        public LogicalOrder CreateOrder(StrategyInterface strategy, TradeDirection tradeDirection, OrderType orderType, double price, int position, int strategyPosition)
        {
            LogicalOrder order = Factory.Engine.LogicalOrder(symbol, strategy);

            order.StrategyId       = 1;
            order.StrategyPosition = strategyPosition;
            order.TradeDirection   = tradeDirection;
            order.Type             = orderType;
            order.Price            = price;
            order.Position         = position * lotSize;
            order.Status           = OrderStatus.Active;
            orders.AddLast(order);
            strategy.Position.Change(strategyPosition, 100.00, TimeStamp.UtcNow);
            return(order);
        }
Beispiel #6
0
        private void SetupWatcher(StrategyInterface strategy)
        {
            if (portfolioType == PortfolioType.SingleSymbol)
            {
                strategy.OnActiveChange += HandleActiveChange;
                strategy.AddEventListener(EventType.LogicalFill, this);
            }
            var watcher = new StrategyWatcher(strategy);

            watchers.Add(strategy, watcher);
            if (strategy.IsActive)
            {
                activeWatchers.Add(watcher);
            }
        }
Beispiel #7
0
        public bool SetOptimizeValues(ModelLoaderInterface loader)
        {
            // Then set them for logging separately to optimization reports.
            Dictionary <string, object> optimizeValues = new Dictionary <string, object>();

            for (int i = 0; i < loader.Variables.Count; i++)
            {
                optimizeValues[loader.Variables[i].Name] = loader.Variables[i].Value;
            }

            bool retVal = true;

            foreach (var kvp in optimizeValues)
            {
                string   name      = kvp.Key;
                object   value     = kvp.Value;
                string[] namePairs = kvp.Key.Split('.');
                if (namePairs.Length < 2)
                {
                    log.Error("Sorry, the optimizer variable '" + kvp.Key + "' was not found.");
                    retVal = false;
                    continue;
                }
                string            strategyName = namePairs[0];
                StrategyInterface strategy     = null;
                foreach (StrategyInterface tempStrategy in GetStrategyList(loader.TopModel.Chain.Tail))
                {
                    if (tempStrategy.Name.CompareWildcard(strategyName, false))
                    {
                        strategy = tempStrategy;
                        if (!SetOptimizeValue(strategy, name, value))
                        {
                            retVal = false;
                            break;
                        }
                    }
                }
                if (strategy == null)
                {
                    log.Error("Sorry, a strategy matching '" + strategyName + "' was never found for optimizer variable '" + kvp.Key + "'.");
                    retVal = false;
                    continue;
                }
            }
            optimizeValueMap.Add(loader.TopModel, optimizeValues);
            return(retVal);
        }
        public void ProcessFill(StrategyInterface strategyInterface, LogicalFill fill)
        {
            if (debug)
            {
                Log.Debug("ProcessFill: " + fill + " for strategy " + strategyInterface);
            }
            var          strategy    = (Strategy)strategyInterface;
            int          orderId     = fill.OrderId;
            LogicalOrder filledOrder = null;

            if (strategyInterface.TryGetOrderById(fill.OrderId, out filledOrder))
            {
                if (debug)
                {
                    Log.Debug("Matched fill with orderId: " + orderId);
                }
                if (!doStrategyOrders && filledOrder.TradeDirection != TradeDirection.ExitStrategy)
                {
                    if (debug)
                    {
                        Log.Debug("Skipping fill, strategy order fills disabled.");
                    }
                    return;
                }
                if (!doExitStrategyOrders && filledOrder.TradeDirection == TradeDirection.ExitStrategy)
                {
                    if (debug)
                    {
                        Log.Debug("Skipping fill, exit strategy orders fills disabled.");
                    }
                    return;
                }
                TryDrawTrade(filledOrder, fill.Price, fill.Position);
                if (debug)
                {
                    Log.Debug("Changed strategy position to " + fill.Position + " because of fill.");
                }
                changePosition(strategy.Data.SymbolInfo, fill);
            }
            else
            {
                throw new ApplicationException("A fill for order id: " + orderId + " was incorrectly routed to: " + strategyInterface.Name);
            }
        }
Beispiel #9
0
        public void WritePassStats(StreamWriter fwriter, StrategyInterface passModel)
        {
            Dictionary <string, object> optimizeValues = optimizeValueMap[passModel];

            if (!headerWritten)
            {
                headerWritten = true;
                string header = passModel.OnGetOptimizeHeader(optimizeValues);
                if (string.IsNullOrEmpty(header))
                {
                    log.Warn("The header string returned from OnGetOptimizeResults for " + passModel.Name + " was empty or null.");
                }
                else
                {
                    fwriter.WriteLine(header);
                }
            }
            string stats = passModel.OnGetOptimizeResult(optimizeValues);

            if (stats != null && stats.Length > 0)
            {
                fwriter.WriteLine(stats);
            }
        }
Beispiel #10
0
 public StrategyWatcher(StrategyInterface strategy)
 {
     this.strategy = strategy;
     this.position = strategy.Result.Position;
 }
        public FillHandlerDefault(StrategyInterface strategyInterface)
        {
            Strategy strategy = (Strategy)strategyInterface;

            graphTrades = strategy.Performance.GraphTrades;
        }
Beispiel #12
0
 internal FillManager(StrategyInterface strategy)
 {
     this.Strategy       = strategy;
     this.changePosition = strategy.Position.Change;
 }
Beispiel #13
0
 public LogicalOrder CreateEntry(StrategyInterface strategy, SymbolInfo symbol, OrderType orderType, double price, int position, int strategyPosition)
 {
     return(CreateOrder(strategy, symbol, TradeDirection.Entry, orderType, price, position, strategyPosition));
 }
Beispiel #14
0
 public Context(StrategyInterface strategy)
 {
     this.strategy = strategy;
 }
Beispiel #15
0
 public FillHandler FillHandler(StrategyInterface strategy)
 {
     return(new FillHandlerDefault(strategy));
 }
Beispiel #16
0
 public LimeProviderTests(string name)
 {
     testName         = name;
     Strategy         = Factory.Utility.Strategy();
     Strategy.Context = new MockContext();
 }
 public LogicalOrder CreateExit(StrategyInterface strategy, OrderType orderType, double price, int strategyPosition)
 {
     return(CreateOrder(strategy, TradeDirection.Exit, orderType, price, 0, strategyPosition));
 }
 public LogicalOrder CreateChange(StrategyInterface strategy, OrderType orderType, double price, int position, int strategyPosition)
 {
     return(CreateOrder(strategy, TradeDirection.Change, orderType, price, position, strategyPosition));
 }
Beispiel #19
0
        private static List <Tuple <double, User> > nearestNeighbours(User selectedUser, List <int> emptyRatings, double smt, int amount)
        {
            List <Tuple <double, User> > nn_Euclidian = new List <Tuple <double, User> >();
            var nn_Pearson = new List <Tuple <double, User> >();
            List <Tuple <double, User> > nn_Cosine = new List <Tuple <double, User> >();

            //loop through all the users and find the users that are most similar to selecteduser
            foreach (var user in userpref)
            {
                var user2 = userpref[user.Key];
                if (selectedUser.id != user.Key)
                {
                    var difference = 0;
                    foreach (var rating in emptyRatings)
                    {
                        if (user2.articleRating[rating] != 0)
                        {
                            difference++;
                        }
                    }

                    if (difference <= 0)
                    {
                        continue;
                    }
                    //TODO refactor this mess, only pearson has treshold atm
                    Console.WriteLine("Compare user: "******" with user: "******"---------------------");

                    ed = new EuclidianDistance();
                    var euclidian_distance = ed.calculate(selectedUser, user2, uniqueArticles);
                    user2.euclidean_distance = euclidian_distance;
                    nn_Euclidian.Add(Tuple.Create(euclidian_distance, user2));
                    Console.WriteLine("Euclidian stuff: " + euclidian_distance);

                    pe = new Pearsons();
                    var correlation = pe.calculate(selectedUser, user2, uniqueArticles);
                    if (correlation >= smt)
                    {
                        var neighbour = Tuple.Create(correlation, user2);
                        if (nn_Pearson.Count < amount)
                        {
                            nn_Pearson.Add(neighbour);
                            if (nn_Pearson.Count() == amount)
                            {
                                smt = nn_Pearson.Min(x => x.Item1);
                            }
                        }
                        else
                        {
                            nn_Pearson.Add(neighbour);
                            var lowestSimilarityUser = nn_Pearson.OrderBy(x => x.Item1).First();
                            nn_Pearson.Remove(lowestSimilarityUser);
                            smt = nn_Pearson.OrderBy(x => x.Item1).First().Item1;
                        }
                    }
                    Console.WriteLine("Pearsons corr: " + correlation);

                    co = new Cosine();
                    var cosine_similarity = co.calculate(selectedUser, user2, uniqueArticles);
                    user2.cosine = cosine_similarity;
                    nn_Cosine.Add(Tuple.Create(cosine_similarity, user2));

                    Console.WriteLine("Cosine similarity: " + cosine_similarity + "\n");
                }
                else
                {
                    continue;
                }
            }
            //maybe print the first 3 of this stuff
            List <Tuple <double, User> > nn_Euclidian_Sorted = nn_Euclidian.OrderByDescending(x => x.Item1).ToList();
            List <Tuple <double, User> > nn_Pearson_Sorted   = nn_Pearson.OrderByDescending(x => x.Item1).ToList();
            List <Tuple <double, User> > nn_Cosine_Sorted    = nn_Cosine.OrderByDescending(x => x.Item1).ToList();

            for (var i = 0; i < amount_nearestNeighbours; i++)
            {
                Console.WriteLine("Nearest {0}", i + 1 + "\n----------------");
                Console.WriteLine("Euclidian nn: " + nn_Euclidian_Sorted[i].Item2.id);
                Console.WriteLine("Pearson nn: " + nn_Pearson_Sorted[i].Item2.id);
                Console.WriteLine("consine nn: " + nn_Cosine_Sorted[i].Item2.id + "\n");
            }
            return(nn_Pearson_Sorted);
        }
Beispiel #20
0
        private bool SetOptimizeValue(StrategyInterface strategy, string name, object value)
        {
            string[] namePairs    = name.Split('.');
            string   propertyName = namePairs[1];
            PropertyDescriptorCollection props    = TypeDescriptor.GetProperties(strategy);
            PropertyDescriptor           property = null;

            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor tempProperty = props[i];
                if (tempProperty.Name == propertyName)
                {
                    property = tempProperty;
                    break;
                }
            }
            if (property == null)
            {
                log.Error("Sorry, the optimizer variable '" + name + "' was not found.");
                return(false);
            }
            if (namePairs.Length == 2)
            {
                if (!SetProperty(strategy, property, value))
                {
                    log.Error("Sorry, the value '" + value + "' isn't valid for optimizer variable '" + name + "'.");
                    return(false);
                }
            }
            else if (namePairs.Length == 3)
            {
                StrategyInterceptorInterface strategySupport = property.GetValue(strategy) as StrategyInterceptorInterface;
                if (strategySupport == null)
                {
                    log.Error("Sorry, the optimizer variable '" + name + "' was not found.");
                    return(false);
                }
                string strategySupportName = namePairs[1];
                propertyName = namePairs[2];
                props        = TypeDescriptor.GetProperties(strategySupport);
                property     = null;
                for (int i = 0; i < props.Count; i++)
                {
                    PropertyDescriptor tempProperty = props[i];
                    if (tempProperty.Name == propertyName)
                    {
                        property = tempProperty;
                        break;
                    }
                }
                if (property == null)
                {
                    log.Error("Sorry, the optimizer variable '" + name + "' was not found.");
                    return(false);
                }
                if (!SetProperty(strategySupport, property, value))
                {
                    log.Error("Sorry, the value '" + value + "' isn't valid for optimizer variable '" + name + "'.");
                    return(false);
                }
            }
            return(true);
        }
 public FillHandlerDefault(StrategyInterface strategyInterface)
 {
     this.strategy = (Strategy)strategyInterface;
     log.Register(this);
 }