TradeStation

Implementing UT Bot in TradeStation

Implementing UT Bot in TradeStation

UT Bot is a very popular script in my TradingView account. Quite a few people asked me to translate it to another programming languages. I already created a version in Python. In this article I’ll show you a way to implement UT Bot strategy in EasyLanguage for TradeStation. I started to look more into TradeStation because of the automation and screener features of TradeStation. nNew

To create a new script in TradeStation click on EasyLaguage button on the TradeStation dashboard:

Writing the code

Create new strategy and you can start coding. First let’s define parameters for our script. We need only 3 parameters: Price source, sensitivity of the algorithm and ATR length.

inputs: 
	Price( Close ) [DisplayName = "Price"], 
	sens( 1 ) [DisplayName = "Sensitivity"], 
	atr_len( 10 ) [DisplayName = "ATR Length"];

Next we have to defined all the variables we’ll be using in the script:

variables: 
	xATR( 0 ),
	nLoss (0), 
	xATRTrailingStop(0),
	cur_pos(0),
	above_sig(false), 
	below_sig(false),
	buy_sig(false), 
	sell_sig(false);

Next we’ll compute ATR and nLoss (trailing value) variables:

xATR  = average(TrueRange, atr_len);
nLoss = sens * xATR;

Now let’s compute trailing price, because of EasyLanguages if’s it looks a bit bulky:

if Price > xATRTrailingStop[1] and Price[1] > xATRTrailingStop[1] then 
	xATRTrailingStop = Maxlist(xATRTrailingStop[1], Price - nLoss)
else begin
	if Price < xATRTrailingStop[1] and Price[1] < xATRTrailingStop[1] then  
		xATRTrailingStop = Minlist(xATRTrailingStop[1], Price + nLoss)
	else begin
		if Price > xATRTrailingStop[1] then 
			xATRTrailingStop = Price - nLoss
		else 
			xATRTrailingStop = Price + nLoss;
	end;
end;

Next let’s compute a variable that will store current position of the strategy. 1 -> Long, -1 -> Short, 0 – Flat

if Price[1] < xATRTrailingStop[1] and Price > xATRTrailingStop[1] then 
	cur_pos = 1
else begin
	if Price[1] > xATRTrailingStop[1] and Price < xATRTrailingStop[1] then  
		cur_pos = -1
	else 
		cur_pos = 0;
end;

Now let’s compute buy and sell signals, their logic is pretty simple:

above_sig = Price >= xATRTrailingStop and Price[1] < xATRTrailingStop[1];
below_sig = xATRTrailingStop >= Price and xATRTrailingStop[1] < Price[1];

buy_sig  = Price > xATRTrailingStop and above_sig;
sell_sig = Price < xATRTrailingStop and below_sig;

The last thing is to trigger orders. You can see that code for orders looks almost like a human language:

if sell_sig then
	SellShort  ( !( "UT_BOT_SE" ) ) next bar at market;

if buy_sig then
	BuyToCover ( !( "UT_BOT_SX" ) ) next bar at market;

if buy_sig then
	Buy ( !( "UT_BOT_LE" ) ) next bar at market;

if sell_sig then
	Sell ( !( "UT_BOT_" ) ) next bar at market;

Next save your script and validate it. If you won’t see any errors then we’re ready to apply it to your chart:

Backtesting Strategy

To apply your strategy to your current chart you can click Studies -> Add Strategy. Find the name of your strategy and add it.

After that you should see orders plotted on your screen:

To see strategy performance you can click Data -> Strategy Performance report:

You should see performance like that in a popup:

Entire code used in this article:

inputs: 
	Price( Close ) [DisplayName = "Price"], 
	sens( 1 ) [DisplayName = "Sensitivity"], 
	atr_len( 10 ) [DisplayName = "ATR Length"];
	
variables: 
	xATR( 0 ),
	nLoss (0), 
	xATRTrailingStop(0),
	cur_pos(0),
	above_sig(false), 
	below_sig(false),
	buy_sig(false), 
	sell_sig(false);

xATR  = average(TrueRange, atr_len);
nLoss = sens * xATR;

if Price > xATRTrailingStop[1] and Price[1] > xATRTrailingStop[1] then 
	xATRTrailingStop = Maxlist(xATRTrailingStop[1], Price - nLoss)
else begin
	if Price < xATRTrailingStop[1] and Price[1] < xATRTrailingStop[1] then  
		xATRTrailingStop = Minlist(xATRTrailingStop[1], Price + nLoss)
	else begin
		if Price > xATRTrailingStop[1] then 
			xATRTrailingStop = Price - nLoss
		else 
			xATRTrailingStop = Price + nLoss;
	end;
end;


if Price[1] < xATRTrailingStop[1] and Price > xATRTrailingStop[1] then 
	cur_pos = 1
else begin
	if Price[1] > xATRTrailingStop[1] and Price < xATRTrailingStop[1] then  
		cur_pos = -1
	else 
		cur_pos = 0;
end;

above_sig = Price >= xATRTrailingStop and Price[1] < xATRTrailingStop[1];
below_sig = xATRTrailingStop >= Price and xATRTrailingStop[1] < Price[1];

buy_sig  = Price > xATRTrailingStop and above_sig;
sell_sig = Price < xATRTrailingStop and below_sig;

if sell_sig then
	SellShort  ( !( "UT_BOT_SE" ) ) next bar at market;

if buy_sig then
	BuyToCover ( !( "UT_BOT_SX" ) ) next bar at market;

if buy_sig then
	Buy ( !( "UT_BOT_LE" ) ) next bar at market;

if sell_sig then
	Sell ( !( "UT_BOT_" ) ) next bar at market;

Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news