📄 financedemochart.php
字号:
<?php
require_once("../lib/FinanceChart.php");
# Utility to compute modulus for large positive numbers. Although PHP has a built-in fmod
# function, it is only for PHP >= 4.2.0. So we need to define our own fmod function.
function fmod2($a, $b) { return $a - floor($a / $b) * $b; }
#
# Create a finance chart based on user selections, which are encoded as query
# parameters. This code is designed to work with the financedemo HTML form.
#
# The timeStamps, volume, high, low, open and close data
$timeStamps = null;
$volData = null;
$highData = null;
$lowData = null;
$openData = null;
$closeData = null;
#/ <summary>
#/ Get 15 minutes data series for timeStamps, highData, lowData, openData, closeData
#/ and volData.
#/ </summary>
#/ <param name="startDate">The starting date/time for the data series.</param>
#/ <param name="endDate">The ending date/time for the data series.</param>
function get15MinData($ticker, $startDate, $endDate) {
#
# In this demo, we use a random number generator to generate the data. In
# practice, you may get the data from a database or by other means. If you do not
# have 15 minute data, you may modify the "drawChart" method below to not using
# 15 minute data.
#
generateRandomData($ticker, $startDate, $endDate, 900);
}
#/ <summary>
#/ Get daily data series for timeStamps, highData, lowData, openData, closeData
#/ and volData.
#/ </summary>
#/ <param name="startDate">The starting date/time for the data series.</param>
#/ <param name="endDate">The ending date/time for the data series.</param>
function getDailyData($ticker, $startDate, $endDate) {
#
# In this demo, we use a random number generator to generate the data. In
# practice, you may get the data from a database or by other means.
#
generateRandomData($ticker, $startDate, $endDate, 86400);
}
#/ <summary>
#/ Get weekly data series for timeStamps, highData, lowData, openData, closeData
#/ and volData.
#/ </summary>
#/ <param name="startDate">The starting date/time for the data series.</param>
#/ <param name="endDate">The ending date/time for the data series.</param>
function getWeeklyData($ticker, $startDate, $endDate) {
#
# If you do not have weekly data, you may call "getDailyData(startDate, endDate)"
# to get daily data, then call "convertDailyToWeeklyData()" to convert to weekly
# data.
#
generateRandomData($ticker, $startDate, $endDate, 86400 * 7);
}
#/ <summary>
#/ Get monthly data series for timeStamps, highData, lowData, openData, closeData
#/ and volData.
#/ </summary>
#/ <param name="startDate">The starting date/time for the data series.</param>
#/ <param name="endDate">The ending date/time for the data series.</param>
function getMonthlyData($ticker, $startDate, $endDate) {
#
# If you do not have weekly data, you may call "getDailyData(startDate, endDate)"
# to get daily data, then call "convertDailyToMonthlyData()" to convert to
# monthly data.
#
generateRandomData($ticker, $startDate, $endDate, 86400 * 30);
}
#/ <summary>
#/ A random number generator designed to generate realistic financial data.
#/ </summary>
#/ <param name="startDate">The starting date/time for the data series.</param>
#/ <param name="endDate">The ending date/time for the data series.</param>
#/ <param name="resolution">The period of the data series.</param>
function generateRandomData($ticker, $startDate, $endDate, $resolution) {
global $timeStamps, $volData, $highData, $lowData, $openData, $closeData;
$db = new FinanceSimulator((int)($ticker), $startDate, $endDate, $resolution);
$timeStamps = $db->getTimeStamps();
$highData = $db->getHighData();
$lowData = $db->getLowData();
$openData = $db->getOpenData();
$closeData = $db->getCloseData();
$volData = $db->getVolData();
}
#/ <summary>
#/ A utility to convert daily to weekly data.
#/ </summary>
function convertDailyToWeeklyData() {
$tmpArrayMath1 = new ArrayMath($timeStamps);
aggregateData($tmpArrayMath1->selectStartOfWeek());
}
#/ <summary>
#/ A utility to convert daily to monthly data.
#/ </summary>
function convertDailyToMonthlyData() {
$tmpArrayMath1 = new ArrayMath($timeStamps);
aggregateData($tmpArrayMath1->selectStartOfMonth());
}
#/ <summary>
#/ An internal method used to aggregate daily data.
#/ </summary>
function aggregateData(&$aggregator) {
global $timeStamps, $volData, $highData, $lowData, $openData, $closeData;
$timeStamps = NTime($aggregator->aggregate(CTime($timeStamps), AggregateFirst));
$highData = $aggregator->aggregate($highData, AggregateMax);
$lowData = $aggregator->aggregate($lowData, AggregateMin);
$openData = $aggregator->aggregate($openData, AggregateFirst);
$closeData = $aggregator->aggregate($closeData, AggregateLast);
$volData = $aggregator->aggregate($volData, AggregateSum);
}
#/ <summary>
#/ Create a financial chart according to user selections. The user selections are
#/ encoded in the query parameters.
#/ </summary>
function drawChart() {
global $timeStamps, $volData, $highData, $lowData, $openData, $closeData;
# In this demo, we just assume we plot up to the latest time. So end date is now.
$endDate = chartTime2(time());
# If the trading day has not yet started (before 9:30am), or if the end date is
# on on Sat or Sun, we set the end date to 4:00pm of the last trading day
while ((fmod2($endDate, 86400) < 9 * 3600 + 30 * 60) || (getChartWeekDay($endDate
) == 0) || (getChartWeekDay($endDate) == 6)) {
$endDate = $endDate - fmod2($endDate, 86400) - 86400 + 16 * 3600;
}
# The duration selected by the user
$durationInDays = (int)($_REQUEST["TimeRange"]);
# Compute the start date by subtracting the duration from the end date.
$startDate = $endDate;
if ($durationInDays >= 30) {
# More or equal to 30 days - so we use months as the unit
$YMD = getChartYMD($endDate);
$startMonth = (int)($YMD / 100) % 100 - (int)($durationInDays / 30);
$startYear = (int)($YMD / 10000);
while ($startMonth < 1) {
$startYear = $startYear - 1;
$startMonth = $startMonth + 12;
}
$startDate = chartTime($startYear, $startMonth, 1);
} else {
# Less than 30 days - use day as the unit. The starting point of the axis is
# always at the start of the day (9:30am). Note that we use trading days, so
# we skip Sat and Sun in counting the days.
$startDate = $endDate - fmod2($endDate, 86400) + 9 * 3600 + 30 * 60;
for($i = 1; $i < $durationInDays; ++$i) {
if (getChartWeekDay($startDate) == 1) {
$startDate = $startDate - 3 * 86400;
} else {
$startDate = $startDate - 86400;
}
}
}
# The moving average periods selected by the user.
$avgPeriod1 = 0;
$avgPeriod1 = (int)($_REQUEST["movAvg1"]);
$avgPeriod2 = 0;
$avgPeriod2 = (int)($_REQUEST["movAvg2"]);
if ($avgPeriod1 < 0) {
$avgPeriod1 = 0;
} else if ($avgPeriod1 > 300) {
$avgPeriod1 = 300;
}
if ($avgPeriod2 < 0) {
$avgPeriod2 = 0;
} else if ($avgPeriod2 > 300) {
$avgPeriod2 = 300;
}
# We need extra leading data points in order to compute moving averages.
$extraPoints = 20;
if ($avgPeriod1 > $extraPoints) {
$extraPoints = $avgPeriod1;
}
if ($avgPeriod2 > $extraPoints) {
$extraPoints = $avgPeriod2;
}
# The data series we want to get.
$tickerKey = $_REQUEST["TickerSymbol"];
# In this demo, we can get 15 min, daily, weekly or monthly data depending on the
# time range.
$resolution = 86400;
if ($durationInDays <= 10) {
# 10 days or less, we assume 15 minute data points are available
$resolution = 900;
# We need to adjust the startDate backwards for the extraPoints. We assume
# 6.5 hours trading time per day, and 5 trading days per week.
$dataPointsPerDay = 6.5 * 3600 / $resolution;
$adjustedStartDate = $startDate - fmod2($startDate, 86400) - (int)(
$extraPoints / $dataPointsPerDay * 7 / 5 + 0.9999999) * 86400 - 2 * 86400
;
# Get the required 15 min data
get15MinData($tickerKey, $adjustedStartDate, $endDate);
} else if ($durationInDays >= 4.5 * 360) {
# 4 years or more - use monthly data points.
$resolution = 30 * 86400;
# Adjust startDate backwards to cater for extraPoints
$YMD = getChartYMD($startDate);
$currentMonth = (int)($YMD / 100) % 100 - $extraPoints;
$currentYear = (int)($YMD / 10000);
while ($currentMonth < 1) {
$currentYear = $currentYear - 1;
$currentMonth = $currentMonth + 12;
}
$adjustedStartDate = chartTime($currentYear, $currentMonth, 1);
# Get the required monthly data
getMonthlyData($tickerKey, $adjustedStartDate, $endDate);
} else if ($durationInDays >= 1.5 * 360) {
# 1 year or more - use weekly points.
$resolution = 7 * 86400;
# Adjust startDate backwards to cater for extraPoints
$adjustedStartDate = $startDate - $extraPoints * 7 * 86400 - 6 * 86400;
# Get the required weekly data
getWeeklyData($tickerKey, $adjustedStartDate, $endDate);
} else {
# Default - use daily points
$resolution = 86400;
# Adjust startDate backwards to cater for extraPoints. We multiply the days
# by 7/5 as we assume 1 week has 5 trading days.
$adjustedStartDate = $startDate - fmod2($startDate, 86400) - (int)((
$extraPoints * 7 + 4) / 5) * 86400 - 2 * 86400;
# Get the required daily data
getDailyData($tickerKey, $adjustedStartDate, $endDate);
}
# We now confirm the actual number of extra points (data points that are before
# the start date) as inferred using actual data from the database.
$extraPoints = count($timeStamps);
for($i = 0; $i < count($timeStamps); ++$i) {
if ($timeStamps[$i] >= $startDate) {
$extraPoints = $i;
break;
}
}
# Check if there is any valid data
if ($extraPoints >= count($timeStamps)) {
# No data - just display the no data message.
$errMsg = new MultiChart(400, 50);
$errMsg->addTitle2(Center, "No data available for the specified time period",
"arial.ttf", 10);
return $errMsg;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -