Skip to content

Commit

Permalink
Added function to check for active trades before placing a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
xCryptoTools committed Apr 24, 2023
1 parent 812d306 commit da04144
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 458 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ Obtain a **v1.1** Twitter API key as follows:

The script can send you e-mail notifications if the keyword was found in a tweet.

- Enable in config.inc.php.
- Make sure to enter from and to e-mail addresses .
- Set $mail['enable'] to true in config.inc.php
- Make sure to enter from and to e-mail addresses.
- SMTP can be used for improved mail delivery.

### ByBit

The script can automatically open a contract on ByBit to long dogecoin with up to 50x leverage.

- Enable in config.inc.php
- Set $bybit['enable'] to true in config.inc.php
- If you do not have an account yet, signup using the referral url: https://www.bybit.com/invite?ref=N617WP for added benefits and a bonus.
- Make sure your ByBit account has derivate trading enabled and is set to 'unified' mode.
- In ByBit go to: API. Create a key with at least the permissions: Contract > Orders (derivates). Contract > Positions (derivates). Derivates API V3 > Trade. Update config.inc.php accordingly.

### Cronjob
Expand Down
8 changes: 5 additions & 3 deletions config.inc.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Doge Twitter PHP bot sample configuration.
* Doge Twitter PHP bot configuration.
* Make sure to complete the config before running the script.
*/

Expand Down Expand Up @@ -46,7 +46,7 @@

$mail['smtp'] = true; // Set to true to use SMTP, set to false for php mail
// If smtp is true, configure the following:
$mail['smtp_secure'] = 'tls'; // usually 'tls'
$mail['smtp_secure'] = 'tls'; // Usually 'tls'
$mail['smtp_host'] = 'smtp.gmail.com';
$mail['smtp_port'] = 587;
$mail['smtp_username'] = '';
Expand All @@ -67,6 +67,8 @@
$bybit['secret_key'] = '';

// Symbol and quantity to buy
$bybit['derivate_symbol_to_buy'] = 'DOGEUSDT'; // symbol to buy
$bybit['derivate_symbol_to_buy'] = 'DOGEUSDT'; // Symbol to buy
$bybit['quantity_to_buy'] = 1000;

$bybit['only_buy_if_not_active'] = true; // Only buy if there is no active trade of this ticker. Can prevent double trades.

11 changes: 7 additions & 4 deletions doge-twitter-php-bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use PHPMailer\PHPMailer\Exception;

// Include config and functions
require('config.inc.php');
require('libraries/functions.php');
require 'config.inc.php';
require 'libraries/twitter.php';
require 'libraries/email.php';
require 'libraries/bybit.php';

// Get tweets
validate_twitter_config($twitter);
$tweet_contains_keyword = check_tweet_contains_keyword($twitter);

// Set and output message
Expand All @@ -26,6 +27,8 @@

// Place ByBit trade if needed
if ($tweet_contains_keyword === true && $bybit['enable'] === true) {
bybit_order($bybit);
if ($bybit['only_buy_if_not_active'] === false || bybit_has_open_position($bybit) === false) {
bybit_order($bybit);
}
}

138 changes: 9 additions & 129 deletions libraries/bybit.php
Original file line number Diff line number Diff line change
@@ -1,133 +1,13 @@
<?php

function check_tweet_contains_keyword($twitter) {

// Generate OAuth 1.0a Authorization header
$oauth_nonce = md5(uniqid(rand(), true));
$oauth_timestamp = time();
$oauth_signature_method = 'HMAC-SHA1';
$oauth_version = '1.0';

// Build query
$query = 'from:' . $twitter['username_to_monitor'];
if ($twitter['include_retweets'] !== true) $query .= ' -filter:nativeretweets -filter:retweets';
if ($twitter['include_quotes'] !== true) $query .= ' -filter:quote';
if ($twitter['include_replies'] !== true) $query .= ' -filter:replies';
if (!empty($twitter['within_time'])) $query .= ' within_time:' . $twitter['within_time'];

$url = 'https://api.twitter.com/1.1/search/tweets.json';

// Authentication
$oauth_base_string = 'GET&' . rawurlencode($url) . '&'
. rawurlencode('count=' . $twitter['limit_count']
. '&oauth_consumer_key=' . $twitter['consumer_key']
. '&oauth_nonce=' . $oauth_nonce
. '&oauth_signature_method=' . $oauth_signature_method
. '&oauth_timestamp=' . $oauth_timestamp
. '&oauth_token=' . $twitter['access_token']
. '&oauth_version=' . $oauth_version
. '&q=' . rawurlencode($query));

$oauth_signature_key = rawurlencode($twitter['consumer_secret']) . '&' . rawurlencode($twitter['access_token_secret']);
$oauth_signature = base64_encode(hash_hmac('sha1', $oauth_base_string, $oauth_signature_key, true));

$oauth_header = 'OAuth oauth_consumer_key="' . rawurlencode($twitter['consumer_key']) . '", '
. 'oauth_nonce="' . rawurlencode($oauth_nonce) . '", '
. 'oauth_signature="' . rawurlencode($oauth_signature) . '", '
. 'oauth_signature_method="' . rawurlencode($oauth_signature_method) . '", '
. 'oauth_timestamp="' . rawurlencode($oauth_timestamp) . '", '
. 'oauth_token="' . rawurlencode($twitter['access_token']) . '", '
. 'oauth_version="' . rawurlencode($oauth_version) . '"';

$headers[] = 'Authorization: ' . $oauth_header;

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url . '?q=' . rawurlencode($query) . '&count=' . $twitter['limit_count']);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Send GET request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch) || empty($response) || isset($response['errors'])) {
curl_close($ch);
echo 'Twitter error! Please check your configuration.'; exit;
} else {
// Decode JSON response
$json_response = json_decode($response, true);
curl_close($ch);
}

