0

I have the following function where certain inputs are given and then 4 outputs are given: -

function rsiNext($dailyGainAvgPrev, $dailyLossAvgPrev,$cpDailyNext){
    if($cpDailyNext > 0){
        $dailyGainAvgNext = (($dailyGainAvgPrev * 13) + $cpDailyNext)/14;
    }else{
        $dailyGainAvgNext = (($dailyGainAvgPrev * 13) + 0)/14;
    }

    if($cpDailyNext < 0){
        $dailyLossAvgNext = (($dailyLossAvgPrev*13) + abs($cpDailyNext))/14;
    }else{
        $dailyLossAvgNext = (($dailyLossAvgPrev*13) + abs(0))/14;
    }
    $relStrNext = $dailyGainAvgNext/$dailyLossAvgNext;
    if($dailyLossAvgNext == 0){
        $relStrIndNext = 100;
    }else{
        $relStrIndNext = 100-(100/(1+$relStrNext));
    }
    return array($dailyGainAvgNext, $dailyLossAvgNext, $relStrNext, $relStrIndNext);
}

I output the values using the following line of code:

//Get value for day 15
list($dailyGainAvg02, $dailyLossAvg02, $relStr02, $relStrInd02) = rsiNext($averageGains14, $averageLosses14, $priceDifferences[15]);
echo '<tr><td>'.$dailyGainAvg02.'</td><td>'.$dailyLossAvg02.'</td><td>'.$relStr02.'</td><td>'.$relStrInd02.'</td></tr>';

Now when I want the value for day 16 I use the following line of code:

//Get value for day 16
list($dailyGainAvg03, $dailyLossAvg03, $relStr03, $relStrInd03) = rsiNext($dailyGainAvg02, $dailyLossAvg02, $priceDifferences[16]);
echo '<tr><td>'.$dailyGainAvg03.'</td><td>'.$dailyLossAvg03.'</td><td>'.$relStr03.'</td><td>'.$relStrInd03.'</td></tr>';

The output of day 15 is the input of day 16, the output of day 16 is the input of day 17. The output of day 17 is the input of day 18, etc...

I need to repeat the list for 100 days. How can I go about it without repeating the list line for another 100 days?

Thank you.

1
  • what would be the initial values for the first day? rsiNext(?, ?, $priceDifferences[1]); Commented Jan 27, 2016 at 7:34

1 Answer 1

1

Assuming you have the $priceDifferences array fully populated, something like the following should do:

$cur_dailyGainAvg = 0; // you need to initialize this value appropriately
$cur_dailyLossAvg = 0; // you need to initialize this value appropriately

for ($idx = 1; $idx <= 100; $idx++) {
  list($new_dailyGainAvg, $new_dailyLossAvg, $new_relStr, $new_relStrInd) = rsiNext($cur_dailyGainAvg, $cur_dailyLossAvg, $priceDifferences[$idx])

  // print
  echo '<tr><td>'.$new_dailyGainAvg.'</td><td>'.$new_dailyLossAvg.'</td><td>'.$new_relStr.'</td><td>'.$new_relStrInd.'</td></tr>';

  // shift the new values onto the current, and repeat the calculation
  $cur_dailyGainAvg = $new_dailyGainAvg;
  $cur_dailyLossAvg = $new_dailyLossAvg;
} 

Basically distinguish between your "current" values, which you feed into your function, and the "new" values that come out, then "shift" the new onto the current ones and repeat.

You may have to check the boundaries of the loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.