skka3134

skka3134

email
telegram

Cryptocurrency Automated Trading Robot: demo

  1. Install Dependencies
sudo /home/skka3134/folder/botTrade/bin/python -m pip install pandas_ta
sudo /home/skka3134/folder/botTrade/bin/python -m pip install schedule
import schedule
import pandas_ta as ta
pd.set_option('display.max_rows',None)
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from  datetime import datetime
import time
  1. Convert timestamp, the to_datetime() function is used to convert a timestamp to a date and time.
    df['dt']=pd.to_datetime(df['timestamp'],unit='ms')

image
15. Calculate indicators, Pandas TA is an open-source module based on the Pandas library, which has hundreds of technical indicators and common indicators. The RSI indicator is based on the principle of supply and demand balance. It evaluates the strength of long and short forces by measuring the percentage of the total amplitude of price increase in a certain period to the average amplitude of price change, and then provides specific operation suggestions. The Exponential Moving Average (EMA) is also a trend indicator. Its construction principle is to calculate the arithmetic average of the closing price and analyze it based on the calculation result to judge the future trend of price changes.

df['rsi']=ta.rsi(df['close'],length=10)
df['ema']=ta.ema(df['close'],length=200)

image
16. Get the latest part of our processed data.

last_row_index=len(df.index)-1
latest_rsi=round(df['rsi'].iloc[-1],2)
latest_price=round(df['close'].iloc[-1],2)
latest_ema=round(df['ema'].iloc[-1],2)
latest_ts=df['timestamp'].iloc[-1]
  1. Create buying conditions. If the latest RSI indicator is less than 20 and the latest price is greater than the latest EMA indicator, then buy 1 ETH. 1. RSI value less than 20, oversold. RSI value greater than 80, overbought.
long_condition=(latest_rsi<20)and(latest_price>latest_ema)
if long_condition:
    order=exchange.create_market_buy_order('ETH/USDT',1)
closed_orders=exchange.fetch_closed_orders('ETH/USDT',limit=2)
  1. Selling conditions.
most_recent_closed_order=closed_orders[-1]
tf_mult=exchange.parse_timeframe('30m')*1000
diff=latest_ts-most_recent_closed_order['timestamp']
last_buy_signal_cnt=int(diff/tf_mult)
exit_condition=(latest_rsi<40)and(last_buy_signal_cnt>10)
if exit_condition:
    order=exchange.create_market_sell_order('ETH/USDT',1)

  1. Loop, complete version.
import ccxt
import schedule
import pandas as pd
import pandas_ta as ta
pd.set_option('display.max_rows',None)
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from  datetime import datetime
import time


exchange = ccxt.binance({
    'apiKey': '',
    'secret': '',
    'enableRateLimit': True,
})


exchange.load_markets()

entry_rsi=30
exit_rsi=40
symbol='ETH/USDT'
timeframe='30m'
tf_mult=exchange.parse_timeframe(timeframe)*1000


def indicators(data):
    data['rsi']=data.ta.rsi(length=10)
    data['ema']=data.ta.ema(length=200)
    return data

def check_buy_sell_signals(df):
    last_row_index=len(df.index)-1
    latest_rsi=round(df['rsi'].iloc[-1],2)
    latest_price=round(df['close'].iloc[-1],2)
    latest_ema=round(df['ema'].iloc[-1],2)
    latest_ts=df['timestamp'].iloc[-1]

    long_condition=(latest_rsi<30)and(latest_price>latest_ema)
    if long_condition:
        order=exchange.create_market_buy_order('ETH/USDT',1)
    closed_orders=exchange.fetch_closed_orders('ETH/USDT',limit=2)

    most_recent_closed_order=closed_orders[-1]
    diff=latest_ts-most_recent_closed_order['timestamp']
    last_buy_signal_cnt=int(diff/tf_mult)

    exit_condition=(latest_rsi<40)and(last_buy_signal_cnt>10)
    if exit-exit_condition:
        order=exchange.create_market_sell_order('ETH/USDT',1)

def run_bot():
    bars=exchange.fetch_ohlcv('SOL/USDT',timeframe='30m',limit=200)
    df=pd.DataFrame(bars[:],columns=['timestamp','open','high','low','close','volume'])
    df['dt']=pd.to_datetime(df['timestamp'],unit='ms')
    df=indicators(df).tail(30)
    check_buy_sell_signals(df)

schedule.every(10).seconds.do(run_bot)

while True:
    schedule.run_pending()
    time.sleep(1)
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.