Exit Condition Code Malfunctioning

Bingy

Member
Hey, so I've never been able to successfully create a multi SELL_TO_CLOSE condition that works correctly. I've done it successfully with opening long positions though. Below is the relevant code of my last failed attempt. You can ignore the "BuySignals", since the misbehavior is occurring with the LondonExitPosition and NYExitPosition. I have pinpointed the error is occurring with the ">=0;" portion of the definitions. For some reason these time based exit conditions work as intended if I make the exit condition only London or NY exits, but once the other code is incorporated it misbehaves and doesn't exit as intended. Instead it just defaults to "ExitSignal2" almost as if the "LondonExitPosition" and "NYExitPosition" doesn't even exist. I'm not sure why this is happening and haven't been able to fix it after many attempts. I have even tried to create another test exit strategy using a NON-time based exit and it still defaults to "ExitSignal2". So, is it not possible to have more than one SELL_TO_CLOSE? I'm baffled at this point.



Code:
#EXIT LONG POSITION
def LondonExitPosition = SecondsFromTime(0345) >= 0;

def NYExitPosition = SecondsFromTime(1145) >= 0;



Def ShouldExit =  if BuySignal then ExitSignal2
else if SecondBuySignal then ExitSignal2
else if SixthBuySignal then ExitSignal2
else if EighthBuySignal then ExitSignal2
else if NinethBuySignal then ExitSignal2

else if LondonLongPosition_1 then LondonExitPosition
else if LondonLongPosition_2 then LondonExitPosition
else if LondonLongPosition_3 then LondonExitPosition

else if NYLongPosition_2 then NYExitPosition

else ExitSignal2



AddOrder(OrderType.SELL_TO_CLOSE, ShouldExit, open[-1], 4, tickcolor = Color.RED, arrowcolor = Color.RED, name = "Exit");
 
Last edited by a moderator:
Solution
Well to start with, it is often the case that things become clearer in code if you write explicit code, rather than implicit code. This is what I mean:
Code:
def LondonExitPosition = SecondsFromTime(0345) >= 0;
is implicit code. While it works, it is not nearly as clear or easy to debug as this:
Code:
def LondonExitPosition = if SecondsFromTime(0345) >= 0 then 1 else 0;
which explicitly defines states for both true (1) and false (0).

That said, your conditions for time are going to be true at any point in the day after your specified time. You can do something like this:
Code:
def LondonExitPosition = if SecondsFromTime(0345) >= 0 AND  SecondsFromTime(0345)[1] < 0 then 1 else 0;
This will pinpoint a single bar where the condition...
Well to start with, it is often the case that things become clearer in code if you write explicit code, rather than implicit code. This is what I mean:
Code:
def LondonExitPosition = SecondsFromTime(0345) >= 0;
is implicit code. While it works, it is not nearly as clear or easy to debug as this:
Code:
def LondonExitPosition = if SecondsFromTime(0345) >= 0 then 1 else 0;
which explicitly defines states for both true (1) and false (0).

That said, your conditions for time are going to be true at any point in the day after your specified time. You can do something like this:
Code:
def LondonExitPosition = if SecondsFromTime(0345) >= 0 AND  SecondsFromTime(0345)[1] < 0 then 1 else 0;
This will pinpoint a single bar where the condition is true, rather than all bars after that.
Put this code into your script to see what values are triggering at what points:
Code:
Def ShouldExitDebug =  if BuySignal then "1"
else if SecondBuySignal then "2"
else if SixthBuySignal then "3"
else if EighthBuySignal then "4"
else if NinethBuySignal then "5"
else if LondonLongPosition_1 then "6"
else if LondonLongPosition_2 then "7"
else if LondonLongPosition_3 then "8"
else if NYLongPosition_2 then "9"
else "10"
AddChartBubble(ShouldExitDebug);
This will tell you which condition is being met first at each bar. Remember that, while ThinkScript is largely functional in design, the if...else if ... else statements are evaluated sequentially, the first true is taken and the rest are not evaluated.
You may also want to consider logic like this:
Code:
Def ShouldExit = 
if BuySignal OR SecondBuySignal OR SixthBuySignal OR EighthBuySignal OR NinethBuySignal then ExitSignal2
else if LondonLongPosition_1 OR LondonLongPosition_2 OR LondonLongPosition_3 then LondonExitPosition
else if NYLongPosition_2 then NYExitPosition
else ExitSignal2
To your statement
"Instead it just defaults to "ExitSignal2" almost as if the "LondonExitPosition" and "NYExitPosition" doesn't even exist."
you quite literally have it defaulting to ExitSignal2 with your catch-all-last-else statement.
Again, people tend to get into trouble assuming that implied definitions of true and false will suffice when explicitly defining meanings (1s and 0s) can make it so so much easier to figure out where it is failing.

After all that, without knowing how your buy conditions are being met and whether they are persistent across bars in the way you intend it is impossible to pinpoint a cause for the discrepancy in intended and actual behaviour of your script.

Long winded... not enough coffee to make it shorter
-mashume
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
294 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top