Login Page - Create Account

Support Board


Date/Time: Mon, 20 May 2024 17:32:32 +0000



[User Discussion] - reseting calculation every day

View Count: 901

[2021-02-22 18:54:07]
User30743 - Posts: 364
How do I reset calculations to start from the first bar of each day (at 8:30)?

I have a price action pattern that i want to track, the problem is that it takes into account also bars BEFORE the start of day - I don't want that. I want to make it start at 8:30 and finish and 14:00

I have this piece of code to get first bar of a day


SCDateTime TradingDayStartDateTime = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.IndexOfLastVisibleBar]);
SCString dateTime = sc.FormatDateTime(TradingDayStartDateTime).GetChars();
auto StartIndex = sc.GetFirstIndexForDate(sc.ChartNumber, TradingDayStartDateTime.GetDate());

then i have som code that actually defines the pattern (it is made of three candles. but now I don't know how to tell: "ok, start searching for it from the StartIndex"

can anyone give me a hint, it must be really trivial i guess, but i just don't know..
Date Time Of Last Edit: 2021-02-22 18:55:56
[2021-02-23 03:02:23]
Flipper_2 - Posts: 57
Normally you would track in a Persistent variable the value you are reseting every day, for example the daily High. On the start of the day clear and reset it.


SCDateTime TradingDayStartDateTime = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.IndexOfLastVisibleBar]);
SCString dateTime = sc.FormatDateTime(TradingDayStartDateTime).GetChars();
auto StartIndex = sc.GetFirstIndexForDate(sc.ChartNumber, TradingDayStartDateTime.GetDate());


float& DlyHigh = sc.GetPersistentFloat(0);

// Day re-starts on this Bar so reset your variables in here
if ( StartIndex == sc.Index) {
DlyHigh = sc.BaseDataIn[SC_HIGH][sc.Index]; // or whatever value you want to reset to like = 0
}
else if(sc.High[sc.Index] > DlyHigh){
DlyHigh = sc.High[sc.Index];
}

Date Time Of Last Edit: 2021-02-23 03:04:45
[2021-02-23 20:12:43]
User30743 - Posts: 364
no, i dont hold any persist vars. i keep track of a candlestick pattern, for simplicity lets say it is a pattern where each bar has higher high then previous

sc.High[sc.Index] > sc.High[sc.Index - 1] && sc.High[sc.Index - 1] > sc.High[sc.Index - 2]

i start trading at 8:30 so I don't want the consider the bars just before the start obviously. how do i do it? i believed something like this, but it does not work..

if (StartIndex == sc.Index) sc.Index = 0;

here is a simple study applied on the chart, marking the rising bars and showing the problem - https://prnt.sc/1051rev - the first two bars after 8:30 should not be blue of course

this is the code for it


SCDateTime TradingDayStartDateTime = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.IndexOfLastVisibleBar]);
SCString s = sc.FormatDateTime(TradingDayStartDateTime).GetChars();
auto StartIndex = sc.GetFirstIndexForDate(sc.ChartNumber, TradingDayStartDateTime.GetDate());

if (StartIndex == sc.Index) sc.Index = 0;

if (sc.High[sc.Index] > sc.High[sc.Index - 1] && sc.High[sc.Index - 1] > sc.High[sc.Index - 2]){
Subgraph_IB[sc.Index] = sc.Index;
Subgraph_IB[sc.Index-1] = sc.Index;
Subgraph_IB[sc.Index-2] = sc.Index;
}

Date Time Of Last Edit: 2021-02-23 20:20:34
[2021-02-23 21:09:24]
Flipper_2 - Posts: 57
if (StartIndex == sc.Index) sc.Index = 0;

This isn't correct. Its saying if the current bar is the start of day set the current bar Index to 0! The bar indexes are read only!


no, i dont hold any persist vars. i keep track of a candlestick pattern

Well you have to. If you want the condition to reset each day you have to have something to re-set. Some sort of bool condition.If you only want the condition to be true once a day its pretty simple with a version of the pattern I suggested above.

int& OncePerDay = sc.GetPersistentInt(0);


if ( StartIndex == sc.Index) {
OncePerDay = 0; // this re-set the Persistent int at the start of each day.
}

if (OncePerDay == 0 && sc.High[sc.Index] > sc.High[sc.Index - 1] && sc.High[sc.Index - 1] > sc.High[sc.Index - 2] ){
Subgraph_IB[sc.Index] = sc.Index;
Subgraph_IB[sc.Index-1] = sc.Index;
Subgraph_IB[sc.Index-2] = sc.Index;
OncePerDay = 1; // Now you have reset the Persistent varrible to 1 so this code block will not trigger until the next day
}

Date Time Of Last Edit: 2021-02-23 21:21:09
[2021-02-23 21:33:25]
User907968 - Posts: 804
No need to over-complicate this, and you don't have to use a persistent variable (unless you really want to).


SCDateTime TradingDayStartDateTime = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.IndexOfLastVisibleBar]);
SCString s = sc.FormatDateTime(TradingDayStartDateTime).GetChars();
auto StartIndex = sc.GetFirstIndexForDate(sc.ChartNumber, TradingDayStartDateTime.GetDate());

if (sc.Index == startIndex)
{
// perform reset actions if necessary
}

if (sc.Index >= startIndex)
{
// evaluate bars here
}

I wouldn't personally use the code as you have it above, but I'm saying that without any real context as to what you are doing with your study and its requirements.

