0

I would like the system able to help me get signal

if -1 then short my order, 0 to not have any order , 1 to long my order.

Here are some scenario i have.

if i have long 0.5 and the signal is -1 now then it should be -1, as it needs to sell 0.5 to close a position then sell extra 0.5 to a short =0.5 posittion.

Then if i am long ing 0.5 the signal is 0 then is normal to be selling 0.5

If i don't hold any position which my net postion is 0. Then if signal is 1, -1 then the system should see my net position as - then it will help me either buy or sell depends on my signal.

Have i done anything wrong on my code?

Can anyone tell me the correct equalation ?

Here is my code

import time
import pandas as pd
import numpy as np
import datetime
from pprint import pprint

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 1000)

exchange = ccxt.bybit({
'apiKey': 'API_KEY',
'secret': 'SECRET',  
})

markets = exchange.load_markets()
symbol = 'BTCUSDT'
market = exchange.market(symbol)

def trade(signal):
long = 0.0
short = 0.0
positions = exchange.fetchPositions(\[symbol\])

    for position in positions:
        side = position['info']['side']
        size = float(position['info']['size'])
    
        if side == 'Buy':
            long += size
        elif side == 'Sell':
            short += size
    
    net_pos = long - short
    target_pos = max_pos * signal
    bet_size = round(target_pos - net_pos,3)
    
    ### trade ###
    
    if target_pos > net_pos:
        if net_pos == 0:
            print('long ed')
            # Place buy order with leverage
            order = exchange.create_order(symbol='BTCUSDT',type= 'market',side= 'buy',  amount= bet_size ,   price= None )
            pprint(order)
    
    elif target_pos < net_pos:
        if net_pos == bet_size:
            print('sell ed')
            # Place sell order with reduce-only and leverage
            order = exchange.create_order(symbol='BTCUSDT',type='market',side='sell', amount=bet_size  , price=None,  params={'reduce_only': True})
            pprint(order)
    
    
    time.sleep(1)
    
    ### get account info after trade ###
    long = 0.0
    short = 0.0
    positions = exchange.fetchPositions([symbol])
    
    for position in positions:
        side = position['info']['side']
        size = float(position['info']['size'])
    
        if side == 'Buy':
            long += size
        elif side == 'Sell':
            short += size
    
    net_pos = long - short
    print('after signal')
    print('current_long', long)
    print('current_short', short)
    print('nav', datetime.datetime.now(), exchange.fetch_balance()['USDT']['total'])

### define variables

pos = 0
max_pos = 0.5 # max amount of btc

while True:

    if datetime.datetime.now().second == 5:
    
        df = pd.read_csv(r'/Users/johnny/Desktop/Testing new/signal.csv')
    
        signal = df['pos'].iloc[-1] ### read the last row
        print('signal', signal)
    
        trade(pos)
    
        time.sleep(1)`


`


error message

    /usr/local/bin/python3 "/Users/johnny/Desktop/Testing new/production1111.py"
johnny@Johnny---MacBook-Pro Testing new % /usr/local/bin/python3 "/Users/johnny/Desktop/Testing new/production1111.py"
signal -1.0
short ed
Traceback (most recent call last):
  File "/Users/johnny/Desktop/Testing new/production1111.py", line 102, in <module>
    trade(pos)
  File "/Users/johnny/Desktop/Testing new/production1111.py", line 60, in trade
    order = exchange.create_order('BTCUSDT','market','sell', bet_size , None,  params={'reduce_only': True})
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ccxt/bybit.py", line 2989, in create_order
    return self.create_contract_order(symbol, type, side, amount, price, params)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ccxt/bybit.py", line 3246, in create_contract_order
    response = getattr(self, method)(self.extend(request, params))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ccxt/base/exchange.py", line 500, in inner
    return entry(_self, **inner_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ccxt/base/exchange.py", line 2637, in request
    return self.fetch2(path, api, method, params, headers, body, config, context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ccxt/base/exchange.py", line 2634, in fetch2
    return self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ccxt/base/exchange.py", line 662, in fetch
    self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ccxt/bybit.py", line 5649, in handle_errors
    self.throw_exactly_matched_exception(self.exceptions['exact'], errorCode, feedback)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ccxt/base/exchange.py", line 2907, in throw_exactly_matched_exception
    raise exact[string](message)
ccxt.base.errors.BadRequest: bybit {"ret_code":35015,"ret_msg":"Qty not in range","result":{},"ext_code":"","ext_info":"","time_now":"1693316465.733898","rate_limit_status":99,"rate_limit":100,"rate_limit_reset_ms":1693316465733}
johnny@Johnny---MacBook-Pro Testing new % 
desertnaut
  • 57,590
  • 26
  • 140
  • 166

0 Answers0