TradingSession
interface cAlgo.API.Internals
DesktopCLICloud
Trading session schedule
Definition
public interface TradingSession
Namespace: cAlgo.API.Internals · Group: Symbols
Examples
using cAlgo.API;
using cAlgo.API.Internals;
using System;
namespace cAlgo
{
// This sample indicator shows how to use a symbol TradingSession
public partial class TradingSessionSample : Indicator
{
private TextBlock _isOpenedTextBlock;
private TextBlock _timeTillCloseTextBlock;
private TextBlock _timeTillOpenTextBlock;
private Symbol _symbol;
protected override void Initialize()
{
var grid = new Grid(6, 2)
{
BackgroundColor = Color.Gold,
Opacity = 0.6,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
var style = new Style();
style.Set(ControlProperty.Padding, 1);
style.Set(ControlProperty.Margin, 2);
style.Set(ControlProperty.BackgroundColor, Color.Black);
style.Set(ControlProperty.FontSize, 8);
_symbol = UseCurrentSymbol ? Symbol : Symbols.GetSymbol(OtherSymbolName);
grid.AddChild(new TextBlock { Text = "Symbol Info", Style = style, HorizontalAlignment = HorizontalAlignment.Center }, 0, 0, 1, 2);
grid.AddChild(new TextBlock { Text = "Time Till Open", Style = style }, 1, 0);
_timeTillOpenTextBlock = new TextBlock { Text = _symbol.MarketHours.TimeTillOpen().ToString(), Style = style };
grid.AddChild(_timeTillOpenTextBlock, 1, 1);
grid.AddChild(new TextBlock { Text = "Time Till Close", Style = style }, 2, 0);
_timeTillCloseTextBlock = new TextBlock { Text = _symbol.MarketHours.TimeTillClose().ToString(), Style = style };
grid.AddChild(_timeTillCloseTextBlock, 2, 1);
grid.AddChild(new TextBlock { Text = "Is Opened", Style = style }, 3, 0);
_isOpenedTextBlock = new TextBlock { Text = _symbol.MarketHours.IsOpened().ToString(), Style = style };
grid.AddChild(_isOpenedTextBlock, 3, 1);
grid.AddChild(new TextBlock { Text = "Trading Sessions #", Style = style }, 4, 0);
grid.AddChild(new TextBlock { Text = _symbol.MarketHours.Sessions.Count.ToString(), Style = style }, 4, 1);
grid.AddChild(new TextBlock { Text = "Trading Session Week Days", Style = style }, 5, 0);
var weekDays = string.Empty;
for (var iSession = 0; iSession < _symbol.MarketHours.Sessions.Count; iSession++)
{
var currentSessionWeekDays = string.Format("{0}({1})-{2}({3})", _symbol.MarketHours.Sessions[iSession].StartDay, _symbol.MarketHours.Sessions[iSession].StartTime, _symbol.MarketHours.Sessions[iSession].EndDay, _symbol.MarketHours.Sessions[iSession].EndTime);
weekDays = iSession == 0 ? currentSessionWeekDays : string.Format("{0}, {1}", weekDays, currentSessionWeekDays);
}
grid.AddChild(new TextBlock { Text = weekDays, Style = style }, 5, 1);
Chart.AddControl(grid);
Timer.Start(TimeSpan.FromSeconds(1));
}
protected override void OnTimer()
{
_timeTillOpenTextBlock.Text = _symbol.MarketHours.TimeTillOpen().ToString();
_timeTillCloseTextBlock.Text = _symbol.MarketHours.TimeTillClose().ToString();
_isOpenedTextBlock.Text = _symbol.MarketHours.IsOpened().ToString();
}
public override void Calculate(int index)
{
}
}
}
import clr
clr.AddReference("cAlgo.API")
from cAlgo.API import *
class TradingSessionSample():
def initialize(self):
grid = Grid(6, 2)
grid.BackgroundColor = Color.Gold
grid.Opacity = 0.6
grid.HorizontalAlignment = HorizontalAlignment.Center
grid.VerticalAlignment = VerticalAlignment.Center
self.style = Style()
self.style.Set(ControlProperty.Padding, 1)
self.style.Set(ControlProperty.Margin, Thickness(2))
self.style.Set(ControlProperty.BackgroundColor, Color.Black)
self.style.Set(ControlProperty.FontSize, 8)
self.symbol = api.Symbol if api.UseCurrentSymbol else api.Symbols.GetSymbol(api.OtherSymbolName)
grid.AddChild(self.get_text_block("Symbol Info", HorizontalAlignment.Center), 0, 0, 1, 2)
grid.AddChild(self.get_text_block("Time Till Open"), 1, 0)
self.timeTillOpenTextBlock = self.get_text_block(str(self.symbol.MarketHours.TimeTillOpen()))
grid.AddChild(self.timeTillOpenTextBlock, 1, 1)
grid.AddChild(self.get_text_block("Time Till Close"), 2, 0)
self.timeTillCloseTextBlock = self.get_text_block(str(self.symbol.MarketHours.TimeTillClose()))
grid.AddChild(self.timeTillCloseTextBlock, 2, 1)
grid.AddChild(self.get_text_block("Is Opened"), 3, 0)
self.isOpenedTextBlock = self.get_text_block(str(self.symbol.MarketHours.IsOpened()))
grid.AddChild(self.isOpenedTextBlock, 3, 1)
grid.AddChild(self.get_text_block("Trading Sessions #"), 4, 0)
grid.AddChild(self.get_text_block(str(self.symbol.MarketHours.Sessions.Count)), 4, 1)
grid.AddChild(self.get_text_block("Trading Session Week Days"), 5, 0)
weekDays = ""
for iSession in range(self.symbol.MarketHours.Sessions.Count):
currentSessionWeekDays = f"{self.symbol.MarketHours.Sessions[iSession].StartDay}({self.symbol.MarketHours.Sessions[iSession].StartTime})-{self.symbol.MarketHours.Sessions[iSession].EndDay}({self.symbol.MarketHours.Sessions[iSession].EndTime})"
weekDays = currentSessionWeekDays if iSession == 0 else f"{weekDays}, {currentSessionWeekDays}"
grid.AddChild(self.get_text_block(weekDays), 5, 1)
api.Chart.AddControl(grid)
api.Timer.Start(1)
def on_timer(self):
self.timeTillOpenTextBlock.Text = str(self.symbol.MarketHours.TimeTillOpen())
self.timeTillCloseTextBlock.Text = str(self.symbol.MarketHours.TimeTillClose())
self.isOpenedTextBlock.Text = str(self.symbol.MarketHours.IsOpened())
def get_text_block(self, text, horizontalAlignment = None):
textBlock = TextBlock()
textBlock.Text = text
textBlock.Style = self.style
if horizontalAlignment is not None:
textBlock.HorizontalAlignment = horizontalAlignment
return textBlock
{
"AccessRights": "None",
"TimeZone": "UTC",
"IsOverlay": true,
"Parameters": [
{
"Name": "UseCurrentSymbol",
"DisplayName": "Use Current Symbol",
"Type": "Boolean",
"DefaultValue": true
},
{
"Name": "OtherSymbolName",
"DisplayName": "Other Symbol Name",
"Type": "String",
"DefaultValue": "GBPUSD"
}
]
}
Properties
| Name | Description |
|---|---|
EndDayDesktopCLICloudDayOfWeek | Day of week when trading session ends |
EndTimeDesktopCLICloudTimeSpan | Time when trading session ends |
StartDayDesktopCLICloudDayOfWeek | Day of week when trading session starts |
StartTimeDesktopCLICloudTimeSpan | Time when trading session starts |