if (!empty($json_response) && !isset($json_response['errors']) && isset($json_response['statuses'])) {

$tweetContainsKeyword = false;
foreach ($json_response['statuses'] as $tweet) {

// Check if the response contains Dogecoin related tweets
if (strpos(strtolower($tweet['text']), strtolower($twitter['keyword_to_monitor'])) !== false) {
$tweetContainsKeyword = true;
}
}
} else {
echo 'Twitter error! Please check your configuration.'; exit;
}

return $tweetContainsKeyword;
}

function send_mail($mail, $message) {

// Mailer
$mailer = new PHPMailer\PHPMailer\PHPMailer;

if ($mail['smtp'] == true) {
$mailer->isSMTP();
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = $mail['smtp_secure'];
$mailer->Host = $mail['smtp_host'];
$mailer->Port = $mail['smtp_port'];
$mailer->Username = $mail['smtp_username'];
$mailer->Password = $mail['smtp_password'];
}
$mailer->Timeout = 10;

$mailer->From = $mail['from'];
$mailer->addAddress($mail['to']);

$mailer->WordWrap = 70;
$mailer->Subject = $message;
$mailer->Body = $message . '
---
This is an automated e-mail from your Twitter bot';

// send mail
if ($mailer->send()) {
echo '<br /><br />E-mail sent succesfully.';
} else {
echo '<br /><br />Error sending e-mail! Please check your configuration.';
}
}

function bybit_has_open_position($bybit) {

$endpoint = '/contract/v3/private/position/list';
$endpoint = '/unified/v3/private/position/list';
$method = 'GET';

$params = 'symbol=' . $bybit['derivate_symbol_to_buy'];
$params = 'category=linear&symbol=' . $bybit['derivate_symbol_to_buy'];
$json_response = bybit_request($bybit, $endpoint, $method, $params);

echo $endpoint.'?'.$params;

echo '<pre>';
print_r($json_response);
die();

if ($json_response['retMsg'] == 'OK') {
if ($json_response['retCode'] === 0) {
if (!empty($json_response['result']['list'])) {
$open_position = false;
foreach ($json_response['result']['list'] as $position) {
Expand All @@ -144,21 +24,21 @@ function bybit_has_open_position($bybit) {
return false;

} else {
echo '<br /><br />Error placing order on ByBit! Please check your configuration.'; exit;
echo '<br /><br />Error checking position on ByBit! Please check your configuration.'; exit;
}
}

function bybit_order($bybit) {

$endpoint = '/contract/v3/private/order/create';
$endpoint = '/unified/v3/private/order/create';
$method = 'POST';
$orderLinkId = uniqid();
$params = '{"symbol" : "' . $bybit['derivate_symbol_to_buy'] . '", "side" : "Buy", "orderType" : "Market", "qty" : "' . $bybit['quantity_to_buy'] . '" , "timeInForce" : "GoodTillCancel" , "orderLinkId" : "' . $orderLinkId . '"}';
$params = '{"category" : "linear", "symbol" : "' . $bybit['derivate_symbol_to_buy'] . '", "side" : "Buy", "orderType" : "Market", "qty" : "' . $bybit['quantity_to_buy'] . '" , "timeInForce" : "GoodTillCancel" , "orderLinkId" : "' . $orderLinkId . '"}';

$json_response = bybit_request($bybit, $endpoint, $method, $params);
if ($json_response['retMsg'] == 'OK' &&
!empty($json_response['result']['orderId']) &&

if ($json_response['retCode'] === 0 &&
!empty($json_response['result']['orderId']) &&
$json_response['result']['orderLinkId'] == $orderLinkId
) {
echo '<br /><br />ByBit order placed succesfully.';
Expand Down
Loading

0 comments on commit da04144

Please sign in to comment.