Strategy exits me out later than I want, sometimes.

WildTiger99

New member
I have a strategy that is quite simple; basically when close crosses (a moving average), I tell the strat to get long. I then code the script to exit out four bars later (weekly or daily). As I'm viewing the chart, every position exits out the position four bars forward, except the last trade either exits out six bars later (XLE) or doesn't exit out at all for another ticker (OIH).

Basic strat: Close crosses above LRadjEMA, get long. Exit the strat after four bars (Bar_numbers). Using weekly charts. XLE or OIH.

I hope someone is out there able to provide an answer, and many thanks in advance!!


Code:
[code]
#The Linear Regression-adjusted EMA strategy
#TASC's September 2022 edition of Traders' Tips includes an article by Vitali Apirine titled "The Linear Regression-Adjusted Exponential Moving Average". This script implements the titular indicator presented in this article.

input price = close;

#LRadjEMA
#input price = close;
input length = 50;
input lrLength = 50;
input highLowLength = 50;
input multiplier = 3;

def lr = Inertia(price, lrLength);
def dist = AbsValue(price - lr);
def maxDist = Highest(dist, highLowLength);
def minDist = Lowest(dist, highLowLength);
def multiplier1 = 2 / (length + 1);
def multiplier2 = multiplier * ((dist - minDist) / (maxDist - minDist));
def alpha = multiplier1 * (1 + multiplier2);
def ma = CompoundValue(1, ma[1] + alpha * (price - ma[1]), Average(price, length));

plot LRAdjEMA = ma;
LRAdjEMA.SetDefaultColor(GetColor(1));


#condition
def condition = price crosses above LRAdjEMA;


#define entry
def entryPrice = EntryPrice();
def long1 = condition;


#define how to exit the strategy
input exit_type = {default Percentage, Combo, Bar_numbers};
plot exit1;

#count number of bars from long1 entry point
def count = if long1[1] then 1 else count[1] + 1;


input percentage_exit = 1.20;
input percentage_below_entry = .80;
input Bar_numbers = 4;

def a = percentage_below_entry;

switch (exit_type) {
case Percentage:
    exit1 = close > ((percentage_exit) * entryPrice);
case Combo:
    exit1 = close > ((percentage_exit) * entryPrice) or close is less than entryPrice * a;
case Bar_numbers:
    exit1 = count == Bar_numbers;
}


AddOrder(OrderType.BUY_TO_OPEN, long1, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "LRadjEMA_long");
AddOrder(OrderType.SELL_TO_CLOSE, exit1, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "LRadjEMA_exit");
[/CODE]

XLE weekly (image of the XLE weekly w/strat)

I have a strategy that is quite simple; basically when close crosses (a moving average), I tell the strat to get long. I then code the script to exit out four bars later (weekly or daily). As I'm viewing the chart, every position exits out the position four bars forward, except the last trade either exits out six bars later (XLE) or doesn't exit out at all for another ticker (OIH).

Basic strat: Close crosses above LRadjEMA, get long. Exit the strat after four bars (Bar_numbers). Using weekly charts. XLE or OIH.

I hope someone is out there able to provide an answer, and many thanks in advance!!


Code:
[code]
#The Linear Regression-adjusted EMA strategy
#TASC's September 2022 edition of Traders' Tips includes an article by Vitali Apirine titled "The Linear Regression-Adjusted Exponential Moving Average". This script implements the titular indicator presented in this article.

input price = close;

#LRadjEMA
#input price = close;
input length = 50;
input lrLength = 50;
input highLowLength = 50;
input multiplier = 3;

def lr = Inertia(price, lrLength);
def dist = AbsValue(price - lr);
def maxDist = Highest(dist, highLowLength);
def minDist = Lowest(dist, highLowLength);
def multiplier1 = 2 / (length + 1);
def multiplier2 = multiplier * ((dist - minDist) / (maxDist - minDist));
def alpha = multiplier1 * (1 + multiplier2);
def ma = CompoundValue(1, ma[1] + alpha * (price - ma[1]), Average(price, length));

plot LRAdjEMA = ma;
LRAdjEMA.SetDefaultColor(GetColor(1));


#condition
def condition = price crosses above LRAdjEMA;


#define entry
def entryPrice = EntryPrice();
def long1 = condition;


#define how to exit the strategy
input exit_type = {default Percentage, Combo, Bar_numbers};
plot exit1;

#count number of bars from long1 entry point
def count = if long1[1] then 1 else count[1] + 1;


input percentage_exit = 1.20;
input percentage_below_entry = .80;
input Bar_numbers = 4;

def a = percentage_below_entry;

switch (exit_type) {
case Percentage:
    exit1 = close > ((percentage_exit) * entryPrice);
case Combo:
    exit1 = close > ((percentage_exit) * entryPrice) or close is less than entryPrice * a;
case Bar_numbers:
    exit1 = count == Bar_numbers;
}


