Backtesting & TSYSI Questions

Login or Register

Please login or register in order to view and post in the Forum.

21 posts / 0 new
Last post
Mickster
Last seen: 2 years 11 months ago
Joined: 06/29/2018 - 17:47
Backtesting & TSYSI Questions

I have a strategy in development and have some questions about what I am seeing when applying the TSYSI to a 5 min chart of the ES.

The attached chart shows 4 trades. The first two trades occurred back to back (trade 2 entry happens on same bar as trade 1 exit). The trade box encompasses both trades instead of separating into two boxes. Is this normal behavior based on the fact that entry and exit are on same bar?

In trade 1, the stop and target lines are not drawn at the correct levels. Why is this?

In trade 4, the target lines are not contiguous (ie, at the same level throughout the trade).

How can I prevent entry and exit on same bar?

Thanks.

0
cpayne
Last seen: 1 year 3 weeks ago
Joined: 03/30/2009 - 00:00
Entry and Exit - Same Bar

Yes, the TSYS indicator can only see the state at end of each bar (when drawing historically).  So it seems that it was long, and stayed long (even though it actually exited and re-entered on same bar).  So it just displays that as still long and still part of same trade. 

One way to prevent exit and re-enry on same bar, is to put your entry rules before your exit rules.  However, this opens up entry and then exit on same bar.  To prevent that, add "AND BARSOPEN > 0" to your exit rules (it may need to be BARSOPEN > 1....experiment to get right setting there).

If it shows your target/stop moving, then it sounds like a rule is probably changing it.  I might need to see the system definition to identify problem there.

Mickster
Last seen: 2 years 11 months ago
Joined: 06/29/2018 - 17:47
Setting BARSOPEN > 0 disables

Setting BARSOPEN > 0 disables exit on the entry bar, so provides a minimum of 2 bars for the trade. This works for my strategy.

