stop # days after a signal

tlee404

New member
Hi,
I am trying to code a Stop a number of days or time periods after a Buy or Sell signal has been set.
Does anyone know a way of doing that? Is it possible?
Thank you

See Solution on the 5th and 6th posts. Thank you to member mashume
 
Last edited:

mashume

Well-known member
VIP
This is certainly not a complete answer, but it may help you get there.

This is offered only as a way to test exit strategies, and will show different exits on different time scales, as it is based on the number of bars passed since a position was opened.

Also, I'm just typing this in the forum, not testing it in ThinkOrSwim.

So here goes... I stole this from another script I wrote to put Mobile ToS style position lines on charts in Desktop ToS.

Code:
input size = 10;
input sell_after = 14;
def liq = positionNetLiq();
def position = positionNetLiq() / size;

def enter = if position != 0 then
        if position[1] == 0 then position
        else enter[1]
    else double.nan ;

plot entryPrice = enter;
plot exit = if isnan(enter[sell_after - 1]) and !isNaN(enter[sell_after]) then 1 else double.nan;

What we've done there is to find when a position opened because the value of enter went from NaN to something when the position opened.
We then have a set 'sell-after' time (in bars, so 14 days on a daily chart, 14 minutes on a 1-minute chart etc...) and generate a sell signal some bars after the change in value from nan to a whatever it is.

I leave it to you to create a strategy around the idea.
 

tlee404

New member
I worked on it for another 3.5 hours on Sunday.
Then I tried my own for another hour today.
I found the code provided by mashume in previous post was above my level of knowledge.
Finally after using masuhme's code I got it to work. 😍 Thank you! Thank you! Thank you!

Here is what I did to create a Sell Order after 5 days.
Code:
input sell_after = 5;                #This is the number of days after the Buy order was placed
#def liq = positionNetLiq();    # was  not needed
def position = (if (net crosses ABOVE 0) then GetDay()+ 5 else 0);    # This sets the start of the count on the Buy order day (net crosses above 0)

def enter = if position != 0 then
        if position[1] == 0 then position
        else enter[1]
    else double.nan ;

plot entryPrice = enter;     # was not neededd
def exit = if isnan(enter[sell_after - 1]) and !isNaN(enter[sell_after]) then 1 else double.nan;      #changed Plot to def

AddOrder(OrderType.SELL_TO_CLOSE,
if isnan(enter[sell_after - 1]) and !isNaN(enter[sell_after]) then 1 else double.nan,
price,100, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "SELL 01");
 
Last edited by a moderator:

dennisjkane

New member
I want to adjust my exit strategy based on how long it's been since I entered into a trade. Thinkscript has EntryPrice() but no version of EntryTime(). I could set a variable to the time that I executed an AddOrder(...TO_OPEN...) but I don't quite know how to do that. BTW, my exit strategy right now is a simple exit if I have either made or loss $0.5 which is an input. I just want to scale the $0.5 to a lower value as hold time increases, e.g. get out at a gain/loss of $0.3 after holding 10 minutes.
BTW, it looks like Strategy Desk had an EntryTime tag but is this tool no long supported. https://www.tdameritrade.com/retail-en_us/resources/pdf/TDA9242.pdf

I looked around for an answer to this here, on reddit and elsewhere... no luck. Thanks in advance.
 

rad14733

Well-known member
VIP
@dennisjkane We have very few ways to track the entry date of a trade... Here are a couple of ideas... You could extrapolate the date as a number and store in a "state" variable... Essentially you would keep storing the same content of entryDate[1] in entryDate on every iteration of the script... Another option would be to set the timeframe for your chart and store a counter in a state variable and by knowing how many bars the chart paints in a day you could determine how many candles before exiting... Those are the best ideas I can come up with on short notice... Just spit-balling...

I haven't checked the code posted above so maybe it could be modified for your needs...
 

XeoNoX

Well-known member
VIP
you can do it using get average price...

scan for the bar thats averageprice is equal to 0 and the next bar averageprice is greater than 0 then gettime() of the bar after 0.

if averageprice bar is 0 and bar after averageprice is greater than 0 from 10 bars ago then flatten/close position
 

Similar threads

Top