Exemple #1
0
        public BacktestSession CreateSession(Guid accountId, TickerType tickerType, BarItemType barType, string barDataFile, Guid cacheId)
        {
            if (backtestSessions == null)
            {
                backtestSessions = new Dictionary <Guid, BacktestSession>();
            }

            this.cacheId = cacheId;

            //this line should raise an exception if file is invalid
            BarItemFile.ValidateFile(barDataFile);

            BacktestSession backtestSession = new BacktestSession(accountId, tickerType, barDataFile, cacheId);

            //backtestSession.SignalClosePosition += OnSignalClosePosition;
            //backtestSession.SignalOpenPosition += OnSignalOpenPosition;

            backtestSession.StrategyClosePosition += OnStrategyClosePosition;
            backtestSession.StrategyOpenPosition  += OnStrategyOpenPosition;

            backtestSessions.Add(backtestSession.SessionId, backtestSession);

            brokerAccounts[accountId].Orders.Initialize(barType, backtestSession.SessionId);

            return(backtestSession);
        }
        public static void TickPostfix(TickList __instance)
        {
            int        currentTickInterval = __instance.TickInterval;
            TickerType currentTickType     = __instance.tickType;

            switch (currentTickType)
            {
            case TickerType.Normal:
                RimThreaded.thingListNormal      = __instance.thingLists[Find.TickManager.TicksGame % currentTickInterval];
                RimThreaded.thingListNormalTicks = RimThreaded.thingListNormal.Count;
                break;

            case TickerType.Rare:
                RimThreaded.thingListRare      = __instance.thingLists[Find.TickManager.TicksGame % currentTickInterval];
                RimThreaded.thingListRareTicks = RimThreaded.thingListRare.Count;
                break;

            case TickerType.Long:
                RimThreaded.thingListLong      = __instance.thingLists[Find.TickManager.TicksGame % currentTickInterval];
                RimThreaded.thingListLongTicks = RimThreaded.thingListLong.Count;
                break;

            case TickerType.Never:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
 public static void RequireTicker(this ThingComp comp, TickerType type)
 {
     if (comp.parent.def.tickerType != type)
     {
         RemoteTechController.Instance.Logger.Error($"{comp.GetType().Name} requires tickerType:{type} in def {comp.parent.def.defName}");
     }
 }
Exemple #4
0
 public static void RequireTicker(this ThingComp comp, TickerType type)
 {
     if (comp.parent.def.tickerType != type)
     {
         Log.Error($"{comp.GetType().Name} requires tickerType:{type} in def {comp.parent.def.defName}");
     }
 }
Exemple #5
0
        public SignalCharting(ISignalChart chartControl, TickerType tickerType, BarItemType barType, object dataStreamSource)
        {
            this.dataStreamSource    = dataStreamSource;
            this.tickerType          = tickerType;
            this.barType             = barType;
            this.chartControl        = chartControl;
            this.registeredAnalytics = new List <AnalyticsItem>();

            SharedCacheFactory.Instance.CacheWriter = new FileCacheWriter("cache");
            SharedCacheFactory.Instance.CacheReader = new FileCacheReader("cache");

            if (dataStreamSource is string)
            {
                FileInfo    file        = new FileInfo(dataStreamSource.ToString());
                long        fileHash    = file.Attributes.GetHashCode() ^ file.CreationTime.Ticks ^ file.LastWriteTime.Ticks ^ file.Length;
                CacheConfig cacheConfig = new CacheConfig("cache");
                cacheConfig.Initialize();
                cacheConfig.Open(CachingModeOption.Reading);
                CacheRow row = cacheConfig.Read(fileHash);
                cacheConfig.Close();

                if (row == null)
                {
                    cacheConfig.Open(CachingModeOption.Writing);
                    cacheId = Guid.NewGuid();
                    cacheConfig.Append(fileHash, cacheId, SessionModeOption.Backtesting);
                    cacheConfig.Close();
                }
                else
                {
                    cacheId = new Guid((byte[])row["SessionId"]);
                }
            }
        }
Exemple #6
0
 public static void Postfix(TickerType ___tickType)
 {
     if (___tickType == TickerType.Long)
     {
         BaseOverlay.SetDirty(typeof(TreeGrowthOverlay));
     }
 }
        static bool Prefix(Thing t)
        {
            if (Multiplayer.Client == null || t.Map == null)
            {
                return(true);
            }

            MapAsyncTimeComp comp       = t.Map.AsyncTime();
            TickerType       tickerType = t.def.tickerType;

            if (tickerType == TickerType.Normal)
            {
                comp.tickListNormal.RegisterThing(t);
            }
            else if (tickerType == TickerType.Rare)
            {
                comp.tickListRare.RegisterThing(t);
            }
            else if (tickerType == TickerType.Long)
            {
                comp.tickListLong.RegisterThing(t);
            }

            return(false);
        }
Exemple #8
0
 public static void Postfix(TickerType ___tickType)
 {
     if (___tickType == TickerType.Long)
     {
         BaseOverlay.SetDirty(typeof(PlantHarvestOverlay));
     }
 }
Exemple #9
0
 public BackgroundSignal(TickerType tickerType, BarItemType barType, object dataStreamSource)
 {
     this.dataStreamSource    = dataStreamSource;
     this.tickerType          = tickerType;
     this.barType             = barType;
     this.registeredAnalytics = new List <AnalyticsItem>();
 }
        public void RegisterToTickManager(ThingWithComps thing)
        {
            TickerType tickerType = thing.def.tickerType;

            if (tickerType == TickerType.Rare)
            {
                return;                                   // already done
            }
            if (tickerType == TickerType.Normal)
            {
                return;                                   // faster than what we like, but we'll manage
            }
            if (tickerType == TickerType.Long)            // not supported
            // If we get anything like this, we might want to know about it eventually.  We could force in
            // CompTickLong support via Harmony patch.
            {
                Log.Error(
                    "Unable to register " + thing + " to TickManager.  Using this kind of LootAffixModifier is " +
                    "(currently) unsupported for ThingDefs with tickerType=Long (like " + thing.def + ")."
                    );
                return;
            }

            // Manipulating TickLists outside of ThingDef.tickerType requires private list access

            // [Reflection] TickManager.tickListRare.RegisterThing(thing)
            FieldInfo tickListRareField = AccessTools.Field(typeof(TickManager), "tickListRare");
            TickList  tickListRare      = (TickList)tickListRareField.GetValue(Find.TickManager);

            tickListRare.RegisterThing(thing);
        }
Exemple #11
0
        public void CloseLastOrder(DateTime time, TickerType tickerType, double bidPrice, double askPrice, BarItem barItem)
        {
            MarketOrder marketOrder = marketOrders[lastTickerOrder[tickerType.Symbol]];

            marketOrder.CloseOrder(time, bidPrice, askPrice, barItem.Time);
            marketOrderCache.AppendMarketOrder(marketOrders.Count, marketOrder.Position, marketOrder.OrderTime, marketOrder.OpeningBarTime, marketOrder.OpeningBidPrice, marketOrder.OpeningAskPrice,
                                               barItem.Time, marketOrder.ClosingBarTime, marketOrder.ClosingBidPrice, marketOrder.ClosingAskPrice);
        }
Exemple #12
0
 public TickList(TickerType tickType)
 {
     this.tickType = tickType;
     for (int i = 0; i < this.TickInterval; i++)
     {
         this.thingLists.Add(new List <Thing>());
     }
 }
Exemple #13
0
        public string GetTicker(TickerType tickerType)
        {
            string streamAddress = null;

            string ticker = string.Format("ticker.{0}", tickerType);

            if (data.ContainsKey(ticker))
                streamAddress = data[ticker];

            return streamAddress;
        }
Exemple #14
0
        public static bool Tick(TickList __instance)
        {
            currentTickType     = tickType(__instance);
            currentTickInterval = get_TickInterval(__instance);
            List <Thing> tr = thingsToRegister(__instance);

            for (int index = 0; index < tr.Count; ++index)
            {
                Thing        i = tr[index];
                List <Thing> b = BucketOf(__instance, i);
                b.Add(i);
            }
            tr.Clear();

            List <Thing> td = thingsToDeregister(__instance);

            for (int index = 0; index < td.Count; ++index)
            {
                Thing        i = td[index];
                List <Thing> b = BucketOf(__instance, i);
                b.Remove(i);
            }
            td.Clear();

            if (DebugSettings.fastEcology)
            {
                Find.World.tileTemperatures.ClearCaches();
                for (int index1 = 0; index1 < thingLists(__instance).Count; ++index1)
                {
                    List <Thing> thingList = thingLists(__instance)[index1];
                    for (int index2 = 0; index2 < thingList.Count; ++index2)
                    {
                        if (thingList[index2].def.category == ThingCategory.Plant)
                        {
                            thingList[index2].TickLong();
                        }
                    }
                }
            }
            thingList1 = thingLists(__instance)[Find.TickManager.TicksGame % currentTickInterval];
            thingQueue = new ConcurrentQueue <Thing>(thingList1);

            CreateTickThreads();
            foreach (EventWaitHandle eventWaitHandle in eventWaitStarts.Values)
            {
                eventWaitHandle.Set();
            }

            monitorThreadWaitHandle.Set();
            MainThreadWaitLoop();

            return(false);
        }
Exemple #15
0
        public int GetTotalWinningOrders(TickerType tickerType)
        {
            int winningOrders = 0;

            foreach (MarketOrder marketOrder in marketOrders.Values)
            {
                if (marketOrder.ProfitOrLossInPips > 0)
                {
                    winningOrders++;
                }
            }
            return(winningOrders);
        }
Exemple #16
0
        public double GetTotalLoss(TickerType tickerType)
        {
            double loss = 0;

            foreach (MarketOrder marketOrder in marketOrders.Values)
            {
                if (marketOrder.Ticker.Symbol == tickerType.Symbol)
                {
                    loss += marketOrder.LossInPips;
                }
            }
            return(loss);
        }
Exemple #17
0
        public int GetTotalBreakevenOrders(TickerType tickerType)
        {
            int breakevenOrders = 0;

            foreach (MarketOrder marketOrder in marketOrders.Values)
            {
                if (marketOrder.ProfitOrLossInPips == 0)
                {
                    breakevenOrders++;
                }
            }
            return(breakevenOrders);
        }
Exemple #18
0
        public double GetTotalProfit(TickerType tickerType)
        {
            double profit = 0;

            foreach (MarketOrder marketOrder in marketOrders.Values)
            {
                if (marketOrder.Ticker.Symbol == tickerType.Symbol)
                {
                    profit += marketOrder.ProfitInPips;
                }
            }
            return(profit);
        }
Exemple #19
0
        public int GetTotalLosingOrders(TickerType tickerType)
        {
            int losingOrders = 0;

            foreach (MarketOrder marketOrder in marketOrders.Values)
            {
                if (marketOrder.ProfitOrLossInPips < 0)
                {
                    losingOrders++;
                }
            }
            return(losingOrders);
        }
Exemple #20
0
        public BacktestSession(Guid accountId, TickerType tickerType, string barDataFile, Guid cacheId, bool cachingEnabled = true)
        {
            this.accountId = accountId;

            this.tickerType      = tickerType;
            this.barDataFile     = barDataFile;
            this.sessionId       = Guid.NewGuid();
            indicators           = new SessionIndicators();
            signals              = new SessionSignals(indicators, cacheId, cachingEnabled);
            signals.EntrySignal += OnEntrySignal;
            signals.ExitSignal  += OnExitSignal;
            //strategies = new SessionStrategies(signals, indicators, cacheId, cachingEnabled);
            //strategies.EntryPosition += OnEntryStrategy;
            //strategies.ExitPosition += OnExitStrategy;
            this.cacheId        = cacheId;
            this.cachingEnabled = cachingEnabled;
        }
        private static int TickInterval(TickerType type)
        {
            switch (type)
            {
            case TickerType.Normal:
                return(1);

            case TickerType.Rare:
                return(250);

            case TickerType.Long:
                return(2000);

            default:
                return(-1);
            }
        }
Exemple #22
0
        public TCTickList(TickList old) : base(GetTickType(old))
        {
            tickType = GetTickType(old);
            FieldInfo tl = AccessTools.Field(typeof(TickList), "thingLists");

            thingLists = tl.GetValue(this) as List <List <Thing> >;
            FieldInfo ttr = AccessTools.Field(typeof(TickList), "thingsToRegister");

            thingsToRegister = ttr.GetValue(this) as List <Thing>;
            FieldInfo ttd = AccessTools.Field(typeof(TickList), "thingsToDeregister");

            thingsToDeregister = ttd.GetValue(this) as List <Thing>;

            for (int i = 0; i < this.TickInterval; i++)
            {
                this.normalThingList.Add(new List <Thing>());
                this.adjustedthingList.Add(new List <Thing>());
            }
        }
Exemple #23
0
        public Guid CreateMarketOrder(DateTime time, TickerType tickerType, MarketOrderState orderState, double bidPrice, double askPrice, BarItem barItem)
        {
            Guid marketOrderId = Guid.NewGuid();

            MarketOrder marketOrder = new MarketOrder(tickerType);

            marketOrder.OpenOrder(time, orderState, bidPrice, askPrice, barItem.Time);
            marketOrders.Add(marketOrderId, marketOrder);

            if (lastTickerOrder.ContainsKey(tickerType.Symbol))
            {
                lastTickerOrder[tickerType.Symbol] = marketOrderId;
            }
            else
            {
                lastTickerOrder.Add(tickerType.Symbol, marketOrderId);
            }

            return(marketOrderId);
        }
        private static bool Prefix(TickList __instance)
        {
            if (!Active)
            {
                return(true);
            }

            for (int i = 0; i < __instance.thingsToRegister.Count; i++)
            {
                __instance.BucketOf(__instance.thingsToRegister[i]).Add(__instance.thingsToRegister[i]);
            }

            __instance.thingsToRegister.Clear();
            for (int j = 0; j < __instance.thingsToDeregister.Count; j++)
            {
                __instance.BucketOf(__instance.thingsToDeregister[j]).Remove(__instance.thingsToDeregister[j]);
            }

            __instance.thingsToDeregister.Clear();
            if (DebugSettings.fastEcology)
            {
                Find.World.tileTemperatures.ClearCaches();
                for (int k = 0; k < __instance.thingLists.Count; k++)
                {
                    System.Collections.Generic.List <Thing> list = __instance.thingLists[k];
                    for (int l = 0; l < list.Count; l++)
                    {
                        if (list[l].def.category == ThingCategory.Plant)
                        {
                            list[l].TickLong();
                        }
                    }
                }
            }

            System.Collections.Generic.List <Thing> list2 = __instance.thingLists[Find.TickManager.TicksGame % __instance.TickInterval];
            for (int m = 0; m < list2.Count; m++)
            {
                Thing sam = list2[m];
                if (!sam.Destroyed)
                {
                    try
                    {
                        TickerType tickerType = __instance.tickType;
                        if (tickerType != TickerType.Normal)
                        {
                            if (tickerType != TickerType.Rare)
                            {
                                if (tickerType == TickerType.Long)
                                {
                                    LogMe(sam, sam.TickLong, "TickLong");
                                }
                            }
                            else
                            {
                                LogMe(sam, sam.TickRare, "TickRare");
                            }
                        }
                        else
                        {
                            LogMe(sam, sam.Tick, "Tick");
                        }
                    }
                    catch (Exception ex)
                    {
                        string text = !list2[m].Spawned ? string.Empty : " (at " + list2[m].Position + ")";
                        if (Prefs.DevMode)
                        {
                            Log.Error(string.Concat("Exception ticking ", list2[m].ToStringSafe(), text, ": ", ex));
                        }
                        else
                        {
                            Log.ErrorOnce(
                                string.Concat("Exception ticking ", list2[m].ToStringSafe(), text,
                                              ". Suppressing further errors. Exception: ", ex),
                                list2[m].thingIDNumber ^ 576876901);
                        }
                    }
                }
            }

            return(false);
        }
Exemple #25
0
        public TickerType getTickerType(String name)
        {
            TickerType result = new TickerType();
            switch (name)
            {
                case "avg":
                    result = TickerType.Avg;
                    break;
                case "high":
                    result = TickerType.High;
                    break;
                case "low":
                    result = TickerType.Low;
                    break;
                case "vwap":
                    result = TickerType.Vwap;
                    break;
                case "last_all":
                    result = TickerType.Last_all;
                    break;
                case "last_local":
                    result = TickerType.Last_local;
                    break;
                case "last_orig":
                    result = TickerType.Last_orig;
                    break;
                case "last":
                    result = TickerType.Last;
                    break;
                case "buy":
                    result = TickerType.Buy;
                    break;
                case "sell":
                    result = TickerType.Sell;
                    break;
                case "vol":
                    result = TickerType.Vol;
                    break;

            }
            return result;
        }
Exemple #26
0
        public static bool Tick(TickList __instance)
        {
            TickerType currentTickType     = tickType(__instance);
            int        currentTickInterval = get_TickInterval2(__instance);

            Thing        i;
            List <Thing> tr = thingsToRegister(__instance);

            for (int index = 0; index < tr.Count; ++index)
            {
                try
                {
                    i = tr[index];
                } catch (ArgumentOutOfRangeException) { break; }
                List <Thing> b = BucketOf2(__instance, i, currentTickInterval);
                b.Add(i);
            }
            lock (tr)
            {
                tr.Clear();
            }

            List <Thing> td = thingsToDeregister(__instance);

            for (int index = 0; index < td.Count; ++index)
            {
                try
                {
                    i = td[index];
                } catch (ArgumentOutOfRangeException) { break; }
                List <Thing> b = BucketOf2(__instance, i, currentTickInterval);
                b.Remove(i);
            }
            lock (td)
            {
                td.Clear();
            }

            if (DebugSettings.fastEcology)
            {
                Find.World.tileTemperatures.ClearCaches();
                for (int index1 = 0; index1 < thingLists(__instance).Count; ++index1)
                {
                    List <Thing> thingList = thingLists(__instance)[index1];
                    for (int index2 = 0; index2 < thingList.Count; ++index2)
                    {
                        if (thingList[index2].def.category == ThingCategory.Plant)
                        {
                            thingList[index2].TickLong();
                        }
                    }
                }
            }

            switch (currentTickType)
            {
            case TickerType.Normal:
                RimThreaded.thingListNormal      = thingLists(__instance)[Find.TickManager.TicksGame % currentTickInterval];
                RimThreaded.thingListNormalTicks = RimThreaded.thingListNormal.Count;
                break;

            case TickerType.Rare:
                RimThreaded.thingListRare      = thingLists(__instance)[Find.TickManager.TicksGame % currentTickInterval];
                RimThreaded.thingListRareTicks = RimThreaded.thingListRare.Count;
                break;

            case TickerType.Long:
                RimThreaded.thingListLong      = thingLists(__instance)[Find.TickManager.TicksGame % currentTickInterval];
                RimThreaded.thingListLongTicks = RimThreaded.thingListLong.Count;
                break;
            }

            return(false);
        }
Exemple #27
0
        public void Tick()
        {
            for (int i = 0; i < this.thingsToRegister.Count; i++)
            {
                this.BucketOf(this.thingsToRegister[i]).Add(this.thingsToRegister[i]);
            }
            this.thingsToRegister.Clear();
            for (int j = 0; j < this.thingsToDeregister.Count; j++)
            {
                this.BucketOf(this.thingsToDeregister[j]).Remove(this.thingsToDeregister[j]);
            }
            this.thingsToDeregister.Clear();
            if (DebugSettings.fastEcology)
            {
                Find.World.tileTemperatures.ClearCaches();
                for (int k = 0; k < this.thingLists.Count; k++)
                {
                    List <Thing> list = this.thingLists[k];
                    for (int l = 0; l < list.Count; l++)
                    {
                        if (list[l].def.category == ThingCategory.Plant)
                        {
                            list[l].TickLong();
                        }
                    }
                }
            }
            List <Thing> list2 = this.thingLists[Find.TickManager.TicksGame % this.TickInterval];

            for (int m = 0; m < list2.Count; m++)
            {
                if (!list2[m].Destroyed)
                {
                    try
                    {
                        TickerType tickerType = this.tickType;
                        if (tickerType != TickerType.Normal)
                        {
                            if (tickerType != TickerType.Rare)
                            {
                                if (tickerType == TickerType.Long)
                                {
                                    list2[m].TickLong();
                                }
                            }
                            else
                            {
                                list2[m].TickRare();
                            }
                        }
                        else
                        {
                            list2[m].Tick();
                        }
                    }
                    catch (Exception ex)
                    {
                        string text = (!list2[m].Spawned) ? string.Empty : (" (at " + list2[m].Position + ")");
                        if (Prefs.DevMode)
                        {
                            Log.Error(string.Concat(new object[]
                            {
                                "Exception ticking ",
                                list2[m].ToStringSafe <Thing>(),
                                text,
                                ": ",
                                ex
                            }), false);
                        }
                        else
                        {
                            Log.ErrorOnce(string.Concat(new object[]
                            {
                                "Exception ticking ",
                                list2[m].ToStringSafe <Thing>(),
                                text,
                                ". Suppressing further errors. Exception: ",
                                ex
                            }), list2[m].thingIDNumber ^ 576876901, false);
                        }
                    }
                }
            }
        }
Exemple #28
0
 private void OnSignalClosePosition(Guid accountId, TickerType tickerType, double bidPrice, double askPrice, BarItem barItem)
 {
     brokerAccounts[accountId].Orders.CloseLastOrder(DateTime.Now, tickerType, bidPrice, askPrice, barItem);
 }
Exemple #29
0
 private void OnSignalOpenPosition(Guid accountId, TickerType tickerType, MarketOrderState position, double bidPrice, double askPrice, BarItem barItem)
 {
     brokerAccounts[accountId].Orders.CreateMarketOrder(DateTime.Now, tickerType, position, bidPrice, askPrice, barItem);
 }
Exemple #30
0
        public void Tick()
        {
            for (int i = 0; i < this.thingsToRegister.Count; i++)
            {
                this.BucketOf(this.thingsToRegister[i]).Add(this.thingsToRegister[i]);
            }
            this.thingsToRegister.Clear();
            for (int j = 0; j < this.thingsToDeregister.Count; j++)
            {
                this.BucketOf(this.thingsToDeregister[j]).Remove(this.thingsToDeregister[j]);
            }
            this.thingsToDeregister.Clear();
            if (DebugSettings.fastEcology)
            {
                Find.World.tileTemperatures.ClearCaches();
                for (int k = 0; k < this.thingLists.Count; k++)
                {
                    List <Thing> list = this.thingLists[k];
                    for (int l = 0; l < list.Count; l++)
                    {
                        if (list[l].def.category == ThingCategory.Plant)
                        {
                            list[l].TickLong();
                        }
                    }
                }
            }
            List <Thing> list2 = this.thingLists[Find.TickManager.TicksGame % this.TickInterval];

            for (int m = 0; m < list2.Count; m++)
            {
                if (!list2[m].Destroyed)
                {
                    try
                    {
                        TickerType tickerType = this.tickType;
                        if (tickerType != TickerType.Normal)
                        {
                            if (tickerType != TickerType.Rare)
                            {
                                if (tickerType == TickerType.Long)
                                {
                                    list2[m].TickLong();
                                }
                            }
                            else
                            {
                                list2[m].TickRare();
                            }
                        }
                        else
                        {
                            list2[m].Tick();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (Prefs.DevMode)
                        {
                            Log.Error("Exception ticking " + list2[m].ToString() + ": " + ex.ToString());
                        }
                    }
                }
            }
        }
Exemple #31
0
        private void OnStrategyOpenPosition(Guid accountId, TickerType tickerType, PositionMode position, double bidPrice, double askPrice, BarItem barItem)
        {
            MarketOrderState marketOrderState = (MarketOrderState)Enum.Parse(typeof(MarketOrderState), Enum.GetName(typeof(PositionMode), position));

            brokerAccounts[accountId].Orders.CreateMarketOrder(DateTime.Now, tickerType, marketOrderState, bidPrice, askPrice, barItem);
        }
Exemple #32
0
 public double GetTotalLoss(Guid accountId, TickerType tickerType)
 {
     return(brokerAccounts[accountId].Orders.GetTotalLoss(tickerType));
 }