/* Pass in a List of doubles, and the number of items in that list that you want to calculate a Weighted Moving Average on a List which is built in sequential order, with the newest items added to the end of the List (higher index values) so in weighted average, the newest items are weighted with a higher percentage of importance */ double Wgtd_Avg_From_List(List<double> _List, int _Period) { // if List is empty, return 0 if (_List.Count < 1) return 0; // for loop limit, use smaller of List Count or the period int _List_Loop_Limit = Math.Min(_List.Count, _Period); //Print("in method, list count= "+ _List.Count +", loop limit= "+ _List_Loop_Limit ); // get weighted avg (higher priority on more recent items) double _wgtd_avg_numerator = 0; double _wgtd_avg_denominator = (0.5 * (_List_Loop_Limit * (_List_Loop_Limit + 1) ) ); double _wgtd_avg = 0.0; // the wghtd mv avg // loop thru the list, starting with newest element and moving to element at _Period // if List has 20 items, and Period is 5, we want to start with item at index 19 (20-1), and repeat down to item at index 14 (20-5) // we count up in the loop so we get the weights correct based on loop index for(int i = 0; i < _List_Loop_Limit; i++) { //Print("in loop, i= "+ i +", limit= "+ _List_Loop_Limit ); // calc numerator, using newest item in List, weight is highest ( * Limit - i) // get newest item in List, using _List.Count -1 - i, times the highest weight, for the newest item _wgtd_avg_numerator += _List[(_List.Count -1 -i)] * (_List_Loop_Limit -i ) ; // weight is number of items in loop, minus the loop index // after last item if( i == _List_Loop_Limit - 1 ) { _wgtd_avg = _wgtd_avg_numerator / _wgtd_avg_denominator ; } //Print(" wgtd avg func, List index= "+ (_List.Count -1 -i) +", input= "+ _List[(_List.Count -1 -i)].ToString("N2") +",\t\t numerator= "+ _List[(_List.Count -1 -i)] +" * "+ (_List_Loop_Limit - i) +" \t\t= "+ (_List[(_List.Count -1 -i)] * (_List_Loop_Limit - i) ) +" += "+ _wgtd_avg_numerator +", denom= "+ _wgtd_avg_denominator); } //Print(" after loop, wgtd_avg= "+ _wgtd_avg); return _wgtd_avg; } // END Wgtd_Avg_From_List
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Method Calculate Weighted Moving Average of doubles in List
Collapse
X
-
Method Calculate Weighted Moving Average of doubles in List
This method can be included in your indicator script, to take the items in a List, added in sequential order (FIFO) and calculate the Weighted Moving Average of the latest N values in the List, applying more importance to more recent items. Comments and print statements included for understanding the code. It's not LINQ but it's easier to understand.
Code:Tags: None
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by carnitron, Today, 08:42 PM
|
0 responses
5 views
0 likes
|
Last Post
by carnitron
Today, 08:42 PM
|
||
Started by strategist007, Today, 07:51 PM
|
0 responses
6 views
0 likes
|
Last Post Today, 07:51 PM | ||
Started by StockTrader88, 03-06-2021, 08:58 AM
|
44 responses
3,974 views
3 likes
|
Last Post
by jhudas88
Today, 06:59 PM
|
||
Started by rbeckmann05, Today, 06:48 PM
|
0 responses
8 views
0 likes
|
Last Post
by rbeckmann05
Today, 06:48 PM
|
||
Started by rhyminkevin, Today, 04:58 PM
|
4 responses
58 views
0 likes
|
Last Post
by dp8282
Today, 06:13 PM
|