private void CommandWatcher_Tick(object sender, EventArgs e) { if (Global.State.Commands.Count > 0) { // take the first one string cmd = Global.State.Commands[0]; string[] cv = cmd.Split(':'); switch (cv[0]) { case "log": if (cv[1] == "on") { Global.State.SendLog = true; } else if (cv[1] == "off") { Global.State.SendLog = false; } break; case "change-timer-speed": break; case "unsubscribe": for (int n = 1; n < cv.Length; n++) { Global.State.Subscriptions[cv[n]].Subscribed = false; } break; case "set-selected-thread-control-id": Global.State.SelectedThreadControlId = int.Parse(cv[1]); Global.State.ThreadControlIdChanged = true; break; case "subscribe": for (int n = 1; n < cv.Length; n++) { Global.State.Subscriptions[cv[n]].Subscribed = true; } break; case "algo-subscribe": for (int n = 1; n < cv.Length; n++) { Global.State.AlgoTraderSubscriptions[cv[n]] = true; } AlgoTraderUI.IsSubscribed = true; break; case "algo-clear-selected-symbol": AlgoTraderUI.SelectedSymbol = ""; break; case "algo-clear-selected-strategy-id": AlgoTraderUI.SelectedStrategyId = ""; break; case "algo-set-selected-strategy-id": AlgoTraderUI.SelectedStrategyId = cv[1]; break; case "algo-set-selected-symbol": AlgoTraderUI.SelectedSymbol = cv[1]; break; case "algo-set-speed": AlgoTraderMethods.SetSpeed((AlgoTraderEnums.AlgoTraderSimulatingSpeed) int.Parse(cv[1])); break; case "algo-unsubscribe-all": AlgoTraderUI.UnsubscribeToAll(); break; case "algo-unsubscribe": for (int n = 1; n < cv.Length; n++) { Global.State.AlgoTraderSubscriptions[cv[n]] = false; } if (AlgoTraderUI.IsUnsubscribedToAll()) { AlgoTraderUI.IsSubscribed = false; } break; case "graph-set-dimensions": AlgoTraderUI.GraphCanvasWidth = int.Parse(cv[1]); AlgoTraderUI.GraphCanvasHeight = int.Parse(cv[2]); break; case "start-scheduler": App.StartAppScheduler(); break; case "set-data-table-sort": // set-data-table-sort:overview-symbols:4:asc //set-data-table-sort:" + dataTableName + ":" + index + ":" + dirLabel); //set-data-table-sort:overview-symbols:0:asc string tableName = cv[1]; string sortColumn = cv[2]; string sortDir = cv[3]; if (!AlgoTraderUI.DataTableSorts.ContainsKey(tableName)) { AlgoTraderUI.DataTableSorts.Add(tableName, new AlgoTraderObjects.DataTableSort()); } AlgoTraderUI.DataTableSorts[tableName].Direction = sortDir; AlgoTraderUI.DataTableSorts[tableName].Column = sortColumn; break; case "stop-scheduler": //string s = Tools.SerializerMethods.SerializeThreadControlTree(); //bool breakme = true; App.StopAppScheduler(); break; default: break; } // remove that item (will always be the first element) Global.State.Commands.Remove(cmd); } }
public static void TryBuildPayload() { if (IsSubscribed) { Current++; if (Current >= Frequency) { Current = 0; lock (LockObj) { Dictionary <string, string> items = new Dictionary <string, string>(); if (Global.State.AlgoTraderSubscriptions["main"] == true) { AlgoTraderMain main = new AlgoTraderMain(); main.CurrentTime = AlgoTraderState.CurrentTime.ToString(); main.SelectedSymbol = SelectedSymbol; main.SelectedStrategyName = AlgoTraderMethods.GetStrategyName(SelectedStrategyId); main.SelectedStrategyId = SelectedStrategyId; using (var output = new StringWriter()) { JSON.Serialize(main, output, Options.PrettyPrint); items.Add("algoTraderMain", output.ToString()); } } if (Global.State.AlgoTraderSubscriptions["log"] == true) { UIObjects.AlgoTraderLog log = new UIObjects.AlgoTraderLog(); log.Log = Global.State.AlgoTrader.TC.Log.Read(Verbosity.Verbose); //log.Log = System.Web.HttpUtility.JavaScriptStringEncode(Global.State.AlgoTrader.TC.Log.Read(Verbosity.Verbose)); using (var output = new StringWriter()) { JSON.Serialize(log, output, Options.PrettyPrint); items.Add("algoTraderLog", output.ToString()); } } if (Global.State.AlgoTraderSubscriptions["overview"] == true) { AlgoTraderOverview overview = buildOverviewObject(); using (var output = new StringWriter()) { JSON.Serialize(overview, output, Options.PrettyPrint); items.Add("algoTraderOverview", output.ToString()); } } if (Global.State.AlgoTraderSubscriptions["chart"] == true) { AlgoTraderChart chart = buildChartObject(); using (var output = new StringWriter()) { JSON.Serialize(chart, output, Options.Default); items.Add("algoTraderChart", output.ToString()); } } Payload = SerializerMethods.DictionarySerializedValuesToJSON(items); } } } }
/// <summary> /// Executes once when the application starts /// </summary> public static void Init() { Global.State.ExecutablePath = AppDomain.CurrentDomain.BaseDirectory; string sourcePath = Global.State.ExecutablePath.Replace("\\compiled\\", ""); Global.Constants.StrategiesFolder = sourcePath + "\\AlgoTrader\\Strategies\\"; Global.Constants.BaseStrategyPath = sourcePath + "\\AlgoTrader\\AlgoTraderBaseStrategy.cs"; // D:\\Projects\\AlgoTrader - New\\Source\\AT\\AlgoTrader\\AlgoTraderBaseStrategy.cs Global.Constants.NodesPath = Global.State.ExecutablePath + "nodes\\"; Global.Constants.DBPath = Global.State.ExecutablePath + "dbs\\"; Global.Constants.StockTicksDBPath = Global.Constants.DBPath + Global.Constants.StockTicksDBFolder + "\\"; Global.Constants.TradingStatsDBPath = Global.Constants.DBPath + Global.Constants.TradingStatsDBFolder + "\\"; DBMethods.Init(); // ThreadPool.SetMinThreads(100, 100); string json = File.ReadAllText(Global.State.ExecutablePath + "schedules\\main.js"); Global.State.SchedulerTC = new ThreadControl("Scheduler"); Global.State.Scheduler = new Scheduler(json, "app"); Global.State.AllSymbols = DBMethods.GetAllSymbols(); //Global.State.AllSymbols.Shuffle(5); Global.State.AllSymbols.Clear(); Global.State.AllSymbols.Add("aapl"); Global.State.AllSymbols.Add("spy"); Global.State.AllSymbols.Add("qqq"); Global.State.AllSymbols.Add("msft"); Global.State.AllSymbols.Add("gld"); Global.State.AllSymbols.Add("amd"); /* * * Global.State.AllSymbols.Add("crc"); * Global.State.AllSymbols.Add("cnp"); * Global.State.AllSymbols.Add("meet"); * Global.State.AllSymbols.Add("eros"); * Global.State.AllSymbols.Add("bimi"); * */ //Global.State.AllSymbols.Add("tsla"); //Global.State.AllSymbols.Add("amd"); //Global.State.AllSymbols.Shuffle(); //Global.State.AllSymbols.RemoveRange(0, 380); /* * Global.State.AllSymbols.Add("spy"); * Global.State.AllSymbols.Add("amd"); * Global.State.AllSymbols.Add("tsla"); * Global.State.AllSymbols.Add("x"); * Global.State.AllSymbols.Add("aapl"); * Global.State.AllSymbols.Add("t"); * Global.State.AllSymbols.Add("xli"); * Global.State.AllSymbols.Add("tsg"); * Global.State.AllSymbols.Add("crc"); * Global.State.AllSymbols.Add("tza"); * Global.State.AllSymbols.Add("carg"); * Global.State.AllSymbols.Add("immu"); */ //Global.State.AllSymbols = new List<string>(); //Global.State.AllSymbols.Add("spy"); /* * if (Global.Constants.FastForTesting) * { * * } */ Global.State.ThreadControlTree = new List <ThreadControl>(); Global.State.ThreadControlTree.Add(Global.State.SchedulerTC); Global.State.Subscriptions = new Dictionary <string, Subscription>(); Global.State.AlgoTraderSubscriptions = new Dictionary <string, bool>(); //Global.State.ForceUpdate = new Dictionary<string, bool>(); // Global.State.SubscriptionStates = new Dictionary<string, object>(); Global.State.Subscriptions.Add("schedulerRunning", new Subscription() { State = false, Interval = 2 }); Global.State.Subscriptions.Add("threadControlTree", new Subscription() { State = long.MaxValue, Interval = 5 }); Global.State.Subscriptions.Add("threadControlState", new Subscription() { State = "md5hashstring", Interval = 7 }); Global.State.Subscriptions.Add("threadControlLog", new Subscription() { State = int.MaxValue, Interval = 3 }); Global.State.Subscriptions.Add("threadControlObject", new Subscription() { State = int.MaxValue, Interval = 2 }); Global.State.Subscriptions.Add("applicationStats", new Subscription() { State = "some string", Interval = 6 }); Global.State.AlgoTraderSubscriptions.Add("main", false); Global.State.AlgoTraderSubscriptions.Add("overview", false); Global.State.AlgoTraderSubscriptions.Add("positions", false); Global.State.AlgoTraderSubscriptions.Add("orders", false); Global.State.AlgoTraderSubscriptions.Add("actions", false); Global.State.AlgoTraderSubscriptions.Add("chart", false); Global.State.AlgoTraderSubscriptions.Add("log", false); InitDataTracker(); // this also stores new ids and names in DB Global.State.UsableStrategies = AlgoTraderMethods.RefreshStrategyNamesAndIds(); }
public void Run(AlgoTraderModes mode, List <FD> days, ThreadControl tc) { TC = tc; ResetData(); AlgoTraderState.Mode = mode; SetIntervalBasedOnMode(); AlgoTraderState.IsSim = AlgoTraderState.Mode == AlgoTraderModes.GatherStats || AlgoTraderState.Mode == AlgoTraderModes.Simulating || AlgoTraderState.Mode == AlgoTraderModes.Backtesting; AlgoTraderState.UseSimOrdering = AlgoTraderState.IsSim || AlgoTraderState.Mode == AlgoTraderModes.FakeLive; PrepDays(days); bool allowToRun = true; if (!AlgoTraderState.IsSim) { ZonedDateTime zdt = UCDT.GetCurrentEastCoastTime(); if (AlgoTraderState.Mode == AlgoTraderModes.Live && zdt.Hour >= 4) { allowToRun = false; } } if (allowToRun) { // live or paper only has one day for (int n = 0; n < Days.Count; n++) { ResetDayData(); AlgoTraderState.CurrentDay = Days[n]; // AlgoTraderUI.SelectedSymbol = Global.State.AllSymbols[UC.GetRandomInteger(0, Global.State.AllSymbols.Count - 1)]; // init the list where nodes, trend lines, etc are stored // AlgoTraderShared.NodesData = new Dictionary<string, NodesData>(); for (int m = 0; m < Global.State.AllSymbols.Count; m++) { AlgoTraderShared.NodesData.Add(Global.State.AllSymbols[m], null); } CollectGarbage(); // this will take a while only when simulating PrepareSimDayNodes(); // starts incrementing CurrentTime immediatly when this is called PrepareTimer(); // waits for 3:55am AlgoTraderMethods.WaitFor(AlgoTraderConfig.TicksStreamManagerAtHour, AlgoTraderConfig.TicksStreamManagerAtMinute, TC); RunStocksDataUpdater(); RunOrdersDataUpdater(); RunTradingManager(); CollectGarbage(); // waits for trading manager to finish WaitToContinue(); TC.StopAllChildrenAndWait(); if (!TC.CheckNotStopped()) { break; } } } TC.Log.AddLine("AlgoTraderProcess Run() complete", Verbosity.Minimal); // it will also set Ended = true ResetData(); }