0 AND $close > 0 AND !empty($position)) { if ($position == 'LONG' OR $position == 'Buy') $gain = $close - $open; else if ($position == 'SHORT' OR $position == 'Sell') $gain = $open - $close; return round($gain, 1); } else { $conn = dbConnect(); global $conn; //protocol($conn, '999', 'cron-bb', 'Problem bei getGain', 'Pos: '.$position.' open: '.$open.' close: '.$close); return (0); } } // GAINDIFF FUER TELEGRAM FORMATIEREN ------------------------------------------------------------------- // ###################################################################################################### // funktioniert (warum auch immer) nur mit doppelten Anführungszeichen function formatGainDiffTG($gain) { if ($gain <= 0) $gainTG = "$ ".$gain. " \u{1F630}"; // U+1F630 else $gainTG = "$ ".$gain. " \u{1F4B0}"; // U+1F4B0 return $gainTG; } // GAIN FUER TELEGRAM FORMATIEREN (BTC) ----------------------------------------------------------------- // ###################################################################################################### // funktioniert (warum auch immer) nur mit doppelten Anführungszeichen function formatGainTG($gain) { $anzeige = 'BTC '.number_format($gain,8).' ($'.BTC2Dollar($gain, getPriceBybit()).')'; if ($gain <= 0) $anzeige = $anzeige. " \u{1F630}"; // U+1F630 else $anzeige = $anzeige. " \u{1F4B0}"; // U+1F4B0 return $anzeige; } // GAIN FUER TELEGRAM FORMATIEREN (USDT) ---------------------------------------------------------------- // ###################################################################################################### // funktioniert (warum auch immer) nur mit doppelten Anführungszeichen function formatGainTG_USDT($gain) { $anzeige = 'USDT '.number_format($gain,2); if ($gain <= 0) $anzeige = $anzeige. " \u{1F630}"; // U+1F630 else $anzeige = $anzeige. " \u{1F4B0}"; // U+1F4B0 return $anzeige; } // TELEGRAM-NACHRICHT SENDEN ---------------------------------------------------------------------------- // ###################################################################################################### function telegram($msg) { //global $telegrambot, $telegramchatid; // Set your Bot ID and Chat ID. $telegrambot = '1233342593:AAGt8M5668iaFpGuhvBFm232sImpE4Oopv4'; $telegramchatid = 462791002; $url = 'https://api.telegram.org/bot'.$telegrambot.'/sendMessage'; $data = array('chat_id' => $telegramchatid, 'text' => $msg, 'parse_mode' => 'html'); $options = array('http' => array('method' => 'POST','header' => "Content-Type:application/x-www-form-urlencoded\r\n",'content' => http_build_query($data),),); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } // PROTOKOLLIEREN --------------------------------------------------------------------------------------- // ###################################################################################################### function protocol($conn, $trade_ID, $triggersource, $remark, $details) { $qs = "SELECT * From Protocol ORDER BY datetime DESC LIMIT 1 "; $protocol = mysqli_fetch_assoc($mq = mysqli_query($conn, $qs)); if ($protocol['remark'] == $remark) { $qs2 = "UPDATE Protocol SET datetime = NOW() WHERE p_ID = '".$protocol['p_ID']."' "; if (!empty($qs2)) $mq2 = mysqli_query($conn, $qs2); } else { $qs2 = "INSERT INTO Protocol(trade_ID, triggersource, remark, details) "; $qs2 .= "values('".$trade_ID."', '".$triggersource."', '".$remark."', '".addslashes($details)."' ) "; } if (! mysqli_query($conn, $qs2)) mail('kontakt@webdesire.de', 'Fehler beim Protokollieren', addslashes($qs2.mysqli_error($conn))); } // HQ-UPDATE -------------------------------------------------------------------------------------------- // ###################################################################################################### function updateHQ($conn, $initiator_HQ, $pending_ID, $pending_position, $pending_status, $current_ID, $current_position, $current_status, $current_peak, $current_SL, $last_wait, $last_open, $last_SL) { $conn = dbConnect(); global $conn; $qs .= "UPDATE HQ SET initiator_HQ = '".$initiator_HQ."', "; // leere Werte werden ignoriert (kein Update), ERASE bedeutet: Wert loeschen (bei IDs Null setzen) if (!empty($pending_ID)) { if ($pending_ID == 'ERASE') $pending_ID = 0; $qs .= "pending_ID = '".$pending_ID."', "; } if (!empty($pending_position)) { if ($pending_position == 'ERASE') $pending_position = ''; $qs .= "pending_position = '".$pending_position."', "; } if (!empty($pending_status)) { if ($pending_status == 'ERASE') $pending_status = ''; $qs .= "pending_status = '".$pending_status."', "; } if (!empty($current_ID)) { if ($current_ID == 'ERASE') $current_ID = 0; $qs .= "current_ID = '".$current_ID."', "; } if (!empty($current_position)) { if ($current_position == 'ERASE') $current_position = ''; $qs .= "current_position = '".$current_position."', "; } if (!empty($current_status)) { if ($current_status == 'ERASE') $current_status = ''; $qs .= "current_status = '".$current_status."', "; } if (!empty($current_peak)) { if ($current_peak == 'ERASE') $current_peak = 0; $qs .= "current_peak = '".$current_peak."', "; } if (!empty($current_SL)) { if ($current_SL == 'ERASE') $current_SL = 0; $qs .= "current_SL = '".$current_SL."', "; } if (!empty($last_wait)) { if ($last_wait == 'ERASE') $last_wait = 0; $qs .= "last_wait = '".$last_wait."', "; } if (!empty($last_open)) { if ($last_open == 'ERASE') $last_open = 0; $qs .= "last_open = '".$last_open."', "; } if (!empty($last_SL)) { if ($last_SL == 'ERASE') $last_SL = 0; $qs .= "last_SL = '".$last_SL."', "; } $qs = substr($qs, 0, -2); // Komma hinten entfernen if (! mysqli_query($conn, $qs)) mail('kontakt@webdesire.de', 'Fehler beim Trading: HQ1', $qs); //telegram($qs); } // PEAK-UPDATE ------------------------------------------------------------------------------------------ // ###################################################################################################### // current_peak aktualisieren, wenn neuer Wert größer (bei LONG) oder kleiner (bei SHORT) als bisheriger function updatePeak($conn, $BTCUSD) { $HQ = getHQ($conn); if ($HQ['current_position'] == 'Buy' AND $BTCUSD > $HQ['current_peak']) $qs .= "UPDATE HQ SET current_peak = '".$BTCUSD."' "; else if ($HQ['current_position'] == 'Sell' AND $BTCUSD < $HQ['current_peak'] OR empty($HQ['current_peak'])) $qs .= "UPDATE HQ SET current_peak = '".$BTCUSD."' "; if (!empty($qs)) $mq = mysqli_query($conn, $qs); // protocol($conn, '999', 'Cron', 'Peak-Update', $BTCUSD); } // HQ-DATEN HOLEN --------------------------------------------------------------------------------------- // ###################################################################################################### function getHQ($conn) { $qsHQ = "SELECT * FROM HQ "; $HQ = mysqli_fetch_assoc($mq = mysqli_query($conn, $qsHQ)); return($HQ); } // HQ LOCK ---------------------------------------------------------------------------------------------- // ###################################################################################################### function setLock($conn, $mode, $initiator_lock) { $qs = "UPDATE HQ SET locked = '".$mode."', initiator_lock = '".$initiator_lock."' "; if (!empty($qs)) $mq = mysqli_query($conn, $qs); } // GEGENTEIL DER POSITION ERMITTELN --------------------------------------------------------------------- // ###################################################################################################### function getOpposite($side) { if($side == 'Buy') $opposite = 'Sell'; else if ($side == 'Sell') $opposite = 'Buy'; return($opposite); } // BOT EIN/AUSSCHALTEN ---------------------------------------------------------------------------------- // ###################################################################################################### function power($conn, $mode) { $qs = "UPDATE Settings SET status = '".$mode."' "; if (!empty($qs)) $mq = mysqli_query($conn, $qs); } // BTC in Dollar umrechnen ------------------------------------------------------------------------------ // ###################################################################################################### function BTC2Dollar($sat, $btc) { $dollar = number_format($btc * $sat, 2); return($dollar); } // BYBIT-CONNECT ---------------------------------------------------------------------------------------- // ###################################################################################################### $public_key = 'UCYCSMBZCXUCRKOMMV'; // MAIN (TradeDesire-Subaccount) // $public_key = 'Bb97NFrGGNSNq145J0'; // MAIN //$public_key = '2XUawXfEi1K2ruB4Ez'; // TRADEDESIRE //$public_key = 'Et0G5yneqwyGYtLOed'; // TEST $secret_key = 'CSLEDDLPEUCIRAZWKHJYIUVTVUHOTAASJVPC'; // MAIN (TradeDesire-Subaccount) // $secret_key = '7nHJ4auYxB1LodwYQCeOJ9xF0r8nHNFhjbcx'; // MAIN // $secret_key = 'PaT5E18cAzVYsxqvbb9y35NYoIn4U2r3GN89'; // TRADEDESIRE // $secret_key = '8cPO2whHVpTkRZyxkkvZLWPL0KeKoBol6NOg'; // TEST //$selector = '-testnet'; $selector = ''; function get_signed_params($params) { global $public_key; global $secret_key; $params = array_merge(['api_key' => $public_key], $params); ksort($params); //decode return value of http_build_query to make sure signing by plain parameter string $signature = hash_hmac('sha256', urldecode(http_build_query($params)), $secret_key); return http_build_query($params) . "&sign=$signature"; } // AKTUELLEN PREIS VON BYBIT HOLEN ---------------------------------------------------------------------- // ###################################################################################################### function getPriceBybit() { global $selector; $bybit_json = file_get_contents('https://api'.$selector.'.bybit.com/spot/quote/v1/ticker/price'); $data = json_decode($bybit_json, true); foreach($data['result'] as $a) { if ($a['symbol'] == 'BTCUSDT') $BTCUSD = $a['price']; } global $conn; $qs = "UPDATE Prices "; $qs .= "SET BTCUSD = '".$BTCUSD."'"; $mq = mysqli_query($conn, $qs); return ($BTCUSD); } // WALLET AUSLESEN -------------------------------------------------------------------------------------- // ###################################################################################################### function getWallet($symbol){ $walletparams = ['symbol' => 'BTCUSD', 'timestamp' => time() * 1000]; global $selector; $url = 'https://api'.$selector.'.bybit.com/spot/v1/account'; $wallet = json_decode(file_get_contents($url . "?" . get_signed_params($walletparams)), true); foreach($wallet['result']['balances'] as $a) { if ($a['coin'] == $symbol) $balance = $a['total']; } return ($balance); } // LISTE DER SPOT-TRADES AUSLESEN ----------------------------------------------------------------------- // ###################################################################################################### function getTradeHistory(){ $walletparams = ['symbol' => 'BTCUSDT', 'timestamp' => time() * 1000]; global $selector; $url = 'https://api'.$selector.'.bybit.com/spot/v1/myTrades'; return json_decode(file_get_contents($url . "?" . get_signed_params($walletparams)), true); } // ACTIVE BUY/SELL-ORDER -------------------------------------------------------------------------------- // ###################################################################################################### function trade_active($side, $symbol, $type, $qty) { $conn = dbConnect(); global $conn; global $selector; $walletBefore_BTC = getWallet('BTC'); // für Berechnung der Ordergröße in BTC $walletBefore_USDT = getWallet('USDT'); // für Gain-Berechnung $setParams = ['timestamp' => time() * 1000]; $setParams = array_merge(['side' => $side, 'symbol' => $symbol, 'type' => $type, 'qty' => $qty, 'time_in_force' => 'GoodTillCancel' ], $setParams); $url = 'https://api'.$selector.'.bybit.com/spot/v1/order'; $params = get_signed_params($setParams); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => $params ) ); $context = stream_context_create($opts); $response = json_decode(file_get_contents($url, false, $context), true); protocol($conn, '888', '', 'trade_active Request', $side.' | '.$symbol.' | '.$type.' | '.$qty.' | '.$price); protocol($conn, '888', '', 'trade_active Response Status', $response['ret_msg']); protocol($conn, '888', '', 'trade_active Full Response', json_encode($response)); sleep(3); // die Ordergröße in BTC errechnen und notieren $walletAfter_BTC = getWallet('BTC'); $order_size = $walletAfter_BTC - $walletBefore_BTC; $qs = "UPDATE HQ SET order_size = '".$order_size."'"; if (! mysqli_query($conn, $qs)) mail('kontakt@webdesire.de', 'Fehler bei HQ-Update Order-Size', $qs); if ($side == 'Buy') { $HQ = getHQ($conn); $qs2 = "UPDATE Trades SET walletBefore = '".number_format($walletBefore_USDT, 2, '.', '')."' WHERE trade_ID = '".$HQ['pending_ID']."' "; if (! mysqli_query($conn, $qs2)) mail('kontakt@webdesire.de', 'Fehler bei Trade-Update walletBefore', $qs2); mail('kontakt@webdesire.de', 'neuer Trade (walletBefore)', $qs2); } return $response['result']['orderId']; } ?>