2010 lượt xem
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.
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.
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
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
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.
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.
Stay healthy