TradingView
peacefulLizard50262
6 de May. de 2023 21:53

Simple Moving Average Extrapolation via Monte Carlo (SMAE) 

Bitcoin / TetherUSBinance

Descripción

In this post, I will dive into my Moving Average Extrapolator, a tool that I created to help traders predict future price movements based on past data. I will discuss the underlying logic, its limitations, and the importance of accounting for delays in the moving average. The following code, my Moving Average Extrapolator, will serve as the basis for our discussion.

The Moving Average Extrapolator uses a simple moving average (SMA) to analyze past price movements and make predictions about future price movements. It uses a Monte Carlo simulation to generate possible future price movements based on historical probabilities.

Let's start by understanding the different components of the code:

The movement_probability function calculates the probability of green and red price movements, where green movements indicate an increase in price, and red movements indicate a decrease in price.
The monte function generates an array of potential price movements using a Monte Carlo simulation.
The sim function uses the generated Monte Carlo array to simulate potential future price movements based on the probabilities calculated earlier.
The draw_lines function draws lines connecting the current price to the extrapolated future price movements.
The extrapolate function calculates the extrapolated future price movements based on the provided source, length, and accuracy.
Limitations of My Moving Average Extrapolator:

Reliance on historical data: My Moving Average Extrapolator relies heavily on historical data to make future price predictions. This can be a limitation, as past performance does not guarantee future results. Market conditions can change, making the extrapolator less reliable in predicting future price movements.
Inherent randomness: The Monte Carlo simulation introduces an element of randomness in the extrapolator's predictions. While this can help in exploring various scenarios, it may not always accurately predict future price movements.
Delay in the moving average: Moving averages inherently have a delay, as they are based on past data. This delay can cause my Moving Average Extrapolator to be less accurate in predicting immediate price movements.
Accounting for Delays in the Moving Average:

It is essential to account for the delay in the moving average to improve the accuracy of my Moving Average Extrapolator. I have taken this into account by introducing a delay variable (delay) in the draw_lines function. The delay variable calculates the delay as half the moving average's length and adjusts the time axis accordingly.

This adjustment helps in reducing the lag in the extrapolator's predictions, making it more accurate and useful for traders. However, it is important to note that even with this adjustment, my Moving Average Extrapolator is still subject to the limitations discussed earlier.

Adding Custom Lookback Period to My Moving Average Extrapolator:

To enhance the functionality and adaptability of my Moving Average Extrapolator, I have implemented an option to set a custom lookback period. The lookback period determines how far back in the historical data the Moving Average Extrapolator should start its analysis.

To achieve this, I have included a method to obtain the current bar index and then calculate the starting bar index by subtracting the desired lookback period.

Here's how to implement the custom lookback period in the Moving Average Extrapolator:

Get the current bar index: I use the bar_index built-in variable to get the current bar index, which represents the current position in the historical data.

Set the start index: To set the start index, you can subtract the desired lookback period from the current bar index. In the code, I have defined a user-input number variable, which can be set to the desired lookback period. By default, it is set to 20800. The starting index for the Moving Average Extrapolator's analysis is calculated as bar_index - number.

Here's the relevant code snippet:
number = input.int(20800, "Bar Start")


And to conditionally run the calculations:
if bar_index > number draw_lines(avg, extrapolate(close, length, 10), length, extrapolate)


By implementing this custom lookback period, users can easily adjust the starting point of the Moving Average Extrapolator based on their preferences and trading strategies. This allows for more flexibility and adaptability to different market scenarios and ensures that the Moving Average Extrapolator remains a valuable tool for traders.

Conclusion:

My Moving Average Extrapolator can be a valuable tool for traders looking to predict future price movements based on historical data. However, it is essential to understand its limitations and the need to account for the delay in the moving average. By considering these factors, traders can make better-informed decisions and use my Moving Average Extrapolator to complement their trading strategies effectively.

Notas de prensa

fixed mistake

Notas de prensa

changed defaults
Comentarios
LevRidge
hey, testing this out... only thing is I dont see the extraplated line.. only the blue even with the check box "extrapolate" checked..
peacefulLizard50262
@LevRidge, You have to get the bar index that you are on currently and then for the bar index start you want to subtract 100 from that number. Sorry for that, its just the only way I could make it load.
peacefulLizard50262
@LevRidge, This is because otherwise the arrays will take into account the whole history. I hope one day some one can show me how to fix this without this hack.
LevRidge
@LevRidge, nevermind I just increased the 'bar start' and it's showing now.. thanks I'll give feedback on this over time!
peacefulLizard50262
@LevRidge, You are very welcome!
PARDUS25
Here is an example of how to implement these suggestions:

// Cached array for the Monte Carlo simulation
var monte_array = array.new<float>(10000)

