public void RebalanceFunctionPeriodDue(Language language)
        {
            TestPortfolioConstructionModel constructionModel;

            if (language == Language.Python)
            {
                constructionModel = new TestPortfolioConstructionModel();
                using (Py.GIL())
                {
                    var func = PyModule.FromString("RebalanceFunc",
                                                   @"
from datetime import timedelta

def RebalanceFunc(time):
    return time + timedelta(days=1)").GetAttr("RebalanceFunc");
                    constructionModel.SetRebalancingFunc(func);
                }
            }
            else
            {
                constructionModel = new TestPortfolioConstructionModel(time => time.AddDays(1));
            }

            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 1), new Insight[0]));
            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(
                               new DateTime(2020, 1, 1, 23, 0, 0), new Insight[0]));

            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 2), new Insight[0]));

            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(
                               new DateTime(2020, 1, 2, 1, 0, 0), new Insight[0]));
        }
示例#2
0
        public static string CallEchoString2(string arg)
        {
            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                if (module == null)
                {
                    module = PyModule.FromString("tt", testmod);
                }

                PyObject func   = module.GetAttr("echostring2");
                var      parg   = new PyString(arg);
                PyObject temp   = func.Invoke(parg);
                var      result = (string)temp.AsManagedObject(typeof(string));
                func.Dispose();
                parg.Dispose();
                temp.Dispose();
                return(result);
            }
            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
        }
        public void RebalanceFunctionDateRules(Language language)
        {
            var dateRules = new DateRules(new SecurityManager(
                                              new TimeKeeper(new DateTime(2015, 1, 1), DateTimeZone.Utc)), DateTimeZone.Utc);

            TestPortfolioConstructionModel constructionModel;

            if (language == Language.Python)
            {
                constructionModel = new TestPortfolioConstructionModel();
                using (Py.GIL())
                {
                    dynamic func = PyModule.FromString("RebalanceFunc",
                                                       @"
import datetime

def RebalanceFunc(dateRules):
    return dateRules.On(datetime.datetime(2015, 1, 10), datetime.datetime(2015, 1, 30))").GetAttr("RebalanceFunc");
                    constructionModel.SetRebalancingFunc(func(dateRules));
                }
            }
            else
            {
                var dateRule = dateRules.On(new DateTime(2015, 1, 10), new DateTime(2015, 1, 30));
                constructionModel = new TestPortfolioConstructionModel(dateRule);
            }

            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2015, 1, 1), new Insight[0]));
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2015, 1, 10), new Insight[0]));
            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2015, 1, 20), new Insight[0]));
            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2015, 1, 29), new Insight[0]));
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 2, 1), new Insight[0]));
            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 2, 2), new Insight[0]));
            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 10, 2), new Insight[0]));
        }
示例#4
0
        static QuantBook()
        {
            //Determine if we are in a Python Notebook
            try
            {
                PythonEngine.Initialize();
                using (Py.GIL())
                {
                    var isPython = PyModule.FromString(Guid.NewGuid().ToString(),
                                                       "try:\n" +
                                                       "   import IPython\n" +
                                                       "   def IsPythonNotebook():\n" +
                                                       "       return (IPython.get_ipython() != None)\n" +
                                                       "except:\n" +
                                                       "   print('No IPython installed')\n" +
                                                       "   def IsPythonNotebook():\n" +
                                                       "       return false\n").GetAttr("IsPythonNotebook").Invoke();
                    isPython.TryConvert(out _isPythonNotebook);
                }
            }
            catch
            {
                //Default to false
                _isPythonNotebook = false;
                Logging.Log.Error("QuantBook failed to determine Notebook kernel language");
            }

            Logging.Log.Trace($"QuantBook started; Is Python: {_isPythonNotebook}");
        }
