### “CryptoBot For Binance - Cryptocurrency Trading PHP Script” Documentation by “Övünç Tükenmez”v1.3.1

# “CryptoBot For Binance - Cryptocurrency Trading PHP Script”

Last Update: 07.08.2021
By: Ovunc Tukenmez
Email: ovunct@live.com

Thank you for purchasing this PHP software. If you have any question that is beyond the scope of this help file, please feel free to send me an email. Thanks!

## Table of Contents

1. Software
   1. Software Info
   2. Used Technologies
   3. Change Log
2. Installation
   1. Requirements
   2. Registering a 10% Discounted Commission Rated Binance Account
   3. Installation Process
3. Trading
   1. Algorithm
   2. User Interface
4. Online Demo
5. Sources and Credits

## 1. Software - top

### 1.1. Software Info - top

CryptoBot For Binance - Cryptocurrency Trading PHP Script is an advanced PHP software to automate trading of crypto coins in Binance crypto-currency exchange. By following defined rules of the trading strategy of the user, it aims to facilitate repetitive trade process fast using API provided by Binance platform.
The software provides web interface to make possible instant user actions on going process if needed.
SQLite database is used to make installation, setup and migration a smooth/easy process.
The internet browser based interface use the system's own API that is available to use in other custom solutions if needed.

### 1.2. Used Technologies - top

The following technologies and libraries are used in the CryptoBot For Binance - Cryptocurrency Trading PHP Script:

- PHP 5.2+
- SQLite

### 1.3. Change Log - top

v1.0.0

- first release.

v1.1.0

- new rule: "Interval Reset Rule: After Successful Sale"
- added turkish translation.

v1.1.1

- bug fix: wrong quote balance calculation in case of buy order expiration.
- updated Turkish translation.

v1.1.2

- bug fix: "sell now" button creates "limit sell order" instead of "market sell order" if limit sell order exists at the time of the request.

v1.1.3

- bug fix: order's quantity and price are not formatted correctly for crypto currency pairs with high precision trading rules.

v1.1.4

- added headers to prevent browser caching.

v1.1.5

- bug fix: actual account balance is fetched after filled buy order.
- updated balance calculations.

v1.2.0

- added "Automatically Fetch Trading Rules On Pair Change" option on settings page.
- new rule: "Buying Rule: Min. (Last Sell Price/Current Price) Ratio".
- database structure changes.

v1.2.1

- fixed PHP notice.

v1.3.0

- new rule: "Buying Rule: Max. Current Price".
- database structure changes.
- updated Turkish translation.

v1.3.1

- fixed number format errors.

## 2. Installation - top

### 2.1. Requirements - top

PHPMLC requires the following software in order to run:

