Help with fold function when subscript is used together

yiwang

New member
I wrote the following code, the goal is to

find the target call price where the probability of reaching the price is less than 0.3, or prob otm is > 0.7.

The problem is at the

# Use fold to simulate finding the strike price
# Use fold to simulate finding the strike price
def otm_strike_call = fold index = atm_strike to atm_strike + 10 do
if 1 - PopTargetPrice(index, timeToExpirationHours, iv) > prob_otm_threshold then index else index;


it complains "no such variable: index"

I feel like the root cause has something to do with the subscript, but I need the subscript to perform complex math computation.

Ruby:
input daysToExpiration = 0;
input closeTime = 1100;

def iv = if(IsNaN(imp_volatility()), iv[1], imp_volatility());
def westTime = closeTime + 300;
def timeToExpirationHours = SecondsTillTime(westTime) / 3600 + 24 * daysToExpiration;

script PopTargetPrice {
    # Input parameters
    input targetPrice = 0;
    input timeToExpirationHours = 0;
    input impliedVolatility = 0.2;
    # Calculate the current price and standard deviation
    def currentPrice = close;
    def timeToExpiration = timeToExpirationHours / 24 / 365;  # Convert hours to years
    def stdDev = currentPrice * impliedVolatility * Sqrt(timeToExpiration);  # Standard deviation of price movement

    # Z-Score Calculation
    def zScore = AbsValue(targetPrice - currentPrice) / stdDev;

    # Approximation of the cumulative distribution using a simplified approach (no Erf)
    def CDF = 1 / (1 + Exp(-1.65451 * zScore));

    # Return the probability
    plot result = 1  - CDF;
}

def prob_otm_threshold = 0.7;
def atm_strike = round(close(), 0);

# Use `fold` to simulate finding the strike price
def otm_strike_call = fold index = atm_strike to atm_strike + 10 do 
    if 1 - PopTargetPrice(index, timeToExpirationHours, iv) > prob_otm_threshold then index else index;

Any ideas to fix it?

Thanks!
 
Solution
Correct, you can not reference a Script{} in a Fold's DO expression. You might be able to in the FROM or TO, if my memory serves me correctly, but that's besides the point. Generally what you have to do is perform the entire calculation in one huge nested parenthetical within a Fold, or you could just hardcode the script reference on 10 different lines and then compare the results. It's really inconvenient, but it's possible.
Correct, you can not reference a Script{} in a Fold's DO expression. You might be able to in the FROM or TO, if my memory serves me correctly, but that's besides the point. Generally what you have to do is perform the entire calculation in one huge nested parenthetical within a Fold, or you could just hardcode the script reference on 10 different lines and then compare the results. It's really inconvenient, but it's possible.
 
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
405 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