Thinkscript - Stop trading after first loss

nextpotato

New member
As a novice at coding, I'm struggling to figure out how to implement what is probably a fairly simple feature into my back-testing script.

I would like to tell the script to stop trading for the rest of the day after the first losing trade, and continue trading the next day.

This is kind of how I was planning on implementing it:
1. Compare entry and exit price of the lot.
2. If exit price is lower, flip a "switch" that disables further buying.
3. Reset the "switch" at next market opening.

My problem is I'm not sure how to get the exit price, and how to implement the "switch" in Thinkscript.

I would greatly appreciate it if anyone can point me in the right direction.
 
@nextpotato
Perhaps not so simple, but here goes. This will get you as far as making only one trade per day.

1. set your entry condition. For this simplistic rendition, I'll take close > ema(9) and for an exit: close < ema(9)
Code:
def enter = if close crosses above ExpAverage(close, 9) then 1 else 0;
def exit = if close crosses below ExpAverage(close, 9) then 1 else 0;

2. set a state variable (these are tricky since thinkscript is generally a functional language)
Code:
def armed = if secondsTillTime(0930) == 0 then 1 else if enter[1] == 1 then 0 else armed[1];
this basically reads as 'set the condition of armed to 1 (true) at 9:30, and then if the value of enter for any given bar is 1 (true) we fire the switch and set it to 0 (false), otherwise, we set it to it's previous value. I think you need to check enter[1] to make sure it's still armed when you get to the addOrder instruction. This should set the value of armed to 0 on the following and all subsequent bars.

3. set your actual order code to check the condition of armed and only enter an order if it is true (1):
Code:
AddOrder(OrderType.BUY_TO_OPEN, enter == 1 and armed == 1, open[-1], 50, Color.ORANGE, Color.ORANGE, "Buy 50 @ " + open[-1]);
AddOrder(OrderType.SELL_TO_CLOSE, exit == 1, open[-1], 50, Color.ORANGE, Color.ORANGE, "Sell 50 @ " + open[-1]);

Maybe. I didn't test this. It may work, it may make smoke pour fourth from your computer or your ears. If it makes you tones of money, good -- please share ;-)

Happy Trading,
Mashume

EDIT:
to decide whether you've made a loss yet is trickier. You can set a value when you enter a trade
Code:
def entry_price = if secondsTillTime(0930) == 0 then double.nan
    else if isNan(entry_price) and enter == 1 then close[-1]
        else if exit == 1 then double.nan
            else entry_price[1];
modify the state of armed then to be something like this:
Code:
def armed =
if secondsTillTime(0930) == 0 then 1 else
    if enter[1] == 1 then 0
    else
        if exit[1] == 1 and close[-1] > entry_price then 1
        else armed[1];
but you can see where it starts to get a wee bit convoluted.

have fun with it!
-mashume
 
Last edited:
Thanks for helping me out! I will experiment with the code and let you know how things turn out.

A quick thought - In order to determine if the trade was a gain or loss, would it make sense to use the floating PnL ( FPL(); ) value somehow? I tried this earlier but was not successful.

Anyway, I will try working your code into mine and see what happens.
 
Last edited:
@mashume

EDIT: I realized that I had some of the syntax different from your code. Buying/Selling only one lot per day is now working. Now I am trying to figure out how to disable buying based on the triggering of stoploss. I will leave my original post below for context.

---

I was not successful in combining your code into mine (it did make it behave differently, for example it disabled selling orders and messed up how the floating PnL is calculated, but I have no idea why), so I took some of your logic and modified it to fit my structure better, but I am still unsuccessful.

Below is a simplified version of my code.

Code:
def buy = close < ema9;
def sell = close > ema9;
def stoploss = close < EntryPrice() - 5;

I have a "stoploss" feature which will sell the lot if the close price drops below a specified amount from entry. My updated plan is to fire the switch to stop trading for the day once a stoploss is triggered.

Code:
def armed = if secondsTillTime(0930) == 0 then 1
else if stoploss[1] == 1 then 0
else armed[1];

AddOrder(OrderType.BUY_TO_OPEN, buy, armed, tickcolor = Color.DARK_GREEN, name = "Buy" + -open[-1], arrowcolor = Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, sell, tickcolor = Color.DARK_RED, name = "Sell" + -open[-1], arrowcolor = Color.PINK);
AddOrder(OrderType.SELL_TO_CLOSE, stoploss, tickColor = Color.DARK_ORANGE, arrowColor = Color.RED, name = "Stop" + -open[-1]);

The logic here is that if stoploss has been triggered, armed becomes 0, thus failing the checks for any further buy orders to go through until 09:30 next day.

Unfortunately in its current state, it will not make any orders at all. Any ideas?
 
Last edited:

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
271 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