need help finishing strategy code

BG1328

New member
new to the forum, new to thinkscript, I have this strategy that I am just about done with but I cant get it to work. One line is red. Seems like an easy fix for a more seasoned thinkscripter.
Code:
input price = close;
input desiredentryvma = 9;
input desiredexitvma = 9;

def entryvma = VMA (legnth = desiredentryvma);
def exitvma = VMA  (legnth = desiredexitvma);

def buylong = price >  entryvma;
def selllong = price < exitvma;

AddOrder(OrderType.BUY_TO_OPEN, buylong, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "open");


AddOrder(OrderType.BUY_TO_CLOSE, selllong, tickcolor = Color.RED, arrowcolor = Color.RED, name = "CLOSE");
 
Welcome to the forums and all that good stuff. Glad to see another trying to learn Thinkscript.

To start with Length is misspelled in your definitions as well as being not needed in this example. Also not sure what you mean by VMA is that supposed to be EMA?

If so then try this instead:
Code:
def entryvma = ExpAverage(price, desiredentryvma);
def exitvma = ExpAverage(price, desiredexitvma);

Cheers,
Rick
 
Welcome to the forums and all that good stuff. Glad to see another trying to learn Thinkscript.

To start with Length is misspelled in your definitions as well as being not needed in this example. Also not sure what you mean by VMA is that supposed to be EMA?

If so then try this instead:
Code:
def entryvma = ExpAverage(price, desiredentryvma);
def exitvma = ExpAverage(price, desiredexitvma);

Cheers,
Rick
I am actually trying to use the variable moving average. if that's possible. Thanks for the reply!
 
Oh, OK. Had not used VMA before and it did not show up on a search in thinkorswim studies. I was thinking volume, volatility, anything but variable, haha. Which I then found after you said it.

A little tweaking gives something like this. I commented out the plot which can be undone if you want it.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2017-2021
#
input price = close;
input length = 9;

def tmp1 = if price > price[1] then price - price[1] else 0;
def tmp2 = if price[1] > price then price[1] - price else 0;
def d2 = sum(tmp1, length);
def d4 = sum(tmp2, length);
def cond = d2 + d4 == 0;
def ad3 = if cond then 0 else (d2 - d4) / (d2 + d4) * 100;
def coeff = 2 / (length + 1) * AbsValue(ad3) / 100;
def asd = compoundValue("visible data" = coeff * price + (if IsNaN(asd[1]) then 0 else asd[1]) * (1 - coeff), "historical data" = price
);

#plot VMA = asd;
#VMA.setDefaultColor(GetColor(0));


def buylong = price >  asd;
def selllong = price < asd;

AddOrder(OrderType.BUY_TO_OPEN, buylong, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "open");


AddOrder(OrderType.BUY_TO_CLOSE, selllong, tickcolor = Color.RED, arrowcolor = Color.RED, name = "CLOSE");
 
Oh, OK. Had not used VMA before and it did not show up on a search in thinkorswim studies. I was thinking volume, volatility, anything but variable, haha. Which I then found after you said it.

A little tweaking gives something like this. I commented out the plot which can be undone if you want it.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2017-2021
#
input price = close;
input length = 9;

def tmp1 = if price > price[1] then price - price[1] else 0;
def tmp2 = if price[1] > price then price[1] - price else 0;
def d2 = sum(tmp1, length);
def d4 = sum(tmp2, length);
def cond = d2 + d4 == 0;
def ad3 = if cond then 0 else (d2 - d4) / (d2 + d4) * 100;
def coeff = 2 / (length + 1) * AbsValue(ad3) / 100;
def asd = compoundValue("visible data" = coeff * price + (if IsNaN(asd[1]) then 0 else asd[1]) * (1 - coeff), "historical data" = price
);

#plot VMA = asd;
#VMA.setDefaultColor(GetColor(0));


def buylong = price >  asd;
def selllong = price < asd;

AddOrder(OrderType.BUY_TO_OPEN, buylong, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "open");


AddOrder(OrderType.BUY_TO_CLOSE, selllong, tickcolor = Color.RED, arrowcolor = Color.RED, name = "CLOSE");
thank you for your time and help with this. Now my issue is the buy and sell signals. I only get one random long entry per chart for some reason and the sell signals won't plot.
 
Good catch, generic. I barely looked at the close order to see if it was correct, Sell_To_Close. I was focused more on the calculation part of the script.
 

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
210 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