AddOrder(OrderType.BUY_TO_OPEN, long1, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "LRadjEMA_long");
AddOrder(OrderType.SELL_TO_CLOSE, exit1, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "LRadjEMA_exit");
[/CODE]

GknAzFP.png
 
Last edited:
Solution
I have a strategy that is quite simple; basically when close crosses (a moving average), I tell the strat to get long. I then code the script to exit out four bars later (weekly or daily). As I'm viewing the chart, every position exits out the position four bars forward, except the last trade either exits out six bars later (XLE) or doesn't exit out at all for another ticker (OIH).

Basic strat: Close crosses above LRadjEMA, get long. Exit the strat after four bars (Bar_numbers). Using weekly charts. XLE or OIH.

I hope someone is out there able to provide an answer, and many thanks in advance!!

XLE weekly (image of the XLE weekly w/strat)

added a bubble to display counter value.
the counter is being reset sometimes, by...
I have a strategy that is quite simple; basically when close crosses (a moving average), I tell the strat to get long. I then code the script to exit out four bars later (weekly or daily). As I'm viewing the chart, every position exits out the position four bars forward, except the last trade either exits out six bars later (XLE) or doesn't exit out at all for another ticker (OIH).

Basic strat: Close crosses above LRadjEMA, get long. Exit the strat after four bars (Bar_numbers). Using weekly charts. XLE or OIH.

I hope someone is out there able to provide an answer, and many thanks in advance!!

XLE weekly (image of the XLE weekly w/strat)

added a bubble to display counter value.
the counter is being reset sometimes, by long1, before the counter counts up to 4.
not sure why yet.....


changed the count formula
moved bar_numbers=

this will ignore new triggers during a count up to bar_numbers

Code:
input Bar_numbers = 4;

def count = if bn == 1 then 0
  else if count[1] <= Bar_numbers then count[1] + 1
  else if long1[1] then 1
  else count[1] + 1;

Code:
# strat_exit_onmacross_00


#have chatted with michael furey a few times, from a fb group
#told him about use...
#he made this acct tonight
#WildTiger99
#22-09-14


#============================================

#https://usethinkscript.com/threads/strategy-exits-me-out-later-than-i-want-sometimes.12672/
#Strategy exits me out later than I want, sometimes.
#  WildTiger99  9/14

#I have a strategy that is quite simple; basically when close crosses (a moving average), I tell the strat to get long. I then code the script to exit out four bars later (weekly or daily). As I'm viewing the chart, every position exits out the position four bars forward, except the last trade either exits out six bars later (XLE) or doesn't exit out at all for another ticker (OIH).

#Basic strat: Close crosses above LRadjEMA, get long. Exit the strat after four bars (Bar_numbers). Using weekly charts. XLE or OIH.

#I hope someone is out there able to provide an answer, and many thanks in advance!!


#The script is below:

#*I have a feeling something is amiss with the def count line, or maybe I'm missing something. Or does not having a full week of trading (holidays) mess with the code/timing/etc.??

#The Linear Regression-adjusted EMA strategy
#TASC's September 2022 edition of Traders' Tips includes an article by Vitali Apirine titled "The Linear Regression-Adjusted Exponential Moving Average". This script implements the titular indicator presented in this article.


def bn = barnumber();
def na = double.nan;

input price = close;

#LRadjEMA
#input price = close;
input length = 50;
input lrLength = 50;
input highLowLength = 50;
input multiplier = 3;

def lr = Inertia(price, lrLength);
def dist = AbsValue(price - lr);
def maxDist = Highest(dist, highLowLength);
def minDist = Lowest(dist, highLowLength);
def multiplier1 = 2 / (length + 1);
def multiplier2 = multiplier * ((dist - minDist) / (maxDist - minDist));
def alpha = multiplier1 * (1 + multiplier2);
def ma = CompoundValue(1, ma[1] + alpha * (price - ma[1]), Average(price, length));

plot LRAdjEMA = ma;
LRAdjEMA.SetDefaultColor(GetColor(1));


#condition
def condition = price crosses above LRAdjEMA;


#define entry
def entryPrice = EntryPrice();
def long1 = condition;



#count number of bars from long1 entry point
#def count = if long1[1] then 1 else count[1] + 1;

input Bar_numbers = 4;

def count = if bn == 1 then 0
  else if count[1] <= Bar_numbers then count[1] + 1
  else if long1[1] then 1
  else count[1] + 1;



input percentage_exit = 1.20;
input percentage_below_entry = .80;


def a = percentage_below_entry;


#define how to exit the strategy
input exit_type = {default Percentage, Combo, Bar_numbers};