Regarding exits, when the TSYS indicator is applied to a chart, do the exits (both target and stop) work intrabar? (doesn't wait for the end of bar).

It looks like the autotrading feature can send live orders directly to a GAIN account. Can you comment about the operation of this feature? Any particular issues to be aware of?

Thanks.

cpayne
Last seen: 1 year 3 weeks ago
Joined: 03/30/2009 - 00:00
Intrabar

You will notice a checkbox in the Trading System window titled "Intrabar".  This checkbox is set on a rule by rule basis.  To turn on for a rule, select rule, check checkbox, and then hit Modify.  When Intrabar is checked, it allows TSYSI to trigger actions intrabar.  This is most commonly used on stops or targets.....  If you are long, and the high of the bar meets your target (let's say V#11), then it will ALWAYS meet that target for rest of the bar.  So if your target rule is..

HI >= V#11

then it makes sense to turn on Intrabar and let the actions (trades) fire the moment that high gets to the V#11 target.

Yes, there is quite a bit to be aware of when transitioning from backtesting to live trading.  In general, the live data is stored in tokens that start with TR_. The most important is TR_POS which is the position size.  It's an important variable because it provides the size of the position, but also whether short (negative) or long (positive) or flat (0).  So the critical info comes from that token.  The backtesting equivalent is POS_SIZE and is a theoretical value managed and manipulated by the backtest.

So there has long been a need to somehow marry these two, so that when opening the chart, POS_SIZE is used so the historical trades may be presented visually, but then while live data is flowing in, switch over to using TR_POS.  That need was satisfied in 13.5.5 with the token POSITION.  If you are not on 13.5.5 you can download from www.linnsoft.com/download

But as I said, it should track POS_SIZE when opening the chart so historical trades show up properly, but it should track the actual trade state (with broker) as live trades are flowing in.  With that understanding of the POSITION token, you should have most if not all of what you need to transition to live trading.

Mickster
Last seen: 2 years 11 months ago
Joined: 06/29/2018 - 17:47
Taking a stab at some logic

Taking a stab at some logic to ensure that a minimum reset distance has been met before allowing a trade, but need some help. This code would only work for the first trade of the session, but it is not even working for that first trade. ie, trades are allowed even if the reset distance has not been met.

NO ACTION STATEMENTS
======================
IF (POS_SOD = 1) THEN (SET (V#8, 0)); /* Reset the Reset Distance Flag (V#8) after the first bar of session */
IF (POS_SOD > 1) AND "Reset Level Achieved" THEN (SET (V#8,1)); /* If Reset Distance met, then Set Flag to 1*/
IF (POS_EOD = 1) THEN (SET (V#8,0)); /* Reset Flag to 0 at end of session */

BUY STATEMENT
==============
"Trade Signal Condition" AND V#8 = 1

Can you comment on this code? Thanks.

cpayne
Last seen: 1 year 3 weeks ago
Joined: 03/30/2009 - 00:00
Managing Fill Price, Stops, Targets in Live Trading

Here is a suggestion on how to manage the fill price, as well as set stops and targets relative to the fill price, and manage in a way that will work both in backtesting (and chart loading with TSYS Indicator) as well as in live action/trading.

Above we discussed how the POSITION token now represents both the theoretical system-maintained POS_SIZE token during backtesting (and chart loadinng) but represents TR_POS token during live data flow into a chart that contains the TSYS indicator (which is in turn setup to deliver live trades).

There is currently no such token that handles both for the entry price.  The token ENTRY is used during backtesting (and chart loading), but the token TR_FILL represent the actual fill price during live trading.

In a no-action rule, we will check to see if TR_POS is non-zero and that TR_FILL is non-zero (and maybe check also for a reasonable value) and in that case, use TR_FILL to set our stops and targets.  Otherwise, fall back on ENTRY. 

We will also manage a V#15 variable which will contain the size of our exit order.  Setting that to absolute value of TR_POS, ABS(TR_POS), when in live trading, or setting to 0 otherwise.  By setting to 0 when we can't rely on the live trade to be in place, then the trading order will still trigger, but with a size of zero and therefore have no impact (instead of getting you short unexpectedly).

The following is the no action rule that is the 1st rule in system.

SET(V#7, 10); /* 10 tick stop and target */
IF(TR_POS != 0 AND TR_FILL > 0 AND ABS(TR_FILL - ENTRY) <= 4 * TINC) THEN (SET(V#13, TR_FILL + V#17 * TINC) AND SET(V#14, TR_FILL - V#17 * TINC) AND SET(V#15, ABS(TR_POS)));
IF(TR_POS == 0 OR TR_FILL <= 0 ORABS(TR_FILL - ENTRY) > 4 * TINC) THEN (SET(V#13, ENTRY+ V#17 * TINC) AND SET(V#14, ENTRY- V#17 * TINC) AND SET(V#15, 0));

Now you have V#13 as your target, and V#14 as your stop.  And V#15 as your target and stop size to be used in the trading order.

Your target rule would just be...

HI >= V#13

and get out at V#13. Stop would be...

LO <= V#14

and exit at V#14.

You may also want to add another measure of assurance by checking that TR_FILL is some reasonable distance from the theoretical ENTRY.  Let's say within 4 ticks...

SET(V#7, 10); /* 10 tick stop and target */
IF(TR_POS != 0 AND TR_FILL > 0 AND ABS(TR_FILL - ENTRY) <= 4 * TINC AND ABS(TR_FILL - ENTRY) <= 4 * TINC) THEN (SET(V#13, TR_FILL + V#17 * TINC) AND SET(V#14, TR_FILL - V#17 * TINC) AND SET(V#15, ABS(TR_POS)));
IF(TR_POS == 0 OR TR_FILL <= 0 OR ABS(TR_FILL - ENTRY) > 4 * TINC OR ABS(TR_FILL - ENTRY) > 4 * TINC) THEN (SET(V#13, ENTRY+ V#17 * TINC) AND SET(V#14, ENTRY- V#17 * TINC) AND SET(V#15, 0));

 

cpayne
Last seen: 1 year 3 weeks ago
Joined: 03/30/2009 - 00:00
Errors Corrected In Syntax of Post Above

I have corrected a couple of errors in the original syntax that was shared in post above.  Should work fine now.

l3tsdo1t
Last seen: 3 years 7 months ago
Joined: 10/13/2016 - 15:10
Thank you for updating the

Thank you for updating the code. Unfortunately I am struggling to get the code to work with shorts as well as longs. I have attempted to update the code to in a variety of manners, however the closest I can come is setting V#13 as my short target and V#14 as my long target (which is inverted to how they should be) with the long only code in my Master. This will correctly display the historical data, however it won't execute any trades. When I add in code to represent the short and/or adjust my short stop and target with the correct V# variables the historical data is no longer accurate.

I have attempted adding: IF(TR_POS != 0 AND TR_FILL < 0 AND ABS(TR_FILL - ENTRY) <= 4 * TINC) THEN (SET(V#13, TR_FILL - V#17 * TINC) AND SET(V#3, TR_FILL + V#17 * TINC) AND SET(V#15, ABS(TR_POS)))
while removing
IF(TR_POS == 0 OR TR_FILL <= 0 ORABS(TR_FILL - ENTRY) > 4 * TINC) THEN (SET(V#13, ENTRY+ V#17 * TINC) AND SET(V#14, ENTRY- V#17 * TINC) AND SET(V#15, 0));
among a variety of other changes to the code to no avail.

Any help and insight into this is as always, greatly appreciated.

cpayne
Last seen: 1 year 3 weeks ago
Joined: 03/30/2009 - 00:00
Fill Price, Stops, and Targets for Shorts

I would fall back on the theoretical token POS_SIZE to determine if you are short or long.  I assume you will be going both short and long in the same system.  I'll write the code below so it handles both short and long...  POS_SIZE will be positive when long and negative when short.  So basically I'm going to simply add IF(POS_SIZE > 0) to the lines we already had in place to limit those to SETTING only when long.  Then I'll duplicate those lines and code them for a short side...

SET(V#7, 10); /* 10 tick stop and target */

/* Long */
IF(POS_SIZE > 0 AND TR_POS != 0 AND TR_FILL > 0 AND ABS(TR_FILL - ENTRY) <= 4 * TINC) THEN (SET(V#13, TR_FILL + V#17 * TINC) AND SET(V#14, TR_FILL - V#17 * TINC) AND SET(V#15, ABS(TR_POS)));
IF(POS_SIZE > 0 AND (TR_POS == 0 OR TR_FILL <= 0 OR ABS(TR_FILL - ENTRY) > 4 * TINC)) THEN (SET(V#13, ENTRY+ V#17 * TINC) AND SET(V#14, ENTRY- V#17 * TINC) AND SET(V#15, 0));

/* Short*/
IF(POS_SIZE < 0 AND TR_POS != 0 AND TR_FILL > 0 AND ABS(TR_FILL - ENTRY) <= 4 * TINC) THEN (SET(V#13, TR_FILL - V#17 * TINC) AND SET(V#14, TR_FILL + V#17 * TINC) AND SET(V#15, ABS(TR_POS)));
IF(POS_SIZE < 0 AND (TR_POS == 0 OR TR_FILL <= 0 OR ABS(TR_FILL - ENTRY) > 4 * TINC)) THEN (SET(V#13, ENTRY - V#17 * TINC) AND SET(V#14, ENTRY + V#17 * TINC) AND SET(V#15, 0));

Now, possibly a simpler approach might have been to leave the syntax as it was, but to just use V#13 as target for longs but stop for shorts, and V#14 as stop for longs but target for short (in the remainder of your rules.

l3tsdo1t
Last seen: 3 years 7 months ago
Joined: 10/13/2016 - 15:10
I'll give the new code a try.

I'll give the new code a try. I had originally attempted to use V#13 as my target for longs/stop for shorts and V#14 as my stop for longs/target for shorts, however I wasn't getting fills when executing to a demo account (I assume the platform doesn't differentiate "live" and "demo" accounts) and the execution of what should have been a trade was being closed out prior to reaching either target or stop. The second issue it presented was that one would not be able to set different values for their stops and targets as a V#13 value of 15 and V#14 value of 10 would cause shorts to have a 15 tick stop and 10 tick target. I can run it that way again and post the results if that would be of help.

Mickster
Last seen: 2 years 11 months ago
Joined: 06/29/2018 - 17:47
I have a few questions on the

I have a few questions on the code below (copied from above).

/* Long */
IF(POS_SIZE > 0 AND TR_POS != 0 AND TR_FILL > 0 AND ABS(TR_FILL - ENTRY) <= 4 * TINC) THEN (SET(V#13, TR_FILL + V#17 * TINC) AND SET(V#14, TR_FILL - V#17 * TINC) AND SET(V#15, ABS(TR_POS)));
IF(POS_SIZE > 0 AND (TR_POS == 0 OR TR_FILL <= 0 OR ABS(TR_FILL - ENTRY) > 4 * TINC)) THEN (SET(V#13, ENTRY+ V#17 * TINC) AND SET(V#14, ENTRY- V#17 * TINC) AND SET(V#15, 0));

1. The purpose of the first line is to establish that the system is in a LONG trade and then sets target and stop values based on the actual entry price. What is the purpose of ensuring that TR_FILL - ENTRY is within 4 ticks?
2. I don't understand where the POSITION token is used. Is it not that same as POS_SIZE and TR_POS? I don't see it used in any of the posted code.
3. What do you mean when you say "I would fall back on the theoretical token POS_SIZE to determine if you are short or long." How does this relate to the POSITION token?
4. What is being assured by this statement? "You may also want to add another measure of assurance by checking that TR_FILL is some reasonable distance from the theoretical ENTRY. Let's say within 4 ticks."
5. Can you explain the purpose of the 2nd line of code? I see that it sets up a 0 size order, but why is that needed?

Thanks for the help.

Pages