Hello,
I coded a page which displays trading data
Here is a screenshot of a typical page
When a user clicks on hide, the text “hide” changes into "show and this trade is not included in the charts or in the summary table. a message shows up letting the user that a trade is hidden, and there is a button which will show all hidden trades
"
So far, all works well.
Here is the code for the variables creating the data
//Get the data for balance graph
$data_b = profit_loss_single_couple($couple, $user_id);
//Get the data for trades counter graph
$data_c = p_l_ֹcounter_single_couple($couple, $user_id);
//Get the data for line graph
$data_l = get_Single_Couple_profit($couple, $user_id);
//var_dump($data_l);
//die();
//Get the data for summary table
$table_data = closed_pos_single_couple($couple, $user_id);
//Get the data for closed positions table
$positions_couple = closed_pos_table_single_couple($couple, $user_id);
// Count Couple hidden trades
$hidden_trades_couple = count_hidden_pos_couple($couple, $user_id);
here is the function which exracts the table data from the DB
function closed_pos_table_single_couple($item, $user_id)
{
global $db;
try
{
$sql = "SELECT id, user_id, ticket, type, size, item, o_time, o_price, c_time, c_price, profit, hideshow
FROM `data`
WHERE `user_id` = :user_id
AND `item` = :item
ORDER BY `c_time` ASC";
$stmt = $db->prepare($sql);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->bindParam(':item', $item, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() == 0)
return 0;
else
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
echo $e;
die();
return false;
}
}
And here is the code for summary table function
function closed_pos_single_couple($item, $user_id)
{
global $db;
try
{
$sql = "SELECT `profit`, `hideshow`, `item`, `user_id`, `o_time`
,SUM(CASE WHEN `profit` >= 0 THEN `profit` ELSE 0.0 END) AS TotalProfit
,SUM(CASE WHEN `profit` < 0 THEN `profit` ELSE 0.0 END) AS TotalLoss
,SUM(CASE WHEN `profit` >= 0 THEN 1 ELSE 0.0 END) AS CountProfit
,SUM(CASE WHEN `profit` < 0 THEN 1 ELSE 0.0 END) AS CountLoss
,AVG(CASE WHEN `profit` >= 0 THEN `profit` ELSE 0.0 END) AS AveProfit
,AVG(CASE WHEN `profit` < 0 THEN `profit` ELSE 0.0 END) AS AveLoss
, MAX(profit), MIN(profit), MIN(`o_time`) AS StartDate, MAX(`o_time`) AS EndDate
,SUM(CASE WHEN `profit` >= 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS BuyProfit
,SUM(CASE WHEN `profit` < 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS BuyLoss
,SUM(CASE WHEN `profit` >= 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS SellProfit
,SUM(CASE WHEN `profit` < 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS SellLoss
,SUM(CASE WHEN `profit` >= 0 AND type LIKE 'buy%' THEN 1 ELSE 0.0 END) AS CountBuyProfit
,SUM(CASE WHEN `profit` < 0 AND type LIKE 'buy%' THEN 1 ELSE 0.0 END) AS CountBuyLoss
,SUM(CASE WHEN `profit` >= 0 AND type LIKE 'sell%' THEN 1 ELSE 0.0 END) AS CountSellProfit
,SUM(CASE WHEN `profit` < 0 AND type LIKE 'sell%' THEN 1 ELSE 0.0 END) AS CountSellLoss
,AVG(CASE WHEN `profit` >= 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS AvgLongProfit
,AVG(CASE WHEN `profit` < 0 AND type LIKE 'buy%'THEN `profit` ELSE 0.0 END) AS AvgLongLoss
,AVG(CASE WHEN `profit` >= 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS AvgShortProfit
,AVG(CASE WHEN `profit` < 0 AND type LIKE 'sell%'THEN `profit` ELSE 0.0 END) AS AvgShortLoss
,MAX(CASE WHEN `profit` >= 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS MaxBuyProfit
,MIN(CASE WHEN `profit` < 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS MaxBuyLoss
,MAX(CASE WHEN `profit` >= 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS MaxSellProfit
,MIN(CASE WHEN `profit` < 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS MaxSellLoss
FROM `data`
WHERE `hideshow` = 'hide'
AND `user_id` = :user_id
AND `item` = :item";
$stmt = $db->prepare($sql);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->bindParam(':item', $item, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() == 0)
return 0;
else
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
echo $e;
die();
return false;
}
}
And here is the jquery which emphasizes the hidden trade
$("td:contains('show')").css({"background-color":"#16e6f4", "font-weight":"bold"});
});
the problem is that I want to let users filter their results by dates, weekdays, hours.
Here is a screenshot of the table storing the filtering criteria
Here is the code which works with the data (with 2 lines which will display the data for this post
// Here a variable wil get user filtering data and will insert all filtering criteria into variables which will
// go to all functions
$filter_criteria = get_user_filter_criteria($user_id);
foreach($filter_criteria as $xrow =>$xval)
{
$start_date = $xval['start_date'];
$end_date = $xval['end_date'];
$weekdays = $xval['weekdays'];
$hour_start = $xval['hour_start'];
$hour_end = $xval['hour_end'];
} // End foreach
var_dump($filter_criteria);
die();
and here is the relevant function
function get_user_filter_criteria($user_id)
{
global $db;
try
{
$sql = "SELECT * FROM `filter` WHERE `user_id` = :user_id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() == 0)
return 0;
else
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
return false;
}
}
Here is the var_dump results
C:\wamp64\www\AHopefully\pages\page_header.php:37: array (size=1) 0 => array (size=7) 'f_id' => string '28' (length=2) 'user_id' => string '30' (length=2) 'start_date' => string '2017-08-14' (length=10) 'end_date' => string '2022-06-09' (length=10) 'weekdays' => string '0,1,2,3,4,5,6' (length=13) 'hour_start' => string '00:00:00' (length=8) 'hour_end' => string '23:59:59' (length=8)
Now I add the variables to the functions and function calls:
these are for the table
function closed_pos_table_single_couple($item, $user_id, $start_date, $end_date, $hour_start, $hour_end, $days)
{
global $db;
try
{
$sql = "SELECT id, user_id, ticket, type, size, item, o_time, o_price, c_time, c_price, profit, hideshow
FROM `data`
WHERE DATE_FORMAT(`o_time`, '%Y-%m-%d') BETWEEN :start_date AND :end_date
AND TIME(`o_time`) BETWEEN :hour_start AND :hour_end
AND WEEKDAY(`o_time`) IN ($days)
AND `user_id` = :user_id
AND `item` = :item
ORDER BY `c_time` ASC";
$stmt = $db->prepare($sql);
$stmt->bindParam(':item', $item, PDO::PARAM_STR);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$stmt->bindParam(':start_date', $start_date, PDO::PARAM_STR);
$stmt->bindParam(':end_date', $end_date, PDO::PARAM_STR);
$stmt->bindParam(':hour_start', $hour_start, PDO::PARAM_STR);
$stmt->bindParam(':hour_end', $hour_end, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() == 0)
return 0;
else
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
echo $e;
die();
return false;
}
}
$positions_couple = closed_pos_table_single_couple($couple, $user_id, $start_date, $end_date, $hour_start, $hour_end, $days);
And these are for the summary table
$table_data = closed_pos_single_couple($couple, $user_id, $start_date, $end_date, $hour_start, $hour_end, $days);
And
function closed_pos_single_couple($item, $user_id, $start_date, $end_date, $hour_start, $hour_end, $days)
{
global $db;
try
{
$sql = "SELECT `profit`, `hideshow`, `item`, `user_id`, `o_time`
,SUM(CASE WHEN `profit` >= 0 THEN `profit` ELSE 0.0 END) AS TotalProfit
,SUM(CASE WHEN `profit` < 0 THEN `profit` ELSE 0.0 END) AS TotalLoss
,SUM(CASE WHEN `profit` >= 0 THEN 1 ELSE 0.0 END) AS CountProfit
,SUM(CASE WHEN `profit` < 0 THEN 1 ELSE 0.0 END) AS CountLoss
,AVG(CASE WHEN `profit` >= 0 THEN `profit` ELSE 0.0 END) AS AveProfit
,AVG(CASE WHEN `profit` < 0 THEN `profit` ELSE 0.0 END) AS AveLoss
, MAX(profit), MIN(profit), MIN(`o_time`) AS StartDate, MAX(`o_time`) AS EndDate
,SUM(CASE WHEN `profit` >= 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS BuyProfit
,SUM(CASE WHEN `profit` < 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS BuyLoss
,SUM(CASE WHEN `profit` >= 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS SellProfit
,SUM(CASE WHEN `profit` < 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS SellLoss
,SUM(CASE WHEN `profit` >= 0 AND type LIKE 'buy%' THEN 1 ELSE 0.0 END) AS CountBuyProfit
,SUM(CASE WHEN `profit` < 0 AND type LIKE 'buy%' THEN 1 ELSE 0.0 END) AS CountBuyLoss
,SUM(CASE WHEN `profit` >= 0 AND type LIKE 'sell%' THEN 1 ELSE 0.0 END) AS CountSellProfit
,SUM(CASE WHEN `profit` < 0 AND type LIKE 'sell%' THEN 1 ELSE 0.0 END) AS CountSellLoss
,AVG(CASE WHEN `profit` >= 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS AvgLongProfit
,AVG(CASE WHEN `profit` < 0 AND type LIKE 'buy%'THEN `profit` ELSE 0.0 END) AS AvgLongLoss
,AVG(CASE WHEN `profit` >= 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS AvgShortProfit
,AVG(CASE WHEN `profit` < 0 AND type LIKE 'sell%'THEN `profit` ELSE 0.0 END) AS AvgShortLoss
,MAX(CASE WHEN `profit` >= 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS MaxBuyProfit
,MIN(CASE WHEN `profit` < 0 AND type LIKE 'buy%' THEN `profit` ELSE 0.0 END) AS MaxBuyLoss
,MAX(CASE WHEN `profit` >= 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS MaxSellProfit
,MIN(CASE WHEN `profit` < 0 AND type LIKE 'sell%' THEN `profit` ELSE 0.0 END) AS MaxSellLoss
FROM `data`
WHERE DATE_FORMAT(`o_time`, '%Y-%m-%d') BETWEEN :start_date AND :end_date
AND TIME(`o_time`) BETWEEN :hour_start AND :hour_end
AND WEEKDAY(`o_time`) IN ($days)
AND `hideshow` = 'hide'
AND `user_id` = :user_id
AND `item` = :item";
$stmt = $db->prepare($sql);
$stmt->bindParam(':item', $item, PDO::PARAM_STR);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$stmt->bindParam(':start_date', $start_date, PDO::PARAM_STR);
$stmt->bindParam(':end_date', $end_date, PDO::PARAM_STR);
$stmt->bindParam(':hour_start', $hour_start, PDO::PARAM_STR);
$stmt->bindParam(':hour_end', $hour_end, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() == 0)
return 0;
else
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
echo $e;
die();
return false;
}
}
now, the hidden trade wont show in the table
And when the show hidden trades button is clicked, it disappears but the trade wont show in the table and won’t be taken in the calculations for the page functions
Here is aresult of a query on the sql page in the DB
What is the problem here ?
What did I do wrong ?