Support Board
Date/Time: Fri, 31 Jan 2025 20:44:32 +0000
[Programming Help] - Calculation triggered by Time Value == comparison continues calculating after Time Values !=
View Count: 1033
[2019-06-01 03:21:38] |
Tom Hanks - Posts: 20 |
Hello friends, So I am triggering a calculation when two time values are equal. The first value I have defined as: int RangeCloseTime = HMS_TIME(7, 1, 0); time value 2: int CurrentTime = sc.BaseDateTimeIn[sc.Index].GetTime(); Calculation: if (RangeCloseTime == CurrentTime) { SCString actualtime; actualtime.Format("actualtime is: %i", CurrentTime); sc.AddMessageToLog(actualtime, 0); SCString Rangetime; Rangetime.Format("RangeClosetime is: %i", RangeCloseTime); sc.AddMessageToLog(Rangetime, 0); CurrentTime = 0; } So I thought that the current time, defined with sc.BaseDateTimeIn[sc.Index].GetTime(), should only equal the constructed time once per day and as a result only calculate once per day. It is calculating way more than that. The messages are just for confirmation of the readings. The multiple recalculations at the same time are messing up my trade logic. How can I make this calculation only happen once, when the constructed time = the time of the current day? My only guess as to why this is happening is that it is recalculating multiple times within the same second and it is not making a comparison down to the millisecond level. I've been stuck on this for a long time, would you mind steering me towards victory? |
[2019-06-01 09:15:40] |
User907968 - Posts: 825 |
Hi, You could use a persistent variable to store a flag that is set when the calculation has been run. sc.GetPersistentInt() for example - int& triggerFlag = sc.GetPersistentInt(1); if (sc.IsFullRecalculation && sc.Index == 0) triggerFlag = 0; // set flag to 0 during initialization int RangeCloseTime = HMS_TIME(7, 1, 0); Time_Value: int CurrentTime = sc.BaseDateTimeIn[sc.Index].GetTime(); Calculation: if (RangeCloseTime == CurrentTime && triggerFlag == 0) { triggerFlag = 1; // set during calculation SCString actualtime; actualtime.Format("actualtime is: %i", CurrentTime); sc.AddMessageToLog(actualtime, 0); SCString Rangetime; Rangetime.Format("RangeClosetime is: %i", RangeCloseTime); sc.AddMessageToLog(Rangetime, 0); CurrentTime = 0; } if (triggerFlag == 1 && RangeCloseTime != CurrentTime) triggerFlag = 0; // reset flag to 0 once times are no longer equal |
[2019-06-01 20:24:16] |
Tom Hanks - Posts: 20 |
Thanks for the suggestion, but unfortunately that doesn't work. I tested to make sure. The problem is that RangeCloseTime == CurrentTime is staying true long enough to cause multiple message log messages in the same second (maybe longer than a second, not sure). Here is portion of the log messages so you can see what I mean: Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeLowPrice is: 1492.699951 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | actualtime is: 25260 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeClosetime is: 25260 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeHighPrice is: 1493.299927 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeLowPrice is: 1492.699951 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | actualtime is: 25260 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeClosetime is: 25260 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeHighPrice is: 1493.299927 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeLowPrice is: 1492.699951 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | actualtime is: 25260 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeClosetime is: 25260 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeHighPrice is: 1493.299927 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeLowPrice is: 1492.699951 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | actualtime is: 25260 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeClosetime is: 25260 | 2019-06-01 14:21:37 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeHighPrice is: 1493.299927 | 2019-06-01 14:21:37 Date Time Of Last Edit: 2019-06-01 20:33:32
|
[2019-06-01 20:47:52] |
Tom Hanks - Posts: 20 |
Working with the SCDateTime Variables and Values: Time Values "Time values are integer (int) values representing the number of seconds since midnight (00:00)." "You can construct a Time Value from Hour, Minute, Second components by using the HMS_TIME() function" "To compare the time components of two different SCDateTime variables, get the time values from them by using the GetTime() function and compare those time values." So based on the above I am doing this correctly. It seems that there is a caveat I'm not discovering when it comes to comparing the equality of time values. |
[2019-06-01 21:19:19] |
User907968 - Posts: 825 |
Hi, Sorry there is an error. Remove the following line from inside the calculation code block - 'CurrentTime = 0' This is causing the triggerFlag to be reset immediately as - (triggerFlag == 1 && RangeCloseTime != CurrentTime) will evaluate to true when CurrentTime = 0 I understand that RangeCloseTime == CurrentTime can be true for multiple calls to the function, but if the flag is being set and retained correctly then the following will not evaluate to true and the calculation block will not execute - (RangeCloseTime == CurrentTime && triggerFlag == 0) |
[2019-06-01 21:48:22] |
Tom Hanks - Posts: 20 |
I was meaning to remove that CurrentTime = 0 part and forgot about it. Thanks a ton, that worked! Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeHighPrice is: 1525.400024 | 2019-06-01 15:42:27 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeLowPrice is: 1524.900024 | 2019-06-01 15:42:27 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | actualtime is: 25260 | 2019-06-01 15:42:27 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeClosetime is: 25260 | 2019-06-01 15:42:27 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeHighPrice is: 1536.400024 | 2019-06-01 15:42:28 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeLowPrice is: 1535.599976 | 2019-06-01 15:42:28 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | actualtime is: 25260 | 2019-06-01 15:42:28 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeClosetime is: 25260 | 2019-06-01 15:42:28 Chart: Replay 15360X: RTYM19 [CB] 1 Min #1 | Study: Opening Range | RangeHighPrice is: 1541.400024 | 2019-06-01 15:42:29 |
To post a message in this thread, you need to log in with your Sierra Chart account: