Consecutive Trades


Category:
0
0

Hello,

I have a strategy that can go long and short. I would like to add a condition to prevent the strategy to open a new trade if the previous one (doesn’t matter if long or short) is to close, meaning within a certain number of bars.

Is there a simple way of doing this?

Many thanks,

RT

Marked as spam
Posted by (Questions: 7, Answers: 6)
Asked on January 5, 2017 12:34 pm
128 views
0
Private answer

From the title of your question and this statement from the question itself.

“to prevent the strategy to open a new trade if the previous one…is to close…within a certain number of bars”

So it seems like you are wanting to hold all orders that open a new position until a certain number of bars have passed since the most recent closing order?

If that is the case, try this:

def conditionForClosingOrder = <some sort of true/false condition>;
def okToOpenNewPosition = if Highest(conditionForClosingOrder, numberOfBars) < 1 then yes else no;

Explanation:

In Thinkscript we have the following:

true == 1 == yes

false == 0 == no

You have your condition that executes the closing trade when it’s value is equal to true(1). We use an ‘if’ statement to test if that condition as been true anytime in the previous numberOfBars. Ok, so it’s either a 1 or a 0 for each bar. 1 is greater than 0 so we can check for this.

 

So if your condition that closes a trade is true, that bar will equal a 1, for true. So then we use the function named “Highest()” to get the highest value of that condition within the previous numberOfBars. If the highest value is 1, we have a position closing condition within that number of bars. If the highest value is 0, we have no closing conditions within those bars.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 5, 2017 1:17 pm