Auto Trade (ALGO) in TOS

@Svanoy I have it go flat then buy on green, then go flat and short on red. then just go flat on blue. the other colors are there to reset the code. This is what I have so far.
Code:
###rsi####
input price = close;
input length = 14;
input OB = 70;
input OS = 30;
input rsiAverageType = AverageType.WILDERS;

def over_bought = OB;
def over_sold = OS ;
def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);
##### Stop Loss / Profit Target ####
input offsetType = {default percent, value, tick};
input longstop = 0.05;
input shortstop = 0.05;
input longtarget = 1.2;
input shorttarget = 1.2;

def entryPrice = EntryPrice();
def mult;
switch (offsetType) {
case percent:
    mult = entryPrice / 100;
case value:
    mult = 1;
case tick:
    mult = TickSize();
}
def longstopPrice = entryPrice - longstop * mult;
def shortstopPrice = entryPrice + shortstop * mult;
def longTargetPrice = entryPrice + longtarget * mult;
def shortTargetPrice = entryPrice - shorttarget * mult;
############
def long = rsi[0] crosses above over_sold[0];
def longexit = rsi[0] crosses below over_bought[0];
def longStopProfit = low <= longstopPrice or high >= longTargetPrice;
def short = rsi[0] crosses below over_bought[0];
def shortexit = rsi[0] crosses above over_sold[0];
def shortStopProfit = high >= shortstopPrice or low<= shortTargetPrice;

AddOrder(OrderType.BUY_AUTO, long, tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "LE");
AddOrder(OrderType.SELL_TO_CLOSE, longexit, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Lexit");
AddOrder(OrderType.SELL_TO_CLOSE, longStopProfit, tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "sp");
AddOrder(OrderType.SELL_AUTO, short, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "SE");
AddOrder(OrderType.BUY_TO_CLOSE, shortexit, tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "stop");
AddOrder(OrderType.BUY_TO_CLOSE, shortStopProfit, tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "sp");

#### Labels ####
AddLabel(yes, "       ", if  long[1] then Color.GREEN else Color.MAGENTA);
AddLabel(yes, "       ", if short[1] then Color.RED else Color.YELLOW);
AddLabel(yes, "       ", if  long[1] and longstopProfit[1] or short[1] and shortstopProfit[1] then Color.BLUE else Color.CYAN);


def NewBar = if close[1] != close[2] or high[1] != high[2] or low[1] != low[2] then yes else Double.NaN;
def Clock = if !IsNaN(NewBar) and Clock[1] == 1 then 0 else if !IsNaN(NewBar) and Clock[1] == 0 then 1 else Clock[1];
AddLabel(yes, "       ", if Clock == 0 then Color.VIOLET else Color.Gray);
 
@aCuddlyTurtle Had to change the AddOrder lines to plot correctly as TOS plots a bar late by default. You will notice the entry plot is plotting a bar early, but does not plot until the actual bar it is referencing closes. The price reference of the entry plot has been adjusted to account for this. I did this to allow a trade to enter and exit on the same bar if a price stop or target is hit.

All AddOrder lines price references are correct.

All of your labels are now displaying the correct colors at the correct times. A long trade reversal to a short trade will trigger the red label only. A short trade reversal to a long trade will trigger a the green label only. Either long or short stop will trigger the blue label only except where the stop is hit on the same bar as the trade open, then corresponding red or green label is turned off.

uitNmtH.png


I ran this with OnDemand to test the different trade scenarios, if I missed anything let me know.

Ruby:
###rsi####
input price = close;
input length = 14;
input OB = 70;
input OS = 30;
input rsiAverageType = AverageType.WILDERS;

def over_bought = OB;
def over_sold = OS ;
def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);
##### Stop Loss / Profit Target ####
input offsetType = {default percent, value, tick};
input longstop = 0.05;
input shortstop = 0.05;
input longtarget = 1.2;
input shorttarget = 1.2;

def entryPrice = open;#EntryPrice();
def mult;
switch (offsetType) {
case percent:
    mult =entryPrice / 100;
case value:
    mult = 1;
case tick:
    mult = TickSize();
}
def longstopPrice;
def shortstopPrice;
def longTargetPrice;
def shortTargetPrice;