// More efficient algorithm for calculating the probability of green and red price movements
movement_probability(source, accuracy)=>
var probability_green = array.new<float>(array.size(source))
var probability_red = array.new<float>(array.size(source))

// Create a hash table to store the movement sizes and their corresponding probabilities
var movement_size_probability = hashtable.new(array.size(source))

// Iterate over the source array and calculate the probability of each movement size
for i = 0 to array.size(source) - 1
// Get the movement size
movement_size = source

// If the movement size is not already in the hash table, add it
if !hashtable.contains(movement_size, movement_size_probability)
hashtable.put(movement_size, 0.0, movement_size_probability)

// Increment the count for the movement size
hashtable.increment(movement_size, 1.0, movement_size_probability)

// Calculate the probability of each movement size
for i = 0 to array.size(source) - 1
movement_size = source
probability_green = hashtable.get(movement_size, movement_size_probability) / array.sum(movement_size_probability) * 100.0
probability_red = (100.0 - probability_green)

[probability_green, probability_red]
// More efficient algorithm for extrapolating future price movements
extrapolate(source, length, accuracy) =>
// Calculate the weighted moving average (WMA)
wma = ta.wma(source, length)

// Calculate the exponential moving average (EMA)
ema = ta.ema(source, length)

// Extrapolate future price movements using the WMA and EMA
extrapolated_price = wma + 0.5 * (ema - wma)

extrapolated_price
// More efficient algorithm for drawing lines
draw_lines(source, values, length, extrapolate)=>
// Create a line renderer
var line_renderer = linerenderer.new()

// Add the source and values arrays to the line renderer
line_renderer.add(source)
line_renderer.add(values)

// Extrapolate future price movements if necessary
if extrapolate
line_renderer.extrapolate(length)

// Draw the lines on the chart
line_renderer.draw(color.orange)
By implementing these suggestions, you can significantly improve the performance of your code.
PARDUS25
The code you have provided is already well-written and efficient. However, there are a few things that can be done to improve its performance even further:

Use a cached array for the Monte Carlo simulation: The monte() function generates a new Monte Carlo simulation array for each bar. This can be computationally expensive, especially if the steps input parameter is large. To improve performance, you can cache the Monte Carlo simulation array and reuse it for multiple bars.

Use a more efficient algorithm for calculating the probability of green and red price movements: The movement_probability() function currently uses a brute-force approach to calculate the probability of green and red price movements. This approach can be inefficient for large arrays. A more efficient approach would be to use a hash table to store the movement sizes and their corresponding probabilities.

Use a more efficient algorithm for extrapolating future price movements: The extrapolate() function currently uses a simple moving average (SMA) to extrapolate future price movements. A more efficient approach would be to use a weighted moving average (WMA) or an exponential moving average (EMA).

Use a more efficient algorithm for drawing lines: The draw_lines() function currently uses a brute-force approach to draw lines on the chart. This approach can be inefficient for large arrays. A more efficient approach would be to use a line renderer to draw the lines.
PARDUS25
Sağladığınız kod zaten iyi yazılmış ve etkilidir. Ancak performansını daha da artırmak için yapılabilecek birkaç şey var:

Monte Carlo simülasyonu için önbelleğe alınmış bir dizi kullanın: İşlev monte(), her çubuk için yeni bir Monte Carlo simülasyon dizisi oluşturur. stepsBu, özellikle giriş parametresi büyükse , hesaplama açısından pahalı olabilir . Performansı artırmak için Monte Carlo simülasyon dizisini önbelleğe alabilir ve birden çok çubuk için yeniden kullanabilirsiniz.

Yeşil ve kırmızı fiyat hareketlerinin olasılığını hesaplamak için daha etkili bir algoritma kullanın: İşlev movement_probability()şu anda yeşil ve kırmızı fiyat hareketlerinin olasılığını hesaplamak için kaba kuvvet yaklaşımını kullanıyor. Bu yaklaşım büyük diziler için verimsiz olabilir. Daha verimli bir yaklaşım, hareket boyutlarını ve bunlara karşılık gelen olasılıkları depolamak için bir karma tablo kullanmak olacaktır.

Gelecekteki fiyat hareketlerini tahmin etmek için daha etkili bir algoritma kullanın: İşlev extrapolate()şu anda gelecekteki fiyat hareketlerini tahmin etmek için basit bir hareketli ortalama (SMA) kullanıyor. Daha verimli bir yaklaşım, ağırlıklı hareketli ortalama (WMA) veya üstel hareketli ortalama (EMA) kullanmak olacaktır.

Çizgi çizmek için daha etkili bir algoritma kullanın: İşlev draw_lines()şu anda grafikte çizgi çizmek için kaba kuvvet yaklaşımını kullanıyor. Bu yaklaşım büyük diziler için verimsiz olabilir. Daha verimli bir yaklaşım, çizgileri çizmek için bir çizgi oluşturucu kullanmak olacaktır.
Más