Need help with TOS strategy script

jgj

New member
VIP
I have a TOS strategy that uses Heikin Ashi, Moving average, and RSI to enter and exit trades. I'm not a programmer but I managed to copy bits and pieces of code from different studies to create the script. It creates a buy signal on the first green heikin ashi candle, where the close is above the moving average, and the RSI > 50. It then creates a sell signal either on the first red heikin ashi candle or when the close is below the moving average. After exiting the trade, if the RSI is still above 50 and the close is still above the moving average, it re-enters the trade again.

It seems to work and it shows the buy and sell signals and re-entries , but I'm not sure if the profits I see on the floating PL is correct.
I tested it on TQQQ, using $1000 on a 5 year daily chart and the floating pl is showing $7098 (shown on bottom left of image and circled) Is this correct? if not, where's my error? Thanks in advance for any help you can give me.


mod note:
The below syntax was rewritten to fix the syntax so it does not repaint.
This script can ONLY be cut & pasted into the STRATEGY tab

Here's the script:
input tradeDollars = 1000;

input price = close;
input ma1_length = 8;
#input ma2_length = 13;
#input ma3_length = 21;
input ma1_type = AverageType.HULL;
#input ma2_type = AverageType.HULL;
#input ma3_type = AverageType.HULL;
input length = 14;
input over_bought = 70;
input over_sold = 30;
input rsiAverageType = AverageType.WILDERS;

def tradesize = Round (tradeDollars / close );
def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);


def haclose = (open + high + low + close) / 4;
def haopen = CompoundValue(1, (haopen[1] + haclose[1]) / 2, (open[1] + close[1]) / 2);

#plot entry_price = haopen;
#entry_price.setpaintingStrategy(paintingStrategy.DASHES);
#entry_price.setdefaultcolor(color.white);

#plot rsi = reference rsi();
#rsi.setPaintingStrategy(paintingStrategy.VALUES_BELOW);
#rsi.hide();


plot ma1 = MovingAverage(ma1_type, haclose, ma1_length);
ma1.SetDefaultColor(Color.WHITE);
#plot ma2 = MovingAverage(ma2_type, haclose, ma2_length);
#ma2.SetDefaultColor(Color.orange);
#plot ma3 = MovingAverage(ma3_type, haclose, ma3_length);
#ma3.SetDefaultColor(Color.red);

def green = haclose > haopen;
def red = haclose <= haopen;

def buy = green and haclose > ma1 and rsi > 30;

#def buy = green and haclose > ma1 and haclose > ma2 and haclose > ma3 and rsi > 50;

def re_entry = green and !green[1] and haclose > ma1 and rsi > 30;

def sell = red or haclose < ma1;

#def sell = red and haclose < ma and rsi < 50;

#def sell = red or haclose < ma3 ;

#def sell_to_open = red and red[1] and haclose < ma;
#def buy_to_close = !red and red[1];
#def long_close = !green and green[1];

#def re_entry = green and !green[1] and haclose > ma1 and haclose > ma2 and haclose > ma3 and rsi > 30;

#Long
AddOrder(OrderType.BUY_TO_OPEN, buy, open[-1], tradeSize = tradesize, Color.WHITE, Color.gray,"B");
AddOrder(OrderType.BUY_TO_OPEN, re_entry, open[-1], tradeSize = tradesize, Color.GREEN, Color.GREEN, "rE ");
AddOrder(OrderType.SELL_TO_CLOSE, sell, open[-1], tradeSize = tradesize, Color.WHITE, Color.gray, "S");

#AddOrder(OrderType.sell_TO_OPEN, sell_to_open, open[-1], tradesize = tradesize, color.white, color.white, "SE");
#AddOrder(ordertype.buy_TO_CLOSE, buy_to_close, open[-1], tradesize = tradesize, color.white, color.white, "SX");

AddLabel(yes," Tradesize : " +(tradedollars), color.green);
 
Last edited by a moderator:
I have a TOS strategy that uses Heikin Ashi, Moving average, and RSI to enter and exit trades. I'm not a programmer but I managed to copy bits and pieces of code from different studies to create the script. It creates a buy signal on the first green heikin ashi candle, where the close is above the moving average, and the RSI > 50. It then creates a sell signal either on the first red heikin ashi candle or when the close is below the moving average. After exiting the trade, if the RSI is still above 50 and the close is still above the moving average, it re-enters the trade again.

It seems to work and it shows the buy and sell signals and re-entries , but I'm not sure if the profits I see on the floating PL is correct.
I tested it on TQQQ, using $1000 on a 5 year daily chart and the floating pl is showing $7098 (shown on bottom left of image and circled) Is this correct? if not, where's my error? Thanks in advance for any help you can give me.

