Untitled

 avatar
unknown
plain_text
2 years ago
1.9 kB
7
Indexable
# Connect to Binance API
api_key = 'API HERE' 
api_secret = 'SECRET API HERE'
client = Client(api_key, api_secret)

symbol = "ETHUSDT"
interval = '1d'
Client.KLINE_INTERVAL_1DAY
klines = client.get_historical_klines(symbol, interval, "1 Jan,2017")
data = pd.DataFrame(klines)
 # create colums name
df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'ignore'])
df.set_index('timestamp', inplace=True)

# Create a target variable
df['next_day_price'] = df['close'].shift(-1)
df['price_change'] = df['next_day_price'] - df['close']
df['price_direction'] = df['price_change'].apply(lambda x: 1 if x > 0 else 0)

# Define the feature set and target variable
X = df[['open', 'high', 'low', 'volume']]
y = df['price_direction']

# Train the model
clf = ExtraTreesRegressor()
clf.fit(X, y)

# Predict the next day's price
prediction = clf.predict([[df['open'].iloc[-1], df['high'].iloc[-1], df['low'].iloc[-1], df['volume'].iloc[-1]]])

# Create an order based on the prediction
if prediction > 0.5:
    # Buy order
    order = client.create_order(
        symbol='ETHUSDT',
        side=Client.SIDE_BUY,
        type=Client.ORDER_TYPE_MARKET,
        quantity=5
    )
else:
    # Sell order
    order = client.create_order(
        symbol='ETHUSDT',
        side=Client.SIDE_SELL,
        type=Client.ORDER_TYPE_MARKET,
        quantity=5
    )

# Set stop loss and stop gain orders
stop_loss_price = df['close'].iloc[-1] * 0.9
stop_gain_price = df['close'].iloc[-1] * 1.1
client.create_order(
    symbol='ETHUSD',
    side=Client.SIDE_SELL,
    type=Client.ORDER_TYPE_STOP_LOSS,
quantity=5,
stopPrice=stop_loss_price
)
client.create_order(
symbol='ETHUSD',
side=Client.SIDE_SELL,
type=Client.ORDER_TYPE_TAKE_PROFIT,
quantity=5,
stopPrice=stop_gain_price
)
Editor is loading...