Simple Breakout Strategy!

BULLFIZZ

Member
@barbaros can you please fix the below script, tried to load it up and i think something is not right. thank you very much.


# Define the length of the breakout period
input breakoutLength = 10;

# Define the amount by which the price must break out
input breakoutThreshold = 2;

# Define the amount of time to hold the position after a breakout
input holdPeriod = 5;

# Calculate the high and low prices over the breakout period
def high = Highest(high, breakoutLength);
def low = Lowest(low, breakoutLength);

# Check if the price has broken out
def breakout = if high[1] < high and low[1] > low then 1 else 0;

# Calculate the entry price for long and short positions
def longEntry = high + breakoutThreshold * tickSize;
def shortEntry = low - breakoutThreshold * tickSize;

# Set the entry and exit conditions for long and short positions
def longCondition = if !breakout[1] and breakout and close > longEntry then 1 else 0;
def shortCondition = if !breakout[1] and breakout and close < shortEntry then 1 else 0;

# Enter a long position if the long condition is met
if longCondition and !IsLong() then
Buy("Long Breakout") next bar at longEntry stop;

# Enter a short position if the short condition is met
if shortCondition and !IsShort() then
SellShort("Short Breakout") next bar at shortEntry stop;

# Exit the long position after the hold period has passed
if IsLong() and !longCondition and !shortCondition and barsSinceEntry > holdPeriod then
Sell("Long Exit") next bar at market;

# Exit the short position after the hold period has passed
if IsShort() and !longCondition and !shortCondition and barsSinceEntry > holdPeriod then
BuyToCover("Short Exit") next bar at market;
 

barbaros

Administrator
Staff member
I’m not familiar with the buy/sell syntax used there. Do you know who created this? As if a conversion from Thinkscript was not completed. However, I will take a shot at rewriting it for Thinkscript if you like.
 

BULLFIZZ

Member
I’m not familiar with the buy/sell syntax used there. Do you know who created this? As if a conversion from Thinkscript was not completed. However, I will take a shot at rewriting it for Thinkscript if you like.

If you can rewrite it that would be very helpful.
Thank you
 

barbaros

Administrator
Staff member
Here is an attempt. Still not working but trying to understand what the author is trying to do.

Code:
# Define the length of the breakout period
input breakoutLength = 10;

# Define the amount by which the price must break out
input breakoutThreshold = 2;

# Define the amount of time to hold the position after a breakout
input holdPeriod = 5;

# Calculate the high and low prices over the breakout period
def high_ = Highest(high, breakoutLength);
def low_ = Lowest(low, breakoutLength);

# Check if the price has broken out
def breakout = if high_[1] < high_ and low_[1] > low_ then 1 else 0;

# Calculate the entry price for long and short positions
def longEntry = high_ + breakoutThreshold * TickSize();
def shortEntry = low_ - breakoutThreshold * TickSize();

# Set the entry and exit conditions for long and short positions
def longCondition = if !breakout[1] and breakout and close > longEntry then 1 else 0;
def shortCondition = if !breakout[1] and breakout and close < shortEntry then 1 else 0;

# Order direction
def Entry = if isNaN(EntryPrice()) then Entry[1] else EntryPrice();
def EntryBar = if Entry != Entry[1] then barNumber() else EntryBar[1];
def barsSinceEntry = barNumber() - EntryBar;

def direction = if longCondition and direction[1] != 1 then 1
                else if direction[1] == 1 and !longCondition and !shortCondition and barsSinceEntry > holdPeriod then 0
                else if shortCondition and direction[1] != -1 then -1
                else if direction[1] == -1 and !longCondition and !shortCondition and barsSinceEntry > holdPeriod then 0
                else direction[1];

# Enter a long position if the long condition is met
#if longCondition and !IsLong() then
#Buy("Long Breakout") next bar at longEntry stop;
addOrder(OrderType.BUY_AUTO, direction crosses above 0);

# Enter a short position if the short condition is met
#if shortCondition and !IsShort() then
#SellShort("Short Breakout") next bar at shortEntry stop;
addOrder(OrderType.SELL_AUTO, direction crosses below 0);

# Exit the long position after the hold period has passed
#if IsLong() and !longCondition and !shortCondition and barsSinceEntry > holdPeriod then
#Sell("Long Exit") next bar at market;
addOrder(OrderType.SELL_TO_CLOSE, direction[1] == 1 and direction == 0);

# Exit the short position after the hold period has passed
#if IsShort() and !longCondition and !shortCondition and barsSinceEntry > holdPeriod then
#BuyToCover("Short Exit") next bar at market;
addOrder(OrderType.BUY_TO_CLOSE, direction[1] == -1 and direction == 0);
 

BULLFIZZ

Member
Thank you for your efforts @barbaros , i was actually using chatgpt to get the script, kindly check this one below.

# Define the length of the lookback period (in bars)
input lookbackPeriod = 20;

# Define the threshold for a breakout (in percent)
input threshold = 5;

# Calculate the high and low for the lookback period
def high = Highest(high, lookbackPeriod);
def low = Lowest(low, lookbackPeriod);

# Calculate the breakout price
def breakoutPrice = (1 + threshold / 100) * (high - low) + low;

# Plot the breakout price
plot breakout = breakoutPrice;
breakout.SetPaintingStrategy(PaintingStrategy.LINE);
breakout.SetLineWeight(2);
breakout.SetDefaultColor(Color.YELLOW);

# Buy when the price crosses above the breakout price
if (close > breakoutPrice) {
Alert("Breakout detected! Buying now.");
Buy();
}
 

barbaros

Administrator
Staff member
Ah, got it. You need to have a state variable to hold the current condition and reset. In the latest one you attached, logic states that it will alert on every bar that is higher than the breakout price.
 
Top