The answer to draw down in php

Sorry if its not clean or clear what im doing here but i needed to make a quick fix for draw down and this is what i came up with, feel free to ask anything and yes its in php :crazy_face:

$PREsub_conn = MYSQL_CONNECTOR($servername, $username, $password, $dbname);

$equery = "SELECT AVG(`PLAN_B_CHANGE_PCT`) FROM `day_trades` WHERE `id` < '$table_number'";

if ($dresult=mysqli_query($PREsub_conn,$equery)){
// Fetch one and one row
while ($get_padv=mysqli_fetch_assoc($dresult))
{
print_r($get_padv);// sleep(1000000);
$raw_stock_chang = $get_padv[“AVG(PLAN_B_CHANGE_PCT)”];
$STOCK_CHANGE = round($raw_stock_chang,2,PHP_ROUND_HALF_UP);
if ($STOCK_CHANGE ==0){
$STOCK_CHANGE= 1;
}
}}

	mysqli_close($PREsub_conn);	

if (isset($STOCK_CHANGE)){

$LPI_A = $_SESSION[($trade[1])]['SC'][1]["LAST_DOWN_ADV"];
$LPI_B = $_SESSION[($trade[1])]['SC'][2]["LAST_DOWN_ADV"];
$LPI_C = $_SESSION[($trade[1])]['SC'][3]["LAST_DOWN_ADV"];
$LPI_D = $_SESSION[($trade[1])]['SC'][4]["LAST_DOWN_ADV"];
$LPI_E = $_SESSION[($trade[1])]['SC'][5]["LAST_DOWN_ADV"];		

//reset mem if the adv is not the same
if ($LPI_A> 0.0001 && $LPI_A <> $STOCK_CHANGE){

$_SESSION[($trade[1])]['SC'][1]["LAST_DOWN_ADV"]="";
$_SESSION[($trade[1])]['SC'][2]["LAST_DOWN_ADV"]="";
$_SESSION[($trade[1])]['SC'][3]["LAST_DOWN_ADV"]="";
$_SESSION[($trade[1])]['SC'][4]["LAST_DOWN_ADV"]="";
$_SESSION[($trade[1])]['SC'][5]["LAST_DOWN_ADV"]="";	

//jab/poke mem for last price index
		$_SESSION[($trade[1])]['LPI'][1]["LAST_PRICE"] = "" ;
		$_SESSION[($trade[1])]['LPI'][2]["LAST_PRICE"] = "" ;
		$_SESSION[($trade[1])]['LPI'][3]["LAST_PRICE"] = "" ;
		}
$LPI_A = $_SESSION[($trade[1])]['SC'][1]["LAST_DOWN_ADV"];
$LPI_B = $_SESSION[($trade[1])]['SC'][2]["LAST_DOWN_ADV"];
$LPI_C = $_SESSION[($trade[1])]['SC'][3]["LAST_DOWN_ADV"];
$LPI_D = $_SESSION[($trade[1])]['SC'][4]["LAST_DOWN_ADV"];
$LPI_E = $_SESSION[($trade[1])]['SC'][5]["LAST_DOWN_ADV"];				
			if ($LPI_A <0.001){
					$_SESSION[($trade[1])]['SC'][1]["LAST_DOWN_ADV"] = $STOCK_CHANGE ;
					}
					
					if (
					//(($LPI_A) + ($LPI_A * '0.03')) < $Memory_3 &&
				//	isset($LPI_A) && $LPI_A >0.01 && $Memory_3 > $LPI_A && $LPI_B < 0.001
				$LPI_B <0.001 &&  isset($LPI_A) &&
				$STOCK_CHANGE ==  $LPI_A
				){
					$_SESSION[($trade[1])]['SC'][2]["LAST_DOWN_ADV"]= $STOCK_CHANGE ;
					}
					
					if (
				//	(($LPI_B) + ($LPI_B * '0.03')) < $Memory_3 &&
				//	isset($LPI_B)&&   $LPI_B >0.01 && $Memory_3 > $LPI_B && $LPI_C < 0.001
				$LPI_C <0.001 &&  isset($LPI_B)&&
				$STOCK_CHANGE ==  $LPI_B
				){
					$_SESSION[($trade[1])]['SC'][3]["LAST_DOWN_ADV"]= $STOCK_CHANGE ;
					}
					
					
				if (
				//	(($LPI_B) + ($LPI_B * '0.03')) < $Memory_3 &&
				//	isset($LPI_B)&&   $LPI_B >0.01 && $Memory_3 > $LPI_B && $LPI_C < 0.001
					$LPI_D <0.001 &&  isset($LPI_C)&&
				$STOCK_CHANGE ==  $LPI_C
				){
					$_SESSION[($trade[1])]['SC'][4]["LAST_DOWN_ADV"]= $STOCK_CHANGE ;
					}	
					
				if (
				//	(($LPI_B) + ($LPI_B * '0.03')) < $Memory_3 &&
				//	isset($LPI_B)&&   $LPI_B >0.01 && $Memory_3 > $LPI_B && $LPI_C < 0.001
					$LPI_E <0.001 &&  isset($LPI_D)&&
				$STOCK_CHANGE ==  $LPI_D
				){
					$_SESSION[($trade[1])]['SC'][5]["LAST_DOWN_ADV"]= $STOCK_CHANGE ;
					}	
					
				
//find and set LPI
//Last Price indicator 
$LPI_A = $_SESSION[($trade[1])]['SC'][1]["LAST_DOWN_ADV"];
$LPI_B = $_SESSION[($trade[1])]['SC'][2]["LAST_DOWN_ADV"];
$LPI_C = $_SESSION[($trade[1])]['SC'][3]["LAST_DOWN_ADV"];
$LPI_D = $_SESSION[($trade[1])]['SC'][4]["LAST_DOWN_ADV"];
$LPI_E = $_SESSION[($trade[1])]['SC'][5]["LAST_DOWN_ADV"];		

if ($LPI_A > 0.001 && $LPI_B > 0.001 && $LPI_C > 0.001 && $LPI_D > 0.001 && $LPI_E > 0.001 ){

$POST_draw_Down = True;

}else{

$POST_draw_Down = False;

}

}