if high[1] >= shortStopPrice[1]{
    longstopPrice = Double.NaN;
    shortstopPrice = Double.NaN;
    longTargetPrice = Double.NaN;
    shortTargetPrice = Double.NaN;
}else if low[1] <= shortTargetPrice[1]{
    longstopPrice = Double.NaN;
    shortstopPrice = Double.NaN;
    longTargetPrice = Double.NaN;
    shortTargetPrice = Double.NaN;
}else if high[1] >= longTargetPrice[1]{
    longstopPrice = Double.NaN;
    shortstopPrice = Double.NaN;
    longTargetPrice = Double.NaN;
    shortTargetPrice = Double.NaN;
}else if low[1] <= longstopPrice[1]{
    longstopPrice = Double.NaN;
    shortstopPrice = Double.NaN;
    longTargetPrice = Double.NaN;
    shortTargetPrice = Double.NaN;
}else if rsi[1] crosses above over_sold[1] and IsNaN(longstopPrice[1]) {
    longstopPrice = entryPrice - longstop * mult;
    shortstopPrice = Double.NaN;
    longTargetPrice = entryPrice + longtarget * mult;
    shortTargetPrice = Double.NaN;
}else if  rsi[1] crosses below over_bought[1] and IsNaN(shortstopPrice[1]) {
    longstopPrice = Double.NaN;
    shortstopPrice = entryPrice + shortstop * mult;
    longTargetPrice = Double.NaN;
    shortTargetPrice = entryPrice - shorttarget * mult;
}else{
    longstopPrice = longstopPrice[1];
    shortstopPrice = shortstopPrice[1];
    longTargetPrice = longTargetPrice[1];
    shortTargetPrice = shortTargetPrice[1];}

############
plot ssp = shortstopprice;
ssp.setpaintingStrategy(paintingstrategy.HORIZONTAL);
plot stp = shortTargetPrice;
stp.setpaintingStrategy(paintingstrategy.HORIZONTAL);
plot lsp = longstopprice;
lsp.setpaintingStrategy(paintingstrategy.HORIZONTAL);
plot ltp = longTargetPrice;
ltp.setpaintingStrategy(paintingstrategy.HORIZONTAL);
############

def long = rsi[0] crosses above over_sold[0];
def longStopT = !isnan(longstopprice) and low <= longstopprice;
def longProfit = !isnan(longTargetPrice) and high >= longTargetPrice;
def short = rsi[0] crosses below over_bought[0];
def shortStopT = !isnan(shortstopprice) and high >= shortstopprice;
def shortProfit = !isnan(shortTargetprice) and low <= shortTargetprice;

AddOrder(OrderType.BUY_AUTO, long[-1], price = open[-2], tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "Buy Auto");
AddOrder(OrderType.SELL_TO_CLOSE, longStopT[-1], price = longstopPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Long Stop");
AddOrder(OrderType.SELL_TO_CLOSE, longProfit[-1], price = longTargetPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Long Profit");
AddOrder(OrderType.SELL_AUTO, short[-1], price = open[-2], tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Sell Auto");
AddOrder(OrderType.BUY_TO_CLOSE, shortStopT[-1], price = shortstopPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Short Stop");
AddOrder(OrderType.BUY_TO_CLOSE, shortProfit[-1], price = shortTargetPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Short Profit");

def LongStopTrigger = (!IsNaN(longStopPrice) and IsNaN(longStopPrice[-1]) and IsNaN(shortStopPrice[-1]) and low <= longStopPrice);
def LongTargetTrigger = (!IsNaN(longStopPrice) and IsNaN(longStopPrice[-1]) and IsNaN(shortStopPrice[-1]) and high >= longTargetPrice);
def ShortStopTrigger = (!IsNaN(shortStopPrice) and IsNaN(shortStopPrice[-1]) and IsNaN(longStopPrice[-1]) and high >= shortStopPrice);
def ShortTargetTrigger = (!IsNaN(shortStopPrice) and IsNaN(shortStopPrice[-1]) and IsNaN(longStopPrice[-1]) and low <= shortTargetPrice);

#### Labels ####
AddLabel(yes, "       ", if  long[1] and !IsNaN(longStopPrice[-1] and IsNaN(longStopPrice[1])) then Color.GREEN else Color.MAGENTA);
AddLabel(yes, "       ", if short[1] and !IsNaN(shortStopPrice[-1] and IsNaN(shortStopPrice[1])) then Color.RED else Color.YELLOW);
AddLabel(IsNaN(LongStopTrigger) and !ShortStopTrigger and !ShortTargetTrigger, "       ",Color.CYAN);
AddLabel(!LongStopTrigger and IsNaN(ShortStopTrigger) and !LongTargetTrigger, "       ",Color.CYAN);
AddLabel(IsNaN(LongStopTrigger) and IsNaN(ShortStopTrigger), "       ",Color.Cyan);
AddLabel(LongStopTrigger, "       ", Color.Blue);
AddLabel(LongTargetTrigger, "       ", Color.Blue);
AddLabel(ShortStopTrigger, "       ",Color.Blue);
AddLabel(ShortTargetTrigger, "       ",Color.Blue);

def NewBar = if close[1] != close[2] or high[1] != high[2] or low[1] != low[2] then yes else Double.NaN;
def Clock = if !IsNaN(NewBar) and Clock[1] == 1 then 0 else if !IsNaN(NewBar) and Clock[1] == 0 then 1 else Clock[1];
AddLabel(yes, "       ", if Clock == 0 then Color.VIOLET else Color.Gray);

Adjusting the stops and targets could make this profitable. Here is the 1D 1MIN chart for /MES from Wednesday with .35 for the stops and .4 for the targets.

njbpaa7.png
 
Last edited:
@Svanoy I should have told you that I'm testing my strategy on Renko bars, So getting In and out of a trade right when the condition is met can mess up the back testing.. There is sometimes where the trade will hit my stoploss on a renko bar but before that bar closes the price will go back in my direction. That's why I had the (1) on the labels so my strategy would only get in and out of trades at the start of a new renko bar. so my orders would match my back testing.
 
@Svanoy Well one thing about my OnDemand is it hardly works and basically doesn't work at all with renko / range bars. you can load the chart to a date you want but when you hit play nothing happens. So to test my labels I had to use normal candle charts with OnDemand.
 
@aCuddlyTurtle replace the bottom half of the script, starting with addorder lines, with this:
Ruby:
AddOrder(OrderType.BUY_AUTO, long[-1], price = open[-2], tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "Buy Auto");
AddOrder(OrderType.SELL_TO_CLOSE, longStopT, price = open[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Long Stop");
AddOrder(OrderType.SELL_TO_CLOSE, longProfit[-1], price = longTargetPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Long Profit");
AddOrder(OrderType.SELL_AUTO, short[-1], price = open[-2], tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Sell Auto");
AddOrder(OrderType.BUY_TO_CLOSE, shortStopT, price = open[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Short Stop");
AddOrder(OrderType.BUY_TO_CLOSE, shortProfit[-1], price = shortTargetPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Short Profit");

def LongStopTrigger = (!IsNaN(longStopPrice) and IsNaN(longStopPrice[-1]) and IsNaN(shortStopPrice[-1]) and low <= longStopPrice);
def LongTargetTrigger = (!IsNaN(longStopPrice) and IsNaN(longStopPrice[-1]) and IsNaN(shortStopPrice[-1]) and high >= longTargetPrice);
def ShortStopTrigger = (!IsNaN(shortStopPrice) and IsNaN(shortStopPrice[-1]) and IsNaN(longStopPrice[-1]) and high >= shortStopPrice);
def ShortTargetTrigger = (!IsNaN(shortStopPrice) and IsNaN(shortStopPrice[-1]) and IsNaN(longStopPrice[-1]) and low <= shortTargetPrice);

#### Labels ####
AddLabel(yes, "       ", if  long[1] and !IsNaN(longStopPrice[-1] and IsNaN(longStopPrice[1])) then Color.GREEN else Color.MAGENTA);
AddLabel(yes, "       ", if short[1] and !IsNaN(shortStopPrice[-1] and IsNaN(shortStopPrice[1])) then Color.RED else Color.YELLOW);

#Display Cyan label when reversing from short to long trade.
AddLabel(!IsNaN(ShortStopPrice[1]) and IsNaN(ShortStopPrice) and !IsNaN(LongStopPrice), "       ", Color.Cyan);
#Display Cyan label when in short trade with stop hit, but bar not closed.
AddLabel(!IsNaN(ShortStopTrigger) and IsNaN(LongStopTrigger) and ShortStopTrigger, "       ", Color.Cyan);
#Display Cyan label when in short trade with neither stop nor target hit.
AddLabel(!ShortStopTrigger and !ShortTargetTrigger and IsNaN(LongStopTrigger) and IsNaN(LongStopPrice[1]), "       ", Color.Cyan);

#Display Cyan label when reversing from long to short trade.
AddLabel(!IsNaN(LongStopPrice[1]) and IsNaN(LongStopPrice) and !IsNaN(ShortStopPrice), "       ", Color.Cyan);
#Display Cyan label when in long trade with stop hit, but bar not closed.
AddLabel(!IsNaN(LongStopTrigger) and IsNaN(ShortStopTrigger) and LongStopTrigger, "       ", Color.Cyan);
#Display Cyan label when in long trade with neither stop nor target hit.
AddLabel(!LongStopTrigger and !LongTargetTrigger and IsNaN(ShortStopTrigger) and IsNaN(ShortStopPrice[1]), "       ", Color.Cyan);

#Display Cyan label when not in trade (3 lines)
AddLabel(IsNaN(ShortStopTrigger) and IsNaN(LongStopTrigger) and IsNaN(ShortStopTrigger[1]) and IsNaN(LongStopTrigger[1]), "       ", Color.Cyan);
AddLabel(ShortProfit[1], "       ", Color.Cyan);
AddLabel(LongProfit[1], "       ", Color.Cyan);

#Diplay Blue label on open of bar after short stop hit.
AddLabel(ShortStopT[1], "       ", Color.Blue);
#Display Blue label when short target hit.
AddLabel(ShortProfit, "       ", Color.Blue);

#Diplay Blue label on open of bar after long stop hit.
AddLabel(LongStopT[1], "       ", Color.Blue);
#Display Blue label when long target hit.
AddLabel(LongProfit, "       ", Color.Blue);


def NewBar = if close[1] != close[2] or high[1] != high[2] or low[1] != low[2] then yes else Double.NaN;
def Clock = if !IsNaN(NewBar) and Clock[1] == 1 then 0 else if !IsNaN(NewBar) and Clock[1] == 0 then 1 else Clock[1];
AddLabel(yes, "       ", if Clock == 0 then Color.VIOLET else Color.Gray);
 
Okay that's what I thought, I just want to make sure.
Hey is it possible on thinkorswim to have a stop loss that starts off at a certain percentage. But then after the trade goes in your favor a certain amount such as point 01%, that becomes the new stop loss? That way like if you enter a trade and it goes green you can lock in the profits? I don't want it to be a trailing stop just like a lock-in profit stop.
 
It's possible, it would be defined in the IF/THEN/ELSE statement controlling the stops and targets.
 
@Svanoy Hey could you check my AHK and see if everything looks ok, Everything seems to work fine except the Blue Label section. Sometimes when I get a blue label which is my stoploss my AHK does not flatten the postion, but sometimes it works.

Code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance [Forced]

CoordMode, Pixel, Screen

Buy = 0
Sell = 0
Delay = 10000

^0:: Pause
^9::
Loop{

;########## Watch for Buy Signal ##########
ImageSearch, FoundX, FoundY, 24, 216, 161, 243, *w21 *h21 C:\Users\Brooke\Desktop\AutoHotKey Images\BuyLabel.tiff

;If found will click open space then flatten then buy then move mouse to open space
    If (ErrorLevel = 0 and Buy = 0)
        {
    delay = 10
        MouseClick, Left, 1004, 74, 1, 0
        delay = 10
;mouse move to flatten
    delay = 10
        MouseClick, Left, 1758, 188, 1, 0
        delay = 10
;mouse move to buy
    delay = 10
        MouseClick, Left, 1529, 188, 1, 0
        Buy = 1
        Sell = 0
    delay = 10
;mouse move back to open space
    delay = 10
    MouseClick, Left, 1004, 74, 1, 0
    delay = 10
        }
;If not found do nothing
    Else If (ErrorLevel = 1)
        {
        }
;########## Watch for Sell Signal ##########
ImageSearch, FoundX, FoundY, 23, 212, 161, 246, *w21 *h21 *1 C:\Users\Brooke\Desktop\AutoHotKey Images\SellLabel.tiff

;If found will click open space then flatten then short then move mouse to open space
    If (ErrorLevel = 0 and Sell = 0)
        {
    delay = 10
        MouseClick, Left, 1004, 74, 1, 0
        delay = 10
;move mouse to flatten
    MouseClick, Left, 1758, 188, 1, 0
        delay = 10
;move mouse to short
    delay = 10
    MouseClick, Left, 1647, 188, 1, 0
        Buy = 0
        Sell = 1
    delay = 10
;move mouse back to open space
    delay = 10
    MouseClick, Left, 1004, 74, 1, 0
    delay = 10
        }
;If not found do nothing
    Else If (ErrorLevel = 1)
        {
        }
;########## Watch for Yellow label ##########
ImageSearch, FoundX, FoundY, 23, 212, 161, 246, *w21 *h21 *1 C:\Users\Brooke\Desktop\AutoHotKey Images\YellowLabel.tiff

;If found click mouse on open space
    If (ErrorLevel = 0 and Buy = 0)
        {
    delay = 10
    MouseClick, Left, 1004, 74, 1, 0
        Buy = 0
        Sell = 0
    delay = 10
        }
;If not found do nothing
    Else If (ErrorLevel = 1)
        {
        }
;########## Watch for Magenta label ##########
ImageSearch, FoundX, FoundY, 23, 212, 161, 246, *w21 *h21 *1 C:\Users\Brooke\Desktop\AutoHotKey Images\MagentaLabel.tiff

;If found click mouse on open space
    If (ErrorLevel = 0 and Sell = 0)
        {
    delay = 10
    MouseClick, Left, 1004, 74, 1, 0
        Buy = 0
        Sell = 0
    delay = 10
        }
;If not found do nothing
    Else If (ErrorLevel = 1)
        {
        }
;########## Watch for Blue label ################ 
ImageSearch, FoundX, FoundY, 23, 212, 161, 246, *w21 *h21 *1 C:\Users\Brooke\Desktop\AutoHotKey Images\BlueLabel.tiff

;If found will click open space then flatten then click mouse on open space
    If (ErrorLevel = 0)
        {
    delay = 10
    MouseClick, Left, 1004, 74, 1, 0
    delay=10
;move mouse to flatten
    delay = 10
    MouseClick, Left, 1758, 188, 1, 0
        delay = 10
;move mouse back to open space
    delay = 10
    MouseClick, Left, 1004, 74, 1, 0
    delay = 10
    }
;If not found do nothing
    Else If (ErrorLevel = 1)
        {
        }
;########## Check for TeamViewer ok lcon and click to close ##########
    ImageSearch, FoundX, FoundY, 0, 0, 1900, 1080, *w20 *19 C:\Users\Brooke\Desktop\AutoHotKey Images\tvok.tiff
    If (ErrorLevel = 0)
        {
        MouseClick, Left, (FoundX+10), (FoundY+10), 1, 0
    delay = 10
    MouseClick, Left, 1004, 74, 1, 0
    Sleep,5000
        }
    Else If (ErrorLevel = 1)
        {
        }

}Return
 
Last edited:
@aCuddlyTurtle I assume you are certain the label turned Blue. Considering the problems we had before with getting your ImageSearch to recognize the labels at all, I would try increasing the allowed shades of variation from 1 to 2, and see if it improves. If you are not sure the label turned Blue then that may be the actual issue.

EDIT: Looking back at the original script you posted, I believe that maybe the label didn't turn Blue base on the conditions that needed to be met.
 
@Svanoy yeah for a while I've been using my ahk with just my buy and sell labels and the yellow and magenta label, while using a TOS stop loss and profit. But just last week I added the blue label and I set my TOS stop loss further away just in case of an emergency, but when I look back at it there's a few times where my trade is not getting out when my algo does.
 
@aCuddlyTurtle I just realized I only fixed the stoploss to signal on the bar after it is hit, I didn't fix the target to signal on the bar after, It still signals as soon as it is hit. I don't use Renko bars so I wasn't thinking about there only being an open and close.

So while it will work as is, you may find that you hit your target, AHK exits the trade, but then at bar close the target is not hit so TOS doesn't plot the exit order and AHK may try to reenter the trade when the target becomes "unhit".

I went ahead and fixed this, so (again) just replace the bottom half of the script, starting with the AddOrder lines, with this:
Ruby:
AddOrder(OrderType.BUY_AUTO, long[-1], price = open[-2], tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "Buy Auto");
AddOrder(OrderType.SELL_TO_CLOSE, longStopT, price = open[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Long Stop");
AddOrder(OrderType.SELL_TO_CLOSE, longProfit, price = open[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Long Profit");
AddOrder(OrderType.SELL_AUTO, short[-1], price = open[-2], tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Sell Auto");
AddOrder(OrderType.BUY_TO_CLOSE, shortStopT, price = open[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Short Stop");
AddOrder(OrderType.BUY_TO_CLOSE, shortProfit, price = open[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Short Profit");

def LongStopTrigger = (!IsNaN(longStopPrice) and IsNaN(longStopPrice[-1]) and IsNaN(shortStopPrice[-1]) and low <= longStopPrice);
def LongTargetTrigger = (!IsNaN(longStopPrice) and IsNaN(longStopPrice[-1]) and IsNaN(shortStopPrice[-1]) and high >= longTargetPrice);
def ShortStopTrigger = (!IsNaN(shortStopPrice) and IsNaN(shortStopPrice[-1]) and IsNaN(longStopPrice[-1]) and high >= shortStopPrice);
def ShortTargetTrigger = (!IsNaN(shortStopPrice) and IsNaN(shortStopPrice[-1]) and IsNaN(longStopPrice[-1]) and low <= shortTargetPrice);

#### Labels ####
AddLabel(yes, "       ", if !IsNaN(longStopPrice) then Color.GREEN else Color.MAGENTA);
AddLabel(yes, "       ", if !IsNaN(shortStopPrice) then Color.RED else Color.YELLOW);

#Display Cyan label when reversing from short to long trade.
AddLabel(!IsNaN(ShortStopPrice[1]) and IsNaN(ShortStopPrice) and !IsNaN(LongStopPrice), "       ", Color.Cyan);
#Display Cyan label when in short trade with stop hit, but bar not closed.
AddLabel(!IsNaN(ShortStopTrigger) and IsNaN(LongStopTrigger) and ShortStopTrigger, "       ", Color.Cyan);
#Display Cyan label when in short trade with target hit, but bar not closed.
AddLabel(!IsNaN(ShortTargetTrigger) and IsNaN(LongTargetTrigger) and ShortTargetTrigger, "       ", Color.Cyan);
#Display Cyan label when in short trade with neither stop nor target hit.
AddLabel(!ShortStopTrigger and !ShortTargetTrigger and IsNaN(LongStopTrigger) and IsNaN(LongStopPrice[1]), "       ", Color.Cyan);

#Display Cyan label when reversing from long to short trade.
AddLabel(!IsNaN(LongStopPrice[1]) and IsNaN(LongStopPrice) and !IsNaN(ShortStopPrice), "       ", Color.Cyan);
#Display Cyan label when in long trade with stop hit, but bar not closed.
AddLabel(!IsNaN(LongStopTrigger) and IsNaN(ShortStopTrigger) and LongStopTrigger, "       ", Color.Cyan);
#Display Cyan label when in long trade with target hit, but bar not closed.
AddLabel(!IsNaN(LongTargetTrigger) and IsNaN(ShortTargetTrigger) and LongTargetTrigger, "       ", Color.Cyan);
#Display Cyan label when in long trade with neither stop nor target hit.
AddLabel(!LongStopTrigger and !LongTargetTrigger and IsNaN(ShortStopTrigger) and IsNaN(ShortStopPrice[1]), "       ", Color.Cyan);

#Display Cyan label when not in trade
AddLabel(IsNaN(ShortStopTrigger) and IsNaN(LongStopTrigger) and IsNaN(ShortStopTrigger[1]) and IsNaN(LongStopTrigger[1]), "       ", Color.Cyan);

#Diplay Blue label on open of bar after short stop hit.
AddLabel(ShortStopT[1], "       ", Color.Blue);
#Display Blue label on open of bar after short target hit.
AddLabel(ShortProfit[1], "       ", Color.Blue);
#Diplay Blue label on open of bar after long stop hit.
AddLabel(LongStopT[1], "       ", Color.Blue);
#Display Blue label on open of bar after long target hit.
AddLabel(LongProfit[1], "       ", Color.Blue);

def NewBar = if close[1] != close[2] or high[1] != high[2] or low[1] != low[2] then yes else Double.NaN;
def Clock = if !IsNaN(NewBar) and Clock[1] == 1 then 0 else if !IsNaN(NewBar) and Clock[1] == 0 then 1 else Clock[1];
AddLabel(yes, "       ", if Clock == 0 then Color.VIOLET else Color.Gray);

I ran this through OnDemand and didn't see any problems, if you find any let me know.
 

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