def exit1;
switch (exit_type) {
case Percentage:
exit1 = close > ((percentage_exit) * entryPrice);
case Combo:
exit1 = close > ((percentage_exit) * entryPrice) or close is less than entryPrice * a;
case Bar_numbers:
exit1 = count == Bar_numbers;
}

input test2 = no;
addchartbubble(test2, low*0.99,
count
, (if count <= (bar_numbers + 1) then color.yellow else color.gray), no);


AddOrder(OrderType.BUY_TO_OPEN, long1, tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "LRadjEMA_long");
AddOrder(OrderType.SELL_TO_CLOSE, exit1, tickcolor = GetColor(5), arrowcolor = GetColor(5), name = "LRadjEMA_exit");

#
 
Last edited:
Solution

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

added a bubble to display counter value.
the counter is being reset sometimes, by long1, before the counter counts up to 4.
not sure why yet.....


changed the count formula
moved bar_number=

this will ignore new triggers during a count up to bar_number

Code:
input Bar_numbers = 4;

def count = if bn == 1 then 0
  else if count[1] <= Bar_numbers then count[1] + 1
  else if long1[1] then 1
  else count[1] + 1;

Code:
# strat_exit_onmacross_00


#have chatted with michael furey a few times, from a fb group
#told him about use...
#he made this acct tonight
#WildTiger99
#22-09-14


#============================================

#https://usethinkscript.com/threads/strategy-exits-me-out-later-than-i-want-sometimes.12672/
#Strategy exits me out later than I want, sometimes.
#  WildTiger99  9/14

#I have a strategy that is quite simple; basically when close crosses (a moving average), I tell the strat to get long. I then code the script to exit out four bars later (weekly or daily). As I'm viewing the chart, every position exits out the position four bars forward, except the last trade either exits out six bars later (XLE) or doesn't exit out at all for another ticker (OIH).

#Basic strat: Close crosses above LRadjEMA, get long. Exit the strat after four bars (Bar_numbers). Using weekly charts. XLE or OIH.

#I hope someone is out there able to provide an answer, and many thanks in advance!!


#The script is below:

#*I have a feeling something is amiss with the def count line, or maybe I'm missing something. Or does not having a full week of trading (holidays) mess with the code/timing/etc.??

#The Linear Regression-adjusted EMA strategy
#TASC's September 2022 edition of Traders' Tips includes an article by Vitali Apirine titled "The Linear Regression-Adjusted Exponential Moving Average". This script implements the titular indicator presented in this article.


def bn = barnumber();
def na = double.nan;

input price = close;

#LRadjEMA
#input price = close;
input length = 50;
input lrLength = 50;
input highLowLength = 50;
input multiplier = 3;

def lr = Inertia(price, lrLength);
def dist = AbsValue(price - lr);
def maxDist = Highest(dist, highLowLength);
def minDist = Lowest(dist, highLowLength);
def multiplier1 = 2 / (length + 1);
def multiplier2 = multiplier * ((dist - minDist) / (maxDist - minDist));
def alpha = multiplier1 * (1 + multiplier2);
def ma = CompoundValue(1, ma[1] + alpha * (price - ma[1]), Average(price, length));

plot LRAdjEMA = ma;
LRAdjEMA.SetDefaultColor(GetColor(1));


#condition
def condition = price crosses above LRAdjEMA;


#define entry
def entryPrice = EntryPrice();
def long1 = condition;



#count number of bars from long1 entry point
#def count = if long1[1] then 1 else count[1] + 1;

input Bar_numbers = 4;

def count = if bn == 1 then 0
  else if count[1] <= Bar_numbers then count[1] + 1
  else if long1[1] then 1
  else count[1] + 1;



input percentage_exit = 1.20;
input percentage_below_entry = .80;


def a = percentage_below_entry;


#define how to exit the strategy
input exit_type = {default Percentage, Combo, Bar_numbers};

def exit1;
switch (exit_type) {
case Percentage:
exit1 = close > ((percentage_exit) * entryPrice);
case Combo:
exit1 = close > ((percentage_exit) * entryPrice) or close is less than entryPrice * a;
case Bar_numbers:
exit1 = count == Bar_numbers;
}

input test2 = no;
addchartbubble(test2, low*0.99,
count
, (if count <= (bar_numbers + 1) then color.yellow else color.gray), no);


AddOrder(OrderType.BUY_TO_OPEN, long1, tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "LRadjEMA_long");
AddOrder(OrderType.SELL_TO_CLOSE, exit1, tickcolor = GetColor(5), arrowcolor = GetColor(5), name = "LRadjEMA_exit");

#
I want to give many thanks to halcyonguy for working this out last night!! Did it on a whim for someone he's just fb friends with. That's just an awesome experience and help to receive. I look forward to using this forum more often, and I hope to assist and engage with more of the members. Thank you once again!!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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