Login Page - Create Account

Support Board


Date/Time: Sun, 24 Nov 2024 15:11:20 +0000



[Programming Help] - How to measure current Trade time Duration via simple alert formula?

View Count: 887

[2023-01-12 08:08:41]
User61168 - Posts: 403
Hello everyone,

Is there a way to measure the trade duration of each open trade? I am not using a time chart. There is Trading:Profit/Loss Text study which displays current trade time duration (ln:21) but I haven't found a way to access these "ln" values in simple alert color bar condition study.
I want to use this as a time stop once the trade is open but stalled and not going into profit target.
I would appreciate any assistance offered.
Date Time Of Last Edit: 2023-01-12 08:10:37
[2023-01-12 08:31:17]
User183724 - Posts: 183
have you tried a Spreadsheet Study? Might try adding one to your chart and then checking out column A which is usually bar time. If it turns out to be what youre looking for, you could write a formula in one of the cells much like ... but not exactly like... a color bar based on alert study. you can get color bars out of a spreadsheet... anyways, thats what id try first... then theres always ACSIL. you can do just about anything you can think up with it.
[2023-01-12 08:41:21]
User61168 - Posts: 403
Thanks for your response. I am a complete noob in spreadsheet studies. Sounds like you are referring to bar time column which is not what I am looking for. Since I am using range bar chart type, each BARTIME at open and close will vary. The clock (or a counter) needs to start the moment I open a trade.

I wish there was a delivered study just for this (and also for MFE and MAE for each trade). Could someone could write a ASCIL code and then publish it to sierra chart to share with their user base? It would be a pretty significant contribution imo.
[2023-01-12 13:16:47]
Sawtooth - Posts: 4118
The Trading:Profig/Loss Text study does not output any of its values to subgraphs, where they could be used for alerts or analysis.

The Spreadsheet System for Trading study shows the timestamp of the latest trade entry, in cell J42.
You could compare this to the Last Bar End DateTime in cell J41, by subtracting J42 from J41.

J41 and J42 update, even with manual entries.
You could use cell J29 to flatten a position after 5 minutes, even a manually entered one, with a formula like this:
=AND(J8<>0,J41-J42 >= 5/1440)
(there are 1440 minutes in 24hrs)

You could display the value in seconds or minutes with the Text Display for Study study, using a formula in a Formula Column.
Date Time Of Last Edit: 2024-05-31 13:05:55
[2023-01-12 14:41:55]
User61168 - Posts: 403
Thanks Tom. I really appreciate sharing your knowledge. I plan to eventually move to spreadsheet system for trading once the strategy works in the live trading environment. Do you think spreadsheet trading will let me calculate MAE and MFE values also for each open trade? I could not find a good solution to this via simple alerts as I could not find a good way to look back into bar history and check when the trade was open (and also because simple alerts does not allow us to do a MAX(H:H[-nn]) array type calculation).
[2023-01-12 14:55:35]
Sawtooth - Posts: 4118
Do you think spreadsheet trading will let me calculate MAE and MFE values also for each open trade?
Yes, this can be done, using the signal bar as the start.
You could also use the Last Entry Fill Price in J48, but it is only the most recent entry, so there would not be any historical values.
However, it would not be possible to create a table of MAE and MFE values also for each open trade, only values or lines on the chart from each entry.

I plan to eventually move to spreadsheet system for trading once the strategy works in the live trading environment.
Don't let the spreadsheet study intimidate you. There's really no reason to wait to use it, since you have already exceeded what Alert syntax can do.
It's just a matter of using a similar but different syntax, and understanding a few basic things about the Spreadsheet System for Trading study format:
- Spreadsheet row 3 is the current bar, and earlier bars are in rows below. All formulas from columns K-Z are entered in row 3.
- Columns AA and above are the outputs of added studies.
- Columns K,L,M,N are the Buy Entry, Buy Exit, Sell Entry, Sell Exit columns, respectively.
- Columns O-Z are for intermediate formulas, as needed.
- Alert syntax ID1.SG1[0] is ID1.SG1@3 in spreadsheet syntax.
- Alert syntax ID1.SG1[-1] is ID1.SG1@4 in spreadsheet syntax, etc.
Date Time Of Last Edit: 2023-01-12 15:04:43
[2023-01-12 16:44:59]
User61168 - Posts: 403
Thanks Tom. I appreciate the encouragement. I am ready to go live next week. Account has been opened with Ironbeam/Teton. So new broker, new order routing is already a significant change to go live with a strategy that I have not forwarded tested. Hence taking it a bit slow and easy until I gain some confidence. This is also my first exposure to running a automated strategy in the futures market so God knows what other obstacles I run into. Hence a bit anxious to see how this journey plays out.

I have developed over 100+ study collections over last 2+ yrs and have shortlisted about 8-9 strategies that have the potential to go live. They are all based on NQ NQ but learning yesterday that I can trade MNQ based on NQ chart, I could in essence go live with all my best performing algos running in parallel in each sub-account.

p.s. Also, the lack of SC support towards automated trading is another major consideration to progress slowly and cautiously.
[2024-05-31 12:39:34]
TraderBiku - Posts: 36
Hello, I am trying to do something similar to what you proposed Sawtooth, trying to flatten a position after X amount of time in Spreadsheets - however, when I subtract J41-J42, I get a very small fractional value, which if I multiply it by 100000, gives me something like a seconds timer, but isn't actually the same as seconds (ie it runs faster than clock time). It does appear to run at a consistent interval though, so perhaps this is enough for my needs - but it does seem a bit strange :)

Is there something funky happening because the date and time are combined together in J41/J42? Is there a way (or a need) to isolate just the time component of what's in those cells? Thanks very much!
[2024-05-31 13:43:35]
Sawtooth - Posts: 4118
when I subtract J41-J42, I get a very small fractional value
The actual value is not important because they are SerialDateTime values and can be very small.
For example:
1 second = 1/86400 = 0.000011574074074074073
1 minute = 1/1440 = 0.0006944444444444445
1 hour = 1/24 = 0.041666666666666664
Spreadsheet Example Formulas and Usage: Using Date Time

So you are comparing a very small decimal value to another very small decimal value, e.g.:
=AND(J8<>0,J41-J42 >= 5/1440)

Is there a way (or a need) to isolate just the time component of what's in those cells?
There is no need, but you could if you want to:
=AND(J8<>0,FRACTIME(J41)-FRACTIME(J42) >= 5/1440)
Edit: You don't want to if your trade duration crosses midnight.
Date Time Of Last Edit: 2024-05-31 16:25:57
[2024-05-31 15:44:03]
TraderBiku - Posts: 36
Gotcha - that it explains it. Many thanks!
[2024-06-02 07:26:30]
User61168 - Posts: 403
I would appreciate if you could post a working version of this study.

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

Login

Login Page - Create Account