Example #1
0
        public static void Plot()
        {
            using (Py.GIL())
            {
                dynamic plt = Py.Import("matplotlib.pylab");
                dynamic np  = Py.Import("numpy");

                var x = Numpy.NewArray(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 });
                var y = np.sin(x);
                //plt.plot(x);
                plt.figure();
                //plt.scatter(x, y);
                plt.show();
            }
            using (Py.GIL())
            {
                var scope = Py.CreateScope();
                var np    = scope.Import("numpy", "np");
                var plt   = scope.Import("matplotlib.pylab", "plt");

                var x = Numpy.NewArray(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 });
                var y = np.sin(x);
                scope.Set("x", x);
                scope.Set("y", y);

                //directly plot
                plt.plot(x, y);
                plt.show();

                //use python slice grammer
                scope.Exec(
                    "fig = plt.figure() \n" +
                    "plt.plot(x[1:], y[1:]) \n" +
                    "plt.show()"
                    );
            }
        }
        public void OldFillModel_NewFillContextAndGetPrices_Py()
        {
            using (Py.GIL())
            {
                var module = PyModule.FromString(Guid.NewGuid().ToString(),
                                                 "from AlgorithmImports import *\n" +
                                                 "class CustomFillModel(FillModel):\n" +
                                                 "   def __init__(self):\n" +
                                                 "       self.FillWasCalled = False\n" +
                                                 "       self.GetPricesWasCalled = False\n" +
                                                 "   def Fill(self, parameters):\n" +
                                                 "       self.FillWasCalled = True\n" +
                                                 "       self.Parameters = parameters\n" +
                                                 "       return Fill(super().MarketFill(parameters.Security, parameters.Order))\n" +
                                                 "   def GetPrices(self, asset, direction):\n" +
                                                 "       self.GetPricesWasCalled = True\n" +
                                                 "       return super().GetPrices(asset, direction)");

                var customFillModel = module.GetAttr("CustomFillModel").Invoke();
                var wrapper         = new FillModelPythonWrapper(customFillModel);

                var result = wrapper.Fill(new FillModelParameters(
                                              _security,
                                              new MarketOrder(_security.Symbol, 1, orderDateTime),
                                              new MockSubscriptionDataConfigProvider(_config),
                                              Time.OneHour
                                              ));

                bool called;
                customFillModel.GetAttr("FillWasCalled").TryConvert(out called);
                Assert.True(called);
                customFillModel.GetAttr("GetPricesWasCalled").TryConvert(out called);
                Assert.True(called);
                Assert.IsNotNull(result);
                Assert.AreEqual(OrderStatus.Filled, result.OrderEvent.Status);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            PythonEngine.Initialize();

            var globals = CreateGlobals();

            while (true)
            {
                Console.Write(">>> ");

                var input = Console.ReadLine();
                if (input == "exit()")
                {
                    return;
                }

                using (Py.GIL())
                {
                    var ret = PythonEngine.RunString($"RESULTS_VARIABLE = {input}", globals.Handle, IntPtr.Zero);

                    if (ret != null)
                    {
                        Console.WriteLine(globals.GetItem("RESULTS_VARIABLE"));
                    }
                    else
                    {
                        var exception = new PythonException();
                        ret = PythonEngine.RunString(input, globals.Handle, IntPtr.Zero);
                        if (ret == null)
                        {
                            exception = new PythonException();
                            Console.WriteLine(exception.Message);
                        }
                    }
                }
            }
        }
Example #4
0
        public void RegistersIndicatorProperlyPythonScript()
        {
            const string code = @"
from clr import AddReference
AddReference('System')
AddReference('QuantConnect.Algorithm')
AddReference('QuantConnect.Indicators')
AddReference('QuantConnect.Common')
AddReference('QuantConnect.Lean.Engine')

from System import *
from QuantConnect import *
from QuantConnect.Securities import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Lean.Engine.DataFeeds import *

algo = QCAlgorithm()

marketHoursDatabase = MarketHoursDatabase.FromDataFolder()
symbolPropertiesDatabase = SymbolPropertiesDatabase.FromDataFolder()
securityService =  SecurityService(algo.Portfolio.CashBook, marketHoursDatabase, symbolPropertiesDatabase, algo, RegisteredSecurityDataTypesProvider.Null, SecurityCacheProvider(algo.Portfolio))
algo.Securities.SetSecurityService(securityService)
dataPermissionManager = DataPermissionManager()
dataManager = DataManager(None, UniverseSelection(algo, securityService, dataPermissionManager), algo, algo.TimeKeeper, marketHoursDatabase, False, RegisteredSecurityDataTypesProvider.Null, dataPermissionManager)
algo.SubscriptionManager.SetDataManager(dataManager)


forex = algo.AddForex('EURUSD', Resolution.Daily)
indicator = IchimokuKinkoHyo('EURUSD', 9, 26, 26, 52, 26, 26)
algo.RegisterIndicator(forex.Symbol, indicator, Resolution.Daily)";

            using (Py.GIL())
            {
                Assert.DoesNotThrow(() => PythonEngine.ModuleFromString("RegistersIndicatorProperlyPythonScript", code));
            }
        }
Example #5
0
        public static List <T> GetListFromList <T>(dynamic pyList, ref List <T> lstMember)
        {
            if (lstMember != null)
            {
                return(lstMember);
            }

            using (Py.GIL())
            {
                lstMember = new List <T>();

                dynamic builtins = Py.Import("builtins");
                var     pyCount  = new PyInt(builtins.len(pyList));
                var     count    = pyCount.ToInt32();

                for (var i = 0; i < count; i++)
                {
                    var element = pyList[i];

                    object created = null;
                    if (typeof(T) == typeof(string))
                    {
                        var pyObj = new PyString(element);
                        created = pyObj.ToString();
                    }
                    else
                    {
                        Debug.Assert(false);
                        return(null);
                    }

                    lstMember.Add((T)created);
                }

                return(lstMember);
            }
        }
Example #6
0
        protected override IndicatorBase <IBaseData> CreateIndicator()
        {
            using (Py.GIL())
            {
                var module = PythonEngine.ModuleFromString(
                    Guid.NewGuid().ToString(),
                    @"
from clr import AddReference
AddReference('QuantConnect.Common')
AddReference('QuantConnect.Indicators')

from QuantConnect import *
from QuantConnect.Indicators import *
from collections import deque
from datetime import datetime, timedelta
from numpy import sum

class CustomSimpleMovingAverage(PythonIndicator):
    def __init__(self, name, period):
        self.Name = name
        self.Value = 0
        self.queue = deque(maxlen=period)

    # Update method is mandatory
    def Update(self, input):
        self.queue.appendleft(input.Value)
        count = len(self.queue)
        self.Value = sum(self.queue) / count
        return count == self.queue.maxlen
"
                    );
                var indicator = module.GetAttr("CustomSimpleMovingAverage")
                                .Invoke("custom".ToPython(), 14.ToPython());

                return(new PythonIndicator(indicator));
            }
        }
Example #7
0
        public static void Pythonnet_(string input_path, string output_path, List <string> imgList)
        {
            // python 설치 경로
            var PYTHON_HOME = "";

            //var PYTHON_HOME = Environment.ExpandEnvironmentVariables(@"\anaconda\");

            // 환경 변수 설정
            Find_conda(ref PYTHON_HOME);
            AddEnvPath(PYTHON_HOME, Path.Combine(PYTHON_HOME, @"envs\deeplab\Library\bin"));

            // Python 홈 설정.
            PythonEngine.PythonHome = PYTHON_HOME;

            // 모듈 패키지 패스 설정.
            PythonEngine.PythonPath = string.Join(
                Path.PathSeparator.ToString(),
                new string[] {
                PythonEngine.PythonPath,
                // pip하면 설치되는 패키지 폴더.
                Path.Combine(PYTHON_HOME, @"envs\deeplab\Lib\site-packages"),
                // 개인 패키지 폴더
                Environment.CurrentDirectory
            }
                );
            // Python 엔진 초기화
            PythonEngine.Initialize();
            using (Py.GIL())
            {
                dynamic        deeplab = Py.Import("python.deeplab");
                OpenFileDialog dialog  = new OpenFileDialog();
                dynamic        func    = deeplab.Caclulating(input_path, output_path, imgList);
                func.cal();
            }

            PythonEngine.Shutdown();
        }
Example #8
0
        public void MultipleSecuritiesQuantBookHistoryTests()
        {
            using (Py.GIL())
            {
                var startDate           = new DateTime(2014, 5, 9);
                var securityTestHistory = _module.MultipleSecuritiesHistoryTest(startDate, null, null);

                // Get the last 5 candles
                var periodHistory = securityTestHistory.test_period_overload(5);

                // Note there is no data for BTCUSD at 2014

                //symbol                 EURUSD         SPY
                //time
                //2014-05-03 00:00:00        NaN        173.580655
                //2014-05-04 20:00:00   1.387185               NaN
                //2014-05-05 20:00:00   1.387480               NaN
                //2014-05-06 00:00:00        NaN        173.903690
                //2014-05-06 20:00:00   1.392925               NaN
                //2014-05-07 00:00:00        NaN        172.426958
                //2014-05-07 20:00:00   1.391070               NaN
                //2014-05-08 00:00:00        NaN        173.423752
                //2014-05-08 20:00:00   1.384265               NaN
                //2014-05-09 00:00:00        NaN        173.229931
                Log.Trace(periodHistory.ToString());

                var count = (periodHistory.shape[0] as PyObject).AsManagedObject(typeof(int));
                Assert.AreEqual(10, count);

                // Get the one day of data
                var timedeltaHistory = securityTestHistory.test_period_overload(TimeSpan.FromDays(8));
                var firstIndex       = (DateTime)(timedeltaHistory.index.values[0] as PyObject).AsManagedObject(typeof(DateTime));

                // EURUSD exchange time zone is NY but data is UTC so we have a 4 hour difference with algo TZ which is NY
                Assert.AreEqual(startDate.AddDays(-8).AddHours(20), firstIndex);
            }
        }
Example #9
0
        public void MultipleSecuritiesQuantBookHistoryTests()
        {
            using (Py.GIL())
            {
                var startDate           = new DateTime(2014, 5, 9);
                var securityTestHistory = _module.MultipleSecuritiesHistoryTest(startDate, null, null);

                // Get the last 5 candles
                var periodHistory = securityTestHistory.test_period_overload(5);

                // Note there is no data for BTCUSD at 2014

                //symbol                 EURUSD         SPY
                //time
                //2014-05-03 00:00:00       NaN  173.580655
                //2014-05-05 01:00:00  1.387565         NaN
                //2014-05-06 00:00:00       NaN  173.903690
                //2014-05-06 01:00:00  1.388335         NaN
                //2014-05-07 00:00:00       NaN  172.426958
                //2014-05-07 01:00:00  1.392495         NaN
                //2014-05-08 00:00:00       NaN  173.423752
                //2014-05-08 01:00:00  1.391480         NaN
                //2014-05-09 00:00:00       NaN  173.229931
                Console.WriteLine(periodHistory);

                var count = (periodHistory.shape[0] as PyObject).AsManagedObject(typeof(int));
                Assert.AreEqual(9, count);

                // Get the one day of data
                var timedeltaHistory = securityTestHistory.test_period_overload(TimeSpan.FromDays(8));
                var firstIndex       = (DateTime)(timedeltaHistory.index.values[0] as PyObject).AsManagedObject(typeof(DateTime));

                // adding an hour because EURUSD exchange time zone, has 1 hour difference with than data time zone
                // due to day light savings change (data is in exchange time zone)
                Assert.AreEqual(startDate.AddDays(-8).AddHours(1), firstIndex);
            }
        }
Example #10
0
            public override ValueType Execute(ValueType arg)
            {
                try
                {
                    // Create a C# user-defined object in Python. Asssing some values.
                    Type   type = typeof(Python.EmbeddingTest.Domain.MyClass);
                    string code = string.Format(@"
import clr
clr.AddReference('{0}')

from Python.EmbeddingTest.Domain import MyClass
obj = MyClass()
obj.Method()
obj.StaticMethod()
obj.Property = 1
obj.Field = 10
", Assembly.GetExecutingAssembly().FullName);

                    using (Py.GIL())
                        using (var scope = Py.CreateScope())
                        {
                            scope.Exec(code);
                            using (PyObject obj = scope.Get("obj"))
                            {
                                Debug.Assert(obj.AsManagedObject(type).GetType() == type);
                                // We only needs its Python handle
                                PyRuntime.XIncref(obj.Handle);
                                return(obj.Handle);
                            }
                        }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    throw;
                }
            }
Example #11
0
        public static dynamic NewConverter()
        {
            var converter = new PyConverter();

            using (Py.GIL())
            {
                //XIncref is needed, or keep the PyObject
                converter.Converters.Add(PythonEngine.Eval("int").Handle, (args) => { return(args.AsManagedObject(typeof(int))); });
                converter.Converters.Add(PythonEngine.Eval("str").Handle, (args) => { return(args.AsManagedObject(typeof(string))); });
                converter.Converters.Add(PythonEngine.Eval("float").Handle, (args) => { return(args.AsManagedObject(typeof(double))); });
                converter.Converters.Add(PythonEngine.Eval("bool").Handle, (args) => { return(args.AsManagedObject(typeof(bool))); });
                converter.Converters.Add(PythonEngine.Eval("list").Handle, (args) =>
                {
                    List <object> list = new List <object>();
                    for (int i = 0; i < args.Length(); i++)
                    {
                        dynamic newVal = converter.Convert(args[i]);
                        list.Add(newVal);
                    }
                    return(list);
                });
                converter.Converters.Add(PythonEngine.Eval("dict").Handle, (args) =>
                {
                    Dictionary <string, object> dict = new Dictionary <string, object>();
                    var dictionaryItems = args.InvokeMethod("items");
                    Console.WriteLine("dictionaryItems");
                    foreach (dynamic keyValue in dictionaryItems)
                    {
                        dynamic newKey   = converter.Convert(keyValue[0]);
                        dynamic newValue = converter.Convert(keyValue[1]);
                        dict.Add(newKey.ToString(), newValue);
                    }
                    return(dict);
                });
            }
            return(converter);
        }
Example #12
0
        private double GetExpectedValue(List <Slice> history)
        {
            var code = @"
import numpy as np
import pandas as pd
import math
from BlackLittermanOptimizationPortfolioConstructionModel import BlackLittermanOptimizationPortfolioConstructionModel as blopcm

def GetDeterminantFromHistory(history):
    returns = dict()
    history = history.lastprice.unstack(0)

    for symbol, df in history.items():
        symbolData = blopcm.BlackLittermanSymbolData(symbol, 1, 5)
        for time, close in df.dropna().items():
            symbolData.Update(time, close)

        returns[symbol] = symbolData.Return
    
    df = pd.DataFrame(returns)
    if df.isna().sum().sum() > 0:
        df[df.columns[-1]] = df[df.columns[-1]].bfill()
        df = df.dropna()

    return np.linalg.det(df)";

            using (Py.GIL())
            {
                dynamic GetDeterminantFromHistory = PythonEngine
                                                    .ModuleFromString("GetDeterminantFromHistory", code)
                                                    .GetAttr("GetDeterminantFromHistory");

                dynamic df = new PandasConverter().GetDataFrame(history);
                return(GetDeterminantFromHistory(df));
            }
        }
Example #13
0
        public void ConvertToSymbolsTest()
        {
            var expected = new List <Symbol>
            {
                Symbol.Create("AIG", SecurityType.Equity, Market.USA),
                Symbol.Create("BAC", SecurityType.Equity, Market.USA),
                Symbol.Create("IBM", SecurityType.Equity, Market.USA),
                Symbol.Create("GOOG", SecurityType.Equity, Market.USA)
            };

            using (Py.GIL())
            {
                // Test Python String
                var test1 = PythonUtil.ConvertToSymbols(new PyString("AIG"));
                Assert.IsTrue(typeof(List <Symbol>) == test1.GetType());
                Assert.AreEqual(expected.FirstOrDefault(), test1.FirstOrDefault());

                // Test Python List of Strings
                var list  = (new List <string> {
                    "AIG", "BAC", "IBM", "GOOG"
                }).ToPyList();
                var test2 = PythonUtil.ConvertToSymbols(list);
                Assert.IsTrue(typeof(List <Symbol>) == test2.GetType());
                Assert.IsTrue(test2.SequenceEqual(expected));

                // Test Python Symbol
                var test3 = PythonUtil.ConvertToSymbols(expected.FirstOrDefault().ToPython());
                Assert.IsTrue(typeof(List <Symbol>) == test3.GetType());
                Assert.AreEqual(expected.FirstOrDefault(), test3.FirstOrDefault());

                // Test Python List of Symbols
                var test4 = PythonUtil.ConvertToSymbols(expected.ToPyList());
                Assert.IsTrue(typeof(List <Symbol>) == test4.GetType());
                Assert.IsTrue(test4.SequenceEqual(expected));
            }
        }
Example #14
0
        /// <summary>
        /// Creates a new instance of <see cref="PythonActivator"/>
        /// </summary>
        /// <param name="type"><see cref="System.Type"/> of the object we wish to create</param>
        /// <param name="value"><see cref="PyObject"/> that contains the python type</param>
        public PythonActivator(Type type, PyObject value)
        {
            Type = type;

            var isPythonQuandl = false;

            using (Py.GIL())
            {
                var pythonType = value.Invoke().GetPythonType();
                isPythonQuandl = pythonType.As <Type>() == typeof(PythonQuandl);
            }

            if (isPythonQuandl)
            {
                Factory = x =>
                {
                    using (Py.GIL())
                    {
                        var instance        = value.Invoke();
                        var valueColumnName = instance.GetAttr("ValueColumnName").ToString();
                        return(new PythonQuandl(valueColumnName));
                    }
                };
            }
            else
            {
                Factory = x =>
                {
                    using (Py.GIL())
                    {
                        var instance = value.Invoke();
                        return(new PythonData(instance));
                    }
                };
            };
        }
Example #15
0
        public void TestVariables()
        {
            using (Py.GIL())
            {
                (ps.Variables() as dynamic)["ee"] = new PyInt(200);
                var a0 = ps.Get <int>("ee");
                Assert.AreEqual(200, a0);

                ps.Exec("locals()['ee'] = 210");
                var a1 = ps.Get <int>("ee");
                Assert.AreEqual(210, a1);

                ps.Exec("globals()['ee'] = 220");
                var a2 = ps.Get <int>("ee");
                Assert.AreEqual(220, a2);

                using (var item = ps.Variables())
                {
                    item["ee"] = new PyInt(230);
                }
                var a3 = ps.Get <int>("ee");
                Assert.AreEqual(230, a3);
            }
        }
Example #16
0
        /// <summary>
        /// Gets the data corresponding to the specified symbol. If the requested data
        /// is of <see cref="MarketDataType.Tick"/>, then a <see cref="List{Tick}"/> will
        /// be returned, otherwise, it will be the subscribed type, for example, <see cref="TradeBar"/>
        /// or event <see cref="Quandl"/> for custom data.
        /// </summary>
        /// <param name="symbol">The data's symbols</param>
        /// <returns>The data for the specified symbol</returns>
        public new dynamic this[Symbol symbol]
        {
            get
            {
                var data = _slice[symbol];

                var dynamicData = data as DynamicData;
                if (dynamicData != null)
                {
                    try
                    {
                        using (Py.GIL())
                        {
                            return(_converter.InvokeMethod("Data", new[] { dynamicData.ToPython() }));
                        }
                    }
                    catch
                    {
                        // NOP
                    }
                }
                return(data);
            }
        }
        public void BackwardsCompatibilitySeries__repr__(string symbol, bool cache = false)
        {
            if (cache)
            {
                SymbolCache.Set("SPY", Symbols.SPY);
            }
            using (Py.GIL())
            {
                dynamic test = PythonEngine.ModuleFromString("testModule",
                                                             $@"
import pandas as pd
from datetime import datetime as dt
def Test(dataFrame, symbol):
    close = dataFrame.lastprice.unstack(0)
    to_append = pd.Series([100], name={symbol}, index=pd.Index([dt.now()], name='time'))
    result = pd.concat([close, to_append], ignore_index=True)
    return repr([result[x] for x in [symbol]])").GetAttr("Test");
                var result = "Remapper";
                Assert.DoesNotThrow(() => result = test(GetTestDataFrame(Symbols.SPY), Symbols.SPY));
                // result should not contain "Remapper" string because the test
                // returns the string representation of a Series object
                Assert.False(result.Contains("Remapper"));
            }
        }
Example #18
0
        public YouTubeDL(string dirAppData, Options options)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            _file       = Path.Combine(dirAppData, _fileName);
            _cookieFile = Path.Combine(dirAppData, _cookieFileName);
            _options    = options;

            var dirPlugin = Path.GetDirectoryName(typeof(YouTubeDL).Assembly.Location);
            var dirPython = Path.Combine(dirPlugin, "python");
            var path      = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);

            Environment.SetEnvironmentVariable("PATH", $"{dirPython};{path}", EnvironmentVariableTarget.Process);

            PythonEngine.Initialize();
            _python = PythonEngine.BeginAllowThreads();

            using (Py.GIL())
            {
                dynamic sys = Py.Import("sys");
                sys.path.append(_file);
            }

            Version = GetVersion();
        }
        /// <summary>
        /// Scan the portfolio and the updated data for a potential margin call situation which may get the holdings below zero!
        /// If there is a margin call, liquidate the portfolio immediately before the portfolio gets sub zero.
        /// </summary>
        /// <param name="issueMarginCallWarning">Set to true if a warning should be issued to the algorithm</param>
        /// <returns>True for a margin call on the holdings.</returns>
        public List <SubmitOrderRequest> GetMarginCallOrders(out bool issueMarginCallWarning)
        {
            using (Py.GIL())
            {
                var value = _model.GetMarginCallOrders(out issueMarginCallWarning);

                // Since pythonnet does not support out parameters, the methods return
                // a tuple where the out parameter comes after the other returned values
                if (!PyTuple.IsTupleType(value))
                {
                    throw new ArgumentException($"{_model.__class__.__name__}.GetMarginCallOrders: Must return a tuple, where the first item is a list and the second a boolean");
                }

                // In this case, the first item holds the list of margin calls
                // and the second the out parameter 'issueMarginCallWarning'
                var marginCallOrders = value[0] as PyObject;
                issueMarginCallWarning = (value[1] as PyObject).GetAndDispose <bool>();

                // Since GetMarginCallOrders may return a python list
                // Need to convert to C# list
                var requests = new List <SubmitOrderRequest>();
                using var iterator = marginCallOrders.GetIterator();
                foreach (PyObject pyObject in iterator)
                {
                    SubmitOrderRequest request;
                    if (pyObject.TryConvert(out request))
                    {
                        requests.Add(request);
                    }
                }
                issueMarginCallWarning |= requests.Count > 0;
                marginCallOrders.Dispose();
                (value as PyObject).Dispose();
                return(requests);
            }
        }
        /// <summary>
        /// Executes synchronous orders to bring the account within margin requirements.
        /// </summary>
        /// <param name="generatedMarginCallOrders">These are the margin call orders that were generated
        /// by individual security margin models.</param>
        /// <returns>The list of orders that were actually executed</returns>
        public List <OrderTicket> ExecuteMarginCall(IEnumerable <SubmitOrderRequest> generatedMarginCallOrders)
        {
            using (Py.GIL())
            {
                var marginCalls = _model.ExecuteMarginCall(generatedMarginCallOrders) as PyObject;

                // Since ExecuteMarginCall may return a python list
                // Need to convert to C# list
                var tickets = new List <OrderTicket>();
                using var iterator = marginCalls.GetIterator();
                foreach (PyObject pyObject in iterator)
                {
                    OrderTicket ticket;
                    if (pyObject.TryConvert(out ticket))
                    {
                        tickets.Add(ticket);
                    }
                    pyObject.Dispose();
                }
                iterator.Dispose();
                marginCalls.Dispose();
                return(tickets);
            }
        }
Example #21
0
        /// <summary>
        /// Spawns a Windows Powershell with the PATH set up to find the
        /// Python for Unity's installed Python interpreter.
        /// </summary>
        public static void SpawnShell()
        {
            EnsureInitialized();
            var shell = "powershell.exe";

            using (Py.GIL())
            {
                dynamic unity_py      = Py.Import("unity_python.common.spawn_process");
                dynamic builtins      = Py.Import("builtins");
                dynamic pyArgs        = builtins.list();                                                        // none
                var     path          = System.Environment.GetEnvironmentVariable("PATH");
                var     pythonBinPath = Path.GetDirectoryName(Path.GetFullPath(PythonSettings.kDefaultPython)); // where the binaries/.exes are
                var     pythonScripts = pythonBinPath + "/Scripts";                                             // where pip and friends are
                path = pythonBinPath + Path.PathSeparator + pythonScripts + Path.PathSeparator + path;
                var env = new PyDict();
                env["PATH"] = path.ToPython();
                dynamic process = unity_py.spawn_process_in_environment(
                    shell,
                    pyArgs,
                    env_override: env,
                    show_window: true
                    );
            }
        }
Example #22
0
        public override string ToString()
        {
            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                using (Py.GIL())
                {
                    var scope = PyScopeManager.Global.Get(CPythonEvaluator.globalScopeName);
                    var pyobj = scope.Get(PythonObjectID.ToString());
                    return(pyobj.ToString());
                }
            }
            catch (Exception e)
            {
                //TODO(DYN-2941) implement Ilogsource or pass logger to python eval so can log to Dynamo console here.
                System.Diagnostics.Debug.WriteLine($"error getting string rep of pyobj {this.PythonObjectID} {e.Message}");
                return(this.PythonObjectID.ToString());
            }
            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
        }
Example #23
0
        public void CustomDataQuantBookHistoryTests(int year, int month, int day, string customDataType, string symbol)
        {
            using (Py.GIL())
            {
                var startDate           = new DateTime(year, month, day);
                var securityTestHistory = _module.CustomDataHistoryTest(startDate, customDataType, symbol);

                // Get the last 5 candles
                var periodHistory = securityTestHistory.test_period_overload(5);
                var count         = (periodHistory.shape[0] as PyObject).AsManagedObject(typeof(int));
                Assert.AreEqual(5, count);

                // Get the one day of data
                var timedeltaHistory = securityTestHistory.test_period_overload(TimeSpan.FromDays(8));
                var firstIndex       =
                    (DateTime)(timedeltaHistory.index.values[0] as PyObject).AsManagedObject(typeof(DateTime));
                Assert.AreEqual(startDate.AddDays(-7), firstIndex);

                // Get the one day of data, ending one day before start date
                var startEndHistory = securityTestHistory.test_daterange_overload(startDate.AddDays(-2));
                firstIndex = (DateTime)(startEndHistory.index.values[0] as PyObject).AsManagedObject(typeof(DateTime));
                Assert.AreEqual(startDate.AddDays(-2).Date, firstIndex.Date);
            }
        }
Example #24
0
        public void PythonGetBySymbolOpenInterest()
        {
            using (Py.GIL())
            {
                dynamic test = PythonEngine.ModuleFromString("testModule",
                                                             @"
from clr import AddReference
AddReference(""QuantConnect.Common"")
from QuantConnect import *
from QuantConnect.Tests import *
from QuantConnect.Data.Market import *

def Test(slice):
    data = slice.Get(OpenInterest)
    value = data[Symbols.AAPL].Value
    if value != 33:
        raise Exception('Unexpected value')").GetAttr("Test");
                var now         = DateTime.UtcNow;
                var TradeBarSpy = new TradeBar {
                    Symbol = Symbols.SPY, Time = now, Value = 8
                };
                var TradeBarAapl = new TradeBar {
                    Symbol = Symbols.AAPL, Time = now, Value = 9
                };
                var quandlSpy = new Quandl {
                    Symbol = Symbols.SPY, Time = now, Value = 10
                };
                var quandlAapl = new Quandl {
                    Symbol = Symbols.AAPL, Time = now, Value = 11
                };
                var openInterest = new OpenInterest(now, Symbols.AAPL, 33);
                var slice        = new Slice(now, new BaseData[] { quandlSpy, TradeBarAapl, quandlAapl, TradeBarSpy, openInterest });

                Assert.DoesNotThrow(() => test(new PythonSlice(slice)));
            }
        }
Example #25
0
        /// <summary>
        /// Creates a type with a given name
        /// </summary>
        /// <param name="type">Python object</param>
        /// <returns>Type object</returns>
        private Type CreateType(PyObject type)
        {
            PythonActivator pythonType;

            if (!_pythonActivators.TryGetValue(type.Handle, out pythonType))
            {
                AssemblyName an;
                using (Py.GIL())
                {
                    an = new AssemblyName(type.Repr().Split('\'')[1]);
                }
                var moduleBuilder = AppDomain.CurrentDomain
                                    .DefineDynamicAssembly(an, AssemblyBuilderAccess.Run)
                                    .DefineDynamicModule("MainModule");

                pythonType = new PythonActivator(moduleBuilder.DefineType(an.Name).CreateType(), type);

                ObjectActivator.AddActivator(pythonType.Type, pythonType.Factory);

                // Save to prevent future additions
                _pythonActivators.Add(type.Handle, pythonType);
            }
            return(pythonType.Type);
        }
        public void Py_Get_UsesTypeName_AsKey_And_ReturnsLastItem()
        {
            var data = new DynamicSecurityData(_dataTypesProvider, _cache);

            _cache.StoreData(new List <TradeBar>
            {
                new TradeBar(DateTime.UtcNow, Symbols.SPY, 10m, 20m, 5m, 1, 10000),
                new TradeBar(DateTime.UtcNow, Symbols.SPY, 10m, 20m, 5m, 3, 10000)
            }, typeof(TradeBar));

            using (Py.GIL())
            {
                dynamic test = PyModule.FromString("testModule",
                                                   @"
from AlgorithmImports import *

def Test(dynamicData):
    data = dynamicData.Get(TradeBar)
    if data.Close != 3:
        raise Exception('Unexpected value')").GetAttr("Test");

                Assert.DoesNotThrow(() => test(data));
            }
        }
Example #27
0
        public static void TrainUsingMlpNetwork()
        {
            using (var py = Py.GIL())
            {
                PythonEngine.Exec(RedirectOutput());
                int numberOfClasses    = 10;
                int vectorLength       = 784;
                int trainVectorCount   = 60000;
                int testVectorCount    = 10000;
                int epoch              = 20;
                int batchSize          = 128;
                var data               = new MnistDataSetGenerator(numberOfClasses, vectorLength, trainVectorCount, testVectorCount);
                var mnistDatasetForMlp = data.GetMnistDataSet();

                PyTuple inputShape      = new PyTuple(new PyObject[] { new PyInt(vectorLength) });
                var     modelDefinition = new MnistMlpNetwokConfiguration()
                                          .AddDense(512, KerasConstants.ActivationFunction.Relu, inputShape)
                                          .AddDropout(0.2)
                                          .AddDense(512, KerasConstants.ActivationFunction.Relu)
                                          .AddDropout(0.2)
                                          .AddDense(10, KerasConstants.ActivationFunction.Softmax)
                                          .Build();

                var executionConfiguration = new KerasExecutionConfiguration <MnistDatasetMlp>();
                executionConfiguration.ModelDefinition      = modelDefinition;
                executionConfiguration.MnistDataset         = mnistDatasetForMlp;
                executionConfiguration.OptimizationFunction = KerasConstants.ActivationFunction.RmsProp;
                executionConfiguration.LossFunction         = KerasConstants.LossFunction.CategoricalCrossentropy;
                executionConfiguration.Metrics   = new PyList(new PyObject[] { new PyString(KerasConstants.Metrics.Accuracy) });
                executionConfiguration.BatchSize = batchSize;
                executionConfiguration.Epochs    = epoch;
                var mnistMlpExec = new MnistMlpExecution(executionConfiguration);
                mnistMlpExec.Fit();
                mnistMlpExec.Evaluate();
            }
        }
        public void Py_Get_TradeBarArray()
        {
            var securityData = new DynamicSecurityData(_dataTypesProvider, _cache);
            var data         = new[]
            {
                new TradeBar(DateTime.UtcNow, Symbols.SPY, 10m, 20m, 5m, 15m, 10000)
            };

            _cache.StoreData(data, typeof(TradeBar));

            using (Py.GIL())
            {
                dynamic test = PyModule.FromString("testModule",
                                                   @"
from AlgorithmImports import *

def Test(dynamicData):
    data = dynamicData.Get(TradeBar)
    if data.Low != 5:
        raise Exception('Unexpected value')").GetAttr("Test");

                Assert.DoesNotThrow(() => test(securityData));
            }
        }
Example #29
0
        public void PyFundamentalData(dynamic input)
        {
            using (Py.GIL())
            {
                var testModule = _module.FundamentalHistoryTest();
                var dataFrame  = testModule.getFundamentals(input[0], input[1], _startDate, _endDate);

                // Should not be empty
                Assert.IsFalse(dataFrame.empty.AsManagedObject(typeof(bool)));

                // Get the test row
                var testRow = dataFrame.loc[_startDate.ToPython()];
                Assert.IsFalse(testRow.empty.AsManagedObject(typeof(bool)));

                // Check the length
                var count = testRow.__len__().AsManagedObject(typeof(int));
                Assert.AreEqual(count, 1);

                // Verify the data value
                var index = testRow.index[0];
                var value = testRow.at[index].AsManagedObject(input[2].GetType());
                Assert.AreEqual(input[2], value);
            }
        }
Example #30
0
        public void PythonSlice_setdefault_default_success()
        {
            using (Py.GIL())
            {
                dynamic test = PythonEngine.ModuleFromString("testModule",
                                                             @"
from AlgorithmImports import *

def Test(slice, symbol, default_value):
    return slice.setdefault(symbol, default_value)").GetAttr("Test");

                var      value       = new Tick();
                var      pythonSlice = GetPythonSlice();
                dynamic  expected    = pythonSlice[Symbols.SPY];
                PyObject result      = null;

                // Since SPY is found, no need to set the default. Therefore it does not throw.
                Assert.DoesNotThrow(() => result = test(GetPythonSlice(), Symbols.SPY, value));
                BaseData actual;
                Assert.IsTrue(result.TryConvert(out actual));
                Assert.AreEqual(expected.Symbol, actual.Symbol);
                Assert.AreEqual(expected.Value, actual.Value);
            }
        }