示例#5
0
        protected override IndicatorBase <IBaseData> CreateIndicator()
        {
            using (Py.GIL())
            {
                var module = PyModule.FromString(
                    Guid.NewGuid().ToString(),
                    @"
from AlgorithmImports import *
from collections import deque

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 = np.sum(self.queue) / count
        return count == self.queue.maxlen
"
                    );
                var indicator = module.GetAttr("CustomSimpleMovingAverage")
                                .Invoke("custom".ToPython(), 14.ToPython());

                return(new PythonIndicator(indicator));
            }
        }
示例#6
0
        public void RegistersIndicatorProperlyPythonScript()
        {
            const string code = @"
from AlgorithmImports import *

AddReference('QuantConnect.Lean.Engine')
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, None), 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(() => PyModule.FromString("RegistersIndicatorProperlyPythonScript", code));
            }
        }
        public void RebalanceFunctionInsightExpiration(Language language)
        {
            TestPortfolioConstructionModel constructionModel;

            if (language == Language.Python)
            {
                constructionModel = new TestPortfolioConstructionModel();
                using (Py.GIL())
                {
                    var func = PyModule.FromString("RebalanceFunc",
                                                   @"
from datetime import timedelta

def RebalanceFunc(time):
    return time + timedelta(days=10)").GetAttr("RebalanceFunc");
                    constructionModel.SetRebalancingFunc(func);
                }
            }
            else
            {
                constructionModel = new TestPortfolioConstructionModel(time => time.AddDays(10));
            }

            constructionModel.SetNextExpiration(new DateTime(2020, 1, 2));
            constructionModel.RebalanceOnInsightChanges = false;
            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 3), new Insight[0]));
            constructionModel.RebalanceOnInsightChanges = true;
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 3), new Insight[0]));
        }
        public void OldImmediateFillModel_MarketFill_Py()
        {
            using (Py.GIL())
            {
                var module = PyModule.FromString(Guid.NewGuid().ToString(),
                                                 "from AlgorithmImports import *\n" +
                                                 "class CustomFillModel(ImmediateFillModel):\n" +
                                                 "   def __init__(self):\n" +
                                                 "       self.MarketFillWasCalled = False\n" +
                                                 "   def MarketFill(self, asset, order):\n" +
                                                 "       self.MarketFillWasCalled = True\n" +
                                                 "       return super().MarketFill(asset, order)");

                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("MarketFillWasCalled").TryConvert(out called);
                Assert.True(called);
                Assert.IsNotNull(result);
                Assert.AreEqual(OrderStatus.Filled, result.OrderEvent.Status);
            }
        }
示例#9
0
        public void RegisterPythonCustomIndicatorProperly()
        {
            const string code = @"
class GoodCustomIndicator :
    def __init__(self):
        self.IsReady = True
        self.Value = 0
    def Update(self, input):
        self.Value = input.Value
        return True
class BadCustomIndicator:
    def __init__(self):
        self.IsReady = True
        self.Value = 0
    def Updat(self, input):
        self.Value = input.Value
        return True";

            using (Py.GIL())
            {
                var module = PyModule.FromString(Guid.NewGuid().ToString(), code);

                var goodIndicator = module.GetAttr("GoodCustomIndicator").Invoke();
                Assert.DoesNotThrow(() => _algorithm.RegisterIndicator(_spy, goodIndicator, Resolution.Minute));

                var actual = _algorithm.SubscriptionManager.Subscriptions
                             .Single(s => s.TickType == LeanData.GetCommonTickType(SecurityType.Equity))
                             .Consolidators.Count;
                Assert.AreEqual(1, actual);

                var badIndicator = module.GetAttr("BadCustomIndicator").Invoke();
                Assert.Throws <NotImplementedException>(() => _algorithm.RegisterIndicator(_spy, badIndicator, Resolution.Minute));
            }
        }
示例#10
0
        public void TestMultipleGenericMethodBinding()
        {
            // Test matching multiple generics
            // i.e. function signature is <T,K>(Generic<T,K> var1)

            // Run in C#
            var class1 = new TestMultipleGenericClass1();
            var class2 = new TestMultipleGenericClass2();

            TestMultipleGenericMethod(class1);
            TestMultipleGenericMethod(class2);

            Assert.AreEqual(1, class1.Value);
            Assert.AreEqual(1, class2.Value);

            // Run in Python
            Assert.DoesNotThrow(() => PyModule.FromString("test", @"
from clr import AddReference
AddReference(""System"")
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *
class1 = TestMethodBinder.TestMultipleGenericClass1()
class2 = TestMethodBinder.TestMultipleGenericClass2()

TestMethodBinder.TestMultipleGenericMethod(class1)
TestMethodBinder.TestMultipleGenericMethod(class2)

if class1.Value != 1 or class2.Value != 1:
    raise AssertionError('Values were not updated')
"));
        }
示例#11
0
        public void TestPyClassGenericBinding()
        {
            // Overriding our generics in Python we should still match with the generic method
            Assert.DoesNotThrow(() => PyModule.FromString("test", @"
from clr import AddReference
AddReference(""System"")
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *

class PyGenericClass(TestMethodBinder.TestGenericClass1):
    pass

class PyMultipleGenericClass(TestMethodBinder.TestMultipleGenericClass1):
    pass

singleGenericClass = PyGenericClass()
multiGenericClass = PyMultipleGenericClass()

TestMethodBinder.TestGenericMethod(singleGenericClass)
TestMethodBinder.TestMultipleGenericMethod(multiGenericClass)
TestMethodBinder.TestMultipleGenericParamsMethod(singleGenericClass, multiGenericClass)

if singleGenericClass.Value != 1 or multiGenericClass.Value != 1:
    raise AssertionError('Values were not updated')
"));
        }
        public void FundamentalUniverseSelectionModelCanBeInherited()
        {
            var code = @"
from AlgorithmImports import *
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class MockUniverseSelectionModel(FundamentalUniverseSelectionModel):
    def __init__(self):
        super().__init__(False)
    def SelectCoarse(self, algorithm, coarse):
        return [Symbol.Create('SPY', SecurityType.Equity, Market.USA)]";

            using (Py.GIL())
            {
                dynamic pyModel = PyModule.FromString(Guid.NewGuid().ToString(), code)
                                  .GetAttr("MockUniverseSelectionModel");

                var model = new UniverseSelectionModelPythonWrapper(pyModel());

                var universes = model.CreateUniverses(new QCAlgorithm()).ToList();
                Assert.AreEqual(1, universes.Count);

                var data = new BaseDataCollection();
                data.Add(new CoarseFundamental());

                var universe = universes.First();
                var symbols  = universe.SelectSymbols(DateTime.Now, data).ToList();
                Assert.AreEqual(1, symbols.Count);

                var expected = Symbol.Create("SPY", SecurityType.Equity, Market.USA);
                var symbol   = symbols.First();
                Assert.AreEqual(expected, symbol);
            }
        }
        public void ManualUniverseSelectionModelCanBeInherited()
        {
            var code = @"
from clr import AddReference
AddReference('QuantConnect.Common')

from QuantConnect import Market, SecurityType, Symbol
from Selection.ManualUniverseSelectionModel import ManualUniverseSelectionModel

class MockUniverseSelectionModel(ManualUniverseSelectionModel):
    def __init__(self):
        super().__init__([Symbol.Create('SPY', SecurityType.Equity, Market.USA)])";

            using (Py.GIL())
            {
                dynamic pyModel = PyModule.FromString(Guid.NewGuid().ToString(), code)
                                  .GetAttr("MockUniverseSelectionModel");

                var model = new UniverseSelectionModelPythonWrapper(pyModel());

                var universes = model.CreateUniverses(new QCAlgorithm()).ToList();
                Assert.AreEqual(1, universes.Count);

                var universe = universes.First();
                var symbols  = universe.SelectSymbols(DateTime.Now, null).ToList();
                Assert.AreEqual(1, symbols.Count);

                var expected = Symbol.Create("SPY", SecurityType.Equity, Market.USA);
                var symbol   = symbols.First();
                Assert.AreEqual(expected, symbol);
            }
        }
示例#14
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 = PyModule.FromString("GetDeterminantFromHistory", code)
                                                    .GetAttr("GetDeterminantFromHistory");

                dynamic df = new PandasConverter().GetDataFrame(history);
                return(GetDeterminantFromHistory(df));
            }
        }
示例#15
0
        public void PlotPythonCustomIndicatorProperly()
        {
            using (Py.GIL())
            {
                var module = PyModule.FromString(
                    Guid.NewGuid().ToString(),
                    @"
from AlgorithmImports import *
class PythonCustomIndicator(PythonIndicator):
    def __init__(self):
        self.Value = 0
    def Update(self, input):
        self.Value = input.Value
        return True"
                    );

                dynamic customIndicator = module.GetAttr("PythonCustomIndicator").Invoke();
                customIndicator.Name = "custom";
                var input = new IndicatorDataPoint();
                input.Value = 10;
                customIndicator.Update(input);
                customIndicator.Current.Value = customIndicator.Value;
                Assert.DoesNotThrow(() => _algorithm.Plot("PlotTest", customIndicator));
                var charts = _algorithm.GetChartUpdates();
                Assert.IsTrue(charts.Where(x => x.Name == "PlotTest").Any());
                Assert.AreEqual(10, charts.First().Series["custom"].Values.First().y);
            }
        }
示例#16
0
        public void PlotCustomIndicatorAsDefault()
        {
            using (Py.GIL())
            {
                var module = PyModule.FromString(
                    Guid.NewGuid().ToString(),
                    @"
from AlgorithmImports import *

class CustomIndicator:
    def __init__(self):
        self.Value = 10
    def Update(self, input):
        self.Value = input.Value
        return True"
                    );

                var customIndicator = module.GetAttr("CustomIndicator").Invoke();
                Assert.DoesNotThrow(() => _algorithm.Plot("PlotTest", customIndicator));
                var charts = _algorithm.GetChartUpdates();
                Assert.IsFalse(charts.Where(x => x.Name == "PlotTest").Any());
                Assert.IsTrue(charts.Where(x => x.Name == "Strategy Equity").Any());
                Assert.AreEqual(10, charts.First().Series["PlotTest"].Values.First().y);
            }
        }
        public void RebalanceFunctionNewInsights(Language language)
        {
            TestPortfolioConstructionModel constructionModel;

            if (language == Language.Python)
            {
                constructionModel = new TestPortfolioConstructionModel();
                using (Py.GIL())
                {
                    var func = PyModule.FromString("RebalanceFunc",
                                                   @"
from datetime import timedelta

def RebalanceFunc(time):
    return time + timedelta(days=1)").GetAttr("RebalanceFunc");
                    constructionModel.SetRebalancingFunc(func);
                }
            }
            else
            {
                constructionModel = new TestPortfolioConstructionModel(time => time.AddDays(1));
            }

            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 1), new Insight[0]));

            var insights = new[] { Insight.Price(Symbols.SPY, Resolution.Daily, 1, InsightDirection.Down) };

            constructionModel.RebalanceOnInsightChanges = false;
            Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 1), insights));
            constructionModel.RebalanceOnInsightChanges = true;
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 1), insights));
        }