An alternative approach for determinining a reset condition based on trading sessions is presented used in "Cumulative Delta Bars - Volume" found in studies8.cpp.
Date Time Of Last Edit: 2021-02-23 21:36:42
[2021-02-23 21:47:21]
Flipper_2 - Posts: 57
if (sc.Index >= startIndex)

But this will continue to evaluate after the condition is true the first time. I believe the idea is to find the FIRST time it happens after the start of day.
[2021-02-23 22:05:27]
User907968 - Posts: 804
I want to make it start at 8:30 and finish and 14:00

Therefore you would need to reset at startIndex (i.e. sc.Index == startIndex) if required, but then also evaluate every bar after the first bar of the day, hence sc.Index >= startIndex. I do not read anywhere about only checking once per day for a patteren, only resetting once per day.

But as I also said, I don't really have any context on how this is being used and for what.
Date Time Of Last Edit: 2021-02-23 22:06:12
[2021-02-23 23:23:32]
Flipper_2 - Posts: 57
I'm guessing thats want the OP wants. Your pattern will mark true for every occurrence - all day. If thats what the OP wants then he he probably doesn't need to reset anything.

My pattern won't check once per day. It will check until its first found then not again. The fact that the OP wants to reset at every day would imply he want the first occurrence.

But anyway will wait for the clarification.
Date Time Of Last Edit: 2021-02-23 23:26:59
[2021-02-24 12:55:19]
User30743 - Posts: 364
no i am not looking for the FIRST occurrence, i am looking for all occurrences, but from the starting times

i believe it is no really clear what the problem is, so i put together a simple study - it opens a long position when there are three consecutive highs.
it trades only in RTH - from 8.30 to 14:00

the point is that it should not consider the bars before open as part of the price action pattern. look at that - https://prnt.sc/105nqgp how is it possible that i opened a trade on the 2. bar? logically, it should not, when i want to consider THREE bars back from the open

this is the code for the study

SCSFExport scsf_TakeTradeOnThirdRisingBar(SCStudyInterfaceRef sc) {
SCSubgraphRef Subgraph_IB = sc.Subgraph[0];
if (sc.SetDefaults) {
sc.GraphName = "Take trade on 3 rising bar";
sc.GraphRegion = 0;
sc.AutoLoop = 1;

sc.Input[0].Name = "Start trading at: ";
sc.Input[0].SetTime(HMS_TIME(8, 30, 0));
sc.Input[1].Name = "Stop trading at: ";
sc.Input[1].SetTime(HMS_TIME(14, 00, 0));
sc.Input[2].Name = "Flat postion at: ";
sc.Input[2].SetTime(HMS_TIME(15, 00, 30));
return;
}

s_SCPositionData pos;
sc.GetTradePosition(pos);
s_SCNewOrder order;
order.OrderQuantity = 1;
order.OrderType = SCT_ORDERTYPE_MARKET;
order.Target1Offset = 10 * sc.TickSize;
order.Stop1Offset = 10 * sc.TickSize;

SCDateTime TradingDayStartDateTime = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.IndexOfLastVisibleBar]);
SCString s = sc.FormatDateTime(TradingDayStartDateTime).GetChars();
auto StartIndex = sc.GetFirstIndexForDate(sc.ChartNumber, TradingDayStartDateTime.GetDate());

bool areTradingHours = sc.BaseDateTimeIn[sc.Index].GetTime() > sc.Input[0].GetTime() && sc.BaseDateTimeIn[sc.Index].GetTime() < sc.Input[1].GetTime();
bool isTimeToFlat = sc.BaseDateTimeIn[sc.Index].GetTime() >= sc.Input[2].GetTime();

bool threeHigherHighs = sc.High[sc.Index] > sc.High[sc.Index - 1] && sc.High[sc.Index - 1] > sc.High[sc.Index - 2];

if (areTradingHours) {
if (sc.Index >= StartIndex) {
if (threeHigherHighs) {
sc.BuyEntry(order);
}
}
}

if (isTimeToFlat) sc.FlattenPosition();
}

this is my time session setting https://prnt.sc/105nx50

would be really glad if someone could explain me the issue
Date Time Of Last Edit: 2021-02-24 13:12:00
[2021-02-24 13:53:52]
User907968 - Posts: 804
SCDateTime TradingDayStartDateTime = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.IndexOfLastVisibleBar]);

This is not a good idea, if you want the start of the current day, replace 'sc.IndexOfLastVisibleBar' with 'sc.ArraySize - 1', otherwise you might change the start date simply by scrolling the chart.

Also, as you are using evening session, 'sc.GetTradingDayStartDateTimeOfBar' will return the bar that corresponds to the globex open, not the cash open.

Take a look at this function - https://www.sierrachart.com/index.php?page=doc/ACSIL_Members_Functions.html#scIsDateTimeInDaySession, it may help.
Date Time Of Last Edit: 2021-02-24 14:00:05
[2021-02-24 17:53:05]
User30743 - Posts: 364
ok
sc.IndexOfLastVisibleBar
can be changed to
sc.Index
or
sc.ArraySize - 1

but it does not make much change anyway
Date Time Of Last Edit: 2021-02-24 17:53:43
[2021-02-24 22:34:39]
Flipper_2 - Posts: 57
shouldn't the start be three bars AFTER the start index then?

if (sc.Index >= StartIndex + 3)

Date Time Of Last Edit: 2021-02-24 22:35:00
[2021-02-25 12:14:32]
User30743 - Posts: 364
yeah, good point..

i have solved it already with a slightly different approach using
sc.IsDateTimeInDaySession(sc.BaseDateTimeIn[sc.Index - 3])

thank you all for the help
Date Time Of Last Edit: 2021-02-25 12:14:49

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account