Lean  $LEAN_TAG$
LeakyBucketControlParameters.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using Newtonsoft.Json;
20 
21 namespace QuantConnect.Packets
22 {
23  /// <summary>
24  /// Provides parameters that control the behavior of a leaky bucket rate limiting algorithm. The
25  /// parameter names below are phrased in the positive, such that the bucket is filled up over time
26  /// vs leaking out over time.
27  /// </summary>
29  {
30  // defaults represent 2 hour max capacity refilling at one seventh the capacity (~17.2 => 18) each day.
31  // rounded up to 18 to prevent a very small decrease in refilling, IOW, if it's defaulting to 17, then
32  // after 7 days have passed, we'll end up being at 119 and not completely refilled, but at 18, on the 6th
33  // day we'll reach 108 and on the seventh day it will top off at 120 since it's not permitted to exceed the max
34  /// <summary>
35  /// Default capacity for leaky bucket
36  /// </summary>
37  public static int DefaultCapacity = Config.GetInt("scheduled-event-leaky-bucket-capacity", 2 * 60);
38 
39  /// <summary>
40  /// Default time interval
41  /// </summary>
42  public static int DefaultTimeInterval = Config.GetInt("scheduled-event-leaky-bucket-time-interval-minutes", 1440);
43 
44  /// <summary>
45  /// Default refill amount
46  /// </summary>
47  public static int DefaultRefillAmount = Config.GetInt("scheduled-event-leaky-bucket-refill-amount", (int)Math.Ceiling(DefaultCapacity/7.0));
48 
49  /// <summary>
50  /// Sets the total capacity of the bucket in a leaky bucket algorithm. This is the maximum
51  /// number of 'units' the bucket can hold and also defines the maximum burst rate, assuming
52  /// instantaneous usage of 'units'. In reality, the usage of 'units' takes times, and so it
53  /// is possible for the bucket to incrementally refill while consuming from the bucket.
54  /// </summary>
55  public int Capacity;
56 
57  /// <summary>
58  /// Sets the refill amount of the bucket. This defines the quantity of 'units' that become available
59  /// to a consuming entity after the time interval has elapsed. For example, if the refill amount is
60  /// equal to one, then each time interval one new 'unit' will be made available for a consumer that is
61  /// throttled by the leaky bucket.
62  /// </summary>
63  public int RefillAmount;
64 
65  /// <summary>
66  /// Sets the time interval for the refill amount of the bucket, in minutes. After this amount of wall-clock
67  /// time has passed, the bucket will refill the refill amount, thereby making more 'units' available
68  /// for a consumer. For example, if the refill amount equals 10 and the time interval is 30 minutes, then
69  /// every 30 minutes, 10 more 'units' become available for a consumer. The available 'units' will
70  /// continue to increase until the bucket capacity is reached.
71  /// </summary>
72  public int TimeIntervalMinutes;
73 
74  /// <summary>
75  /// Initializes a new instance of the <see cref="LeakyBucketControlParameters"/> using default values
76  /// </summary>
78  {
82  }
83 
84  /// <summary>
85  /// Initializes a new instance of the <see cref="LeakyBucketControlParameters"/> with the specified value
86  /// </summary>
87  /// <param name="capacity">The total capacity of the bucket in minutes</param>
88  /// <param name="refillAmount">The number of additional minutes to add to the bucket
89  /// after <paramref name="timeIntervalMinutes"/> has elapsed</param>
90  /// <param name="timeIntervalMinutes">The interval, in minutes, that must pass before the <paramref name="refillAmount"/>
91  /// is added back to the bucket for reuse</param>
92  public LeakyBucketControlParameters(int capacity, int refillAmount, int timeIntervalMinutes)
93  {
94  Capacity = capacity;
95  RefillAmount = refillAmount;
96  TimeIntervalMinutes = timeIntervalMinutes;
97  }
98  }
99 }