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:
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);
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: