Thinkscript - Stop trading after first loss


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.


Well-known member
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)
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)
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):
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,

to decide whether you've made a loss yet is trickier. You can set a value when you enter a trade
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:
def armed =
if secondsTillTime(0930) == 0 then 1 else
    if enter[1] == 1 then 0
        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!
Last edited:


New member
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:


New member

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.

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.

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:

Similar threads