PineCoders

Averages - PineCoders FAQ

The need to calculate averages (arithmetic mean) comes up here and there in scripts. When you want the average of a value only when a given condition has occurred, you will often not be able to use the standard sma function in Pine because it will average all values in the series. Even if you take care to use your condition to set non-conforming values to zero, this ends up skewing your results with zero values.

This script calculates:
1. The running average of all up volumes,
2. The average of last x occurrences of up volume ,
3. The average up volume occurrences in last y bars.
Phát hành các Ghi chú:
  • Made code for cases 2 and 3 into functions.
  • Added examples of multiple-result functions.
Phát hành các Ghi chú: Conversion to v4.
Phát hành các Ghi chú: Fixes to comments.
Phát hành các Ghi chú: Simplified Case 1 code.
Phát hành các Ghi chú: Changes to comments.
Phát hành các Ghi chú: Updated calculations.
Mã nguồn mở

Với tinh thần của TradingView, tác giả đã xuất bản tập lệnh theo mã nguồn mở, vì thế trader có thể dễ dàng hiểu và tùy chỉnh được. Bạn có thể sử dụng miễn phí, hoặc tùy chỉnh lại mã đã được cấp phép bởi Quy tắc Chung. Bạn có thể sử dụng nó trên biểu đồ.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?
Tools and ideas for all Pine coders: http://www.pinecoders.com
Our Pine FAQ & Code: http://www.pinecoders.com/faq_and_code/
Pine news broadcasts: https://t.me/PineCodersSquawkBox or https://twitter.com/PineCoders

Bình luận

super mega powerful script, thank you, guys!
Phản hồi
PineCoders hendecagon
@hendecagon, Thx. We need to update case 3. Would be more efficient and elegant to use:
avgWhenInLast(_src, _cond, _cnt) => sum(_cond ? _src : 0, _cnt) / sum(_cond ? 1 : 0, _cnt)
Phản hồi
Sir, each of your strategies contains a huge treasure. I understand your educational purpose. You are drawing the Da Vinci code, the code for trading.:)
Phản hồi
PineCoders flush6789
@flush6789, ) Thx for your nice comment. Appreciate. Happy you find the code useful.
Phản hồi
Well done!
But maybe you know how to limit averaging period with a custom event (stop loss, take profit or opposite condition) instead of amount of bars?
For example I am trying to find a script to replace the builtin "strategy.position_avg_price" function. I need it for my study script dca alertconditions.
Phản hồi
PineCoders MoneyButton
@MoneyButton, If the point where you will need to start averaging from can be detected as your script is executing on it, then you can use technique 1b and add a condition to reset the values upon encountering that start condition. Don't even need a loop. The loop is only required when the conditions required for your calcs can only be known in the future, and so you need to wait for that future to occur, then go back.
Phản hồi
MoneyButton PineCoders
@PineCoders, thank you for a quick reply.

It would be great if you could show an example script with a "reset_condition" set to reset the values. :)
I am new with coding and it is difficult for me to imagine or develop scripts, however when I see a working one, I can understand how it works and then implement it into my strategy.
Phản hồi
PineCoders MoneyButton
@MoneyButton, This is a new function which returns the average of a series counted from the last moment a given condition was true. The function also returns the number of bars elapsed since the condition was true, so the length over which the average is calculated:
//@version=4
study("Average Since", "", true)

// Returns the average of `_src` since `_cond` was last true, and the number of bars since.
f_avgSince(_src, _cond) =>
    // _src  : series to average.
    // _cond : condition upon which average is reset.
    var _cumTotal = 0.
    var _cumCount = 0
    if _cond
        // When condition is true, reset avg to zero.
        _cumTotal := _src
        _cumCount := 1
    else
        // Continue calculating running numbers.
        _cumTotal := _cumTotal + _src
        _cumCount := _cumCount + 1
    _avg = _cumTotal / _cumCount
    [_avg, _cumCount - 1]

hi = highest(input(50))
// Condition which will reset our avg: when a new high is detected.
newHi = hi != hi[1]
// Call our function and get 2 results from it simultaneously.
[avgSinceNewHigh, barsSinceNewHigh] = f_avgSince(close, newHi)

plot(avgSinceNewHigh, "avgSinceNewHigh", color.aqua, 2)
plotchar(newHi, "newHi", "•", location.top)
plotchar(barsSinceNewHigh, "barsSinceNewHigh", "", location.top)

Phản hồi
MoneyButton PineCoders
@PineCoders, you are a star, Thank you so much!
Stay healthy
Phản hồi
PineCoders MoneyButton
@MoneyButton, Remember that this sort of function should always be executed on every bar, so can't be embedded in an `if` or ternary block that would prevent it from being executed on some bars. See our FAQ post here if it's not clear to you why this is: https://www.pinecoders.com/faq_and_code/#why-do-some-functions-and-built-ins-evaluate-incorrectly-in-if-or-ternary--blocks
Phản hồi
Trang chủ Bộ lọc cổ phiếu Bộ lọc Forex Bộ lọc Tiền điện tử Lịch kinh tế Cách thức hoạt động Tính năng Biểu đồ Trả phí Giới thiệu bạn Quy tắc Áp dụng Trung tâm Trợ giúp Giải pháp cho Website & Nhà môi giới Widget Giải pháp biểu đồ Thư viện Biểu đồ Lightweight Blog & Tin tức Twitter
Hồ sơ Tùy chỉnh Hồ sơ Tài khoản và Thanh toán Giới thiệu bạn Xu Ticket Hỗ trợ của tôi Trung tâm Trợ giúp Tin nhắn riêng Trò chuyện Đăng xuất