Mov Avg Cross OVER

rvaidyamath

New member
Hello all

Here is the strategy :

cond1 = ema 50 > 200
Cond2 = close crosses above 20 ema and 20 EMA > 50
Buy = cond1 and cond2
Close buy : price crosses below 20 and price < ema50

Reality : Price can go and buy, but as per above condition is true ; what if the price was not below 50EMA ; so the position is not closed; this time it will give buy condition when clsoe crosses above 20 EMA ; as there is already position open; i dont want this buy signal ; having trouble in lookback period to check was there any buy trigger happened here or not; same reverse applies for sell condition and close sell as well ....

Thanks
Raj
 
Solution
Hello all

Here is the strategy :

cond1 = ema 50 > 200
Cond2 = close crosses above 20 ema and 20 EMA > 50
Buy = cond1 and cond2
Close buy : price crosses below 20 and price < ema50

Reality : Price can go and buy, but as per above condition is true ; what if the price was not below 50EMA ; so the position is not closed; this time it will give buy condition when clsoe crosses above 20 EMA ; as there is already position open; i dont want this buy signal ; having trouble in lookback period to check was there any buy trigger happened here or not; same reverse applies for sell condition and close sell as well ....

Thanks
Raj

if you want to ignore same signals, you can do something like this.
this creates a series of...
Hello all

Here is the strategy :

cond1 = ema 50 > 200
Cond2 = close crosses above 20 ema and 20 EMA > 50
Buy = cond1 and cond2
Close buy : price crosses below 20 and price < ema50

Reality : Price can go and buy, but as per above condition is true ; what if the price was not below 50EMA ; so the position is not closed; this time it will give buy condition when clsoe crosses above 20 EMA ; as there is already position open; i dont want this buy signal ; having trouble in lookback period to check was there any buy trigger happened here or not; same reverse applies for sell condition and close sell as well ....

Thanks
Raj

if you want to ignore same signals, you can do something like this.
this creates a series of pulses, during the buy period.

def long_trade = if bn == 1 then 0
else if long_close8 then 0
else if long_open8 then 1
else long_trade[1];

then add a formula that looks for changes in long_trade, from 0 to 1 and from 1 to 0.


Code:
#avg_crossover_trade

#https://usethinkscript.com/threads/mov-avg-cross-over.17125/
#Mov Avg Cross OVER

def na = double.nan;
def bn = barnumber();
input price = close;

#input avg_type = AverageType.Simple;
input avg1_type = AverageType.EXPONENTIAL;
def avg1_data = price;
input avg1_len = 20;
def avg1 = MovingAverage( avg1_type , avg1_data , avg1_len );
plot zavg1 = avg1;

input avg2_type = AverageType.EXPONENTIAL;
def avg2_data = price;
input avg2_len = 50;
def avg2 = MovingAverage( avg2_type , avg2_data , avg2_len );
plot zavg2 = avg2;

input avg3_type = AverageType.EXPONENTIAL;
def avg3_data = price;
input avg3_len = 200;
def avg3 = MovingAverage( avg3_type , avg3_data , avg3_len );
plot zavg3 = avg3;

#---------------------

def long_open1 = avg2 > avg3;
def long_open2 = avg1 > avg2;
def long_open3 = close crosses above avg1; 
def long_open8 = long_open1 and long_open2 and long_open3;

def long_close1 = price crosses below avg1;
def long_close2 = price < avg2;
def long_close8 = long_close1 and long_close2;

input show_all_signals = no;
addverticalline(show_all_signals and long_open8, "BUY", color.cyan);
addverticalline(show_all_signals and long_close8, "SELL", color.yellow);

# read buy/sell signals. if a duplicate signal, it stays the same
def long_trade = if bn == 1 then 0
 else if long_close8 then 0
 else if long_open8 then 1
 else long_trade[1];

# look for transitions of trade signal
def long_open11 = !long_trade[1] and long_trade;
def long_close11 = long_trade[1] and !long_trade;

addverticalline(long_open11, "BUY", color.green);
addverticalline(long_close11, "SELL", color.red);

#
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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