Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
binance script
#1







def initialize(state):
state.counter = 0



@schedule(interval="1h",
symbol="BTCUSDT”)




If state.counter < 4:
state.counter += 1
Else:
state.counter = 0

if counter == 4: (every four hours)




macd = data.macd(12,26,9)
signalLine = macd[‘macd_signal’]



- Check if bot has position

has_position = has_open_position(data.symbol, truncated=True)









import numpy as np










def initialize(state):

state.counter = 0




@schedule(interval="1h", symbol="BTCTUSD")

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],"BTCTUSD")




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,"BTCTUSD")




if angle > 30 and balance_base<buy_amount:

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 < -30 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 get the following error message:


> Engine error: File "bot.py", line 9.
> Engine error: state.counter = 0.
> Engine error: ^.
> Engine error: IndentationError: expected an indented block.
> File "bot.py", line 9
state.counter = 0
^
IndentationError: expected an indented block

What is wrong?

I believe there will be more error messages after that as well.
Reply
#2
Try using code tags as described in BBCode to make any code you post easier for people to read.

Your error in line 9 is exactly what it sounds like, Python is expecting an indented block. When you define your function initialize(), all code you want included in that function should be indented (4 spaces is the standard).

def initialize(state):
    state.counter = 0
    # and so on...
Reply
#3
Hi again. Thanks for input.

I have recieved this as pseudo code. And I am a newbie at python this was the pseudo code:

PSEUDO Code:


1) Initialize State Counter
First we want a counter that counts the hours for our angle calculation. Therefore we need a counter that we can initialise in the state of the bot.
At the start of the bot the counter is zero.


def initialize(state):
state.counter = 0

2) Define Scheduling:
You could schedule your bot every hour
@schedule(interval="1h",
symbol="BTCUSDT”)


3) Define the Handler

1) increment the counter until it is 4 and reset:

If state.counter < 4:
state.counter += 1
Else:
state.counter = 0

2) calculate angle if counter == 4: (every four hours)


- First get your signal line:

macd = data.macd(12,26,9)
signalLine = macd[‘macd_signal’]



- Check if bot has position

has_position = has_open_position(data.symbol, truncated=True)



- Calculate the angle:

This can be a bit tricky here. First representing the signalLine as a straight line is only an approximation.

Probably the easiest way is to calculate the slope of the last four candles and use numpy arctan to convert to the angle.


Here is sample I just did which should get you started:





import numpy as np










def initialize(state):

state.counter = 0




@schedule(interval="1h", symbol="BTCTUSD")

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],"BTCTUSD")




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,"BTCTUSD")




if angle > 30 and balance_base<buy_amount:

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 < -30 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 have a new error message:

> Engine error: File "bot.py", line 13.
> Engine error: @schedule(interval="1h",symbol="BTCUSDT”).
> Engine error: ^.
> Engine error: SyntaxError: EOL while scanning string literal.
> File "bot.py", line 13
@schedule(interval="1h",symbol="BTCUSDT”)
^
SyntaxError: EOL while scanning string literal


I will try to go through some introductionary courses in python to understand the BBCode later. I am just trying to set up a trading bot on the site trality.com
Reply
#4
Ok, so you were given pseudo-code to start with. Did you do anything other than copy and paste your pseudo-code into a Python compiler? We need to see what you're trying to do before we can help you.

(Also, it sounds like you didn't even bother to click on the BBCode link. Try reading a bit of it if you really want help.)
Reply


Forum Jump:

User Panel Messages

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