示例#18
0
        public void SetDefaultWarmUpPeriodProperly()
        {
            using (Py.GIL())
            {
                var module = PyModule.FromString(
                    Guid.NewGuid().ToString(),
                    @"
from AlgorithmImports import *
from collections import deque

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 = np.sum(self.queue) / count
        return count == self.queue.maxlen
"
                    );
                var pythonIndicator = module.GetAttr("CustomSimpleMovingAverage")
                                      .Invoke("custom".ToPython(), 14.ToPython());
                var indicator = new PythonIndicator(pythonIndicator);

                Assert.AreEqual(0, indicator.WarmUpPeriod);
            }
        }
示例#19
0
        public void ScanPyConsolidator()
        {
            using (Py.GIL())
            {
                var module = PyModule.FromString(Guid.NewGuid().ToString(),
                                                 "from AlgorithmImports import *\n" +
                                                 "class CustomConsolidator():\n" +
                                                 "   def __init__(self):\n" +
                                                 "       self.ScanWasCalled = False\n" +
                                                 "       self.InputType = QuoteBar\n" +
                                                 "       self.OutputType = QuoteBar\n" +
                                                 "       self.Consolidated = None\n" +
                                                 "       self.WorkingData = None\n" +
                                                 "   def Scan(self,time):\n" +
                                                 "       self.ScanWasCalled = True\n");

                var customConsolidator = module.GetAttr("CustomConsolidator").Invoke();
                var wrapper            = new DataConsolidatorPythonWrapper(customConsolidator);

                var time   = DateTime.Today;
                var period = TimeSpan.FromMinutes(1);

                wrapper.Scan(DateTime.Now);

                bool called;
                customConsolidator.GetAttr("ScanWasCalled").TryConvert(out called);
                Assert.True(called);
            }
        }
