File: /var/www/html/wp-content/plugins/bsx/inc/camp-tabs.php
<?php
add_shortcode('bsx-tab-candle', 'bsx_tab_candle');
function bsx_tab_candle() {
$id = get_queried_object_id();
$c = cc($id);
$kpis = $c['kpis'];
//get and format status
$cstat = getStatus($id);
$burn = (floatval(get_field("tenure",$id)) / 3 + 15) * 3600 * 24;
$s = strtotime($kpis->edt) - $burn;
if($cstat=="Closed") {
$e = strtotime($kpis->xdt) + $burn;
} else {
$e = strtotime($kpis->cdt) + $burn;
}
$series = bsx_rest_get("mkt", ['ysym'=>$c['ysym'],
'start'=>$s,
'end'=>$e,
'inteval' => "1d"]);
if (empty($series)) {
return [];
}
$ts = [];
foreach ($series as $row) {
if($row['dt']==0) continue;
$timestamp = is_numeric($row['dt'])
? intval($row['dt'])
: strtotime($row['dt']);
$ts[] = [
'x' => $timestamp * 1000,
'y' => [
(float)$row['open'],
(float)$row['high'],
(float)$row['low'],
(float)$row['close']
]
];
}
$corp = $c['corp'];
$chart_id = 'chart-' . uniqid();
// Annotations vorbereiten
$annotations = ['xaxis' => [], 'points' => []];
$edt = wp_date('U',strtotime($kpis->edt)) * 1000;
$eprc = $kpis->eprc;
$annotations['xaxis'][] = [
'x' => $edt,
'borderColor' => '#ffcc33',
'label' => [
'borderColor' => '#ffcc33',
'style' => ['color' => '#eee', 'background' => '#1a6c24'],
'text' => 'Entry'
]
];
$xdt = wp_date('U',strtotime($kpis->xdt)) * 1000;
$xprc = $kpis->eprc;
$annotations['xaxis'][] = [
'x' => $xdt,
'borderColor' => '#ffcc33',
'label' => [
'borderColor' => '#ffcc33',
'style' => ['color' => '#eee', 'background' => '#861029'],
'text' => 'Close'
]
];
$annotations_json = wp_json_encode($annotations);
// side text ------------------------------------------------------
$cntr = get_field("contract",$id);
$cntr_id = $cntr[0]->ID;
$cass = get_field("cassandra",$id);
$cass_id = $cass[0]->ID;
$comp = get_field("company",$id);
$comp_id = $comp[0]->ID;
$ysym = get_field("ysym", $cntr_id);
$fx = get_field("currency", $cntr_id);
$kpi_rows = array(
'Entry date/time' => $kpis->edt,
'Entry price' => sprintf("%8.3f %s", $kpis->eprc, $fx),
''=>'',
'Last date/time' => $kpis->cdt,
'Last price' => sprintf("%8.3f %s", $kpis->cprc, $fx),
' '=>'',
'Exit date/time' => ($cstat=='Closed') ? $kpis->xdt : '',
'Exit price' => ($cstat=='Closed') ? $kpis->xprc : '',
' '=>'',
'Absolute return' => sprintf("% 7.2f%%",100 * $kpis->perf)
);
enqueue_apex();
// ---------------------------------------------------------------------------
ob_start();
?>
<div class="ctab-browser-container">
<div class="ctab-sidetext-container">
<?php foreach ($kpi_rows as $label => $value): ?>
<div class="ctab-sidetext-item">
<span class="st-label"><?php echo esc_html($label); ?></span>
<span class="st-val"><?php echo esc_html($value); ?></span>
</div>
<?php endforeach; ?>
</div>
<div class="ctab-chart-container">
<div id=<?php echo esc_attr($chart_id); ?> style="width: 100%;">
<script>
document.addEventListener("DOMContentLoaded", function() {
var options = {
series: [{
name: 'Quote History',
data: <?php echo wp_json_encode($ts); ?>
}],
tooltip:{
theme: 'dark'
},
chart: {
type: 'candlestick',
height: 400,
toolbar: {
show: true,
tools: {
download: true,
zoom: true,
zoomin: true,
zoomout: true,
pan: true,
reset: true
}
}
},
annotations: <?php echo $annotations_json; ?>,
plotOptions: {
candlestick: {
colors: { upward: '#00B746', downward: '#EF403C' }
}
},
xaxis: { type: 'datetime' },
yaxis: {
tooltip: { enabled: true },
labels: { formatter: v => v.toFixed(2) }
},
title: {
text: "<?php echo $corp; ?>",
align: 'left'
},
xaxis: {
type: 'datetime',
labels: {
datetimeUTC: false // Zeige lokale Zeit statt UTC
}
},
yaxis: {
min: (min) => {
const magnitude = Math.pow(10, Math.floor(Math.log10(min)));
const step = magnitude / 2; // 0.5, 5, 50, etc.
return Math.floor(min / step) * step;
},
max: (max) => {
const magnitude = Math.pow(10, Math.floor(Math.log10(max)));
const step = magnitude / 2;
return Math.ceil(max / step) * step;
},
tickAmount: 6,
labels: {
formatter: (val) => {
return val >= 10
? Math.round(val)
: val >= 1
? val.toFixed(2)
: val.toFixed(3);
}
}
},
plotOptions: {
candlestick: {
colors: {
upward: '#00B746', // Grün für steigende Kerzen
downward: '#EF403C' // Rot für fallende Kerzen
}
}
},
grid: {
borderColor: '#e7e7e7',
row: {
colors: ['#111010', '#242323'],
opacity: 0
}
}
};
var chart = new ApexCharts(
document.querySelector("#<?php echo esc_js($chart_id); ?>"),
options
);
chart.render();
});
</script>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode('bsx-tab-pl', 'bsx_tab_pl');
function bsx_tab_pl() {
$id = get_queried_object_id();
$c = cc($id);
$kpis = $c['kpis'];
$edt = $kpis->edt;
$eprc = $kpis->eprc;
$xdt = $kpis->xdt;
$xprc = $kpis->xprc;
$ls = $kpis->ls;
if(empty($xdt)) $xdt = time();
//get and format status
$cstat = getStatus($id);
$burn = (floatval(get_field("tenure",$id)) / 3 + 15) * 3600 * 24;
$s = strtotime($kpis->edt) - $burn;
if($cstat=="Closed") {
$e = strtotime($kpis->xdt) + $burn;
} else {
$e = strtotime($kpis->cdt) + $burn;
}
$series = bsx_rest_get("mkt", ['ysym'=>$c['ysym'],
'start'=>$s,
'end'=>$e,
'interval' => "1d"]);
if (empty($series)) {
return [];
}
$ts = [];
foreach ($series as $row) {
if($row['dt']==0) continue;
$timestamp = is_numeric($row['dt'])
? intval($row['dt'])
: strtotime($row['dt']);
$ts[] = [
'x' => $timestamp * 1000,
'y' => (float)$row['close']
];
}
$ext = [];
$act = [];
if($xdt=='0000-00-00 00:00:00') {
$lst = $kpis->cdt;
} else {
$lst = $xdt;
}
foreach ($ts as $r) {
$tmp = [
'x' => $r['x'],
'y' => (floatval($r['y']) / floatval($eprc) - 1) * floatval($ls) * 100
];
if($r['x'] >= (strtotime($edt)-3600*24)*1000 && $r['x'] <= strtotime($lst)*1000) {
$act[] = $tmp;
}
$ext[] = $tmp;
}
$chart_id = "chart-id-".uniqid();
// side text ------------------------------------------------------
$cntr = get_field("contract",$id);
$cntr_id = $cntr[0]->ID;
$cass = get_field("cassandra",$id);
$cass_id = $cass[0]->ID;
$comp = get_field("company",$id);
$comp_id = $comp[0]->ID;
$ysym = get_field("ysym", $cntr_id);
$fx = get_field("currency", $cntr_id);
$kpi_rows = array(
'Entry date/time' => $kpis->edt,
'Entry price' => sprintf("%8.3f %s", $kpis->eprc, $fx),
''=>'',
'Last date/time' => $kpis->cdt,
'Last price' => sprintf("%8.3f %s", $kpis->cprc, $fx),
' '=>'',
'Exit date/time' => ($cstat=='Closed') ? $kpis->xdt : '',
'Exit price' => ($cstat=='Closed') ? $kpis->xprc : '',
' '=>'',
'Absolute return' => sprintf("% 6.2f%%",100 * $kpis->perf)
);
// -----------------------------------------------
enqueue_apex();
ob_start();
?>
<div class="ctab-browser-container">
<div class="ctab-sidetext-container">
<?php foreach ($kpi_rows as $label => $value): ?>
<div class="ctab-sidetext-item">
<span class="st-label"><?php echo esc_html($label); ?></span>
<span class="st-val"><?php echo esc_html($value); ?></span>
</div>
<?php endforeach; ?>
</div>
<div class="ctab-chart-container">
<div id=<?php echo esc_attr($chart_id); ?> style="width: 100%;">
<script>
document.addEventListener("DOMContentLoaded", function() {
var options = {
annotations:{
yaxis: [{
y:0
}]
},
title: {
text: "P/L (absolute)",
align: 'left'
},
tooltip:{
theme: 'dark'
},
chart: {
id: '<?php echo esc_js($chart_id); ?>',
type: "area",
height: 400,
foreColor: "#888888",
toolbar: {
autoSelected: "pan",
show: false
}
},
stroke: {
curve: 'straight'
},
plotOptions: {
area: {
fillTo: 'origin',
}
},
colors: ['#ffcc33','#030204'],
dataLabels: {
enabled: false
},
series: [{
name: 'P/L',
data: <?php echo wp_json_encode($act); ?>,
color: '#ffcc33',
},
{
name: 'extended',
data: <?php echo wp_json_encode($ext); ?>,
color: '#353337',
}
],
plotOptions: {
area: {
fillTo: 'origin',
}
},
xaxis: {
type: 'datetime',
labels: {
datetimeUTC: false // Zeige lokale Zeit statt UTC
}
},
yaxis: {
min: (min) => Math.min(-5, Math.floor(min / 5) * 5),
max: (max) => Math.max(10, Math.ceil(max / 5) * 5),
tickAmount: 6,
labels: {
formatter: (val) => `${Math.round(val)}%`
}
},
grid: {
borderColor: '#21201e',
row: {
colors: ['#040103', '#040203'],
opacity: 0
}
}
};
var chart = new ApexCharts(
document.querySelector("#<?php echo esc_js($chart_id); ?>"),
options
);
chart.render();
});
</script>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode('bsx-tab-sl', 'bsx_tab_sl');
function bsx_tab_sl() {
$id = get_queried_object_id();
$c = cc($id);
$kpis = $c['kpis'];
$series = bsx_rest_get("sl", ['cid'=>$id]);
if (empty($series)) {
return;
}
$cstat = getStatus($id);
$pnl_data = [];
$sl_data = [];
foreach ($series as $row) {
if($row['dt'] == 0) continue;
$timestamp = is_numeric($row['dt'])
? intval($row['dt'])
: strtotime($row['dt']);
$x = date("Y-m-d H:i:s", $timestamp);
$pnl_data[] = ['x' => $x, 'y' => (float)$row['PnL']];
$sl_data[] = ['x' => $x, 'y' => (float)$row['StopLoss']];
}
$formatted_series = [
['name' => 'PnL', 'data' => $pnl_data],
['name' => 'StopLoss', 'data' => $sl_data]
];
$chart_id = 'chart-cass-line-plot-' . uniqid();
// side text ------------------------------------------------------
$last_pnl = floatval(array_last($pnl_data)['y']);
$last_sl = floatval(array_last($sl_data)['y']);
$curdiff = $last_pnl - $last_sl;
$maxpnl = max($pnl_data);
$lb = ($cstat=="Active") ? floatval(sprintf("%6.2f%%", 100 * floatval($last_sl))) - floatval(sprintf("%6.2f", 100 * floatval($last_pnl))) : 0;
$kpi_rows = array(
'Current Status' => $cstat,
' ' => '',
'PnL' => sprintf("% 6.2f%%", 100 * floatval($kpis->perf)),
'Remaining loss buffer' => $lb,
' '=>'',
'Max. PnL' => sprintf("%6.2f%%", floatval(max(array_column($pnl_data,'y'))))
);
// -----------------------------------------------
enqueue_apex();
ob_start();
?>
<div class="ctab-browser-container">
<div class="ctab-sidetext-container">
<?php foreach ($kpi_rows as $label => $value): ?>
<div class="ctab-sidetext-item">
<span class="st-label"><?php echo esc_html($label); ?></span>
<span class="st-val"><?php echo esc_html($value); ?></span>
</div>
<?php endforeach; ?>
</div>
<div class="ctab-chart-container">
<div id="<?php echo esc_attr($chart_id); ?>" style="width: 100%;">
<script>
document.addEventListener("DOMContentLoaded", function() {
var series = <?php echo wp_json_encode($formatted_series); ?>;
console.log('First point:', JSON.stringify(series[0].data[0]));
var options = {
series: series,
colors:['#ffcc33', '#ebaecb'],
chart: {
id: '<?php echo esc_js($chart_id); ?>',
height: 400,
type: "line"
},
xaxis: {
type: 'datetime'
},
tooltip: {
theme: 'dark'
},
stroke: {
width: [4,2],
curve: 'smooth',
dashArray: [0,10]
}
};
console.log('Options:', options);
var chart = new ApexCharts(
document.querySelector("#<?php echo esc_js($chart_id); ?>"),
options
);
chart.render().then(function() {
console.log('Chart rendered successfully');
}).catch(function(err) {
console.error('Chart render error:', err);
});
});
</script>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode('inline-reports', 'render_inline_reports');
function render_inline_reports() {
global $tz;
$id = get_queried_object_id();
$kpis = getKpis($id);
$reps = getCampReps($id);
$ysym = getYsym($id);
$reps = get_posts(array(
'post_type'=>'report',
'posts_per_page'=> -1
));
$reports = [];
foreach($reps as $rep) {
$cmp = get_field("campaign", $rep->ID);
if(empty($cmp)) continue;
$cid = $cmp[0]->ID;
if($cid != $id) continue;
$rid = $rep->ID;
$reports[] = array(
'id' => $rid,
'title' => get_the_title($rid),
'date' => get_field('pub_date', $rid),
'summary' => nl2br(get_field('summary', $rid)),
'website' => get_field("website", $rid),
'link' => get_permalink()
);
}
ob_start();
// HTML ausgeben
?>
<div class="reports-browser-container">
<div class="reports-list">
<?php foreach ($reports as $report): ?>
<div class="report-item"
data-id="<?php echo esc_attr($report['id']); ?>"
data-summary="<?php echo esc_attr($report['summary']); ?>"
data-link="<?php echo esc_url($report['website']); ?>">
<div class="report-info">
<div class="report-title"><?php echo esc_html($report['title']); ?></div>
<div class="report-date"><?php echo esc_html($report['date']); ?></div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="reports-detail">
<div class="detail-placeholder">Move mouse over a report to read a summary, click on it for full report</div>
<div class="detail-content"></div>
</div>
</div>
<?php
return ob_get_clean();
}
function ctab_scripts_enqueue (){
// inline Report
// -------------------------------------------------------------------------------------------
wp_enqueue_script(
'inline-report',
BSX_PLUGIN_URL . 'ass/js/inline-report.js',
array('jquery'),
'2.0.0',
true
);
wp_enqueue_style(
'report-inline',
BSX_PLUGIN_URL . 'ass/css/inline-report.css',
array(),
'2.0.0'
);
wp_enqueue_style(
'camp-tabs',
BSX_PLUGIN_URL . 'ass/css/camp-tabs.css',
array(),
'3.0.0'
);
}
add_action('wp_enqueue_scripts', 'ctab_scripts_enqueue',99,2);