candlestick_timeseries.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright 2021-2025 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. # Retrieved history:
  19. # (Open, Close, Low, High)
  20. stock_history = [
  21. (311.05, 311.00, 310.75, 311.33),
  22. (308.53, 308.31, 307.72, 309.00),
  23. (307.35, 306.24, 306.12, 307.46),
  24. (306.35, 304.90, 304.34, 310.10),
  25. (304.90, 302.99, 302.27, 307.00),
  26. (303.03, 301.66, 301.20, 303.25),
  27. (301.61, 299.58, 299.50, 301.89),
  28. (299.58, 297.95, 297.80, 300.06),
  29. (297.95, 299.03, 297.14, 299.67),
  30. (299.03, 301.87, 296.71, 301.89),
  31. (301.89, 299.40, 298.73, 302.93),
  32. (299.50, 299.35, 298.83, 299.50),
  33. (299.35, 299.20, 299.19, 299.68),
  34. (299.42, 300.50, 299.42, 300.50),
  35. (300.70, 300.65, 300.32, 300.75),
  36. (300.65, 299.91, 299.91, 300.76),
  37. ]
  38. start_date = datetime.datetime(year=2022, month=10, day=21)
  39. period = datetime.timedelta(seconds=4 * 60 * 60) # 4 hours
  40. data = {
  41. # Compute date series
  42. "Date": [start_date + n * period for n in range(0, len(stock_history))],
  43. # Extract open values
  44. "Open": [v[0] for v in stock_history],
  45. # Extract close values
  46. "Close": [v[1] for v in stock_history],
  47. # Extract low values
  48. "Low": [v[2] for v in stock_history],
  49. # Extract high values
  50. "High": [v[3] for v in stock_history],
  51. }
  52. md = """
  53. <|{data}|chart|type=candlestick|x=Date|open=Open|close=Close|low=Low|high=High|>
  54. """
  55. if __name__ == "__main__":
  56. Gui(md).run(title="Chart - Candlestick - Timeline")