	

<?php 
	public function first_strategy(){
			$first_strategy_users = $this->db->table('trade_process')->select('uusersid')->where('strategy_mode','1')->distinct()->where('trade_status','1')->get()->getResult();
   //           $i=0;
			// foreach ($first_strategy_users as $key => $value) {
			// 	$userid[$i] = $value->uusersid;
			// 	$i++;
			// }

			$userid[0] = 131;

			for ($i=0; $i < count($userid); $i++) { 



				$userid_bind_process = $this->db->table('bind_process')->where('uusersid',$userid[$i])->get()->getRow();

				$trade_process_array = $this->db->table('trade_process')->where('uusersid',$userid[$i])->where('trade_status','1')->where('strategy_mode','1')->get()->getResult();

			
			

			
				

				for ($j=0; $j < count($trade_process_array); $j++) {

					$trade_process = $trade_process_array[$j];

					if ($userid_bind_process && $trade_process && $trade_process->trade_status == '1') {
						
						$key = $userid_bind_process->api_key;
						$secret = $userid_bind_process->secret_key;
						$api = new Binance\API($key,$secret,true);

					$ticker = $api->prices($trade_process->pair); // Make sure you have an updated ticker object for this to work
					
					$trade_where = array(
						'uuserid' => $userid[$i],
						// 'type' => '1',
						'trade_process_id' => $trade_process->id,
					);

					$last_buy = $this->db->table('api_trade_history')->where($trade_where)->orderBy('id','DESC')->get()->getRow();

					$last_buy_live_price = $last_buy->live_price;

					$this->session->set('cron_pair',strtolower($trade_process->pair));

					$live_price =  $ticker[$trade_process->pair];

					$dummy_update = array(
                       "pair" => $trade_process->pair,
                       "price" => $live_price,
                       "updated_time" => time()
					);

					$this->db->table('live_price')->insert($dummy_update);



                          $ticks = $api->candlesticks($trade_process->pair, "5m");
                           $res = end($ticks);
                           $previous_high_price = $res['close'];


				

					if ($last_buy_live_price > $live_price) {

						          if($last_buy->type == 1)  {  
						$margin_call_drop = $trade_process->margincall_drop;
						$explode_margin_call_drop = explode(',', $margin_call_drop);
						$exploded = array_combine(range(1, count($explode_margin_call_drop)), $explode_margin_call_drop);
						$old_price = '';
         
						for ($k=1; $k<=count($exploded); $k++) {
							
					        $last_buy = $this->db->table('api_trade_history')->where($trade_where)->orderBy('id','DESC')->get()->getRow();
							if ($old_price == '') {
								$old_price = $last_buy->live_price;
							}
                           

							

							$call_drop_where = array(
								'trade_process_id' => $trade_process->id,
								'uuserid' => $userid[$i],
								'margin_call_drops' => $k
							);


							$call_drop = $this->db->table('api_trade_history')->where($call_drop_where)->get()->getRow();
							
                 			if (!$call_drop) {

								$check_percentage = ($old_price - $live_price) / $old_price * 100 ;

								if ($check_percentage >= $exploded[$k]) {

									$explode_buyin_ratio_times = explode(',', $trade_process->buyin_ratio_times);
									$exploded_buy_ratio = array_combine(range(1, count($explode_buyin_ratio_times)), $explode_buyin_ratio_times);

									if($trade_process->open_position_doubled != 'on' || $trade_process->open_position_doubled == '')
										$quantity = 1;
									else
										$quantity = 2;
									$price = $trade_process->first_buy_amount * $quantity;

									$quantity = $price * $exploded_buy_ratio[$k];

									if ($trade_process->margin_call == '1' ) {
										$buy_done = $this->marketbuy($userid[$i],$trade_process->id,$quantity,$k,$api,$trade_process->pair);
										$call_drop_where1 = array(
										'trade_process_id' => $trade_process->id,
										'uuserid' => $userid[$i],
										
										);
								
								$call_drop = $this->db->table('api_trade_history')->where($call_drop_where1)->get()->getRow();

								$old_price = $call_drop->live_price;
								if($buy_done)
								break;
									}
								}

							}else{

								$call_drop_where1 = array(
									'trade_process_id' => $trade_process->id,
									'uuserid' => $userid[$i],
									// 'margin_call_drops' => $k
								);
								
								$call_drop = $this->db->table('api_trade_history')->where($call_drop_where1)->get()->getRow();

								$old_price = $call_drop->live_price;
							}
						}
							
	
					}
				else if($last_buy->type == 2){
			
			        if($previous_high_price < $live_price){
                  		$sell_record_update = $this->db->table('trade_process')->set('flag_buy','1')->where("id",$trade_process->id)->update();
                  	}

                  	  $trade = $this->db->table('trade_process')->where("id",$trade_process->id)->get()->getRow();
           


                  	if($trade->flag_buy == 1) {

                  	$buy_in_callback_temp = $live_price * $trade_process->buyin_callback / 100;
					$buy_in_callback_temp = $buy_in_callback_temp + $live_price;

					if($buy_in_callback_temp >= $live_price){

						if($trade_process->open_position_doubled != 'on' || $trade_process->open_position_doubled == ''){
								$quantity = 1;
						}
						else
						{
								$quantity = 2;
						}
								$price = $trade_process->first_buy_amount * $quantity;
								$quantity = $price ;
								$ticker = $api->prices();
								$balances = $api->balances($ticker);
								$total_asset = $balances['USDT']['available'];

									if ($trade_process->margin_call == '1' ) {
										if($quantity >= 11 && $total_asset > $quantity)
										$buy_result = $this->marketbuy($userid[$i],$trade_process->id,$quantity,$k,$api,$trade_process->pair);
										$buy_done = $this->marketbuy($userid[$i],$trade_process->id,$quantity,$k,$api,$trade_process->pair);
										$call_drop_where1 = array(
										'trade_process_id' => $trade_process->id,
										'uuserid' => $userid[$i]
										);
								
								$call_drop = $this->db->table('api_trade_history')->where($call_drop_where1)->get()->getRow();

								$old_price = $call_drop->live_price;
								if($buy_done)
								break;
									}
					            }
                  	}
				}

						
              
			        }

			 else{


				if($last_buy->type == 2){  //after take profit call market up buyincallback call

					$buy_in_callback_temp = $last_buy->live_price * $trade_process->buyin_callback / 100;
					$buy_in_callback_temp = $buy_in_callback_temp + $last_buy->live_price;

				

					if($buy_in_callback_temp >= $live_price){

						if($trade_process->open_position_doubled != 'on' || $trade_process->open_position_doubled == '')
								$quantity = 1;
						else
								$quantity = 2;
								$price = $trade_process->first_buy_amount * $quantity;
								$quantity = $price ;

								
								$ticker = $api->prices(); // Make sure you have an updated ticker object for this to work
								$balances = $api->balances($ticker);
								$total_asset = $balances['USDT']['available'];

									if ($trade_process->margin_call == '1' ) {
										if($quantity >= 11 && $total_asset > $quantity)
										$buy_result = $this->marketbuy($userid[$i],$trade_process->id,$quantity,$k,$api,$trade_process->pair);
										$buy_done = $this->marketbuy($userid[$i],$trade_process->id,$quantity,$k,$api,$trade_process->pair);
										$call_drop_where1 = array(
										'trade_process_id' => $trade_process->id,
										'uuserid' => $userid[$i],
										);
								
								$call_drop = $this->db->table('api_trade_history')->where($call_drop_where1)->get()->getRow();

								$old_price = $call_drop->live_price;
								if($buy_done)
								break;
									}
					            }

				}
				else { 

					

					

					if($trade_process->oneshot_limit == "" || $trade_process->oneshot_limit == 0) { // normal take profit call

						$trade_where = array(
							'uuserid' => $userid[$i],
							'type' => '1',
							'status' => 0,
							'trade_process_id' => $trade_process->id,
						);


						$buy_records = $this->db->table('api_trade_history')->selectAvg('live_price')->where($trade_where)->orderBy('id','DESC')->get()->getRow();

							
						// $check_percentage = ($live_price - $buy_records->live_price) / $buy_records->live_price * 100 ;

						// echo"<pre>";
						// print_r($check_percentage);
						// echo"</pre>";
						// die;

						// $buy_records->live_price= 418.2;

						if ($buy_records->live_price > 0 && $buy_records != '') {

							$avg_price = round($buy_records->live_price,8) * $trade_process->whole_profit_ratio / 100;

							$max_increase_price = $buy_records->live_price + $avg_price ;
							
							$max_increase_price = round($max_increase_price,8);

							$max_down_percentage = $max_increase_price * $trade_process->whole_profit_callback / 100;

							$max_down_price = $max_increase_price - $max_down_percentage;

							$max_down_price = round($max_down_price,8);

						
								echo"<pre>";
								print_r("MAX ".$max_increase_price);
								print_r("LIVE PRICE ".$live_price);
								print_r("MAX DOWN  ".$max_down_price);
								echo"</pre>";
								// exit();

								// $max_increase_price = 417.5;
								// $max_down_price = 417.5;
                            
                            if ($live_price >= $max_increase_price) {
                            	$sell_record_update = $this->db->table('trade_process')->set('flag','1')->where("id",$trade_process->id)->update();
                            }


                        $trade = $this->db->table('trade_process')->where("id",$trade_process->id)->get()->getRow();

                        // echo"<pre>";
                        // print_r($trade);
                        // echo"</pre>";
                        // die;

                     

							if ($trade->flag == 1) {

								$trade_where = array(
									'uuserid' => $userid[$i],
									'type' => '1',
									'status' => 0,
									'trade_process_id' => $trade_process->id,
								);


								$buyed_quantity = $this->db->table('api_trade_history')->selectSum('received_amount')->where($trade_where)->orderBy('id','DESC')->get()->getRow();

								
								
								$quantity = number_format($buyed_quantity->received_amount,1);
								$price = $max_increase_price; // Try to sell it for 0.5 btc
								$stopPrice = $max_down_price; // Sell immediately if price goes below 0.4 btc
								// $quantity = 0.07;

								echo"<pre>";
								print_r("MAX ".$max_increase_price);
								print_r("LIVE PRICE ".$live_price);
								print_r("MAX DOWN  ".$max_down_price);
								echo"</pre>";
								// exit();
								$ticker = $api->prices(); // Make sure you have an updated ticker object for this to work
								$balances = $api->balances($ticker);
								$total_asset = $balances[substr($trade->pair,0,-4)]['available'];

								
							
								
								if($max_down_price == $live_price){
									if(!empty($quantity) && $total_asset >= $quantity)
								           $result = $this->take_profit($userid[$i],$trade_process->id,$quantity,$price,$live_price,$api,$trade_process->pair,$buy_records->live_price);
								    else
								    	$result = 0;
								} else if($live_price > $avg_price && $max_down_price <= $live_price){
									if(!empty($quantity) && $total_asset >= $quantity)
                                        $result = $this->take_profit($userid[$i],$trade_process->id,$quantity,$price,$live_price,$api,$trade_process->pair,$buy_records->live_price);
                                    else
                                    	$result = 0;
								}

							
							

							if($result)
									$buy_record_update = $this->db->table('api_trade_history')->set('status','1')->where($trade_where)->update();

							if ($trade_process->strategy_type != 'Cycle Strategy' && $result) {
									$this->db->table('trade_process')->set('trade_status','2')->where('id',$trade_process->id)->update();
								}

								$margin_call_drop = $trade_process->margincall_drop;

								$explode_margin_call_drop = explode(',', $margin_call_drop);
								$exploded = array_combine(range(1, count($explode_margin_call_drop)), $explode_margin_call_drop);

								$last_call_drop = array_key_last($exploded);

								$call_drop_where = array(
									'trade_process_id' => $trade_process->id,
									'uuserid' => $userid[$i],
									'margin_call_drops' => $last_call_drop
								);

								$call_drop_check = $this->db->table('api_trade_history')->where($call_drop_where)->get()->getRow();


								if ($call_drop_check != '') {
									if($result)
									$this->db->table('trade_process')->set('trade_status','2')->where('id',$trade_process->id)->update();
								}
								

								// return true;
							}
						}
						}else{
							echo"<pre>";
							print_r($trade_process);
							echo"</pre>";
							$trade = $this->db->table('trade_process')->where("id",$trade_process->id)->get()->getRow();

                             
                             if($trade_process->oneshot_limit <= $live_price){   // one shot hit new feature
									$trade_where = array(
									'uuserid' => $userid[$i],
									'type' => '1',
									'status' => 0,
									'trade_process_id' => $trade_process->id,
									);

									$buy_records = $this->db->table('api_trade_history')->selectAvg('live_price')->where($trade_where)->orderBy('id','DESC')->get()->getRow();


								    $buyed_quantity = $this->db->table('api_trade_history')->selectSum('received_amount')->where($trade_where)->orderBy('id','DESC')->get()->getRow();

								    echo"<pre>";
								    print_r($buy_records);
								    print_r($buyed_quantity);
								    echo"</pre>";

								
								
									$quantity = number_format($buyed_quantity->received_amount,1);
									$ticker = $api->prices(); // Make sure you have an updated ticker object for this to work
									$balances = $api->balances($ticker);
									$total_asset = $balances[substr($trade->pair,0,-4)]['available'];

									
									if(!empty($quantity) && $total_asset >= $quantity)
								           $result = $this->take_profit($userid[$i],$trade_process->id,$quantity,$price,$live_price,$api,$trade_process->pair,$buy_records->live_price);
								    else
								    	$result = 0;

								    if($result){
								      $this->db->table('trade_process')->set('trade_status','2')->where('id',$trade_process->id)->update();
								    }

                             }


						}
					}
				}

				}
				$this->session->remove('live_price');
				$this->session->remove('cron_pair');
					$this->session->remove('high_price');
					$this->session->remove('previous_high_price');
			}
		}

	}
