Triggering conditions only when previous condition has been met any number of bars before

apaul015

New member
I'm trying to code short and cover signals based on a break of the Pre-market Lows, basically short when a candle closes below PM low and cover when a candle closes above the 9 EMA.

I'm having trouble writing a script that only triggers the Cover signal when a Short signal has been "true" in the last "x" amount of bars. For example, I'd have a Short signal, then a Cover signal, but then another Cover signal right after without there being a Short signal. I'm basically trying to say that once there is a Short Signal, I'm in an active position, and I can only Cover when there's an active position.

Can anyone help me out? I'm thinking I need to use fold, but I can't seem to figure it out.

Here's what I'm working with right now:

def sc1 = close < PMLow;
def sc2 = close[1] >= PMLow;

def short = sc1 and sc2;

def cc1 = open[1] < PMLow;
def cc1 = close > ema
or close > high[1]
or close > PMLow
or SecondsTillTime(1550) == 0;

def cover = cc1 and cc2;

The problem is my cc1 condition. The Cover triggers for the first Short, but then it would immediately Cover again if Price is waaaay below the PM Low.
 
Last edited:
@apaul015
See if this works for you.
Ruby:
def sc1 = close < PMLow and close[1] >= PMLow;
def cc1 = open[1] < PMLow and (close > ema or close > high[1] or close > PMLow or SecondsTillTime(1550) == 0);

def short;
def cover;

if sc1 {
short = yes;
cover = Double.NaN;
} else if short[1] and cc1 {
short = Double.NaN;
cover = yes;
}else{
short = short[1];
cover = cover[1];
}
 

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

@apaul015
See if this works for you.
Ruby:
def sc1 = close < PMLow and close[1] >= PMLow;
def cc1 = open[1] < PMLow and (close > ema or close > high[1] or close > PMLow or SecondsTillTime(1550) == 0);

def short;
def cover;

if sc1 {
short = yes;
cover = Double.NaN;
} else if short[1] and cc1 {
short = Double.NaN;
cover = yes;
}else{
short = short[1];
cover = cover[1];
}


@Svanoy

Thanks for the idea, but unfortunately doesn't work for me. Would you mind explaining your thought process for this? I feel like it's really close.
I'm still getting a double cover because my cover conditions are still true, but my short conditions are dependent on a candle close below the PM Low after opening above. Still thinking through how to adjust this.

Sorry, I'm still relatively new to coding so it takes me a while to conceptualize things.
 
@Svanoy

Thanks for the idea, but unfortunately doesn't work for me. Would you mind explaining your thought process for this? I feel like it's really close.
I'm still getting a double cover because my cover conditions are still true, but my short conditions are dependent on a candle close below the PM Low after opening above. Still thinking through how to adjust this.

Sorry, I'm still relatively new to coding so it takes me a while to conceptualize things.

@Svanoy

I think the problem is my cover conditions and there's too much "or" statements, so it can give a signal to cover up to three times within the same candle! I'm thinking how to rework this, but if you have any ideas I'd appreciate it.
 
My code above carries the value of the short variable to signal that a position is open and cannot trigger a close without 'short' being true.
I'm still getting a double cover because my cover conditions are still true,
I have a feeling your original post is not your complete code.
What does your code for triggering the cover signal look like?

The biggest issues right off are
1. Your sc1 condition of close<PMLow. This could trigger mulitple times during a candle and then at the close of the candle, close could end up being above PMLow.

2. Your close condition should be using high instead of close.
 
Last edited:
You could also break up your close conditions and use an order line for each.
Ruby:
def sc1 = close[1] < APLow and close[2] >= APLow;
def cc1 = open[1] < APLow and high > ema;
def cc2 = open[1] < APLow and high > high[1];
def cc3 = open[1] < APLow and high > APLow;
def cc4 = open[1] < APLow and SecondsTillTime(1550) == 0;

def short;
def cover1;
def cover2;
def cover3;
def cover4;

if sc1 {
    short = yes;
    cover1 = Double.NaN;
    cover2 = Double.NaN;
    cover3 = Double.NaN;
    cover4 = Double.NaN;
} else if short[1] and cc1 {
    short = Double.NaN;
    cover1 = yes;
    cover2 = Double.NaN;
    cover3 = Double.NaN;
    cover4 = Double.NaN;
} else if short[1] and cc2 {
    short = Double.NaN;
    cover1 = Double.NaN;
    cover2 = yes;
    cover3 = Double.NaN;
    cover4 = Double.NaN;
} else if short[1] and cc3 {
    short = Double.NaN;
    cover1 = Double.NaN;
    cover2 = Double.NaN;
    cover3 = yes;
    cover4 = Double.NaN;
} else if short[1] and cc4 {
    short = Double.NaN;
    cover1 = Double.NaN;
    cover2 = Double.NaN;
    cover3 = Double.NaN;
    cover4 = yes;
} else {
    short = short[1];
    cover1 = cover1[1];
    cover2 = cover2[1];
    cover3 = cover3[1];
    cover4 = cover4[1];
}

