Help on Symbol.MarketHours data to close positions on last bar of Friday evening.
Created at 28 Mar 2021, 18:52
CA
Help on Symbol.MarketHours data to close positions on last bar of Friday evening.
28 Mar 2021, 18:52
Hello,
My initial goal is to make a cBot close all positions on the last bar on a Friday(1h timeframe). So I thought I will get the information from the Symbol.MarketHours object.
Apparently, I miss some piece of information to make things work perfectly, see below:
On the Log tab, I have the output of the corresponding properties of the class.
- Everything regarding timeframe (inside the application), is set to UTC +0 (Timezone on bot and user offset)
- The end time from the last session says that ends at 20:54:59
- The time left for the market to close is 59 minutes
- These messages are printed on 19:55 (so the math add up)
- However, as I hover my mouse on the chart, I can see that there is another candle(shown with red arrow), with opening time of 21:00 (see bottom left corner), that is the actual last candle of the market on Friday, before closing for the weekend
- What is the correct way to piece all the information together?
Many thanks!
Replies
amusleh
29 Mar 2021, 12:27
This sample might help you:
using cAlgo.API;
using System;
using System.Globalization;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class CloseAllOnMarketCloseSample : Robot
{
private TimeSpan _closeBeforeTime;
[Parameter("Close Before", DefaultValue = "00:05:00")]
public string CloseBeforeTime { get; set; }
protected override void OnStart()
{
if (!TimeSpan.TryParse(CloseBeforeTime, CultureInfo.InvariantCulture, out _closeBeforeTime))
{
Print("You have provided invalid value for Close Before parameter");
Stop();
}
Timer.Start(1);
}
protected override void OnTimer()
{
var timeTillClose = Symbol.MarketHours.TimeTillClose();
if (!Symbol.MarketHours.IsOpened() || timeTillClose > _closeBeforeTime) return;
foreach (var position in Positions)
{
ClosePosition(position);
}
}
}
}
@amusleh
PanagiotisCharalampous
29 Mar 2021, 10:13
Hi cAlgoBuddy,
Please note that Symbol.MarketHours provides the current market hours for the symbol and not historical information. Therefore there could be discrepancies in backtesting due to daylight saving time differences.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous