예제 #1
0
        public void OldImmediateFillModel_MarketFill_Py()
        {
            using (Py.GIL())
            {
                var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(),
                                                           "from clr import AddReference\n" +
                                                           "AddReference(\"QuantConnect.Common\")\n" +
                                                           "from QuantConnect.Orders.Fills import ImmediateFillModel\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);
            }
        }
        public void NewFillContext_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" +
                                                 "   def Fill(self, parameters):\n" +
                                                 "       self.FillWasCalled = True\n" +
                                                 "       return super().Fill(parameters)");

                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);
                Assert.IsNotNull(result);
                Assert.AreEqual(OrderStatus.Filled, result.OrderEvent.Status);
            }
        }
        public void OldFillModel_NewFillContextAndGetPrices_Py()
        {
            using (Py.GIL())
            {
                var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(),
                                                           "from clr import AddReference\n" +
                                                           "AddReference(\"QuantConnect.Common\")\n" +
                                                           "from QuantConnect.Orders.Fills import FillModel, Fill\n" +
                                                           "from QuantConnect.Orders import OrderEvent\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)
                                              ));

                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);
            }
        }
예제 #4
0
 /// <summary>
 /// Used to set the <see cref="FillModelPythonWrapper"/> instance if any
 /// </summary>
 public void SetPythonWrapper(FillModelPythonWrapper pythonWrapper)
 {
     PythonWrapper = pythonWrapper;
 }
예제 #5
0
 /// <summary>
 /// Sets the fill model
 /// </summary>
 /// <param name="fillModel">Model that represents a fill model</param>
 public void SetFillModel(PyObject fillModel)
 {
     FillModel = new FillModelPythonWrapper(fillModel);
 }