Python Forum
problem about slope in python script for bitcoin trading
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem about slope in python script for bitcoin trading
#1
I am trying to run this script at trality.com which is a trading site for cryptocurrencies. I am looking for a calculation for the angle of the bitcoin slope traded against ethereum. But I am not sure what the script does to calculate the angle. I get very small angles. Can someone identify why the angles are so small. I want an angle based on a time interval of 1 hour or 4 hours. And I want to buy when the angle is above 30 degrees. I dont know if it uses radians or degrees in the script. Thank you.







import numpy as np

def initialize(state):

    state.counter = 0

@schedule(interval="1h", symbol="ETHBTC")
def handler(state, data):

    angle = 0
            
    macd_ind = data.macd(12,26,9)

    if macd_ind is None:
        return

    signal = macd_ind['macd_signal']

    has_position = has_open_position(data.symbol, truncated=True)
    balance_base = float(query_balance_free(data.base))
    balance_quoted = float(query_balance_free(data.quoted))
    buy_amount = balance_quoted * 0.80 / data.close_last

    plot("signal",signal[-1],"ETHBTC")

    if state.counter < 4:
        state.counter += 1
    else:
        state.counter = 0

    if state.counter == 4:

        lastsignals = signal[-4:]

        # calculating the slope of last 4 candles
        slope = (lastsignals[-1] - lastsignals[0]) / 3
        angle = np.rad2deg(np.arctan(slope))

        print("slope: ",slope)
        print("angle: ",angle)
        plot("angle of signal",angle,"ETHBTC")

        if angle > 0.26: # 15 degrees

            print("-------")
            print("Checking for buying possibility of {}".format(data.symbol))
            print("buy amount:",buy_amount)
            print("buy price:", data.close_last)

            create_order(symbol=data.symbol,amount = buy_amount)


        elif angle < -10 and has_position:
            print("-------")
            print("Checking for selling possibility of {}".format(data.symbol))
            print("sell amount:",balance_base)
            print("sell price:",data.close_last)

            close_position(data.symbol)
Reply
#2
I have made it running:

import numpy as np

def initialize(state):
    state.counter = 0
    state.lastsignals = []

@schedule(interval="1d", symbol="BNBUSDT")
def handler(state, data):
    angle = 0
    signal = data.sma(2).last
    has_position = has_open_position(data.symbol, truncated=True)
    balance_base = float(query_balance_free(data.base))
    balance_quoted = float(query_balance_free(data.quoted))
    buy_amount = balance_quoted * 0.80 / data.close_last

    if state.counter < 2:
        state.counter += 1
        state.lastsignals.append(signal)
    else:
        
        state.lastsignals.append(signal)

    if state.counter == 2:
        plot("signal", state.lastsignals[-1], "BNBUSDT")

        # calculating the slope of last 4 candles
        slope = (state.lastsignals[-1] - state.lastsignals[-2]) / 0.125
        angle = np.rad2deg(np.arctan(slope))
        print("slope: ", slope)
        print("angle: ", angle)
        plot("angle of signal", angle, "BNBUSDT")

        if angle > 10: 
            print("-------")
            print("Checking for buying possibility of {}".format(data.symbol))
            print("buy amount:", buy_amount)
            print("buy price:", data.close_last)
            create_order(symbol=data.symbol, amount = buy_amount)

        elif angle < -5 and has_position:
            print("-------")
            print("Checking for selling possibility of {}".format(data.symbol))
            print("sell amount:", balance_base)
            print("sell price:",data.close_last)

            close_position(data.symbol)
I am trading in BNB on a one day interval. Feel free to test it out on trality.com. They are in beta so it is free. They also have trading testing for the past year or more for coins on binance. Cheers
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Pairs Trading Simulation Kiitoos 0 199 Feb-19-2024, 08:27 PM
Last Post: Kiitoos
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,007 Jun-29-2023, 11:57 AM
Last Post: gologica
  how to manage crypto trading flooding data from exchange servers Mikeardy 0 1,209 Dec-26-2021, 08:31 PM
Last Post: Mikeardy
  Script stop work after 3 actioins - PLEASE WHERE IS THE PROBLEM? rondon442 0 1,532 Sep-27-2021, 05:40 PM
Last Post: rondon442
  Problem executing a script on a remote host tester_V 3 2,398 Sep-26-2021, 04:25 AM
Last Post: tester_V
  cbot ctrader automate trading hl29951 1 1,624 Jul-25-2021, 08:14 PM
Last Post: Larz60+
  problem with sphinx and file directory in script kiyoshi7 0 2,248 Mar-11-2021, 03:52 PM
Last Post: kiyoshi7
  Code should download and output trading data tayy 2 2,594 Mar-04-2021, 04:07 AM
Last Post: tayy
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,788 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  slope intercept form Dasiey12 6 3,911 Jun-14-2020, 06:34 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020