示例#20
0
        public void NewFeeModelModel_GetOrderFee_Py()
        {
            using (Py.GIL())
            {
                var module = PyModule.FromString(Guid.NewGuid().ToString(),
                                                 "from AlgorithmImports import *\n" +
                                                 "class CustomFeeModel(FeeModel):\n" +
                                                 "   def __init__(self):\n" +
                                                 "       self.CalledGetOrderFee = False\n" +
                                                 "   def GetOrderFee(self, parameters):\n" +
                                                 "       self.CalledGetOrderFee = True\n" +
                                                 "       return OrderFee(CashAmount(15, \"USD\"))");

                var customFeeModel = module.GetAttr("CustomFeeModel").Invoke();
                var wrapper        = new FeeModelPythonWrapper(customFeeModel);

                var result = wrapper.GetOrderFee(new OrderFeeParameters(
                                                     _security,
                                                     new MarketOrder(_security.Symbol, 1, orderDateTime)
                                                     ));

                bool called;
                customFeeModel.GetAttr("CalledGetOrderFee").TryConvert(out called);
                Assert.True(called);
                Assert.IsNotNull(result);
                Assert.AreEqual(15, result.Value.Amount);
                Assert.AreEqual(Currencies.USD, result.Value.Currency);
            }
        }
        public void BackwardsCompatibilityDataFrameOtherParameterFunctions(string method, string index, bool cache)
        {
            // Cannot compare non identically indexed dataframes
            if (method == ".compare(other)" && _newerPandas)
            {
                return;
            }

            if (cache)
            {
                SymbolCache.Set("SPY", Symbols.SPY);
            }

            using (Py.GIL())
            {
                dynamic test = PyModule.FromString("testModule",
                                                   $@"
def Test(df, other, symbol):
    df = df{method}
    df = df.lastprice.unstack(level=0)
    # If not DataFrame, return
    if not hasattr(df, 'columns'):
        return
    if df.iloc[-1][{index}] is 0:
        raise Exception('Data is zero')").GetAttr("Test");

                Assert.DoesNotThrow(() => test(GetTestDataFrame(Symbols.SPY), GetTestDataFrame(Symbols.AAPL), Symbols.SPY));
            }
        }
        public void Py_StoreData_GetAll_UsesTypeName()
        {
            var data = new DynamicSecurityData(_dataTypesProvider, _cache);

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

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

def Test(dynamicData):
    data = dynamicData.GetAll(TradeBar)
    if len(data) != 1:
        raise Exception('Unexpected length')
    if data[0].Close != 1:
        raise Exception('Unexpected value')").GetAttr("Test");

                Assert.DoesNotThrow(() => test(data));
            }
        }
        public void BackwardsCompatibilitySeriesOtherParameterFunctions(string method, string index, bool cache)
        {
            // Cannot compare non identically indexed dataframes
            if (method == ".compare(other)" && _newerPandas)
            {
                return;
            }

            if (cache)
            {
                SymbolCache.Set("SPY", Symbols.SPY);
            }

            using (Py.GIL())
            {
                dynamic test = PyModule.FromString("testModule",
                                                   $@"
def Test(df, other, symbol):
    series, other = other.lastprice, df.lastprice
    series = series{method}
    # If not Series, return
    if not hasattr(series, 'index') or type(series) is tuple:
        return
    if series.loc[{index}].iloc[-1] is 0:
        raise Exception('Data is zero')").GetAttr("Test");

                Assert.DoesNotThrow(() => test(GetTestDataFrame(Symbols.SPY), GetTestDataFrame(Symbols.AAPL), Symbols.SPY));
            }
        }
        private void SetPortfolioConstruction(Language language, PortfolioBias portfolioBias = PortfolioBias.LongShort)
        {
            _algorithm.SetPortfolioConstruction(new BLOPCM(new UnconstrainedMeanVariancePortfolioOptimizer(), portfolioBias));
            if (language == Language.Python)
            {
                try
                {
                    using (Py.GIL())
                    {
                        var name     = nameof(BLOPCM);
                        var instance = PyModule.FromString(name, GetPythonBLOPCM()).GetAttr(name).Invoke(((int)portfolioBias).ToPython());
                        var model    = new PortfolioConstructionModelPythonWrapper(instance);
                        _algorithm.SetPortfolioConstruction(model);
                    }
                }
                catch (Exception e)
                {
                    Assert.Ignore(e.Message);
                }
            }

            var changes = SecurityChangesTests.AddedNonInternal(_algorithm.Securities.Values.ToList().ToArray());

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
示例#25
0
 private static void AssetCode(string code)
 {
     using (Py.GIL())
     {
         dynamic module = PyModule.FromString(Guid.NewGuid().ToString(), code);
         Assert.DoesNotThrow(() => module.RunTest());
     }
 }
示例#26
0
 private PyObject CreateCustomBuyingPowerModel(string code)
 {
     using (Py.GIL())
     {
         var module = PyModule.FromString("CustomBuyingPowerModel", code);
         return(module.GetAttr("CustomBuyingPowerModel").Invoke());
     }
 }
示例#27
0
 public void Setup()
 {
     using (Py.GIL())
     {
         var pyModule = PyModule.FromString("module", testModule);
         containsTest    = pyModule.GetAttr("ContainsTest");
         containsKeyTest = pyModule.GetAttr("ContainsKeyTest");
     }
 }
示例#28
0
 public void DoesNotThrowWhenDerivedFromCSharpModel()
 {
     using (Py.GIL())
     {
         var module = PyModule.FromString(nameof(ValidateImplementationOf), DerivedFromCsharp);
         var model  = module.GetAttr("DerivedFromCSharpModel");
         Assert.That(() => model.ValidateImplementationOf <IModel>(), Throws.Nothing);
     }
 }
示例#29
0
 public void DoesNotThrowWhenInterfaceFullyImplemented()
 {
     using (Py.GIL())
     {
         var module = PyModule.FromString(nameof(ValidateImplementationOf), FullyImplemented);
         var model  = module.GetAttr("FullyImplementedModel");
         Assert.That(() => model.ValidateImplementationOf <IModel>(), Throws.Nothing);
     }
 }
 private PyObject CreateCustomMarginCallModel(string code, SecurityPortfolioManager portfolio)
 {
     using (Py.GIL())
     {
         var     module = PyModule.FromString("CustomMarginCallModel", code);
         dynamic CustomMarginCallModel = module.GetAttr("CustomMarginCallModel");
         return(CustomMarginCallModel(portfolio, null));
     }
 }