- PHP version 5.2+
- SQLite 3+
- PHP cURL support should be active on webserver. (https://www.php.net/manual/en/book.curl.php)

### 2.2. Registering a 10% Discounted Commission Rated Binance Account - top

Binance takes commission on each trade. By using following link to register an account, you can benefit 10 percent off on commissions!
https://www.binance.com/en/register?ref=WRS9W5SV

This offer is only available on registration step. This will make huge difference especially if you're going to trade a lot so please make sure you use the correct link:
https://www.binance.com/en/register?ref=WRS9W5SV

### 2.3. Installation Process - top

Folder Structure:
The folder structure of the "CryptoBot" folder is as follows:
Move all files from "code" folder to your web server's root or subfolder (like cryptobot). You can use XAMPP for local hosting.
The script should now be accessed by visiting the installed location's url like "http://localhost/cryptobot/".
Setting up the System:
Navigate to "Settings" page using the link on navbar.
Setting page opens:
System's api url and Binance api keys should be defined in order the script works properly.
System's api url should point to the address of the file "api.php" in script folder.
To get your Binance account api keys, visit Binance API management page and create a new API key if it doesn't exist.
https://www.binance.com/en/my/settings/api-management
Please make sure that "Enable Reading" and "Enable Spot & Margin Trading" permissions are checked in "API restrictions" section.
Once you've created your API key, enter API key and API secret key to the related fields in settings page.
API secret key is shown only once when creating API key. If you didn't copied/noted it, please generate a new API key to get the required keys.
Price check interval is used by the trading interface to trigger api peridically. It is defined as 150 ms by default.
Binance has several restrictions on its API usage. Please take into the account the "request weight per minute" hard-limit when changing this value. For more information on the API usage restrictions, please visit the following link:
https://www.binance.com/en/support/faq/360004492232
For the current weights for API methods, please refer to the documentation:
https://binance-docs.github.io/apidocs/spot/en/#spot-account-trade
Date timezone and date format can be customized using related fields.
For possible values, please refer related pages from the PHP documentation:
https://www.php.net/manual/en/timezones.php
https://www.php.net/manual/en/datetime.format.php
Click "Update" button to save changes.

## 3. Trading - top

### 3.1. Algorithm - top

Even though the user can modify behaviour of the script by implementing his/her own trading strategy using several parameters, the system has the core business logic to follow.
The general idea is to buy low and sell higher in a perfect situation.
Once the script starts at the time T1, the status becomes "waiting_for_opportunity" and then it continuously fetches the current price of selected base asset in a quote asset value.
The current price of the asset at a time can be called as C.
In each price update, system compares the value with the previous high and low of the interval.
So the highest (H) and lowest (L) prices of the interval are saved to database to use in buy or sell decision.
Interval High/Low ratio that should be met to make buy action can be defined by user.
Trend of the price at higher perspective can be useful in order to make buy decision. So user can define first and second ranges that are before of current time to make comparison of their averages.
Maximum interval can be defined too. If maximum interval duration is exceeded, the new interval will start automatically.
The user can also trigger creation of market-buy/market-sell order by clicking "Buy Now"/"Sell Now" buttons.
When the buy condition is met, the system creates limit-buy order at current price. Status changes to "created_buy_order".
In each buy order check interval, the system checks order status. If it's "FILLED", status changes to "waiting_to_sell".
If auto-sell is enabled, system creates limit-sell order at target price and starts watching status of the created order.
In good scenario, the order status becomes "FILLED" and transaction completes at a profit.
If unexpected things happen in the market and price starts to fall rapidly and reaches to the point E ("Max. (Buy Price/Current Price) Ratio" is exceeded), the script can automatically create market-sell order to prevent further losses if user has activated "escape plan".
Several rules can be enabled according to trader's strategy.
The system can also work in "manual mode" by disabling "Auto-Buy" and "Auto-Sell" settings. In that case the user can use manual action buttons.

### 3.2. User Interface - top

Start/Pause/Stop Buttons:
Start button trigger start/continue of trading process.
Pause button trigger pause of trading process.
Stop button trigger stop of trading process that will reset current interval data including trading status. Subsequent Start Button click will reset status to "Waiting for opportunity".
Reset Interval Button:
Reset Interval button clears interval data such as high/low values and starts new interval
Base and Quote Asset Trading Pair:
The script will call "exchangeInfo" method of Binance Api to fill trading rules automatically so if you're changing a base or quote asset, you can leave Asset Price Tick Size and Asset Lot Step Size fields empty. You can manually change them later.
To get most up-to-date information about Trading rules, please visit the following link:
https://www.binance.com/en/trade-rule
Changing base or quote asset will clear all current interval data and trading status.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Base Asset Trading Balance:
The trading balance of base asset.
Sell order quantity is calculated using this value and the price of symbol.
If you are just starting for trade, you can leave this value as "0".
Once the base asset is bought with quote balance, the base asset balance will be automatically increased.
Changing trading pair will reset this value.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Quote Asset Trading Balance:
The trading balance of quote asset.
Buy order quantity is calculated using this value and the price of symbol.
If you are just starting for trade, please make sure that the balance you allocate for trading meets minimum order size.
To get most up-to-date information about Trading rules, please visit the following link:
https://www.binance.com/en/trade-rule
Once the base asset is sold, the quote asset balance will be automatically increased.
Changing trading pair will reset this value.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Trading Fee Rate:
Binance take commission on each trade. System takes into account this value to calculate quantity for order.
To guarantee to not get "insufficient balance" errors, it is suggested to leave this value as "0.01" whether or not using BNB for commissions.
The value is defined as percentage.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Last Sell Price:
After each filled sell order, system records sell price to be considered in "Buying Rule: Min. (Last Sell Price/Current Price) Ratio" rule if it is enabled.
To change this value, the system should be paused first.
The value is defined as decimal.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Auto-Buy & Auto-Sell:
It is possible to tell script whether to automatically buy or sell if the defined conditions met.
If "Auto-Buy" checkbox is unchecked, the system will wait for user's (Buy Now) action even if there is buying opportunity.
If "Auto-Sell" checkbox is unchecked, the system will wait for user's (Sell Now) action even if there is selling opportunity.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Trend Calculation First Duration:
To find out current trend automatically, the script uses two time range to compare averages.
First range can be explained as "from the field value before now to current time".
The value is defined in seconds.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Trend Calculation Second Duration:
To find out current trend automatically, the script uses two time range to compare averages.
Second range can be explained as "from the field value before start of the first range to start of first range".
The value is defined in seconds.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Buy Order Check Interval:
Once limit-buy order is created, script will check its status against filled periodically.
The checking period time in seconds can be customized using this field.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Sell Order Check Interval:
Once limit-sell order is created, script will check its status against filled periodically.
The checking period time in seconds can be customized using this field.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
(Sell Price/Buy Price) Ratio:
To make decision to automatically create limit-sell order, script watches out for each price update if the current asset price is higher than the buy price at a user defined profit rate.
In example, if you are willing to sell if the current price is higher than 2 percent of the buy-price, then the "1.02" should be entered as a value.
The form should be submitted by clicking "Save Changes" button or hitting Enter in order to save changes.
Manual Action Buttons:
If the script waits for some condition occurs in order to create buy/sell order but user want to trigger creation of instant market-buy or market-sell orders, the related button can be clicked.
"Buy Now" button becomes enabled if the current status is "waiting_for_opportunity" or "created_buy_order".
"Sell Now" button becomes enabled if the current status is "waiting_to_sell" or "created_sell_order".
"Cancel" buttons in "Current Buy Order" and "Current Sell Order" areas send cancellation request for the current order.

## RULES

Interval Reset Rule: Max. (Interval High/Low) Ratio:
If this rule is enabled and current interval's high/low ratio is higher than the defined value, a new interval will be started on "waiting_for_opportunity" status.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Interval Reset Rule: Max. Interval Duration:
If this rule is enabled and current interval's duration exceeded the defined value, a new interval will be started on "waiting_for_opportunity" status.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Min. (Interval High/Low) Ratio:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the current interval's high/low ratio value is at minimum of defined value.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Min. (Current Price/Interval Low) Ratio:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the (current asset price/interval's low) ratio value is at minimum of defined value.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Max. (Current Price/Interval Low) Ratio:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the (current asset price/interval's low) ratio value is at maximum of defined value.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Min. Interval Duration:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the the current interval duration exceeded defined value.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Buy Order Validity:
When the limit-buy order is created, the system status becomes "created_buy_order" and system starts to check order status periodically (for each Buy Order Check Interval).
If this rule is enabled and elapsed time from the order creation date is higher than defined value, system will cancel current order and status will revert to "waiting_for_opportunity". The form should be submitted by clicking "Save Changes" button in order to save changes.
Selling Rule: Sell Order Validity:
When the limit-sell order is created, the system status becomes "created_sell_order" and system starts to check order status periodically (for each Sell Order Check Interval).
If this rule is enabled and elapsed time from the order creation date is higher than defined value, system will cancel current order, sell all trading base asset at market price and status will revert to "waiting_for_opportunity". The form should be submitted by clicking "Save Changes" button in order to save changes.
Stop Rule: Max. Number of Trades:
If this rule is enabled and total number of trades from the start reach to the defined value, system will stop processing.
When a limit-buy or limit-sell order is filled, the trade count increases by 1.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Escape Plan: Max. (Buy Price/Current Price) Ratio:
If this plan is enabled and Auto-Sell is enabled and the system status is "waiting_to_sell", the system will automatically create market-sell order if the (buy price/current asset price) ratio is greater than the defined value to prevent any further losses.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Stop Rule: Stop On Escape:
If this rule is enabled and the system will stop in case of the escape plan is processed.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Current Trend is Downward:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the current trend is Downward.
Trend is determined by Trend Calculation First Duration and Trend Calculation Second Duration.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Current Trend is Upward:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the current trend is Upward.
Trend is determined by Trend Calculation First Duration and Trend Calculation Second Duration.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Last Set Boundary is Bottom:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the interval's low value is set last.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Min. (Last Sell Price/Current Price) Ratio:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the (last sell price/current asset price) ratio value is at minimum of defined value.
Last sell price is automatically updated after each filled sell order.
To update last sell price manually, system should be paused and form should be submitted by clicking "Save Changes" button after edit action.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Buying Rule: Buying Rule: Max. Current Price:
If this rule is enabled and Auto-Buy is enabled, the system will create buy order on "waiting_for_opportunity" status only if the current asset price is lower than defined value.
The value is defined as decimal.
The form should be submitted by clicking "Save Changes" button in order to save changes.
Interval Reset Rule: After Successful Sale:
If this rule is enabled a new interval will be started after a successful sale.
The form should be submitted by clicking "Save Changes" button in order to save changes.

## 4. Online Demo - top

Online demo can be accessed using the following link:
https://jetphp.com/cryptobot/demo/

## 5. Sources and Credits - top

The following images, icons, libraries or other files are used to build the software:

- Twitter Bootstrap 3.4.1
- jQuery
- Font Awesome 4.7.0
- Public Rest API for Binance

Once again, thank you so much for purchasing CryptoBot For Binance - Cryptocurrency Trading PHP Script. For any kind of bug reports, feedback, feature requests, or even if you just want to say hello, feel free to email me. I'll be really happy about it.

Ovunc Tukenmez

Go To Table of Contents