UrQuan

Jimmy-gay

Dec 7th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calc_max_profit(ERT_YesterdayStockPrice) {    
  2.     var result = {
  3.         "buyAt" : 0,
  4.         "sellAt" : 0,
  5.         "profit" : 0
  6.     }
  7.     right = 0;
  8.     left = 0;
  9.     for (var i = 0; i < ERT_YesterdayStockPrice.length; i++){
  10.         if (ERT_YesterdayStockPrice[i] < ERT_YesterdayStockPrice[left]) {
  11.             left = i;
  12.             if (left > right) {
  13.                 right = left;
  14.             }
  15.         }
  16.         if (ERT_YesterdayStockPrice[i] >= ERT_YesterdayStockPrice[right]) {
  17.             right = i;
  18.         }
  19.         if (ERT_YesterdayStockPrice[right] - ERT_YesterdayStockPrice[left] > result.profit) {
  20.             result.profit = ERT_YesterdayStockPrice[right] - ERT_YesterdayStockPrice[left];
  21.             result.sellAt = right;
  22.             result.buyAt = left;
  23.         }
  24.     }
  25.     return result;
  26. }
Add Comment
Please, Sign In to add comment