Advertisement
Guest User

VWAPnodaily

a guest
May 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #
  2. # TD Ameritrade IP Company, Inc. (c) 2011-2016
  3. #
  4.  
  5. declare hide_on_daily;
  6. input timeFrame = {default DAY, WEEK, MONTH};
  7.  
  8. def cap = getAggregationPeriod();
  9. def errorInAggregation =
  10. timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
  11. timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
  12. assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
  13.  
  14. def yyyyMmDd = getYyyyMmDd();
  15. def periodIndx;
  16. switch (timeFrame) {
  17. case DAY:
  18. periodIndx = yyyyMmDd;
  19. case WEEK:
  20. periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
  21. case MONTH:
  22. periodIndx = roundDown(yyyyMmDd / 100, 0);
  23. }
  24. def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
  25.  
  26. def volumeSum;
  27. def volumeVwapSum;
  28. def volumeVwap2Sum;
  29.  
  30. if (isPeriodRolled) {
  31. volumeSum = volume;
  32. volumeVwapSum = volume * vwap;
  33. volumeVwap2Sum = volume * Sqr(vwap);
  34. } else {
  35. volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
  36. volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
  37. volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
  38. }
  39. def price = volumeVwapSum / volumeSum;
  40. def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
  41.  
  42. plot VWAP = price;
  43.  
  44. VWAP.setDefaultColor(getColor(0));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement