Exit Position error

Ansari
Ansari edited October 15 in Python client
I made a client that isnt exiting a position for example my algorithm bought a share of HINDUNILVR on 7th and cant sell it even after Take profit has been hit it keeps showing me the error

Skipping HINDUNILVR due to insufficient capital for minimum 1 share

I have 18 shares of the company and it still wont sell it the code block for this is the code block
def trade(ticker, action, capital):
df = fetch_historical_data(ticker)
if df is None:
return
latest_price = df['close'].iloc[-1]
quantity = int(capital // latest_price) # Convert quantity to int, ensure no float error

if action == "buy" and quantity < 1:
print(f"Skipping {ticker} due to insufficient capital for minimum 1 share.")
return

try:
if action == "buy":
print(f"Placing buy order for {ticker} - Quantity: {quantity}")
if is_market_open():
order_id = kite.place_order(
variety=kite.VARIETY_REGULAR,
exchange=kite.EXCHANGE_NSE,
tradingsymbol=ticker,
transaction_type=kite.TRANSACTION_TYPE_BUY,
quantity=quantity,
order_type=kite.ORDER_TYPE_MARKET,
product=kite.PRODUCT_CNC # CNC for delivery
)
log_trade(ticker, 'buy', latest_price, quantity)
print(f"Buy order placed. Order ID: {order_id}")
# Start monitoring the price for take profit and stop loss
monitor_price(ticker, latest_price, quantity)
else:
print("Market is closed. Cannot place buy order.")

elif action == "sell":
print(f"Placing sell order for {ticker} - Quantity: {quantity}")
if is_market_open():
# Check if the ticker is in holdings
holdings = kite.holdings()
holding_quantity = 0
for holding in holdings:
if holding['tradingsymbol'] == ticker:
holding_quantity = holding['quantity']
break

if holding_quantity < quantity:
print(f"Skipping {ticker} due to insufficient holdings for minimum 1 share.")
return

order_id = kite.place_order(
variety=kite.VARIETY_REGULAR,
exchange=kite.EXCHANGE_NSE,
tradingsymbol=ticker,
transaction_type=kite.TRANSACTION_TYPE_SELL,
quantity=quantity,
order_type=kite.ORDER_TYPE_MARKET,
product=kite.PRODUCT_CNC
)
log_trade(ticker, 'sell', latest_price, quantity)
print(f"Sell order placed. Order ID: {order_id}")
traded_tickers_today.add(ticker) # Add the ticker to traded tickers for the day
held_tickers.discard(ticker) # Remove the ticker from held tickers
else:
print("Market is closed. Cannot place sell order.")

except Exception as e:
print(f"Error placing {action} trade for {ticker}: {e}")
Sign In or Register to comment.