forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQCAlgorithm.Python.cs
1195 lines (1093 loc) · 65 KB
/
QCAlgorithm.Python.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using System;
using QuantConnect.Securities;
using NodaTime;
using System.Collections.Generic;
using QuantConnect.Python;
using Python.Runtime;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Data.Fundamental;
using System.Linq;
using QuantConnect.Brokerages;
using QuantConnect.Scheduling;
using QuantConnect.Util;
namespace QuantConnect.Algorithm
{
public partial class QCAlgorithm
{
private readonly Dictionary<IntPtr, PythonIndicator> _pythonIndicators = new Dictionary<IntPtr, PythonIndicator>();
public PandasConverter PandasConverter { get; private set; }
/// <summary>
/// Sets pandas converter
/// </summary>
public void SetPandasConverter()
{
PandasConverter = new PandasConverter();
}
/// <summary>
/// AddData a new user defined data source, requiring only the minimum config options.
/// The data is added with a default time zone of NewYork (Eastern Daylight Savings Time).
/// This method is meant for custom data types that require a ticker, but have no underlying Symbol.
/// Examples of data sources that meet this criteria are U.S. Treasury Yield Curve Rates and Trading Economics data
/// </summary>
/// <param name="type">Data source type</param>
/// <param name="ticker">Key/Ticker for data</param>
/// <param name="resolution">Resolution of the data</param>
/// <returns>The new <see cref="Security"/></returns>
public Security AddData(PyObject type, string ticker, Resolution? resolution = null)
{
return AddData(type, ticker, resolution, null, false, 1m);
}
/// <summary>
/// AddData a new user defined data source, requiring only the minimum config options.
/// The data is added with a default time zone of NewYork (Eastern Daylight Savings Time).
/// This adds a Symbol to the `Underlying` property in the custom data Symbol object.
/// Use this method when adding custom data with a ticker from the past, such as "AOL"
/// before it became "TWX", or if you need to filter using custom data and place trades on the
/// Symbol associated with the custom data.
/// </summary>
/// <param name="type">Data source type</param>
/// <param name="underlying">The underlying symbol for the custom data</param>
/// <param name="resolution">Resolution of the data</param>
/// <returns>The new <see cref="Security"/></returns>
/// <remarks>
/// We include three optional unused object parameters so that pythonnet chooses the intended method
/// correctly. Previously, calling the overloaded method that accepts a string would instead call this method.
/// Adding the three unused parameters makes it choose the correct method when using a string or Symbol. This is
/// due to pythonnet's method precedence, as viewable here: https://github.com/QuantConnect/pythonnet/blob/9e29755c54e6008cb016e3dd9d75fbd8cd19fcf7/src/runtime/methodbinder.cs#L215
/// </remarks>
public Security AddData(PyObject type, Symbol underlying, Resolution? resolution = null)
{
return AddData(type, underlying, resolution, null, false, 1m);
}
/// <summary>
/// AddData a new user defined data source, requiring only the minimum config options.
/// This method is meant for custom data types that require a ticker, but have no underlying Symbol.
/// Examples of data sources that meet this criteria are U.S. Treasury Yield Curve Rates and Trading Economics data
/// </summary>
/// <param name="type">Data source type</param>
/// <param name="ticker">Key/Ticker for data</param>
/// <param name="resolution">Resolution of the Data Required</param>
/// <param name="timeZone">Specifies the time zone of the raw data</param>
/// <param name="fillDataForward">When no data available on a tradebar, return the last data that was generated</param>
/// <param name="leverage">Custom leverage per security</param>
/// <returns>The new <see cref="Security"/></returns>
public Security AddData(PyObject type, string ticker, Resolution? resolution, DateTimeZone timeZone, bool fillDataForward = false, decimal leverage = 1.0m)
{
return AddData(type.CreateType(), ticker, resolution, timeZone, fillDataForward, leverage);
}
/// <summary>
/// AddData a new user defined data source, requiring only the minimum config options.
/// This adds a Symbol to the `Underlying` property in the custom data Symbol object.
/// Use this method when adding custom data with a ticker from the past, such as "AOL"
/// before it became "TWX", or if you need to filter using custom data and place trades on the
/// Symbol associated with the custom data.
/// </summary>
/// <param name="type">Data source type</param>
/// <param name="underlying">The underlying symbol for the custom data</param>
/// <param name="resolution">Resolution of the Data Required</param>
/// <param name="timeZone">Specifies the time zone of the raw data</param>
/// <param name="fillDataForward">When no data available on a tradebar, return the last data that was generated</param>
/// <param name="leverage">Custom leverage per security</param>
/// <returns>The new <see cref="Security"/></returns>
/// <remarks>
/// We include three optional unused object parameters so that pythonnet chooses the intended method
/// correctly. Previously, calling the overloaded method that accepts a string would instead call this method.
/// Adding the three unused parameters makes it choose the correct method when using a string or Symbol. This is
/// due to pythonnet's method precedence, as viewable here: https://github.com/QuantConnect/pythonnet/blob/9e29755c54e6008cb016e3dd9d75fbd8cd19fcf7/src/runtime/methodbinder.cs#L215
/// </remarks>
public Security AddData(PyObject type, Symbol underlying, Resolution? resolution, DateTimeZone timeZone, bool fillDataForward = false, decimal leverage = 1.0m)
{
return AddData(type.CreateType(), underlying, resolution, timeZone, fillDataForward, leverage);
}
/// <summary>
/// AddData a new user defined data source, requiring only the minimum config options.
/// This method is meant for custom data types that require a ticker, but have no underlying Symbol.
/// Examples of data sources that meet this criteria are U.S. Treasury Yield Curve Rates and Trading Economics data
/// </summary>
/// <param name="dataType">Data source type</param>
/// <param name="ticker">Key/Ticker for data</param>
/// <param name="resolution">Resolution of the Data Required</param>
/// <param name="timeZone">Specifies the time zone of the raw data</param>
/// <param name="fillDataForward">When no data available on a tradebar, return the last data that was generated</param>
/// <param name="leverage">Custom leverage per security</param>
/// <returns>The new <see cref="Security"/></returns>
public Security AddData(Type dataType, string ticker, Resolution? resolution, DateTimeZone timeZone, bool fillDataForward = false, decimal leverage = 1.0m)
{
// NOTE: Invoking methods on BaseData w/out setting the symbol may provide unexpected behavior
var baseInstance = dataType.GetBaseDataInstance();
if (!baseInstance.RequiresMapping())
{
var symbol = new Symbol(
SecurityIdentifier.GenerateBase(dataType, ticker, Market.USA, baseInstance.RequiresMapping()),
ticker);
return AddDataImpl(dataType, symbol, resolution, timeZone, fillDataForward, leverage);
}
// If we need a mappable ticker and we can't find one in the SymbolCache, throw
Symbol underlying;
if (!SymbolCache.TryGetSymbol(ticker, out underlying))
{
throw new InvalidOperationException($"The custom data type {dataType.Name} requires mapping, but the provided ticker is not in the cache. " +
$"Please add this custom data type using a Symbol or perform this call after " +
$"a Security has been added using AddEquity, AddForex, AddCfd, AddCrypto, AddFuture, AddOption or AddSecurity. " +
$"An example use case can be found in CustomDataAddDataRegressionAlgorithm");
}
return AddData(dataType, underlying, resolution, timeZone, fillDataForward, leverage);
}
/// <summary>
/// AddData a new user defined data source, requiring only the minimum config options.
/// This adds a Symbol to the `Underlying` property in the custom data Symbol object.
/// Use this method when adding custom data with a ticker from the past, such as "AOL"
/// before it became "TWX", or if you need to filter using custom data and place trades on the
/// Symbol associated with the custom data.
/// </summary>
/// <param name="dataType">Data source type</param>
/// <param name="underlying"></param>
/// <param name="resolution">Resolution of the Data Required</param>
/// <param name="timeZone">Specifies the time zone of the raw data</param>
/// <param name="fillDataForward">When no data available on a tradebar, return the last data that was generated</param>
/// <param name="leverage">Custom leverage per security</param>
/// <returns>The new <see cref="Security"/></returns>
/// <remarks>
/// We include three optional unused object parameters so that pythonnet chooses the intended method
/// correctly. Previously, calling the overloaded method that accepts a string would instead call this method.
/// Adding the three unused parameters makes it choose the correct method when using a string or Symbol. This is
/// due to pythonnet's method precedence, as viewable here: https://github.com/QuantConnect/pythonnet/blob/9e29755c54e6008cb016e3dd9d75fbd8cd19fcf7/src/runtime/methodbinder.cs#L215
/// </remarks>
public Security AddData(Type dataType, Symbol underlying, Resolution? resolution = null, DateTimeZone timeZone = null, bool fillDataForward = false, decimal leverage = 1.0m)
{
var symbol = QuantConnect.Symbol.CreateBase(dataType, underlying, Market.USA);
return AddDataImpl(dataType, symbol, resolution, timeZone, fillDataForward, leverage);
}
/// <summary>
/// Adds the provided final Symbol with/without underlying set to the algorithm.
/// This method is meant for custom data types that require a ticker, but have no underlying Symbol.
/// Examples of data sources that meet this criteria are U.S. Treasury Yield Curve Rates and Trading Economics data
/// </summary>
/// <param name="dataType">Data source type</param>
/// <param name="symbol">Final symbol that includes underlying (if any)</param>
/// <param name="resolution">Resolution of the Data required</param>
/// <param name="timeZone">Specifies the time zone of the raw data</param>
/// <param name="fillDataForward">When no data available on a tradebar, return the last data that was generated</param>
/// <param name="leverage">Custom leverage per security</param>
/// <returns>The new <see cref="Security"/></returns>
private Security AddDataImpl(Type dataType, Symbol symbol, Resolution? resolution, DateTimeZone timeZone, bool fillDataForward, decimal leverage)
{
var alias = symbol.ID.Symbol;
SymbolCache.Set(alias, symbol);
if (timeZone != null)
{
// user set time zone
MarketHoursDatabase.SetEntryAlwaysOpen(Market.USA, alias, SecurityType.Base, timeZone);
}
//Add this new generic data as a tradeable security:
var config = SubscriptionManager.SubscriptionDataConfigService.Add(
dataType,
symbol,
resolution,
fillDataForward,
isCustomData: true,
extendedMarketHours: true);
var security = Securities.CreateSecurity(symbol, config, leverage, addToSymbolCache: false);
AddToUserDefinedUniverse(security, new List<SubscriptionDataConfig> { config });
return security;
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This is for coarse fundamental US Equity data and
/// will be executed on day changes in the NewYork time zone (<see cref="TimeZones.NewYork"/>
/// </summary>
/// <param name="pyObject">Defines an initial coarse selection</param>
public void AddUniverse(PyObject pyObject)
{
Func<IEnumerable<CoarseFundamental>, object> coarseFunc;
Universe universe;
if (pyObject.TryConvert(out universe))
{
AddUniverse(universe);
}
else if (pyObject.TryConvert(out universe, allowPythonDerivative: true))
{
AddUniverse(new UniversePythonWrapper(pyObject));
}
else if (pyObject.TryConvertToDelegate(out coarseFunc))
{
AddUniverse(coarseFunc.ConvertToUniverseSelectionSymbolDelegate());
}
else
{
using (Py.GIL())
{
throw new ArgumentException($"QCAlgorithm.AddUniverse: {pyObject.Repr()} is not a valid argument.");
}
}
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This is for coarse and fine fundamental US Equity data and
/// will be executed on day changes in the NewYork time zone (<see cref="TimeZones.NewYork"/>
/// </summary>
/// <param name="pyObject">Defines an initial coarse selection or a universe</param>
/// <param name="pyfine">Defines a more detailed selection with access to more data</param>
public void AddUniverse(PyObject pyObject, PyObject pyfine)
{
Func<IEnumerable<CoarseFundamental>, object> coarseFunc;
Func<IEnumerable<FineFundamental>, object> fineFunc;
Universe universe;
if (pyObject.TryConvert(out universe) && pyfine.TryConvertToDelegate(out fineFunc))
{
AddUniverse(universe, fineFunc.ConvertToUniverseSelectionSymbolDelegate());
}
else if (pyObject.TryConvertToDelegate(out coarseFunc) && pyfine.TryConvertToDelegate(out fineFunc))
{
AddUniverse(coarseFunc.ConvertToUniverseSelectionSymbolDelegate(),
fineFunc.ConvertToUniverseSelectionSymbolDelegate());
}
else
{
using (Py.GIL())
{
throw new ArgumentException($"QCAlgorithm.AddUniverse: {pyObject.Repr()} or {pyfine.Repr()} is not a valid argument.");
}
}
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This can be used to return a list of string
/// symbols retrieved from anywhere and will loads those symbols under the US Equity market.
/// </summary>
/// <param name="name">A unique name for this universe</param>
/// <param name="resolution">The resolution this universe should be triggered on</param>
/// <param name="pySelector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
public void AddUniverse(string name, Resolution resolution, PyObject pySelector)
{
var selector = pySelector.ConvertToDelegate<Func<DateTime, object>>();
AddUniverse(name, resolution, selector.ConvertToUniverseSelectionStringDelegate());
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This can be used to return a list of string
/// symbols retrieved from anywhere and will loads those symbols under the US Equity market.
/// </summary>
/// <param name="name">A unique name for this universe</param>
/// <param name="pySelector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
public void AddUniverse(string name, PyObject pySelector)
{
var selector = pySelector.ConvertToDelegate<Func<DateTime, object>>();
AddUniverse(name, selector.ConvertToUniverseSelectionStringDelegate());
}
/// <summary>
/// Creates a new user defined universe that will fire on the requested resolution during market hours.
/// </summary>
/// <param name="securityType">The security type of the universe</param>
/// <param name="name">A unique name for this universe</param>
/// <param name="resolution">The resolution this universe should be triggered on</param>
/// <param name="market">The market of the universe</param>
/// <param name="universeSettings">The subscription settings used for securities added from this universe</param>
/// <param name="pySelector">Function delegate that accepts a DateTime and returns a collection of string symbols</param>
public void AddUniverse(SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, PyObject pySelector)
{
var selector = pySelector.ConvertToDelegate<Func<DateTime, object>>();
AddUniverse(securityType, name, resolution, market, universeSettings, selector.ConvertToUniverseSelectionStringDelegate());
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This will use the default universe settings
/// specified via the <see cref="UniverseSettings"/> property. This universe will use the defaults
/// of SecurityType.Equity, Resolution.Daily, Market.USA, and UniverseSettings
/// </summary>
/// <param name="T">The data type</param>
/// <param name="name">A unique name for this universe</param>
/// <param name="selector">Function delegate that performs selection on the universe data</param>
public void AddUniverse(PyObject T, string name, PyObject selector)
{
AddUniverse(T.CreateType(), SecurityType.Equity, name, Resolution.Daily, Market.USA, UniverseSettings, selector);
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This will use the default universe settings
/// specified via the <see cref="UniverseSettings"/> property. This universe will use the defaults
/// of SecurityType.Equity, Market.USA and UniverseSettings
/// </summary>
/// <param name="T">The data type</param>
/// <param name="name">A unique name for this universe</param>
/// <param name="resolution">The epected resolution of the universe data</param>
/// <param name="selector">Function delegate that performs selection on the universe data</param>
public void AddUniverse(PyObject T, string name, Resolution resolution, PyObject selector)
{
AddUniverse(T.CreateType(), SecurityType.Equity, name, resolution, Market.USA, UniverseSettings, selector);
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This will use the default universe settings
/// specified via the <see cref="UniverseSettings"/> property. This universe will use the defaults
/// of SecurityType.Equity, and Market.USA
/// </summary>
/// <param name="T">The data type</param>
/// <param name="name">A unique name for this universe</param>
/// <param name="resolution">The epected resolution of the universe data</param>
/// <param name="universeSettings">The settings used for securities added by this universe</param>
/// <param name="selector">Function delegate that performs selection on the universe data</param>
public void AddUniverse(PyObject T, string name, Resolution resolution, UniverseSettings universeSettings, PyObject selector)
{
AddUniverse(T.CreateType(), SecurityType.Equity, name, resolution, Market.USA, universeSettings, selector);
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This will use the default universe settings
/// specified via the <see cref="UniverseSettings"/> property. This universe will use the defaults
/// of SecurityType.Equity, Resolution.Daily, and Market.USA
/// </summary>
/// <param name="T">The data type</param>
/// <param name="name">A unique name for this universe</param>
/// <param name="universeSettings">The settings used for securities added by this universe</param>
/// <param name="selector">Function delegate that performs selection on the universe data</param>
public void AddUniverse(PyObject T, string name, UniverseSettings universeSettings, PyObject selector)
{
AddUniverse(T.CreateType(), SecurityType.Equity, name, Resolution.Daily, Market.USA, universeSettings, selector);
}
/// <summary>
/// Creates a new universe and adds it to the algorithm. This will use the default universe settings
/// specified via the <see cref="UniverseSettings"/> property.
/// </summary>
/// <param name="T">The data type</param>
/// <param name="securityType">The security type the universe produces</param>
/// <param name="name">A unique name for this universe</param>
/// <param name="resolution">The epected resolution of the universe data</param>
/// <param name="market">The market for selected symbols</param>
/// <param name="selector">Function delegate that performs selection on the universe data</param>
public void AddUniverse(PyObject T, SecurityType securityType, string name, Resolution resolution, string market, PyObject selector)
{
AddUniverse(T.CreateType(), securityType, name, resolution, market, UniverseSettings, selector);
}
/// <summary>
/// Creates a new universe and adds it to the algorithm
/// </summary>
/// <param name="T">The data type</param>
/// <param name="securityType">The security type the universe produces</param>
/// <param name="name">A unique name for this universe</param>
/// <param name="resolution">The epected resolution of the universe data</param>
/// <param name="market">The market for selected symbols</param>
/// <param name="universeSettings">The subscription settings to use for newly created subscriptions</param>
/// <param name="selector">Function delegate that performs selection on the universe data</param>
public void AddUniverse(PyObject T, SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, PyObject selector)
{
AddUniverse(T.CreateType(), securityType, name, resolution, market, universeSettings, selector);
}
/// <summary>
/// Creates a new universe and adds it to the algorithm
/// </summary>
/// <param name="dataType">The data type</param>
/// <param name="securityType">The security type the universe produces</param>
/// <param name="name">A unique name for this universe</param>
/// <param name="resolution">The epected resolution of the universe data</param>
/// <param name="market">The market for selected symbols</param>
/// <param name="universeSettings">The subscription settings to use for newly created subscriptions</param>
/// <param name="pySelector">Function delegate that performs selection on the universe data</param>
public void AddUniverse(Type dataType, SecurityType securityType, string name, Resolution resolution, string market, UniverseSettings universeSettings, PyObject pySelector)
{
var marketHoursDbEntry = MarketHoursDatabase.GetEntry(market, name, securityType);
var dataTimeZone = marketHoursDbEntry.DataTimeZone;
var exchangeTimeZone = marketHoursDbEntry.ExchangeHours.TimeZone;
var symbol = QuantConnect.Symbol.Create(name, securityType, market, baseDataType: dataType);
var config = new SubscriptionDataConfig(dataType, symbol, resolution, dataTimeZone, exchangeTimeZone, false, false, true, true, isFilteredSubscription: false);
var selector = pySelector.ConvertToDelegate<Func<IEnumerable<IBaseData>, object>>();
AddUniverse(new FuncUniverse(config, universeSettings, SecurityInitializer, baseDatas =>
{
var result = selector(baseDatas);
return ReferenceEquals(result, Universe.Unchanged)
? Universe.Unchanged : ((object[])result)
.Select(x => x is Symbol ? (Symbol)x : QuantConnect.Symbol.Create((string)x, securityType, market, baseDataType: dataType));
}
));
}
/// <summary>
/// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
/// from the consolidator.
/// </summary>
/// <param name="symbol">The symbol to register against</param>
/// <param name="indicator">The indicator to receive data from the consolidator</param>
/// <param name="resolution">The resolution at which to send data to the indicator, null to use the same resolution as the subscription</param>
/// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x)</param>
public void RegisterIndicator(Symbol symbol, PyObject indicator, Resolution? resolution = null, PyObject selector = null)
{
RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution), selector);
}
/// <summary>
/// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
/// from the consolidator.
/// </summary>
/// <param name="symbol">The symbol to register against</param>
/// <param name="indicator">The indicator to receive data from the consolidator</param>
/// <param name="resolution">The resolution at which to send data to the indicator, null to use the same resolution as the subscription</param>
/// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x)</param>
public void RegisterIndicator(Symbol symbol, PyObject indicator, TimeSpan? resolution = null, PyObject selector = null)
{
RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution), selector);
}
/// <summary>
/// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
/// from the consolidator.
/// </summary>
/// <param name="symbol">The symbol to register against</param>
/// <param name="indicator">The indicator to receive data from the consolidator</param>
/// <param name="pyObject">The python object that it is trying to register with, could be consolidator or a timespan</param>
/// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x)</param>
public void RegisterIndicator(Symbol symbol, PyObject indicator, PyObject pyObject, PyObject selector = null)
{
try
{
// First check if this is just a regular IDataConsolidator
IDataConsolidator dataConsolidator;
if (!pyObject.TryConvert(out dataConsolidator))
{
// If not then try and wrap it as a custom Python consolidator
dataConsolidator = new DataConsolidatorPythonWrapper(pyObject);
}
RegisterIndicator(symbol, indicator, dataConsolidator, selector);
return;
}
catch
{
}
// Finally, since above didn't work, just try it as a timespan
// Issue #4668 Fix
using (Py.GIL())
{
try
{
// tryConvert does not work for timespan
TimeSpan? timeSpan = pyObject.As<TimeSpan>();
if (timeSpan != default(TimeSpan))
{
RegisterIndicator(symbol, indicator, timeSpan, selector);
}
}
catch
{
throw new ArgumentException("Invalid third argument, should be either a valid consolidator or timedelta object");
}
}
}
/// <summary>
/// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
/// from the consolidator.
/// </summary>
/// <param name="symbol">The symbol to register against</param>
/// <param name="indicator">The indicator to receive data from the consolidator</param>
/// <param name="consolidator">The consolidator to receive raw subscription data</param>
/// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x)</param>
public void RegisterIndicator(Symbol symbol, PyObject indicator, IDataConsolidator consolidator, PyObject selector = null)
{
IndicatorBase<IndicatorDataPoint> indicatorDataPoint;
IndicatorBase<IBaseDataBar> indicatorDataBar;
IndicatorBase<TradeBar> indicatorTradeBar;
if (indicator.TryConvert(out indicatorDataPoint))
{
RegisterIndicator(symbol, indicatorDataPoint, consolidator, selector?.ConvertToDelegate<Func<IBaseData, decimal>>());
return;
}
else if (indicator.TryConvert(out indicatorDataBar))
{
RegisterIndicator(symbol, indicatorDataBar, consolidator, selector?.ConvertToDelegate<Func<IBaseData, IBaseDataBar>>());
return;
}
else if (indicator.TryConvert(out indicatorTradeBar))
{
RegisterIndicator(symbol, indicatorTradeBar, consolidator, selector?.ConvertToDelegate<Func<IBaseData, TradeBar>>());
return;
}
RegisterIndicator(symbol, WrapPythonIndicator(indicator), consolidator, selector?.ConvertToDelegate<Func<IBaseData, IBaseData>>());
}
/// <summary>
/// Plot a chart using string series name, with value.
/// </summary>
/// <param name="series">Name of the plot series</param>
/// <param name="pyObject">PyObject with the value to plot</param>
/// <seealso cref="Plot(string,decimal)"/>
public void Plot(string series, PyObject pyObject)
{
using (Py.GIL())
{
try
{
var value = (((dynamic)pyObject).Current.Value as PyObject).GetAndDispose<decimal>();
Plot(series, value);
}
catch
{
var pythonType = pyObject.GetPythonType().Repr();
throw new ArgumentException($"QCAlgorithm.Plot(): The last argument should be a QuantConnect Indicator object, {pythonType} was provided.");
}
}
}
/// <summary>
/// Plots the value of each indicator on the chart
/// </summary>
/// <param name="chart">The chart's name</param>
/// <param name="first">The first indicator to plot</param>
/// <param name="second">The second indicator to plot</param>
/// <param name="third">The third indicator to plot</param>
/// <param name="fourth">The fourth indicator to plot</param>
/// <seealso cref="Plot(string,string,decimal)"/>
public void Plot(string chart, Indicator first, Indicator second = null, Indicator third = null, Indicator fourth = null)
{
Plot(chart, new[] { first, second, third, fourth }.Where(x => x != null).ToArray());
}
/// <summary>
/// Plots the value of each indicator on the chart
/// </summary>
/// <param name="chart">The chart's name</param>
/// <param name="first">The first indicator to plot</param>
/// <param name="second">The second indicator to plot</param>
/// <param name="third">The third indicator to plot</param>
/// <param name="fourth">The fourth indicator to plot</param>
/// <seealso cref="Plot(string,string,decimal)"/>
public void Plot(string chart, BarIndicator first, BarIndicator second = null, BarIndicator third = null, BarIndicator fourth = null)
{
Plot(chart, new[] { first, second, third, fourth }.Where(x => x != null).ToArray());
}
/// <summary>
/// Plots the value of each indicator on the chart
/// </summary>
/// <param name="chart">The chart's name</param>
/// <param name="first">The first indicator to plot</param>
/// <param name="second">The second indicator to plot</param>
/// <param name="third">The third indicator to plot</param>
/// <param name="fourth">The fourth indicator to plot</param>
/// <seealso cref="Plot(string,string,decimal)"/>
public void Plot(string chart, TradeBarIndicator first, TradeBarIndicator second = null, TradeBarIndicator third = null, TradeBarIndicator fourth = null)
{
Plot(chart, new[] { first, second, third, fourth }.Where(x => x != null).ToArray());
}
/// <summary>
/// Automatically plots each indicator when a new value is available
/// </summary>
public void PlotIndicator(string chart, PyObject first, PyObject second = null, PyObject third = null, PyObject fourth = null)
{
var array = GetIndicatorArray(first, second, third, fourth);
PlotIndicator(chart, array[0], array[1], array[2], array[3]);
}
/// <summary>
/// Automatically plots each indicator when a new value is available
/// </summary>
public void PlotIndicator(string chart, bool waitForReady, PyObject first, PyObject second = null, PyObject third = null, PyObject fourth = null)
{
var array = GetIndicatorArray(first, second, third, fourth);
PlotIndicator(chart, waitForReady, array[0], array[1], array[2], array[3]);
}
/// <summary>
/// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically
/// updated on the symbol's subscription resolution
/// </summary>
/// <param name="symbol">The symbol whose values we want as an indicator</param>
/// <param name="selector">Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value)</param>
/// <param name="filter">Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter</param>
/// <param name="fieldName">The name of the field being selected</param>
/// <returns>A new FilteredIdentity indicator for the specified symbol and selector</returns>
public FilteredIdentity FilteredIdentity(Symbol symbol, PyObject selector = null, PyObject filter = null, string fieldName = null)
{
var resolution = GetSubscription(symbol).Resolution;
return FilteredIdentity(symbol, resolution, selector, filter, fieldName);
}
/// <summary>
/// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically
/// updated on the symbol's subscription resolution
/// </summary>
/// <param name="symbol">The symbol whose values we want as an indicator</param>
/// <param name="resolution">The desired resolution of the data</param>
/// <param name="selector">Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value)</param>
/// <param name="filter">Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter</param>
/// <param name="fieldName">The name of the field being selected</param>
/// <returns>A new FilteredIdentity indicator for the specified symbol and selector</returns>
public FilteredIdentity FilteredIdentity(Symbol symbol, Resolution resolution, PyObject selector = null, PyObject filter = null, string fieldName = null)
{
var name = CreateIndicatorName(symbol, fieldName ?? "close", resolution);
var pyselector = PythonUtil.ToFunc<IBaseData, IBaseDataBar>(selector);
var pyfilter = PythonUtil.ToFunc<IBaseData, bool>(filter);
var filteredIdentity = new FilteredIdentity(name, pyfilter);
RegisterIndicator(symbol, filteredIdentity, resolution, pyselector);
return filteredIdentity;
}
/// <summary>
/// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically
/// updated on the symbol's subscription resolution
/// </summary>
/// <param name="symbol">The symbol whose values we want as an indicator</param>
/// <param name="resolution">The desired resolution of the data</param>
/// <param name="selector">Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value)</param>
/// <param name="filter">Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter</param>
/// <param name="fieldName">The name of the field being selected</param>
/// <returns>A new FilteredIdentity indicator for the specified symbol and selector</returns>
public FilteredIdentity FilteredIdentity(Symbol symbol, TimeSpan resolution, PyObject selector = null, PyObject filter = null, string fieldName = null)
{
var name = $"{symbol}({fieldName ?? "close"}_{resolution.ToStringInvariant(null)})";
var pyselector = PythonUtil.ToFunc<IBaseData, IBaseDataBar>(selector);
var pyfilter = PythonUtil.ToFunc<IBaseData, bool>(filter);
var filteredIdentity = new FilteredIdentity(name, pyfilter);
RegisterIndicator(symbol, filteredIdentity, ResolveConsolidator(symbol, resolution), pyselector);
return filteredIdentity;
}
/// <summary>
/// Gets the historical data for the specified symbol. The exact number of bars will be returned.
/// The symbol must exist in the Securities collection.
/// </summary>
/// <param name="tickers">The symbols to retrieve historical data for</param>
/// <param name="periods">The number of bars to request</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>A python dictionary with pandas DataFrame containing the requested historical data</returns>
public PyObject History(PyObject tickers, int periods, Resolution? resolution = null)
{
var symbols = tickers.ConvertToSymbolEnumerable();
return PandasConverter.GetDataFrame(History(symbols, periods, resolution));
}
/// <summary>
/// Gets the historical data for the specified symbols over the requested span.
/// The symbols must exist in the Securities collection.
/// </summary>
/// <param name="tickers">The symbols to retrieve historical data for</param>
/// <param name="span">The span over which to retrieve recent historical data</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>A python dictionary with pandas DataFrame containing the requested historical data</returns>
public PyObject History(PyObject tickers, TimeSpan span, Resolution? resolution = null)
{
var symbols = tickers.ConvertToSymbolEnumerable();
return PandasConverter.GetDataFrame(History(symbols, span, resolution));
}
/// <summary>
/// Gets the historical data for the specified symbol between the specified dates. The symbol must exist in the Securities collection.
/// </summary>
/// <param name="tickers">The symbols to retrieve historical data for</param>
/// <param name="start">The start time in the algorithm's time zone</param>
/// <param name="end">The end time in the algorithm's time zone</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>A python dictionary with pandas DataFrame containing the requested historical data</returns>
public PyObject History(PyObject tickers, DateTime start, DateTime end, Resolution? resolution = null)
{
var symbols = tickers.ConvertToSymbolEnumerable();
return PandasConverter.GetDataFrame(History(symbols, start, end, resolution));
}
/// <summary>
/// Gets the historical data for the specified symbols between the specified dates. The symbols must exist in the Securities collection.
/// </summary>
/// <param name="type">The data type of the symbols</param>
/// <param name="tickers">The symbols to retrieve historical data for</param>
/// <param name="start">The start time in the algorithm's time zone</param>
/// <param name="end">The end time in the algorithm's time zone</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>pandas.DataFrame containing the requested historical data</returns>
public PyObject History(PyObject type, PyObject tickers, DateTime start, DateTime end, Resolution? resolution = null)
{
var symbols = tickers.ConvertToSymbolEnumerable();
var requestedType = type.CreateType();
var requests = symbols.Select(x =>
{
var security = Securities[x];
var config = security.Subscriptions.OrderByDescending(s => s.Resolution)
.FirstOrDefault(s => s.Type.BaseType == requestedType.BaseType);
if (config == null) return null;
return _historyRequestFactory.CreateHistoryRequest(config, start, end, GetExchangeHours(x), resolution);
});
return PandasConverter.GetDataFrame(History(requests.Where(x => x != null)).Memoize());
}
/// <summary>
/// Gets the historical data for the specified symbols. The exact number of bars will be returned for
/// each symbol. This may result in some data start earlier/later than others due to when various
/// exchanges are open. The symbols must exist in the Securities collection.
/// </summary>
/// <param name="type">The data type of the symbols</param>
/// <param name="tickers">The symbols to retrieve historical data for</param>
/// <param name="periods">The number of bars to request</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>pandas.DataFrame containing the requested historical data</returns>
public PyObject History(PyObject type, PyObject tickers, int periods, Resolution? resolution = null)
{
var symbols = tickers.ConvertToSymbolEnumerable();
var requestedType = type.CreateType();
var requests = symbols.Select(x =>
{
var security = Securities[x];
var config = security.Subscriptions.OrderByDescending(s => s.Resolution)
.FirstOrDefault(s => s.Type.BaseType == requestedType.BaseType);
if (config == null) return null;
var res = GetResolution(x, resolution);
var exchange = GetExchangeHours(x);
var start = _historyRequestFactory.GetStartTimeAlgoTz(x, periods, res, exchange);
return _historyRequestFactory.CreateHistoryRequest(config, start, Time.RoundDown(res.ToTimeSpan()), exchange, res);
});
return PandasConverter.GetDataFrame(History(requests.Where(x => x != null)).Memoize());
}
/// <summary>
/// Gets the historical data for the specified symbols over the requested span.
/// The symbols must exist in the Securities collection.
/// </summary>
/// <param name="type">The data type of the symbols</param>
/// <param name="tickers">The symbols to retrieve historical data for</param>
/// <param name="span">The span over which to retrieve recent historical data</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>pandas.DataFrame containing the requested historical data</returns>
public PyObject History(PyObject type, PyObject tickers, TimeSpan span, Resolution? resolution = null)
{
return History(type, tickers, Time - span, Time, resolution);
}
/// <summary>
/// Gets the historical data for the specified symbols between the specified dates. The symbols must exist in the Securities collection.
/// </summary>
/// <param name="type">The data type of the symbols</param>
/// <param name="symbol">The symbol to retrieve historical data for</param>
/// <param name="start">The start time in the algorithm's time zone</param>
/// <param name="end">The end time in the algorithm's time zone</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>pandas.DataFrame containing the requested historical data</returns>
public PyObject History(PyObject type, Symbol symbol, DateTime start, DateTime end, Resolution? resolution = null)
{
var security = Securities[symbol];
// verify the types match
var requestedType = type.CreateType();
var config = security.Subscriptions.OrderByDescending(s => s.Resolution)
.FirstOrDefault(s => s.Type.BaseType == requestedType.BaseType);
if (config == null)
{
var actualType = security.Subscriptions.Select(x => x.Type.Name).DefaultIfEmpty("[None]").FirstOrDefault();
throw new ArgumentException("The specified security is not of the requested type. Symbol: " + symbol.ToString() + " Requested Type: " + requestedType.Name + " Actual Type: " + actualType);
}
var request = _historyRequestFactory.CreateHistoryRequest(config, start, end, GetExchangeHours(symbol), resolution);
return PandasConverter.GetDataFrame(History(request).Memoize());
}
/// <summary>
/// Gets the historical data for the specified symbols. The exact number of bars will be returned for
/// each symbol. This may result in some data start earlier/later than others due to when various
/// exchanges are open. The symbols must exist in the Securities collection.
/// </summary>
/// <param name="type">The data type of the symbols</param>
/// <param name="symbol">The symbol to retrieve historical data for</param>
/// <param name="periods">The number of bars to request</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>pandas.DataFrame containing the requested historical data</returns>
public PyObject History(PyObject type, Symbol symbol, int periods, Resolution? resolution = null)
{
if (resolution == Resolution.Tick) throw new ArgumentException("History functions that accept a 'periods' parameter can not be used with Resolution.Tick");
var res = GetResolution(symbol, resolution);
var start = _historyRequestFactory.GetStartTimeAlgoTz(symbol, periods, res, GetExchangeHours(symbol));
var end = Time.RoundDown(res.ToTimeSpan());
return History(type, symbol, start, end, resolution);
}
/// <summary>
/// Gets the historical data for the specified symbols over the requested span.
/// The symbols must exist in the Securities collection.
/// </summary>
/// <param name="type">The data type of the symbols</param>
/// <param name="symbol">The symbol to retrieve historical data for</param>
/// <param name="span">The span over which to retrieve recent historical data</param>
/// <param name="resolution">The resolution to request</param>
/// <returns>pandas.DataFrame containing the requested historical data</returns>
public PyObject History(PyObject type, Symbol symbol, TimeSpan span, Resolution? resolution = null)
{
return History(type, symbol, Time - span, Time, resolution);
}
/// <summary>
/// Sets the specified function as the benchmark, this function provides the value of
/// the benchmark at each date/time requested
/// </summary>
/// <param name="benchmark">The benchmark producing function</param>
public void SetBenchmark(PyObject benchmark)
{
using (Py.GIL())
{
var pyBenchmark = PythonUtil.ToFunc<DateTime, decimal>(benchmark);
if (pyBenchmark != null)
{
SetBenchmark(pyBenchmark);
return;
}
SetBenchmark((Symbol)benchmark.AsManagedObject(typeof(Symbol)));
}
}
/// <summary>
/// Sets the brokerage to emulate in backtesting or paper trading.
/// This can be used to set a custom brokerage model.
/// </summary>
/// <param name="model">The brokerage model to use</param>
public void SetBrokerageModel(PyObject model)
{
IBrokerageModel brokerageModel;
if (!model.TryConvert(out brokerageModel))
{
brokerageModel = new BrokerageModelPythonWrapper(model);
}
SetBrokerageModel(brokerageModel);
}
/// <summary>
/// Sets the security initializer function, used to initialize/configure securities after creation
/// </summary>
/// <param name="securityInitializer">The security initializer function or class</param>
public void SetSecurityInitializer(PyObject securityInitializer)
{
var securityInitializer1 = PythonUtil.ToAction<Security>(securityInitializer);
if (securityInitializer1 != null)
{
SetSecurityInitializer(securityInitializer1);
return;
}
SetSecurityInitializer(new SecurityInitializerPythonWrapper(securityInitializer));
}
/// <summary>
/// Downloads the requested resource as a <see cref="string"/>.
/// The resource to download is specified as a <see cref="string"/> containing the URI.
/// </summary>
/// <param name="address">A string containing the URI to download</param>
/// <param name="headers">Defines header values to add to the request</param>
/// <returns>The requested resource as a <see cref="string"/></returns>
public string Download(string address, PyObject headers) => Download(address, headers, null, null);
/// <summary>
/// Downloads the requested resource as a <see cref="string"/>.
/// The resource to download is specified as a <see cref="string"/> containing the URI.
/// </summary>
/// <param name="address">A string containing the URI to download</param>
/// <param name="headers">Defines header values to add to the request</param>
/// <param name="userName">The user name associated with the credentials</param>
/// <param name="password">The password for the user name associated with the credentials</param>
/// <returns>The requested resource as a <see cref="string"/></returns>
public string Download(string address, PyObject headers, string userName, string password)
{
var dict = new Dictionary<string, string>();
if (headers != null)
{
using (Py.GIL())
{
// In python algorithms, headers must be a python dictionary
// In order to convert it into a C# Dictionary
if (PyDict.IsDictType(headers))
{
foreach (PyObject pyKey in headers)
{
var key = (string)pyKey.AsManagedObject(typeof(string));
var value = (string)headers.GetItem(pyKey).AsManagedObject(typeof(string));
dict.Add(key, value);
}
}
else
{
throw new ArgumentException($"QCAlgorithm.Fetch(): Invalid argument. {headers.Repr()} is not a dict");
}
}
}
return Download(address, dict, userName, password);
}
/// <summary>
/// Send a debug message to the web console:
/// </summary>
/// <param name="message">Message to send to debug console</param>
/// <seealso cref="Log(PyObject)"/>
/// <seealso cref="Error(PyObject)"/>
public void Debug(PyObject message)
{
Debug(message.ToSafeString());
}
/// <summary>
/// Send a string error message to the Console.
/// </summary>
/// <param name="message">Message to display in errors grid</param>
/// <seealso cref="Debug(PyObject)"/>
/// <seealso cref="Log(PyObject)"/>
public void Error(PyObject message)
{
Error(message.ToSafeString());
}
/// <summary>
/// Added another method for logging if user guessed.
/// </summary>
/// <param name="message">String message to log.</param>
/// <seealso cref="Debug(PyObject)"/>
/// <seealso cref="Error(PyObject)"/>
public void Log(PyObject message)
{
Log(message.ToSafeString());
}
/// <summary>
/// Terminate the algorithm after processing the current event handler.
/// </summary>
/// <param name="message">Exit message to display on quitting</param>
public void Quit(PyObject message)
{
Quit(message.ToSafeString());
}