Support Board
Date/Time: Sat, 01 Mar 2025 01:29:59 +0000
Executing orders at bar close
View Count: 1786
[2020-12-18 17:16:11] |
User820318 - Posts: 40 |
Dear SC Engineering team I was recently told that using the below function to execute orders at the bar close is not best practise sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED
However there are several examples in the TradingSystem.cpp in the ACS_Source folder that use this precise logic to enter at the close. This documentation suggests a different method (ACSIL Programming Concepts: Determining if Last Chart Bar is Closed). Is it possible to provide an explicit example? Thank you for your time. Date Time Of Last Edit: 2020-12-18 17:16:34
|
[2020-12-19 08:34:28] |
User820318 - Posts: 40 |
Still trying to get to the bottom of this. Would I be correct in assuming that by calling the following condition in my trade logic sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED
the system will be executing at the open of a bar which means that a new tick has to be registered for the new bar to open? If I want to execute the order at the close of the time based bar irrespective of whether a new tick comes in to open the next bar then I would need to follow the instructions here (ACSIL Programming Concepts: Determining if Last Chart Bar is Closed). I would greatly appreciate a response as to whether the above interpretation is correct. |
[2020-12-19 15:07:49] |
User907968 - Posts: 833 |
I would greatly appreciate a response as to whether the above interpretation is correct.
This is explained here - ACSIL Interface Members - Functions: sc.GetBarHasClosedStatus()Specifically this paragraph: The very last bar in the chart is never considered a closed bar until there is a new bar added to the chart. It is not possible to know otherwise because of the following reasons: The chart bars are based upon a variable timeframe like Number of Trades or Volume and the ending can never be known until there is a new bar, or because there is not a trade at the very final second of a fixed time bar.
Is it possible to provide an explicit example?
You could try: n_ACSIL::s_BarPeriod barPeriod; sc.GetBarPeriodParameters(barPeriod); int barClosed = 0; if (barPeriod.ChartDataType == INTRADAY_DATA && barPeriod.IntradayChartBarPeriodType == IBPT_DAYS_MINS_SECS) { int secondsPerBar = barPeriod.IntradayChartBarPeriodParameter1; SCDateTime barDateTime = sc.BaseDateTimeIn[sc.Index]; barDateTime.AddSeconds(secondsPerBar); if (sc.GetCurrentDateTime() >= barDateTime) barClosed = 1; } if (barClosed) { /// do some stuff if the bar is closed } else { /// do some other stuff if the bar is not closed } |
[2020-12-19 16:40:48] |
User820318 - Posts: 40 |
Many thanks 907968. I havent tested your code but will do now. I ended up writing the below which seems to work. I put all the variables to subgraphs so I could see then update in the spreadsheet and it seems to do the trick. I went down the path of converting various time variables to integers and working with those. Code pasted below for reference - it will draw a line at when there are x (defined in settings) seconds remaining until the bar close. Obvious the stud itself is useless but general idea might serve useful for executingf commands at or before (defined by offset) candle close. #include "sierrachart.h"
SCDLLName("BarTime") SCSFExport scsf_BarTime(SCStudyInterfaceRef sc) { // Inputs SCInputRef CloseOffset = sc.Input[0]; // Outputs SCSubgraphRef BarTime = sc.Subgraph[0]; SCSubgraphRef BarStartTime = sc.Subgraph[1]; SCSubgraphRef CurrentTime = sc.Subgraph[2]; SCSubgraphRef CloseSig = sc.Subgraph[3]; if (sc.SetDefaults) { sc.StudyVersion = 20201219; sc.GraphName = "Bar Time"; sc.AutoLoop = 1; //Automatic looping is enabled. sc.GraphRegion = 0; sc.UpdateAlways = 1; // Set this study to continuously update // Inputs CloseOffset.Name = "Offset from Close (secs)"; CloseOffset.SetInt(1); CloseOffset.SetDescription("CloseOffset"); // Outputs BarTime.Name = "BarTime"; BarTime.DrawStyle = DRAWSTYLE_HIDDEN; BarStartTime.Name = "BarStartTime"; BarStartTime.DrawStyle = DRAWSTYLE_HIDDEN; CurrentTime.Name = "CurrentTime"; CurrentTime.DrawStyle = DRAWSTYLE_HIDDEN; CloseSig.Name = "Signal Bar Close"; CloseSig.DrawStyle = DRAWSTYLE_DASH; CloseSig.PrimaryColor = RGB(221, 160, 221); CloseSig.LineWidth = 2; CloseSig.DrawZeros = false; return; } // Section 2 - Do data processing here // Declare variables int& SecondsPerBar = sc.GetPersistentInt(1); int& NowTime = sc.GetPersistentInt(2); int& BarStart = sc.GetPersistentInt(3); SCDateTime CurrentDateTime; // Price Data at Index float Cl = sc.Close[sc.Index]; // Get the DateTime at the current index. SCDateTime BarDateTime = sc.BaseDateTimeIn[sc.Index]; BarStart = BarDateTime.GetTimeInSeconds(); BarStartTime[sc.Index] = BarStart; // to subgraph // Get bar period n_ACSIL::s_BarPeriod BarPeriod; sc.GetBarPeriodParameters(BarPeriod); if (BarPeriod.ChartDataType == INTRADAY_DATA && BarPeriod.IntradayChartBarPeriodType == IBPT_DAYS_MINS_SECS) { SecondsPerBar = BarPeriod.IntradayChartBarPeriodParameter1; } BarTime[sc.Index] = SecondsPerBar; // to subgraph // Get current time if (sc.IsReplayRunning()) CurrentDateTime = sc.CurrentDateTimeForReplay; else CurrentDateTime = sc.CurrentSystemDateTime; NowTime = CurrentDateTime.GetTimeInSeconds(); CurrentTime[sc.Index] = NowTime; // to subgraph if (NowTime >= BarStart + SecondsPerBar - CloseOffset.GetInt()) { CloseSig[sc.Index] = Cl; } } Date Time Of Last Edit: 2020-12-19 16:44:01
|
[2020-12-20 00:01:03] |
|
We know what you are looking for and this ACSIL function will be in the next release: sc.IsLastBarClosedBasedOnElapsedTime(const int ChartNumber) Sierra Chart Support - Engineering Level Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy: https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service: Sierra Chart Teton Futures Order Routing |
[2020-12-20 09:25:17] |
User820318 - Posts: 40 |
You guys are the best! Thank you.
|
[2021-09-13 22:39:56] |
jmt816 - Posts: 45 |
is the new function (IsLastBarClosedBasedOnElapsedTime) working? Can't find any mention of it other than in the release notes and this thread, and I have not been able to get anything other than a zero for the return value. That was in replay, in live mode it did work. Is there any way to get this working in replay mode? quick code snippet (in case I'm doing something wrong): int IsBarClosed = sc.IsLastBarClosedBasedOnElapsedTime (sc.ChartNumber);
if (! IsBarClosed ) { return; } Thanks, Jeff Date Time Of Last Edit: 2021-09-13 22:48:01
|
To post a message in this thread, you need to log in with your Sierra Chart account: