Need assistance with finishing strategy code

nowhmar

New member
I am creating a strategy that uses two Variable moving averages the 10, and the 50 vma I need some help finishing the code...


Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#

input price = close;
input fastlegnth  = 10;
input slowlength = 50;


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 buy = fastlegnth > slowlegnth ;
def sell = slowlegnth < fastlegnth;


AddOrder(OrderType.BUY_AUTO, fastlegnth  crosses above Slowlegnth, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "long");
AddOrder(OrderType.SELL_AUTO, fastlegnth crosses below fastlegnth, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "take profit");
 
Last edited:
it is not functional I need assistance to get it to work. I am a new thinkscripter.

Your buy and sell variables should be using crosses above and crosses below, and you AddOrder()'s should be using buy and sell as the conditions... I haven't checked beyond that but those items jumped right out... See if that gets you further...
 
Your buy and sell variables should be using crosses above and crosses below, and you AddOrder()'s should be using buy and sell as the conditions... I haven't checked beyond that but those items jumped right out... See if that gets you further...
that seemed to decrease some of the red highlighting. still isn't working though I just don't know what else I am missing. here is the latest of what I changed. I would appreciate any further guidance.


Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#

input price = close;
input fastlegnth  = 10;
input slowlength = 50;


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 buy = fastlegnth crosses above slowlegnth;
def sell = fastlegnth crosses below slowlegnth;


AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "long");
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "take profit");
 
your compoundvalue() has 2 parameters , instead of 3. you are missing the 1st parameter.

if the barnumber is <= the 1st number, then use the 3rd parameter/formula.

if the barnumber is > the 1st number, then use the 2nd parameter/formula

i've never seen the parameter names used before in compoundvalue, but probably ok.

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue
do you mean a second group of def statements for the other moving average? I switched the 1 for the two. It didnt help...
Code:
def tmp1 = if price > price[2] then price - price[2] 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
);
 
@nowhmar Your d2, d4, coeff length's are undefined. Compoundvalue is fine since you specified visible and historical data. Your buy and sell conditions have some spelling problem. I made an assumption of what you want. Remember to save it as a strategy.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#

input price = close;
input fastlength  = 10;
input slowlength = 50;

script VMA {
input price = close;
input length = 10;
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;
}

def vma_fast = VMA(price, fastlength);
def vma_slow = VMA(price, slowlength);

def buy = vma_fast crosses above vma_slow;
def sell = vma_fast crosses below vma_slow;


AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "long");
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "take profit");
 
do you mean a second group of def statements for the other moving average? I switched the 1 for the two. It didnt help...
Code:
def tmp1 = if price > price[2] then price - price[2] 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
);
no, i mean what i wrote. the compoundvalue() funtion is not configured correctly.
 
@nowhmar Your d2, d4, coeff length's are undefined. Compoundvalue is fine since you specified visible and historical data. Your buy and sell conditions have some spelling problem. I made an assumption of what you want. Remember to save it as a strategy.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#

input price = close;
input fastlength  = 10;
input slowlength = 50;

script VMA {
input price = close;
input length = 10;
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;
}

def vma_fast = VMA(price, fastlength);
def vma_slow = VMA(price, slowlength);

def buy = vma_fast crosses above vma_slow;
def sell = vma_fast crosses below vma_slow;


AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "long");
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "take profit");
thats exactly what I wanted thanks for that.
 

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