Hidden Redirect Link

Two Trading Sessions


Hidden Redirect Link
Loading...

# indie:lang_version = 5

from indie import indicator, MainContext, TimeFrame, time_frame_unit from indie import color, param from indie.color import rgba from indie.schedule import ScheduleRule, Schedule, ALL_DAYS from indie.drawings import Rectangle, AbsolutePosition from datetime import time from math import isnan

@indicator('Trading Sessions', overlay_main_pane=True)

# ===================================== # Beijing session settings # =====================================

@param.int('b_start_hour', default=8, min=0, max=23, title='Beijing Start Hour') @param.int('b_start_minute', default=0, min=0, max=59, title='Beijing Start Minute') @param.int('b_end_hour', default=14, min=0, max=23, title='Beijing End Hour') @param.int('b_end_minute', default=0, min=0, max=59, title='Beijing End Minute')

# ===================================== # US session settings # =====================================

@param.int('u_start_hour', default=22, min=0, max=23, title='US Start Hour') @param.int('u_start_minute', default=30, min=0, max=59, title='US Start Minute') @param.int('u_end_hour', default=5, min=0, max=23, title='US End Hour') @param.int('u_end_minute', default=0, min=0, max=59, title='US End Minute')

# ===================================== # Color settings # =====================================

@param.color('beijing_color', default=rgba(255, 102, 102, 1.0), title='Beijing Color') @param.float('beijing_transparency', default=90.0, min=0.0, max=100.0, step=1.0, title='Beijing Transparency')

@param.color('us_color', default=rgba(102, 204, 255, 1.0), title='US Color') @param.float('us_transparency', default=90.0, min=0.0, max=100.0, step=1.0, title='US Transparency')

class Main(MainContext):

def __init__( self, b_start_hour, b_start_minute, b_end_hour, b_end_minute, u_start_hour, u_start_minute, u_end_hour, u_end_minute ):

# ================================= # Trading schedules # =================================

self.beijing = Schedule( rules=[ ScheduleRule( start=time(b_start_hour, b_start_minute), end=time(b_end_hour, b_end_minute), days=ALL_DAYS ) ], timezone='Asia/Shanghai' )

self.us = Schedule( rules=[ ScheduleRule( start=time(u_start_hour, u_start_minute), end=time(u_end_hour, u_end_minute), days=ALL_DAYS ) ], timezone='Asia/Shanghai' )

# ================================= # Beijing state # =================================

self.b_active = False self.b_start = 0.0 self.b_high = 0.0 self.b_low = 0.0

# Initialize rectangle in __init__ self.b_rect = Rectangle( AbsolutePosition(0.0, 0.0), AbsolutePosition(0.0, 0.0), line_color=color.TRANSPARENT, bg_color=color.TRANSPARENT, middle_line_color=color.TRANSPARENT )

# ================================= # US state # =================================

self.u_active = False self.u_start = 0.0 self.u_high = 0.0 self.u_low = 0.0

# Initialize rectangle in __init__ self.u_rect = Rectangle( AbsolutePosition(0.0, 0.0), AbsolutePosition(0.0, 0.0), line_color=color.TRANSPARENT, bg_color=color.TRANSPARENT, middle_line_color=color.TRANSPARENT )

def calc( self, beijing_color, beijing_transparency, us_color, us_transparency ):

# ================================= # Timeframe restriction # =================================

if self.time_frame > TimeFrame(1, time_frame_unit.HOUR): return

current_time = self.time[0]

if isnan(current_time): return

# ================================= # Colors and transparency # =================================

b_opacity = 1.0 - beijing_transparency / 100.0 u_opacity = 1.0 - us_transparency / 100.0

b_fill = beijing_color(b_opacity) u_fill = us_color(u_opacity)

# ================================= # Session detection # =================================

in_beijing = current_time in self.beijing in_us = current_time in self.us

# ================================= # Beijing Session # =================================

if in_beijing:

# New Beijing session if not self.b_active:

self.b_active = True

self.b_start = current_time self.b_high = self.high[0] self.b_low = self.low[0]

# Create a new rectangle for this session self.b_rect = Rectangle( AbsolutePosition( self.b_start, self.b_high ), AbsolutePosition( current_time, self.b_low ), line_color=color.TRANSPARENT, bg_color=b_fill, middle_line_color=color.TRANSPARENT )

else:

# Update session high and low self.b_high = max( self.b_high, self.high[0] )

self.b_low = min( self.b_low, self.low[0] )

# Update rectangle coordinates on EVERY execution self.b_rect.point_a = AbsolutePosition( self.b_start, self.b_high )

self.b_rect.point_b = AbsolutePosition( current_time, self.b_low )

# Redraw existing rectangle immediately self.b_rect.bg_color = b_fill

self.chart.draw(self.b_rect)

else:

self.b_active = False

# ================================= # US Session # =================================

if in_us:

# New US session if not self.u_active:

self.u_active = True

self.u_start = current_time self.u_high = self.high[0] self.u_low = self.low[0]

# Create a new rectangle for this session self.u_rect = Rectangle( AbsolutePosition( self.u_start, self.u_high ), AbsolutePosition( current_time, self.u_low ), line_color=color.TRANSPARENT, bg_color=u_fill, middle_line_color=color.TRANSPARENT )

else:

# Update session high and low self.u_high = max( self.u_high, self.high[0] )

self.u_low = min( self.u_low, self.low[0] )

# Update rectangle coordinates on EVERY execution self.u_rect.point_a = AbsolutePosition( self.u_start, self.u_high )

self.u_rect.point_b = AbsolutePosition( current_time, self.u_low )

# Redraw existing rectangle immediately self.u_rect.bg_color = u_fill

self.chart.draw(self.u_rect)

else:

self.u_active = False

return

© Licensed under MIT

Comments

loading