Please see attached image:



http://tos.mx/kkUtWHs

Here's the script:
input tradeDollars = 1000;

input price = close;
input ma1_length = 8;
#input ma2_length = 13;
#input ma3_length = 21;
input ma1_type = AverageType.HULL;
#input ma2_type = AverageType.HULL;
#input ma3_type = AverageType.HULL;
input length = 14;
input over_bought = 70;
input over_sold = 30;
input rsiAverageType = AverageType.WILDERS;

def tradesize = Round (tradeDollars / close );
def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);


def haclose = (open + high + low + close) / 4;
def haopen = CompoundValue(1, (haopen[1] + haclose[1]) / 2, (open[1] + close[1]) / 2);

#plot entry_price = haopen;
#entry_price.setpaintingStrategy(paintingStrategy.DASHES);
#entry_price.setdefaultcolor(color.white);

#plot rsi = reference rsi();
#rsi.setPaintingStrategy(paintingStrategy.VALUES_BELOW);
#rsi.hide();


plot ma1 = MovingAverage(ma1_type, haclose, ma1_length);
ma1.SetDefaultColor(Color.WHITE);
#plot ma2 = MovingAverage(ma2_type, haclose, ma2_length);
#ma2.SetDefaultColor(Color.orange);
#plot ma3 = MovingAverage(ma3_type, haclose, ma3_length);
#ma3.SetDefaultColor(Color.red);

def green = haclose > haopen;
def red = haclose <= haopen;

def buy = green and haclose > ma1 and rsi > 30;

#def buy = green and haclose > ma1 and haclose > ma2 and haclose > ma3 and rsi > 50;

def re_entry = green and !green[1] and haclose > ma1 and rsi > 30;

def sell = red or haclose < ma1;

#def sell = red and haclose < ma and rsi < 50;

#def sell = red or haclose < ma3 ;

#def sell_to_open = red and red[1] and haclose < ma;
#def buy_to_close = !red and red[1];
#def long_close = !green and green[1];

#def re_entry = green and !green[1] and haclose > ma1 and haclose > ma2 and haclose > ma3 and rsi > 30;

#Long
AddOrder(OrderType.BUY_TO_OPEN, buy, open, tradeSize = tradesize, Color.WHITE, Color.gray,"B");
AddOrder(OrderType.BUY_TO_OPEN, re_entry, open, tradeSize = tradesize, Color.GREEN, Color.GREEN, "rE ");
AddOrder(OrderType.SELL_TO_CLOSE, sell, open, tradeSize = tradesize, Color.WHITE, Color.gray, "S");

#AddOrder(OrderType.sell_TO_OPEN, sell_to_open, open, tradesize = tradesize, color.white, color.white, "SE");
#AddOrder(ordertype.buy_TO_CLOSE, buy_to_close, open, tradesize = tradesize, color.white, color.white, "SX");

AddLabel(yes," Tradesize : " +(tradedollars), color.green);

you are using open for a buy , instead of open[-1] to buy on the next bar.
so you are getting a buy signal during a candle, then using that bars open to buy, same thing as going back in time.
can't be done.

replace the addorders with these...
Code:
AddOrder(OrderType.BUY_TO_OPEN, buy, open[-1], tradeSize = tradesize, Color.WHITE, Color.gray,"B");
AddOrder(OrderType.BUY_TO_OPEN, re_entry, open[-1], tradeSize = tradesize, Color.GREEN, Color.GREEN, "rE ");
AddOrder(OrderType.SELL_TO_CLOSE, sell, open[-1], tradeSize = tradesize, Color.WHITE, Color.gray, "S");

and most stocks will show a loss
 
  • Like
Reactions: jgj

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

you are using open for a buy , instead of open[-1] to buy on the next bar.
so you are getting a buy signal during a candle, then using that bars open to buy, same thing as going back in time.
can't be done.

replace the addorders with these...
Code:
AddOrder(OrderType.BUY_TO_OPEN, buy, open[-1], tradeSize = tradesize, Color.WHITE, Color.gray,"B");
AddOrder(OrderType.BUY_TO_OPEN, re_entry, open[-1], tradeSize = tradesize, Color.GREEN, Color.GREEN, "rE ");
AddOrder(OrderType.SELL_TO_CLOSE, sell, open[-1], tradeSize = tradesize, Color.WHITE, Color.gray, "S");

and most stocks will show a loss
Thank you. Now I understand what I did wrong. I appreciate it.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
295 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