L&S Random

In order to prove our point we did something very simple. We programmed the dumbest algorithm we possible could: L&S Random. L&S Random is an algorithm which trades randomly with leverage. This algorithm trades 24/7 and always uses stop loss and take profit orders.

How does the algorithm work?

Let’s dive into more details, this is how the algorithm works: When the algorithm starts it checks if there is any open position. If there is none, it chooses whether to go short or long randomly with a probability of 0.5 for each side. If there is any active order pending it cancels it. Then it places a limit order a few dollars below the last market price with a take profit 3% above the entry price and stop loss 1% below the entry price. Then it waits for two hours and repeats.

If there is a position it closes it at market and cancel any active order. Then it chooses to go long or short randomly and wait for two hours.

random_algo How the algorithm works

So in order to try this algorithm on the market the first thing we need is a way to interact with the servers of the exchange. We implemented the REST API of Bybit in Python. The code is available on our website and our Github account. The following code uses our implementation of the Bybit’s REST API

Then we need some code to place the orders randomly. You will find the code below and our Github account

#! /usr/bin/python3
import os
os.chdir('') # working dir
import rest_api as api
from random import random
from time import sleep
from math import floor
import logging
logger=logging.getLogger()
logger.handlers = []
logging.basicConfig(filename=f"{os.getcwd()}/random_bot.log",format='%(asctime)s - %(process)d-%(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.DEBUG)


# --------------------------

api_key="" # put your your API key
secret="" # put your secret key
leverage= 4 # Set the leverage to 4
# --------------------------

session=api.Account(api_key,secret,leverage)


##

def random_side(p=0.5):
    x=random()
    if x<p: return "Buy"
    return "Sell"


if __name__ == '__main__':
    logging.info('Bot starts')

    while True:
        if session.my_position()['result'][0]['side']=="Buy":
            session.cancel_all_pending_order()
            logging.info('Close long position @ market')
            size=session.my_position()['result'][0]['size']
            session.market_close("Sell", size)

        if session.my_position()['result'][0]['side']=="Sell":
            session.cancel_all_pending_order()
            logging.info('Close short position @ market')
            size=session.my_position()['result'][0]['size']
            session.market_close("Buy", size)



        if session.my_position()['result'][0]['side']=="None":
            session.cancel_all_pending_order()
            logging.info('No position')
            if session.get_active_order()['result']['data'][0]['order_status']!="Cancelled":
                logging.info('Cancel active orders if any')
                session.cancel_active_order(session.get_active_order()['result']['data'][0]['order_id'])

            side=random_side()
            logging.info('Choose random side')
            if side=="Buy": # Go long for random reason
                logging.info('Going long randomly')
                price=floor(float(session.get_orderbook()['result'][0]['ask_price'])-0.5)
                stop_loss=floor(float(price*0.99)) # SL @ 1%
                take_profit=floor(float(price*1.03)) # TP @ 3 %
                wallet_balance=session.my_position()['result'][0]['wallet_balance']
                leverage=session.my_position()['result'][0]['leverage']
                size=floor((wallet_balance*leverage*price-1)*0.9)
                session.place_active_order(side, size, price, stop_loss, take_profit)

            else: # Go short for random reason
                logging.info('Going short randomly')
                price=floor(float(session.get_orderbook()['result'][0]['bid_price'])+0.5)
                stop_loss=floor(float(price*1.01)) # SL @ 1%
                take_profit=floor(float(price*0.97)) # TP @ 3 %
                wallet_balance=session.my_position()['result'][0]['wallet_balance']
                leverage=session.my_position()['result'][0]['leverage']
                size=floor((wallet_balance*leverage*price-1)*0.9)
                session.place_active_order(side, size, price, stop_loss, take_profit)
        logging.info('Sleep for 2 hours')
        sleep(2*3600)



To run the script simply type in a terminal:

>>> chmod +x name_of_the_script.py
>>> nohup /path/to/name_of_the_script.py &

To stop the script:

>>> ps -ef
>>> kill -9 <pid of the script>

Then we tried the algorithm on Bybit’s Testnet and here is a the P&L curve we obtained:

pnl P&L curve

So the algorithm was profitable on the period of time it was tested.

Conclusion

What should you take out of this article? Should you trade randomly? Of course not. What this article shows is that risk management and discipline are more important than the trading strategy that is used. The only reason our algorithm did not get liquidated is because it trades 24/7 and has very simple but strict risk management practices. It always uses a stop loss and a take profit order.

Different studies have shown that more than 90% of retail traders lose money. We have just shown that an algorithm that uses no strategy outperforms 90% of retail traders.

Risk management is the least interesting thing in trading but the most important. The main reason why retail traders are loosing money is because they neglect it. You can be wrong most of the time but still be profitable if you have good risk management techniques. There are already many things online about risk management, so we are not going to develop this topic here. We do recommend this very good Youtube video by CryptoCred.


To conclude, the goal of this article was to highlight how discipline is the best way not to lose money trading. Do not get overwhelmed by your emotions, always trade with a plan and stick to it. Cut your losses and let your profits run but do not be too greedy. If you do all of that and have a strategy on top of that you are going to be very profitable


casino Try L&S Random