jason5480

chrono_utils

Library "chrono_utils"
📝Description
Collection of objects and common functions that are related to datetime windows session days and time ranges. The main purpose of this library is to handle time-related functionality and make it easy to reason about a future bar and see if it is part of a predefined user session and/or inside a datetime window. All existing session functions I found in the documentation e.g. "not na(time(timeframe, session, timezone))" are not suitable for strategies, since the execution of the orders is delayed by one bar due to the execution happening at the bar close. So a prediction for the next bar is necessary. Moreover, a history operator with a negative value is not allowed e.g. `not na(time(timeframe, session, timezone))` expression is not valid. Thus, I created this library to overcome this small but very important limitation. In the meantime, I added useful functionality to handle session-based behavior. An interesting utility that emerged from this development is data anomaly detection where a comparison between the prediction and the actual value is happening. If those two values are different then a data inconsistency happens between the prediction bar and the actual bar (probably due to a holiday or half session day etc..)

🟢How to Guide
To use the functionality this library provides in your script you have to import it first!
Copy the import statement of the latest release by pressing the copy button below and then paste it into your script. Give a short name to this library so you can refer to it later on. The import statement should look like this:
import jason5480/chrono_utils/1 as chr

To predict the next bar on the chart you have to initialize a ChartSession object and call the "next_bar" method on that object.
// Initialize the chart session
chr.ChartSession chartSession = chr.ChartSession.new().from_chart()
// Predict the next bar based on the chart session
chr.Bar nextBar = chartSession.next_bar()
In this case, the library will do its best to guess the next bar based on the information it has from the chart.

The Bar object offers a variety of methods to use and you can check if it is part of a session or window.
To check if a given bar is inside a window first of all you have to initialize a DateTimeWindow object.
A code example is the following:
var dateTimeWindow = chr.DateTimeWindow.new().init(fromDateTime = timestamp('01 Jan 2023 00:00'), toDateTime = timestamp('01 Jan 2024 00:00'))

Then you have to "ask" the bar if it is inside that window:
// Filter bars outside the datetime window
bool dateFilterApproval = nextBar.is_in_window(dateTimeWindow)

You can visualize the result by drawing the background of the bars that are outside the given window:
bgcolor(color = dateFilterApproval ? na : color.new(color.fuchsia, 90), offset = offsetBars, title = 'Datetime Window Filter')

In the same way, you can "ask" the bar if it is inside a session.
First of all, you should initialize a UserSession object.
A code example is the following:
var userSession = chr.UserSession.new().from_sess_string(sess = '0800-1700:23456', refTimezone = 'UTC')

Then check if the given bar is inside the session like that:
// Filter bars outside the user-defined sessions
bool sessionFilterApproval = nextBar.is_in_session(userSession)

You can visualize the result by drawing the background of the bars that are outside the given session:
bgcolor(color = sessionFilterApproval ? na : color.new(color.red, 90), offset = offsetBars, title = 'Session Filter')

🏋️‍♂️ Please check the code sections "EXAMPLE DATETIME WINDOW FILTER" and "EXAMPLE SESSION FILTER" for a complete example of how to utilize the full potential of this library including user input settings!

⚠️Caveats
As I mentioned in the description there are some cases that the prediction of the next bar is not accurate. A wrong prediction will affect the outcome of the filtering. The main reasons this could happen are the following:
  • Public holidays when the market is closed
  • Half trading days usually before public holidays
  • Change in the daylight saving time (DST)
  • A data anomaly of the chart, where there are missing and/or inconsistent data.
  • A bug in this library (Please report by PM sending the symbol, timeframe, and settings)


Special thanks to @robbatt for the constructive feedback 🏆. Without him, the exposed API of this library would be very lengthy and complicated to use. Thanks to him, now the user of this library will be able to get the most, with only a few lines of code!

If you're interested in exploring and discovering other high quality free strategies come join us in Discord: discord.gg/2wkm9QqSuY
파인 라이브러리

트레이딩뷰 정신에 따라 오써는 이 파인 코드를 오픈 소스 라이브러리로 퍼블리쉬하여 당사 커뮤니티의 다른 파인 프로그래머들이 쓸 수 있도록 하였습니다. 오써에게 찬사를! 여러분은 이 라이브러리를 프라이빗 또는 오픈 소스 퍼블리케이션에 쓸 수 있지만 퍼블리케이션에 재사용은 하우스룰을 따릅니다.

면책사항

이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.

이 라이브러리를 쓰시겠습니까?

텍스트를 클립보드에 카피한 뒤 님의 스크립트에 붙여 넣기.