AddOrder(OrderType.SELL_TO_OPEN, condition = short, price = open, tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");
AddOrder(OrderType.BUY_TO_CLOSE, condition = cover1, price = ema, tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");
AddOrder(OrderType.BUY_TO_CLOSE, condition = cover2, price = high[1], tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");
AddOrder(OrderType.BUY_TO_CLOSE, condition = cover3, price = APLow, tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");
AddOrder(OrderType.BUY_TO_CLOSE, condition = cover4, price = close, tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");

L8tXrdf.png
 
So I'm actually using this as a greater backtest I'm using with Strategies on a 5 min chart. I think since I'm looking at past data, the close<PMLow condition should be okay, right?

I'm trying to see my profitability based on different cover conditions.
I've been working on this all day so it's been revised a bit, but here's the code I'm working with right now.
I divided the conditions for the short and cover just so I could see it better for myself. For the cover conditions, I want it to trigger if cc1 and cc2 is true with any of the following of cc3 through cc6. The problem is that if any combination of cc3 through cc6 is valid, then it'll trigger twice.

def markethours = GetTime() > RegularTradingStart(GetYYYYMMDD()) and SecondsTillTime(1545) > 0;


def EOD = today and SecondsFromTime(1350) > 0;
def opr = SecondsFromTime(0930) >= 0 and SecondsTillTime(0935) > 0;

## SHORT CONDITIONS ##

def sc1 = markethours;
def sc2 = close < PMLow;
def sc3 = if opr then open > PMLow else if !opr then close[1] >= PMLow else Double.NaN;


## COVER CONDITIONS ##
def cc1 = markethours;
def cc2 = open[1] < PMLow;
def cc3 = close > ema;
def cc4 = close > high[1];
def cc5 = close > PMLow;
def cc6 = SecondsTillTime(1550) == 0;


def short;
def cover;

if sc1 and sc2 and sc3 {
short = yes;
cover = double.nan;
} else if (short[1] and cc1 and cc2) or cc3 or cc4 or cc5 or cc6 {
short = double.nan;
cover = yes;
}else{
short = short[1];
cover = cover[1];
}
 
You could also break up your close conditions and use an order line for each.
Ruby:
def sc1 = close[1] < APLow and close[2] >= APLow;
def cc1 = open[1] < APLow and high > ema;
def cc2 = open[1] < APLow and high > high[1];
def cc3 = open[1] < APLow and high > APLow;
def cc4 = open[1] < APLow and SecondsTillTime(1550) == 0;

def short;
def cover1;
def cover2;
def cover3;
def cover4;

if sc1 {
    short = yes;
    cover1 = Double.NaN;
    cover2 = Double.NaN;
    cover3 = Double.NaN;
    cover4 = Double.NaN;
} else if short[1] and cc1 {
    short = Double.NaN;
    cover1 = yes;
    cover2 = Double.NaN;
    cover3 = Double.NaN;
    cover4 = Double.NaN;
} else if short[1] and cc2 {
    short = Double.NaN;
    cover1 = Double.NaN;
    cover2 = yes;
    cover3 = Double.NaN;
    cover4 = Double.NaN;
} else if short[1] and cc3 {
    short = Double.NaN;
    cover1 = Double.NaN;
    cover2 = Double.NaN;
    cover3 = yes;
    cover4 = Double.NaN;
} else if short[1] and cc4 {
    short = Double.NaN;
    cover1 = Double.NaN;
    cover2 = Double.NaN;
    cover3 = Double.NaN;
    cover4 = yes;
} else {
    short = short[1];
    cover1 = cover1[1];
    cover2 = cover2[1];
    cover3 = cover3[1];
    cover4 = cover4[1];
}

AddOrder(OrderType.SELL_TO_OPEN, condition = short, price = open, tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");
AddOrder(OrderType.BUY_TO_CLOSE, condition = cover1, price = ema, tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");
AddOrder(OrderType.BUY_TO_CLOSE, condition = cover2, price = high[1], tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");
AddOrder(OrderType.BUY_TO_CLOSE, condition = cover3, price = APLow, tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");
AddOrder(OrderType.BUY_TO_CLOSE, condition = cover4, price = close, tradeSize = 1, tickcolor = Color.WHITE, arrowcolor = Color.WHITE,"");

L8tXrdf.png


Didn't see you post this before I replied, I'll take a look! This might work a lot better.
 
@apaul015
So I'm actually using this as a greater backtest I'm using with Strategies on a 5 min chart. I think since I'm looking at past data, the close<PMLow condition should be okay, right?
It's ok'ish for backtesting only, but not good practice.

} else if (short[1] and cc1 and cc2) or cc3 or cc4 or cc5 or cc6 {
The parentheses is line make it true during 5 different instances:
1. short[1] and cc1 and cc2 all being true.
2. cc3 being true on its own.
3. cc4 being true on its own.
4. cc5 being true on its own.
5. cc6 being true on its own.

I want it to trigger if cc1 and cc2 is true with any of the following
Lets make this a condition of it own just for ease of writing the code.
Ruby:
def ccMain = short[1] and cc1 and cc2;
We can now define each close condition easier.
Ruby:
} else ccMain and (cc3 or cc4 or cc5 or cc6) {
Now, if either cc3, cc4, cc5, or cc6 are true, ccMain has to be true as well for the line of code to return true.

You will still need to use a different order lines to close the position as the prices at which the position closes are different depending on which condition is triggered.
 
@apaul015

It's ok'ish for backtesting only, but not good practice.


The parentheses is line make it true during 5 different instances:
1. short[1] and cc1 and cc2 all being true.
2. cc3 being true on its own.
3. cc4 being true on its own.
4. cc5 being true on its own.
5. cc6 being true on its own.


Lets make this a condition of it own just for ease of writing the code.
Ruby:
def ccMain = short[1] and cc1 and cc2;
We can now define each close condition easier.
Ruby:
} else ccMain and (cc3 or cc4 or cc5 or cc6) {
Now, if either cc3, cc4, cc5, or cc6 are true, ccMain has to be true as well for the line of code to return true.

You will still need to use a different order lines to close the position as the prices at which the position closes are different depending on which condition is triggered.


I actually tweaked it the way you posted earlier about breaking it up, and it works the way I intended! Really appreciate it, I learned a lot!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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