Skip to content

Commit

Permalink
Added stop loss and take profit options for ByBit
Browse files Browse the repository at this point in the history
  • Loading branch information
xCryptoTools committed Apr 24, 2023
1 parent 67b022a commit 3dc8a07
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The script can automatically open a contract on ByBit to long dogecoin with up t

- 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.
- Make sure your ByBit account has derivate trading enabled and is set to 'unified' mode. Your default leverage setting will be used so make sure to set that as well.
- In ByBit go to: API. Create a key with at least the permissions: `Contract - Orders (derivates)` , `Contract - Positions (derivates)` and `Derivates API V3 - Trade` . Update config.inc.php accordingly.

### Cronjob
Expand Down
9 changes: 7 additions & 2 deletions config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$mail['enable'] = false; // Send an e-mail when tweet trigger is met? [true/false]

$mail['to'] = ''; // Send e-mail to this address
$mail['from'] = ''; // Sender e-mail address, make sure this matches server/smtp to prevent spam marking
$mail['from'] = ''; // Sender e-mail address. Make sure this matches server/smtp to prevent spam marking

$mail['smtp'] = true; // Set to true to use SMTP, set to false for php mail
// If smtp is true, configure the following:
Expand Down Expand Up @@ -70,5 +70,10 @@
$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.
// Set to false to only buy if there is no active trade on the symbol yet. Can prevent double trades
$bybit['only_buy_if_not_active'] = true;

// Stop loss and Take profit. Percentages are the move of the underlying ticker (excluding leverage!)
$bybit['stop_loss_percentage'] = 5; // Example: '5'. Can be set to false to disable stop loss.
$bybit['take_profit_percentage'] = 20; // Example: '20'. Can be set to false to disable take profit.

34 changes: 34 additions & 0 deletions libraries/bybit.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,47 @@ function bybit_has_open_position($bybit) {
}
}

function bybit_get_last_price($bybit) {

$endpoint = '/derivatives/v3/public/tickers';
$method = 'GET';
$params = 'category=linear&symbol=' . $bybit['derivate_symbol_to_buy'];
$json_response = bybit_request($bybit, $endpoint, $method, $params);

if ($json_response['retCode'] === 0 &&
!empty($json_response['result']['list']) &&
!empty($json_response['result']['list'][0]['lastPrice']) &&
$json_response['result']['list'][0]['symbol'] == $bybit['derivate_symbol_to_buy']
) {
return $json_response['result']['list'][0]['lastPrice'];

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

function bybit_order($bybit) {

$last_price = null;
if (
($bybit['stop_loss_percentage'] !== false && $bybit['stop_loss_percentage'] > 0) ||
($bybit['take_profit_percentage'] !== false && $bybit['take_profit_percentage'] > 0)
) {
$last_price = bybit_get_last_price($bybit);
}

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

if ($bybit['stop_loss_percentage'] !== false && $bybit['stop_loss_percentage'] > 0) {
$params = str_replace('}', ', "stopLoss" : "' . ($last_price - ($last_price / 100 * $bybit['stop_loss_percentage'])) . '"}', $params);
}
if ($bybit['take_profit_percentage'] !== false && $bybit['take_profit_percentage'] > 0) {
$params = str_replace('}', ', "takeProfit" : "' . ($last_price + ($last_price / 100 * $bybit['take_profit_percentage'])) . '"}', $params);
}

$json_response = bybit_request($bybit, $endpoint, $method, $params);

if ($json_response['retCode'] === 0 &&
Expand Down

0 comments on commit 3dc8a07

Please sign in to comment.