Я не могу понять, почему указанные значения неверны, а также риск того, что расчеты вознаграждения будут неправильными, я не уверен, в чем я ошибаюсь.
По сути, чтобы найти ближайший тейк-профит к 1:3, насколько это возможно, при условии входа. Стоп-лосс будет началом предыдущей зоны для покупки и концом для продажи.
`
# Define the zones list as provided
zones = [
(5039.5, 5114),
(5182, 5221),
(5254, 5275.5),
(5288.5, 5304.5),
(5335.75, 5353.25),
(5365.5, 5386),
(5419.5, 5457.5),
(5528, 5604)
]
def buy(price):
stop_loss_candidates = [zone[0] for zone in zones if zone[0] < price]
stop_loss = max(stop_loss_candidates) if stop_loss_candidates else None
take_profit_candidates = [zone[0] for zone in zones if zone[0] > price]
take_profit = None
for tp in take_profit_candidates:
if stop_loss is not None and tp - price >= 3 * (price - stop_loss):
take_profit = tp
break
return stop_loss, take_profit
def sell(price):
stop_loss_candidates = [zone[1] for zone in zones if zone[1] > price]
stop_loss = min(stop_loss_candidates) if stop_loss_candidates else None
take_profit_candidates = [zone[1] for zone in zones if zone[1] < price]
take_profit = None
found_take_profit = False
for tp in take_profit_candidates:
if stop_loss is not None and price - tp >= 3 * (stop_loss - price):
take_profit = tp
found_take_profit = True
break
if found_take_profit:
break
return stop_loss, take_profit
def calculate_buy_risk_reward(price, stop_loss, take_profit):
if stop_loss is None or take_profit is None:
return None
risk = price - stop_loss
reward = take_profit - price
if risk == 0:
return None
return reward / risk
def calculate_sell_risk_reward(price, stop_loss, take_profit):
if stop_loss is None or take_profit is None:
return None
risk = stop_loss - price
reward = price - take_profit
if risk == 0:
return None
return reward / risk
# Test prices
test_prices = [5321, 5300, 5200, 5100, 5500, 5280, 5242, 5219, 5204, 5441.75]
# Run buy and sell functions for each test price and calculate risk-reward ratio
results = []
for price in test_prices:
buy_result = buy(price)
sell_result = sell(price)
buy_risk_reward = None
if buy_result is not None:
buy_risk_reward = calculate_buy_risk_reward(price, *buy_result)
sell_risk_reward = None
if sell_result is not None:
sell_risk_reward = calculate_sell_risk_reward(price, *sell_result)
results.append((price, buy_result, buy_risk_reward, sell_result, sell_risk_reward))
# Output the results
for price, buy_result, buy_risk_reward, sell_result, sell_risk_reward in results:
print(f"Price: {price}")
print(f" Buy Result: {buy_result}, Risk-Reward Ratio (Buy): {buy_risk_reward}")
print(f" Sell Result: {sell_result}, Risk-Reward Ratio (Sell): {sell_risk_reward}")
print()
Подробнее здесь: https://stackoverflow.com/questions/785 ... nd-trading
Не могу понять, почему моя логика неверна, простая функция для поиска торгового стоп-лосса и тейк-профита с использовани ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение