Support Board
Date/Time: Sat, 23 Nov 2024 08:52:33 +0000
[User Discussion] - Flatten trades at specific time - Market Replay
View Count: 613
[2024-05-28 11:46:46] |
User194332 - Posts: 40 |
Hi I am testing some auto trading using the Alert condition studies but would like to be flat at a specific time (End of Day). I've set the time in the trade panel & Global P&L but still seeing trades run overnight. Is there any other way to flatten at the time specified? This is on Market replay. Regards Avi |
[2024-05-28 12:10:31] |
Sierra_Chart Engineering - Posts: 17145 |
Exactly how are you doing the automated trading? What specific study are you using?
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, use the Teton service: Sierra Chart Teton Futures Order Routing Date Time Of Last Edit: 2024-05-28 13:53:11
|
[2024-05-29 03:51:52] |
User194332 - Posts: 40 |
i'm using a study : trading system based on alert condition and using a formula in the alert condition for cross from below for Longs // Cross from above for shorts. I've set the trading time to be 09:30 - 15:58 and want to be flat for End of day (say 15:58pm est). In the trade panel, I've the the cancel all & flatten to 15:58 but it doesn't appear to flatten during Market Replay. |
[2024-05-29 12:58:58] |
emmanuel - Posts: 57 |
Here's an example, however don't use it blindly. Make sure you understand it well. In short, it means when there's an open position outside of the trading hours, automatically close it. SCDateTime startTime;
SCDateTime endTime; SCDateTime barTime = sc.BaseDateTimeIn[sc.Index].GetTime(); startTime.SetDate(barTime); startTime.SetTimeHMS_MS(9, 30, 0, 0); endTime.SetDate(barTime); endTime.SetTimeHMS_MS(15, 58, 0, 0); // barTime is at the start of the bar, so you may need to adjust the minutes according to the chart's timeframe. bool isTradingSession = barTime >= startTime && barTime < endTime; s_SCPositionData positionData; sc.GetTradePosition(positionData); if(!sc.IsFullRecalculation && !isTradingSession && positionData.PositionQuantity != 0) { sc.FlattenAndCancelAllOrders(); } |
[2024-05-29 17:22:40] |
User194332 - Posts: 40 |
Thanks - I'm not a developer but think I understand the code. Will test it out. Would this sit in a separate study that I have to compile and add to the chart studies ? I'm assuming this would not be on the server side so the Application has to be open and connected. Is this correct? |
[2024-05-29 19:39:02] |
emmanuel - Posts: 57 |
Yes, since you're using a pre-made study, you can use my code example to create a new ASCIL study (which you would compile) and then add the study to the same chart. This is not server-side code, so yes Sierra Chart needs to be running and the study needs to be on an open chart. The chart doesn't have to be visible; Meaning it can be minimized. When back-testing or doing a replay, Sierra Chart doesn't have to be connected for the study to work, since the trades are simulated locally. Obviously for real-time trading Sierra Chart would need to be connected. Regarding real-time trading, note that since this is client-side code, if you get disconnected from the Internet the study will not be able to function. The study doesn't use the computer's local time but rather the date-time of the bar. So if it doesn't receive market data, it won't do anything. It wouldn't be able to flatten trades anyway. Once reconnected to the Internet the study will "catch up" and start working. |
[2024-06-09 13:16:28] |
User194332 - Posts: 40 |
Thanks Emmanuel And just for my understanding, why does the Cancel & Flatten at a set time in the trade Panel not work? Is this a only on Replay or live as well? Regards Avi |
[2024-06-09 13:30:00] |
emmanuel - Posts: 57 |
The "Cancel & Flatten at a set time" only works during real-time trading. It doesn't work for replays nor back-tests. See Basic Trading and the Trade Window: A >> Flatten And Cancel at Set Time
Date Time Of Last Edit: 2024-06-09 13:30:15
|
[2024-06-13 03:16:16] |
User61168 - Posts: 403 |
This is SC delivered solution to flatten trades at a specific time. Works great in replay also :- Edit: In the Alert box, just put ID7.SG1<>0 where id7 is position qty Date Time Of Last Edit: 2024-06-13 03:17:37
|
Screenshot 2024-06-12 201454.png / V - Attached On 2024-06-13 03:15:18 UTC - Size: 111.88 KB - 104 views |
[2024-09-08 16:06:03] |
User509533 - Posts: 57 |
In the custom study/DLL provided by Sierra Chart (SierraChartStudies_64.dll), I am unable to locate the $$Flatten-OnTick-TimeStop study as mentioned in the last post. Can you help me understand what I might be doing wrong? This is SC delivered solution to flatten trades at a specific time. Works great in replay also :-
Edit: In the Alert box, just put ID7.SG1<>0 where id7 is position qty Date Time Of Last Edit: 2024-09-08 17:41:10
|
[2024-09-08 18:47:04] |
User61168 - Posts: 403 |
Hi, Add the study mentioned here>>> Trading System Based on Alert Condition The SC document is not updated. This study now supports buy entry, but exit, sell entry, sell exit AND FLATTEN. Select Flatten, specify what time duration you want to flatten and that’s about all that is required.simple and elegant one simple alert study to code all the flatten/closeAll needs. |
[2024-09-08 22:31:23] |
User509533 - Posts: 57 |
Thank you for your assistance! However, the issue persists— the trade is still not flattening after 3 PM CST during the back-test. I'm unsure what the correct alert condition would be in this case. Additionally, the condition ID7.SG1<>0 is returning an error. Hi,
Add the study mentioned here>>> Trading System Based on Alert Condition The SC document is not updated. This study now supports buy entry, but exit, sell entry, sell exit AND FLATTEN. Select Flatten, specify what time duration you want to flatten and that’s about all that is required.simple and elegant one simple alert study to code all the flatten/closeAll needs. |
[2024-09-09 01:29:34] |
User61168 - Posts: 403 |
1) Time duration is set incorrectly in the screenshot. It should be 15:00 16:00 assuming the timeframe on your chart is set to central time zone. 2) Replace ID7.SG1 with whatever you have for “Trading:position quantity” study. 3) Make sure you have data in your chart atleast a minute after 13:00 4) if you only trade manually then please be careful with these two settings as it is required to enable Trade>Autotrading enabled global and chart. Both should be checked 5) trade > Trade simulation mode on is checked 6) trade > trading locked is unchecked Always ensure #5 is checked. |
To post a message in this thread, you need to log in with your Sierra Chart account: