コード例 #1
0
        public async Task <IActionResult> IntradayTimeSeries(int id)
        {
            string userId = this.User.QID();

            if (userId == null)
            {
                return(Unauthorized());
            }

            QuantApp.Kernel.User user = QuantApp.Kernel.User.FindUser(userId);

            Instrument instrument = Instrument.FindInstrument(id);
            var        ts         = instrument.GetTimeSeries(TimeSeriesType.Last);

            var jres = new List <object[]>();

            if (ts != null && ts.Count != 0)
            {
                int count = ts.Count;

                var t0 = instrument is Strategy && (instrument as Strategy).Portfolio != null ? ts[Math.Max(0, count - 10000)] : 0;

                for (int i = Math.Max(0, count - 10000); i < count; i++)
                {
                    DateTime date  = ts.DateTimes[i];
                    double   value = ts[i] - t0;

                    jres.Add(new object[] { Utils.Utils.ToJSTimestamp(date), value });
                }
            }

            return(Ok(jres));
        }
コード例 #2
0
        public async Task <IActionResult> TimeSeries(int id, string spot_type, string lastDate, int days = 20, int bid = -1, string b_type = "")
        {
            string userId = this.User.QID();

            if (userId == null)
            {
                return(Unauthorized());
            }


            QuantApp.Kernel.User user       = QuantApp.Kernel.User.FindUser(userId);
            Instrument           instrument = Instrument.FindInstrument(id);

            TimeSeries ts_close = instrument.GetTimeSeries(instrument.InstrumentType == InstrumentType.ETF || instrument.InstrumentType == InstrumentType.Equity ? TimeSeriesType.AdjClose : TimeSeriesType.Close, DataProvider.DefaultProvider, true);

            if (instrument.InstrumentType == InstrumentType.Strategy && spot_type != null && spot_type.Trim() != "" && spot_type.Trim() != "spot_close")
            {
                Strategy strategy = (instrument as Strategy);

                if (strategy != null)
                {
                    int tid = int.MinValue;
                    if (!int.TryParse(spot_type, out tid))
                    {
                        tid = strategy.MemoryTypeInt(spot_type);
                    }
                    ts_close = strategy.GetMemorySeries(tid);
                }

                if (ts_close == null || (ts_close != null && ts_close.Count == 0))
                {
                    ts_close = instrument.GetTimeSeries((TimeSeriesType)Enum.Parse(typeof(TimeSeriesType), spot_type), DataProvider.DefaultProvider, true);
                }
            }
            else if (!string.IsNullOrWhiteSpace(spot_type) && spot_type.Trim() != "spot_close")
            {
                ts_close = instrument.GetTimeSeries((TimeSeriesType)Enum.Parse(typeof(TimeSeriesType), spot_type), DataProvider.DefaultProvider, true);
            }


            TimeSeries bm_close = null;

            if (bid != -1)
            {
                Instrument bm = Instrument.FindInstrument(bid);

                bm_close = bm.GetTimeSeries(bm.InstrumentType == InstrumentType.ETF || bm.InstrumentType == InstrumentType.Equity ? TimeSeriesType.AdjClose : TimeSeriesType.Last, DataProvider.DefaultProvider, true);


                if (bm.InstrumentType == InstrumentType.Strategy && b_type != null && b_type.Trim() != "" && b_type.Trim() != "spot_close")
                {
                    Strategy strategy = (bm as Strategy);

                    if (strategy != null)
                    {
                        int tid = int.MinValue;
                        if (!int.TryParse(b_type, out tid))
                        {
                            tid = strategy.MemoryTypeInt(b_type);
                        }
                        bm_close = strategy.GetMemorySeries(tid);
                    }

                    if (bm_close == null || (bm_close != null && bm_close.Count == 0))
                    {
                        bm_close = instrument.GetTimeSeries((TimeSeriesType)Enum.Parse(typeof(TimeSeriesType), b_type), DataProvider.DefaultProvider, true);
                    }
                }
                else if (!string.IsNullOrWhiteSpace(b_type) && b_type.Trim() != "spot_close")
                {
                    bm_close = bm.GetTimeSeries((TimeSeriesType)Enum.Parse(typeof(TimeSeriesType), b_type), DataProvider.DefaultProvider, true);
                }
            }


            if (lastDate != null && ts_close != null && ts_close.Count != 0)
            {
                ts_close = ts_close.GetRange(ts_close.DateTimes[0], string.IsNullOrWhiteSpace(lastDate) ? DateTime.Today : DateTime.Parse(lastDate), AQI.AQILabs.Kernel.Numerics.Util.TimeSeries.RangeFillType.None);
            }


            var jres = new List <object[]>();

            if (ts_close != null && ts_close.Count != 0)
            {
                int count = ts_close.Count;

                var isStrategy = instrument is Strategy && (instrument as Strategy).Portfolio != null;

                var t0 = isStrategy ? ts_close[Math.Max(0, count - 10000)] : 0;

                double max = ts_close[0] - t0;
                for (int i = Math.Max(0, count - 10000); i < count; i++)
                {
                    DateTime date = ts_close.DateTimes[i];
                    double   spot = ts_close[i] - t0;
                    double   vol  = 0;

                    try
                    {
                        vol = (count > days && i >= days ? ts_close.GetRange(i - days, i).LogReturn().StdDev *Math.Sqrt(252) : 0);
                    }
                    catch { }

                    max = Math.Max(spot, max);
                    double maxdd = isStrategy ? spot - max : (spot / max - 1.0);

                    if (instrument.InstrumentType == InstrumentType.Strategy && spot_type != null && spot_type.Trim() != "" && spot_type.Trim() != "spot_close")
                    {
                        vol   = 0;
                        max   = 0;
                        maxdd = 0;
                    }

                    double bm_spot = 0;
                    if (bm_close != null)
                    {
                        bm_spot = bm_close[date, AQI.AQILabs.Kernel.Numerics.Util.TimeSeries.DateSearchType.Previous];
                    }

                    jres.Add(new object[] { Utils.Utils.ToJSTimestamp(ts_close.DateTimes[i]), spot, vol * 100, maxdd * (isStrategy ? 1 : 100), bm_spot });
                    //jres.Add(new object[] { Utils.Utils.ToJSTimestamp(ts_close.DateTimes[i]), spot, vol, maxdd, flags.ContainsKey(date.Date) ? flags[date.Date].Content : "", flags.ContainsKey(date.Date) ? flags[date.Date].ID : "", bm_spot });
                }
            }

            return(Ok(jres));
        }
コード例 #3
0
ファイル: LabController.cs プロジェクト: cmqd/coflows-quant
        public async Task LabPatch(string id, string url = "")
        {
            string cokey = Request.Cookies["coflows"];

            if (cokey != null)
            {
                if (AccountController.sessionKeys.ContainsKey(cokey))
                {
                    QuantApp.Kernel.User quser = QuantApp.Kernel.User.FindUserBySecret(AccountController.sessionKeys[cokey]);
                    if (quser == null)
                    {
                        var content = Encoding.UTF8.GetBytes("Not Authorized...");
                        await Response.Body.WriteAsync(content, 0, content.Length);

                        return;
                    }
                }
                else
                {
                    var content = Encoding.UTF8.GetBytes("Not Authorized...");
                    await Response.Body.WriteAsync(content, 0, content.Length);

                    return;
                }
            }
            else
            {
                var content = Encoding.UTF8.GetBytes("Not Authorized...");
                await Response.Body.WriteAsync(content, 0, content.Length);

                return;
            }

            if (!Program.useJupyter)
            {
                Console.WriteLine("Jupyter is not on: " + id + " " + url);
                var content = Encoding.UTF8.GetBytes("This CoFlows container doesn't have Jupter started...");
                await Response.Body.WriteAsync(content, 0, content.Length);
            }
            else
            {
                if (url == null)
                {
                    url = "";
                }
                var queryString = Request.QueryString;
                url = "/lab/" + id + "/" + url + queryString.Value;

                var headers = new List <KeyValuePair <string, string> >();

                foreach (var head in Request.Headers)
                {
                    foreach (var val in head.Value)
                    {
                        try
                        {
                            headers.Add(new KeyValuePair <string, string>(head.Key, val));
                        }
                        catch {}
                    }
                }

                var mem = new MemoryStream();
                // Request.Body.CopyTo(mem);
                try
                {
                    await Request.Body.CopyToAsync(mem);
                }
                catch (Exception e) { Console.WriteLine(e); }
                mem.Position = 0;

                var _headers = headers;

                var streamContent = new StreamContent(mem);

                QuantApp.Kernel.User.ContextUser = new QuantApp.Kernel.UserData();
                HttpClient _httpClient = new HttpClient();
                if (_headers != null)
                {
                    foreach (var head in _headers)
                    {
                        try
                        {
                            _httpClient.DefaultRequestHeaders.Add(head.Key, head.Value);
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }

                int labPort = 0;
                if (Program.useJupyter && !LabDB.ContainsKey(cokey + id))
                {
                    QuantApp.Kernel.User quser = QuantApp.Kernel.User.FindUserBySecret(AccountController.sessionKeys[cokey]);
                    bool isRoot = quser.ID == "QuantAppSecure_root";

                    lastLabPort++;
                    LabDB[cokey + id] = lastLabPort;

                    var userName   = System.Text.RegularExpressions.Regex.Replace(quser.Email, "[^a-zA-Z0-9 -]", "_");
                    var createUser = "******" + userName + "';userExists = newUser in list(map(lambda x: x.split(':')[0], subprocess.check_output(['getent', 'passwd']).decode('utf-8').split('\\n'))); print('User exists: ' + newUser) if userExists else subprocess.check_call(['adduser', '--gecos', '\"First Last,RoomNumber,WorkPhone,HomePhone\"', '--disabled-password', '--home', f'/app/mnt/home/{newUser}/', '--shell', '/bin/bash', f'{newUser}']); print('no need to edit .bashrc') if (('PS1=\"\\\\u:\\\\w> \"' in open(f'/app/mnt/home/{newUser}/.bashrc').read())) else open(f'/app/mnt/home/{newUser}/.bashrc', 'a').write('PS1=\"\\\\u:\\\\w> \"')";
                    // var createUser = "******" + userName + "';userExists = newUser in list(map(lambda x: x.split(':')[0], subprocess.check_output(['getent', 'passwd']).decode('utf-8').split('\\n'))); print('User exists: ' + newUser) if userExists else subprocess.check_call(['adduser', '--gecos', '\"First Last,RoomNumber,WorkPhone,HomePhone\"', '--disabled-password', '--home', f'/app/mnt/home/{newUser}/', '--shell', '/bin/bash', f'{newUser}']); print('no need to edit .bashrc') if userExists else open(f'/app/mnt/home/{newUser}/.bashrc', 'a').write('PS1=\"\\\\u:\\\\w> \"')";
                    var code = isRoot ? "import subprocess;subprocess.check_call(['jupyter', 'lab', '--port=" + lastLabPort + "', '--NotebookApp.notebook_dir=/app/mnt/', '--ip=*', '--NotebookApp.allow_remote_access=True', '--allow-root', '--no-browser', '--NotebookApp.token=\'\'', '--NotebookApp.password=\'\'', '--NotebookApp.disable_check_xsrf=True', '--NotebookApp.base_url=/lab/" + id + "'], cwd='/app/')" : "import subprocess;subprocess.check_call(['sudo', '-u', '" + userName + "', 'jupyter', 'lab', '--port=" + lastLabPort + "', '--NotebookApp.notebook_dir=/app/mnt/', '--ip=*', '--NotebookApp.allow_remote_access=True', '--no-browser', '--NotebookApp.token=\'\'', '--NotebookApp.password=\'\'', '--NotebookApp.disable_check_xsrf=True', '--NotebookApp.base_url=/lab/" + id + "'], cwd='/app/mnt/home/" + userName + "')";
                    var th   = new System.Threading.Thread(() => {
                        using (Py.GIL())
                        {
                            Console.WriteLine("Starting Jupyter...");

                            if (!isRoot)
                            {
                                Console.WriteLine(createUser);
                                PythonEngine.Exec(createUser);
                            }

                            Console.WriteLine(code);
                            PythonEngine.Exec(code);
                        }
                    });
                    th.Start();
                    System.Threading.Thread.Sleep(1000 * 5);
                }

                labPort = LabDB[cokey + id];

                var response = await _httpClient.PatchAsync("http://localhost:" + labPort + url, streamContent);

                var content = await response.Content.ReadAsByteArrayAsync();

                var rheaders = new List <KeyValuePair <string, string> >();

                try
                {
                    foreach (var head in response.Headers)
                    {
                        foreach (var val in head.Value)
                        {
                            try
                            {
                                rheaders.Add(new KeyValuePair <string, string>(head.Key, val));
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                            }
                        }
                    }
                }
                catch (Exception e) {}


                foreach (var head in rheaders)
                {
                    try
                    {
                        string key = head.Key.ToString();
                        string val = head.Value.ToString();

                        if (key == "Set-Cookie")
                        {
                            string[] vals       = val.Split(';');
                            string[] keyValPair = vals[0].Split('=');
                            string   cname      = keyValPair[0];

                            string cval   = keyValPair[1].Replace("\"", "");
                            var    option = new CookieOptions();
                            if (vals.Length > 1)
                            {
                                string[] expiryPair = vals[1].Split('=');
                                string   date_str   = expiryPair[1];
                                option.Expires = DateTime.Parse(date_str);
                            }

                            if (vals.Length > 2)
                            {
                                string[] pathPair = vals[2].Split('=');
                                option.Path = pathPair[1];
                            }

                            Response.Cookies.Append(cname, cval, option);
                        }
                        else
                        {
                            Response.Headers.Add(key, val);
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }

                Response.StatusCode = (int)response.StatusCode;
                if (response.Content.Headers.ContentType != null)
                {
                    Response.ContentType = response.Content.Headers.ContentType.ToString();
                }
                Response.ContentLength = content.Length;

                if (content.Length > 0 && Response.StatusCode != 204)
                {
                    await Response.Body.WriteAsync(content, 0, content.Length);
                }
            }
        }