candlestick-timeseries.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # -----------------------------------------------------------------------------------------
  12. # To execute this script, make sure that the taipy-gui package is installed in your
  13. # Python environment and run:
  14. # python <script>
  15. # -----------------------------------------------------------------------------------------
  16. import datetime
  17. from taipy.gui import Gui
  18. if __name__ == "__main__":
  19. # Retrieved history:
  20. # (Open, Close, Low, High)
  21. stock_history = [
  22. (311.05, 311.00, 310.75, 311.33),
  23. (308.53, 308.31, 307.72, 309.00),
  24. (307.35, 306.24, 306.12, 307.46),
  25. (306.35, 304.90, 304.34, 310.10),
  26. (304.90, 302.99, 302.27, 307.00),
  27. (303.03, 301.66, 301.20, 303.25),
  28. (301.61, 299.58, 299.50, 301.89),
  29. (299.58, 297.95, 297.80, 300.06),
  30. (297.95, 299.03, 297.14, 299.67),
  31. (299.03, 301.87, 296.71, 301.89),
  32. (301.89, 299.40, 298.73, 302.93),
  33. (299.50, 299.35, 298.83, 299.50),
  34. (299.35, 299.20, 299.19, 299.68),
  35. (299.42, 300.50, 299.42, 300.50),
  36. (300.70, 300.65, 300.32, 300.75),
  37. (300.65, 299.91, 299.91, 300.76),
  38. ]
  39. start_date = datetime.datetime(year=2022, month=10, day=21)
  40. period = datetime.timedelta(seconds=4 * 60 * 60) # 4 hours
  41. data = {
  42. # Compute date series
  43. "Date": [start_date + n * period for n in range(0, len(stock_history))],
  44. # Extract open values
  45. "Open": [v[0] for v in stock_history],
  46. # Extract close values
  47. "Close": [v[1] for v in stock_history],
  48. # Extract low values
  49. "Low": [v[2] for v in stock_history],
  50. # Extract high values
  51. "High": [v[3] for v in stock_history],
  52. }
  53. md = """
  54. # Candlestick - Timeline
  55. <|{data}|chart|type=candlestick|x=Date|open=Open|close=Close|low=Low|high=High|>
  56. """
  57. Gui(md).run()