Şuanki Dizin: /home/wwwdreamtechnolo/www/sharemarkettraders.com/software/ |
Şuanki Dosya : /home/wwwdreamtechnolo/www/sharemarkettraders.com/software/ema.php |
<?php /** * EMA Crossing Strategy * * This code uses the Exponential Moving Average (EMA) indicator to identify potential buy and sell signals in a share market. * * The EMA crossing strategy is based on the idea that when the short-term EMA crosses above the long-term EMA, it is a signal to buy. * Conversely, when the short-term EMA crosses below the long-term EMA, it is a signal to sell. * * This is a simple and effective strategy that can be used to identify potential trading opportunities. * * @param array $data An array of share market data * @return array An array of buy and sell signals */ /* function emaCrossingStrategy($data) { // Get the closing prices $closingPrices = array_column($data, 'Close'); // Calculate the short-term EMA $shortTermEMA = ema($closingPrices, 3); // Calculate the long-term EMA $longTermEMA = ema($closingPrices, 30); // Initialize the buy and sell signals array $signals = []; // Loop through the data for ($i = 0; $i < count($data); $i++) { // Check if the short-term EMA has crossed above the long-term EMA if ($shortTermEMA[$i] > $longTermEMA[$i]) { // If it has, add a buy signal to the array $signals[] = [ 'signal' => 'buy', 'date' => $data[$i]['Date'], 'price' => $closingPrices[$i] ]; } // Check if the short-term EMA has crossed below the long-term EMA if ($shortTermEMA[$i] < $longTermEMA[$i]) { // If it has, add a sell signal to the array $signals[] = [ 'signal' => 'sell', 'date' => $data[$i]['Date'], 'price' => $closingPrices[$i] ]; } } // Return the buy and sell signals return $signals; } function ema($data, $period) { // Initialize the EMA values array $emaValues = []; // Calculate the initial EMA value $emaValues[0] = array_sum($data) / $period; // Calculate the subsequent EMA values for ($i = 1; $i < count($data); $i++) { $emaValues[$i] = (($data[$i] - $emaValues[$i - 1]) * 2 / $period) + $emaValues[$i - 1]; } // Return the EMA values return $emaValues; } // Sample data $data = [ [ 'Date' => '2023-11-14', 'Close' => 100.00 ], [ 'Date' => '2023-11-15', 'Close' => 102.00 ], [ 'Date' => '2023-11-16', 'Close' => 104.00 ], [ 'Date' => '2023-11-17', 'Close' => 103.00 ], [ 'Date' => '2023-11-18', 'Close' => 101.00 ], [ 'Date' => '2023-11-19', 'Close' => 99.00 ] ]; // Get the buy and sell signals $signals = emaCrossingStrategy($data); // Print the buy and sell signals print_r($signals);*/ /***************************************EMA STRATEGY FUNCTIONS**************************************************/ /** * EMA Crossing Strategy * * This code uses the Exponential Moving Average (EMA) indicator to identify potential buy and sell signals in a share market. * * The EMA crossing strategy is based on the idea that when the short-term EMA crosses above the long-term EMA, it is a signal to buy. * Conversely, when the short-term EMA crosses below the long-term EMA, it is a signal to sell. * * This is a simple and effective strategy that can be used to identify potential trading opportunities. * * @param array $data An array of share market data * @return array An array of buy and sell signals */ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); date_default_timezone_set('Asia/Kolkata'); require_once('lib/function.php'); $db = new login_function(); $dbobj= new login_function(); function emaCrossingStrategy($data) { // Get the closing prices $closingPrices = array_column($data, 'Close'); // Calculate the short-term EMA $shortTermEMA = ema($closingPrices, 10); // Calculate the long-term EMA $longTermEMA = ema($closingPrices, 30); // Initialize the buy and sell signals array $signals = []; // Loop through the data for ($i = 0; $i < count($data); $i++) { // Check if the short-term EMA has crossed above the long-term EMA if ($shortTermEMA[$i] > $longTermEMA[$i]) { // If it has, add a buy signal to the array $signals[] = [ 'signal' => 'BUY', 'price' => $closingPrices[$i] ]; } // Check if the short-term EMA has crossed below the long-term EMA if ($shortTermEMA[$i] < $longTermEMA[$i]) { // If it has, add a sell signal to the array $signals[] = [ 'signal' => 'SELL', 'price' => $closingPrices[$i] ]; } } // Return the buy and sell signals return $signals; } /** * Exponential Moving Average (EMA) * * This function calculates the EMA of a series of numbers. * * @param array $data The series of numbers * @param int $period The EMA period * @return array The EMA values */ function ema($data, $period) { // Initialize the EMA values array $emaValues = []; // Calculate the initial EMA value $emaValues[0] = array_sum($data) / $period; // Calculate the subsequent EMA values for ($i = 1; $i < count($data); $i++) { $emaValues[$i] = (($data[$i] - $emaValues[$i - 1]) * 2 / $period) + $emaValues[$i - 1]; } // Return the EMA values return $emaValues; } /*************************************END EMA STRATEGY FUNCTIONS************************************************/ $var_symbol_token = 99926000; $symbol_name = "NIFTY 50"; $closingPrices = array(); //$closingPrices = $db->get_all_last_candles_data_for_ema($var_symbol_token); $closingPrices = array_reverse($closingPrices); //print_r($closingPrices); if(!empty($closingPrices)) { // Get the buy and sell signals $signals = emaCrossingStrategy($closingPrices); echo "<br /><br /><br /><br /><br /><br /><br />"; // print_r($signals); if(!empty($signals)) { foreach($signals as $buy_sell_signal) { $strategy_type = "THREE_THIRTY"; $buy_type = $buy_sell_signal; /*$main_comment = "3-30"; $comment = "-"; $dbobj->add_new_log_data($strategy_type,$buy_type,$main_comment,$comment,$comment,$comment,$comment);*/ if($symbol_name=="Nifty Bank" OR $symbol_name=="Nifty 50" OR $symbol_name=="Nifty Fin Service") { echo "Inside Candle"; /* $main_comment = "3-30 Inside"; $comment = "-"; $dbobj->add_new_log_data($strategy_type,$buy_type,$main_comment,$comment,$comment,$comment,$comment);*/ //place_trade_entry($symbol_name,$var_symbol_token,$strategy_type, $current_candle_close,$var_interval,$var_from_date,$var_to_date,$buy_type); /*$main_comment = "3-30 After"; $comment = "-"; $dbobj->add_new_log_data($strategy_type,$buy_type,$main_comment,$comment,$comment,$comment,$comment);*/ } } } } /*********************************************************************/ // Sample data array $data = $db->get_all_last_candles_data_for_ema_new($var_symbol_token); $data = array_reverse($data); print_r($data); // Function to calculate EMA function calculateEMA($data, $period) { $multiplier = 2 / ($period + 1); $ema = []; $ema[0] = $data[0]; for ($i = 1; $i < count($data); $i++) { $ema[$i] = ($data[$i] - $ema[$i - 1]) * $multiplier + $ema[$i - 1]; } return $ema; } // Calculate EMAs $shortEMA = calculateEMA($data, 3); $longEMA = calculateEMA($data, 30); // Determine buy/sell signals based on crossover $signals = []; for ($i = 1; $i < count($data); $i++) { if ($shortEMA[$i] > $longEMA[$i] && $shortEMA[$i - 1] < $longEMA[$i - 1]) { $signals[$i] = 'Buy'; } elseif ($shortEMA[$i] < $longEMA[$i] && $shortEMA[$i - 1] > $longEMA[$i - 1]) { $signals[$i] = 'Sell'; } else { $signals[$i] = 'Hold'; } } print_r($signals); /*// Displaying buy/sell signals echo "Date\t\t|\tSignal\n"; echo "--------------------------\n"; for ($i = 1; $i < count($data); $i++) { echo "Data $i\t|\t$signals[$i]\n"; }*/ ?>
Linux 65-254-81-4.cprapid.com 5.14.0-284.11.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Tue May 9 05:49:00 EDT 2023 x86_64
Apache
65.254.81.4