How to say set a condition of preventing shortselling in a strategy?

Lunvrsolace

New member
Hey guys i've been working on a HMA strategy for tos. the problem right now is is i dont know how to set a strategy condition that says you can only long as the next order after the last trade closed Or to not trigger a sell signal until there’s at least been 1 buy signal before..
I've been back testing the code to see its accuracy and i see that it keeps getting caught in short positions at the beginning of an uptrend if it has enough strength.

VveVCrX.jpg


Opened a short position right after a 'Close"

R362C9m.jpg


Same thing here. basically want the strategy to be inactive until there's a "buy" or "avg down" after a "close"


Heres Code that can be used as a study on the 1Min Chart

Code:
input price = close;
input displace = 0;

input over_sold = -100;
input over_bought = 100;


def linDev = lindev(price, 1000);
def CCI = if linDev == 0 then 0 else (price - Average(price, 1000)) / linDev / 0.015;

def EMA1 = MovingAverage(AverageType.EXPONENTIAL, price, 90);
def HMA1 = MovingAverage(AverageType.HULL, price, 1800)[-displace];
def HMA2 = MovingAverage(AverageType.HULL, price, 180)[-displace];
def HMA3 = MovingAverage(AverageType.HULL, price, 40)[-displace];

def MainBullishZone = HMA1 > HMA1[6];
def MainBearishZone = HMA1 < HMA1[6];
def SubBullishZone = HMA2 > HMA2[200];
def SubBearishZone = HMA2 < HMA2[35];
def IntraBullishZone = HMA3 > HMA3[10];
def IntraBearishZone = HMA3 < HMA3[10];

plot BUYSignal = MainBullishZone and SubBearishZone and IntraBullishZone and (HMA1 < HMA2) and (HMA1 < HMA3) and (HMA3 < HMA2) and price < HMA3 and price < EMA1 and CCI < 100;

plot AvgDownSignal = MainBullishZone and SubBearishZone and intraBullishZone and price < HMA1 and price < HMA2 and CCI < 100 and HMA2 < HMA2[155];

plot BuyExitSignal = price crosses below HMA2 and MainBullishZone and SubBullishZone and IntraBearishZone and price > HMA1;

plot SELLSignal = MainBearishZone and SubBullishZone and IntraBearishZone and (HMA1 > HMA2) and (HMA1 > HMA3) and (HMA3 > HMA2)and price < HMA3 and price > EMA1 and CCI > -100;


BUYSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BUYSignal.SetDefaultColor(Color.Blue);

AvgDownSignal.SetpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
AvgDownSignal.SetDefaultColor(Color.DARK_GREEN);

BuyExitSignal.SetpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
BuyExitSignal.SetDefaultColor(Color.DARK_GREEN);

SELLSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SELLSignal.SetDefaultColor(Color.RED);


Heres the code to be used as the strategy on the 1Min Chart

Code:
##### Betsy #####
#CONSERVATIVE SETTINGS*
# MainLength = 1800
# Sensitivity = 6

#AGGRESSIVE SETTINGS*
# MainLength = 1000
# Sensitivity = 15



input price = close;
input MainLength = 1000;
input Sensitivity = 15;
input BuyAmount = 20;
input SellAmount = 1;
input SellHeavyAmount = 35;
input AvgDownAmount = 20;


def linDev = LinDev(price, 1000);
def CCI = if linDev == 0 then 0 else (price - Average(price, 1000)) / linDev / 0.015;
def CCIDrop = CCI > CCI[1];

def EMA1 = MovingAverage(AverageType.EXPONENTIAL, price, 90);
def HMA1 = MovingAverage(AverageType.HULL, price, MainLength);
def HMA2 = MovingAverage(AverageType.HULL, price, 180);
def HMA3 = MovingAverage(AverageType.HULL, price, 40);

def MainBullishZone = HMA1 > HMA1[Sensitivity];
def MainBearishZone = HMA1 < HMA1[Sensitivity] and CCIDrop;
def SubBullishZone = HMA2 > HMA2[200];
def SubBearishZone = HMA2 < HMA2[35];
def IntraBullishZone = HMA3 > HMA3[10];
def IntraBearishZone = HMA3 < HMA3[10];

def BUYSignal = MainBullishZone and SubBearishZone and IntraBullishZone and (HMA1 < HMA2) and (HMA1 < HMA3) and (HMA3 < HMA2) and price < HMA3 and price < EMA1 and CCI < 100;

def AvgDownSignal = MainBullishZone and SubBearishZone and IntraBullishZone and price < HMA1 and price < HMA2 and CCI < 100 and HMA2 < HMA2[155];

def BuyExitSignal = price crosses below HMA2 and MainBullishZone and SubBullishZone and IntraBearishZone and price > HMA1 and CCI < 100;

def SELLSignal = MainBearishZone and SubBullishZone and IntraBearishZone and (HMA1 > HMA2) and (HMA1 > HMA3) and (HMA3 > HMA2) and price < HMA3 and price > EMA1;

def EmergencySELL = SubBearishZone and CCI < -30;

def EmergencyCOVER = CCI > 5 and CCI < 100 and CCI > CCI[200];

def Short =  price crosses below HMA2 and MainBullishZone and SubBullishZone and IntraBearishZone and price > HMA1 and CCI > 140;


input crossingType = {default above, below};

def avg1 = MovingAverage(AverageType.EXPONENTIAL, close, 60);
def avg2 = MovingAverage(AverageType.EXPONENTIAL, close, 180);

def signal = Crosses(avg1, avg2, crossingType == crossingType.above);
def signal2 = avg1 crosses above avg2;

AddOrder(condition = BUYSignal, name = "BUY", price = close, tradeSize = BuyAmount);
AddOrder(condition = AvgDownSignal, name = "AvgDown", tradeSize = AvgDownAmount);
AddOrder(OrderType.SELL_AUTO, BuyExitSignal, name = "Sell", tradeSize = SellAmount);
AddOrder(OrderType.SELL_AUTO, Short, name = "Sell Heavy", tradeSize = SellHeavyAmount);
AddOrder(condition = MainBearishZone, name = "Close", type = OrderType.SELL_TO_CLOSE);
AddOrder(OrderType.BUY_TO_CLOSE, SELLSignal, name = "COVER");
 
Last edited:

jmoney

New member
I'm a bit confused by the question, but it sounds like you don't want this strategy to short at all. If that is the case, you can remove the last three lines of the strategy, and change the SELL_AUTO for buy exit signal to SELL_TO_CLOSE. I hope that helps
 

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.
Top