Aşağıdaki şekilde çözüme kavuşturdum. Tam istediğim gibi çalışıyor. İşine yarayan varsa alsan kullansın. Kodlar biraz karışık ama idare edin. Hoşca kalın.
from binance1 import Binance
import config
import time
from pprint import pprint
binance = Binance(
api_key=config.API_DETAILS['API_KEY'],
api_secret=config.API_DETAILS['API_SECRET']
)
def __set_stop_loss(current_price:float, stopyuzde:float, leverage:float, is_pos:str):
if is_pos == 'sell':
new_loss = float(current_price * (1 + abs(stopyuzde / leverage)))
else:
new_loss = float(current_price * (1 - abs(stopyuzde / leverage)))
return new_loss
sembol = 'DOGEUSDT'
buy_price = 0.104764
pozisyon = 'sell'
stoploss = -0.10 # fiyat %10 düşerse stop
kaldirac = 20
#trailing_stop = True
trailing_stop_positive = 0.05 # %0.5 artan ile stop loss koy
trailing_stop_positive_offset = 0.05 # fiyat %0.3 artarsa takip etmeye başla
#Buy işlemi long
anastoplos_buy = __set_stop_loss(buy_price,stoploss,kaldirac,pozisyon)
pozitifstoploss_buy = __set_stop_loss(buy_price,trailing_stop_positive_offset,kaldirac,"sell")
#print("BUY" ,anastoplos_buy,pozitifstoploss_buy)
#sell işlemi short
anastoplos_sell = __set_stop_loss(buy_price,stoploss,kaldirac,"sell")
pozitifstoploss_sell = __set_stop_loss(buy_price,trailing_stop_positive_offset,kaldirac,"buy")
#print("SELL ana" ,anastoplos_sell,pozitifstoploss_sell)
while True:
current_price = binance.get_price(sembol)
if pozisyon == 'buy':
print("ana stop loss:",anastoplos_buy,"pozitif stop loss:",pozitifstoploss_buy)
if current_price < anastoplos_buy:
print("Stop oldun sat")
break
if current_price > pozitifstoploss_buy:
print("Trailling Stop Loss Aktif Buy")
higher_stop_buy = current_price > pozitifstoploss_buy
lower_stop_buy = current_price < pozitifstoploss_buy
if higher_stop_buy:
print("Higher : ", higher_stop_buy)
pozitifstoploss_buy = __set_stop_loss(current_price,trailing_stop_positive_offset,kaldirac,"sell")
anastoplos_buy = __set_stop_loss(current_price,trailing_stop_positive,kaldirac,pozisyon)
print("Stop loss degisti: ",pozitifstoploss_buy)
print("Ana stop loss değişti",anastoplos_buy)
else:
print("ana stop loss:",anastoplos_sell,"pozitif stop loss:",pozitifstoploss_sell)
if current_price > anastoplos_sell:
print("Stop oldun sat")
break
if current_price < pozitifstoploss_sell:
print("Trailling Stop Loss Aktif Sell")
higher_stop_sell = current_price < pozitifstoploss_sell
lower_stop_sell = current_price > pozitifstoploss_sell
if higher_stop_sell:
print("Higher : ", higher_stop_sell)
anastoplos_sell = __set_stop_loss(current_price,stoploss,kaldirac,pozisyon)
pozitifstoploss_sell = __set_stop_loss(current_price,trailing_stop_positive_offset,kaldirac,"buy")
print("Stop loss degisti: ",pozitifstoploss_sell)
print("Ana stop loss değişti",anastoplos_sell)
time.sleep(3)