0% found this document useful (0 votes)
71 views2 pages

Trade Bot

The document provides an example of how to modify a trade execution function to calculate trade size based on account balance and risk management strategy. It shows adding a call to a calculate_trade_size function, which takes the current price, account balance, and risk percentage as inputs to determine the number of units to trade. This allows executing trades with a user-defined risk level, such as 70%, by factoring in the maximum risk capital amount, pip distance to the stop loss, and desired risk per trade.

Uploaded by

Andrei Nohai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views2 pages

Trade Bot

The document provides an example of how to modify a trade execution function to calculate trade size based on account balance and risk management strategy. It shows adding a call to a calculate_trade_size function, which takes the current price, account balance, and risk percentage as inputs to determine the number of units to trade. This allows executing trades with a user-defined risk level, such as 70%, by factoring in the maximum risk capital amount, pip distance to the stop loss, and desired risk per trade.

Uploaded by

Andrei Nohai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

To adjust the code to invest $1000 with a risk management of 70%, you can modify

the execute_trade function to include the trade size calculation based on your
account balance and risk management strategy. Here's an example of how to do that:

python
Copy code
# Define trade execution function
def execute_trade(signal):
# Get current market price
params = {
'instruments': currency_pair
}
prices = client.pricing.get(params=params)['prices']
price = prices[0]['closeoutAsk'] if signal == 'buy' else prices[0]
['closeoutBid']

# Define order parameters


order_params = {
'price': price,
'stopLossOnFill': {
'timeInForce': 'GTC',
'price': price + (price - df['lower_band'].iloc[-1]) * 0.1, # set stop
loss 10% away from lower Bollinger Band
},
'timeInForce': 'FOK',
'instrument': currency_pair,
'units': calculate_trade_size(price, 1000, 0.7), # calculate trade size
based on account balance and risk management
'type': 'MARKET',
}

# Execute trade
if signal == 'buy':
order = orders.OrderCreate(accountID=account_id, data=order_params)
response = client.request(order)
print(response)
elif signal == 'sell':
order_params['units'] = -order_params['units']
order = orders.OrderCreate(accountID=account_id, data=order_params)
response = client.request(order)
print(response)
else:
print('No trade executed')
In this modified version of the function, we added a call to calculate_trade_size
to calculate the trade size based on the account balance and risk management.
Here's the code for the

calculate_trade_size function with a risk management of 70%:

python
Copy code
def calculate_trade_size(price, account_balance, risk):
# Calculate the maximum amount of capital to risk per trade
max_risk_capital = account_balance * risk

# Calculate the amount of pips between the entry price and stop loss
stop_loss_pips = abs(price - (price - df['lower_band'].iloc[-1]) * 0.1)

# Calculate the position size in units


position_size = max_risk_capital / stop_loss_pips

# Round down to the nearest 100 units


position_size = int(position_size / 100.0) * 100

return position_size
This function takes the current market price (price), the account balance
(account_balance), and the risk management factor (risk) as inputs and returns the
position size in units. The position size is calculated based on the maximum amount
of capital to risk per trade, the number of pips between the entry price and stop
loss, and the desired risk per trade.

With this modified code, you can execute trades with a risk management of 70% by
calling the execute_trade function with the appropriate signal ('buy' or 'sell').
Keep in mind that the actual risk of the trade may vary depending on the slippage
and execution speed of the trade. It's also important to monitor your account
balance and adjust the trade size accordingly to maintain a consistent level of
risk per trade.

You might also like