screen testing
This commit is contained in:
parent
8391cec49e
commit
047f4cf84b
82
main.py
82
main.py
@ -485,8 +485,11 @@
|
|||||||
# if __name__ == "__main__":
|
# if __name__ == "__main__":
|
||||||
# asyncio.run(Screen.wrapper(main))
|
# asyncio.run(Screen.wrapper(main))
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import shutil
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
from asciimatics.screen import Screen, ManagedScreen
|
||||||
|
from asciimatics.widgets import Frame, Layout, Text
|
||||||
from bfxapi import Position
|
from bfxapi import Position
|
||||||
|
|
||||||
from bfxbot import BfxBot
|
from bfxbot import BfxBot
|
||||||
@ -497,79 +500,54 @@ import bfxapi
|
|||||||
from bfxbot.currency import Symbol
|
from bfxbot.currency import Symbol
|
||||||
from bfxbot.models import Strategy, SymbolStatus, PositionState, Event, EventKind
|
from bfxbot.models import Strategy, SymbolStatus, PositionState, Event, EventKind
|
||||||
from bfxbot.utils import TAKER_FEE, net_pl_percentage
|
from bfxbot.utils import TAKER_FEE, net_pl_percentage
|
||||||
|
from strategy import TrailingStopStrategy
|
||||||
|
|
||||||
dotenv.load_dotenv()
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
class TrailingStopStrategy(Strategy):
|
|
||||||
BREAK_EVEN_PERC = TAKER_FEE
|
|
||||||
MIN_PROFIT_PERC = TAKER_FEE * 2.5
|
|
||||||
GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 1.5
|
|
||||||
MAX_LOSS_PERC = -3.75
|
|
||||||
OFFER_PERC = 0.01
|
|
||||||
|
|
||||||
TRAIL_STOP_PERCENTAGES = {
|
|
||||||
PositionState.MINIMUM_PROFIT: 0.27,
|
|
||||||
PositionState.PROFIT: 0.14
|
|
||||||
}
|
|
||||||
|
|
||||||
def position_on_tick(self, position: Position, ss: SymbolStatus) -> (PositionState, List[Event]):
|
|
||||||
events = []
|
|
||||||
|
|
||||||
pl_perc = net_pl_percentage(position.profit_loss_percentage, TAKER_FEE)
|
|
||||||
prev = ss.previous_position_w(position.id)
|
|
||||||
|
|
||||||
if pl_perc > self.GOOD_PROFIT_PERC:
|
|
||||||
state = PositionState.PROFIT
|
|
||||||
elif self.MIN_PROFIT_PERC <= pl_perc < self.GOOD_PROFIT_PERC:
|
|
||||||
state = PositionState.MINIMUM_PROFIT
|
|
||||||
elif 0.0 <= pl_perc < self.MIN_PROFIT_PERC:
|
|
||||||
state = PositionState.BREAK_EVEN
|
|
||||||
elif self.MAX_LOSS_PERC < pl_perc < 0.0:
|
|
||||||
state = PositionState.LOSS
|
|
||||||
else:
|
|
||||||
state = PositionState.CRITICAL
|
|
||||||
|
|
||||||
if not prev or prev.state == state:
|
|
||||||
return state, events
|
|
||||||
|
|
||||||
if state ==PositionState.PROFIT:
|
|
||||||
events.append(Event(EventKind.REACHED_GOOD_PROFIT, position.id, ss.current_tick))
|
|
||||||
elif state == PositionState.MINIMUM_PROFIT:
|
|
||||||
events.append(Event(EventKind.REACHED_MIN_PROFIT, position.id, ss.current_tick))
|
|
||||||
elif state == PositionState.BREAK_EVEN:
|
|
||||||
events.append(Event(EventKind.REACHED_BREAK_EVEN, position.id, ss.current_tick))
|
|
||||||
elif state== PositionState.LOSS:
|
|
||||||
events.append(Event(EventKind.REACHED_LOSS, position.id, ss.current_tick))
|
|
||||||
else:
|
|
||||||
events.append(Event(EventKind.REACHED_MAX_LOSS, position.id, ss.current_tick))
|
|
||||||
events.append(Event(EventKind.CLOSE_POSITION, position.id, ss.current_tick))
|
|
||||||
|
|
||||||
return state, events
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
API_KEY = os.getenv("API_KEY")
|
API_KEY = os.getenv("API_KEY")
|
||||||
API_SECRET = os.getenv("API_SECRET")
|
API_SECRET = os.getenv("API_SECRET")
|
||||||
|
|
||||||
if API_KEY == None:
|
if API_KEY is None:
|
||||||
print("API_KEY is not set! Set the var in the .env file.")
|
print("API_KEY is not set! Set the var in the .env file.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if API_SECRET == None:
|
if API_SECRET is None:
|
||||||
print("API_SECRET is not set! Set the var in the .env file.")
|
print("API_SECRET is not set! Set the var in the .env file.")
|
||||||
return
|
return
|
||||||
|
|
||||||
bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET)
|
bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET, tick_duration=20)
|
||||||
strategy = TrailingStopStrategy()
|
strategy = TrailingStopStrategy()
|
||||||
bot.set_strategy(Symbol.BTC, strategy)
|
bot.set_strategy(Symbol.BTC, strategy)
|
||||||
|
eh = bot.event_handler(Symbol.BTC)
|
||||||
|
|
||||||
await bot.start()
|
await bot.start()
|
||||||
|
|
||||||
eh = bot.event_handler(Symbol.BTC)
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
print("WAITING...")
|
await bot_loop(bot)
|
||||||
|
|
||||||
|
|
||||||
|
@ManagedScreen
|
||||||
|
async def bot_loop(bot: BfxBot, screen: Screen = None):
|
||||||
|
prepare_tui(screen)
|
||||||
|
screen.play()
|
||||||
|
|
||||||
await bot.update()
|
await bot.update()
|
||||||
|
|
||||||
|
def prepare_tui(screen: Screen):
|
||||||
|
w, h = shutil.get_terminal_size()
|
||||||
|
|
||||||
|
frame = Frame(screen, w, h, has_border=False)
|
||||||
|
|
||||||
|
info_layout = Layout([1])
|
||||||
|
graph_layout = Layout([1])
|
||||||
|
footer_layout = Layout([1])
|
||||||
|
|
||||||
|
frame.add_layout(info_layout)
|
||||||
|
frame.add_layout(graph_layout)
|
||||||
|
frame.add_layout(footer_layout)
|
||||||
|
|
||||||
|
info_layout.add_widget(Text(label="Test"))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
53
strategy.py
Normal file
53
strategy.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
from bfxapi import Position
|
||||||
|
|
||||||
|
from bfxbot.models import Strategy, PositionState, SymbolStatus, Event, EventKind
|
||||||
|
from bfxbot.utils import TAKER_FEE, net_pl_percentage
|
||||||
|
|
||||||
|
|
||||||
|
class TrailingStopStrategy(Strategy):
|
||||||
|
BREAK_EVEN_PERC = TAKER_FEE
|
||||||
|
MIN_PROFIT_PERC = TAKER_FEE * 2.5
|
||||||
|
GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 1.5
|
||||||
|
MAX_LOSS_PERC = -3.75
|
||||||
|
OFFER_PERC = 0.01
|
||||||
|
|
||||||
|
TRAIL_STOP_PERCENTAGES = {
|
||||||
|
PositionState.MINIMUM_PROFIT: 0.27,
|
||||||
|
PositionState.PROFIT: 0.14
|
||||||
|
}
|
||||||
|
|
||||||
|
def position_on_tick(self, position: Position, ss: SymbolStatus) -> (PositionState, List[Event]):
|
||||||
|
events = []
|
||||||
|
|
||||||
|
pl_perc = net_pl_percentage(position.profit_loss_percentage, TAKER_FEE)
|
||||||
|
prev = ss.previous_position_w(position.id)
|
||||||
|
|
||||||
|
if pl_perc > self.GOOD_PROFIT_PERC:
|
||||||
|
state = PositionState.PROFIT
|
||||||
|
elif self.MIN_PROFIT_PERC <= pl_perc < self.GOOD_PROFIT_PERC:
|
||||||
|
state = PositionState.MINIMUM_PROFIT
|
||||||
|
elif 0.0 <= pl_perc < self.MIN_PROFIT_PERC:
|
||||||
|
state = PositionState.BREAK_EVEN
|
||||||
|
elif self.MAX_LOSS_PERC < pl_perc < 0.0:
|
||||||
|
state = PositionState.LOSS
|
||||||
|
else:
|
||||||
|
state = PositionState.CRITICAL
|
||||||
|
|
||||||
|
if not prev or prev.state == state:
|
||||||
|
return state, events
|
||||||
|
|
||||||
|
if state ==PositionState.PROFIT:
|
||||||
|
events.append(Event(EventKind.REACHED_GOOD_PROFIT, position.id, ss.current_tick))
|
||||||
|
elif state == PositionState.MINIMUM_PROFIT:
|
||||||
|
events.append(Event(EventKind.REACHED_MIN_PROFIT, position.id, ss.current_tick))
|
||||||
|
elif state == PositionState.BREAK_EVEN:
|
||||||
|
events.append(Event(EventKind.REACHED_BREAK_EVEN, position.id, ss.current_tick))
|
||||||
|
elif state== PositionState.LOSS:
|
||||||
|
events.append(Event(EventKind.REACHED_LOSS, position.id, ss.current_tick))
|
||||||
|
else:
|
||||||
|
events.append(Event(EventKind.REACHED_MAX_LOSS, position.id, ss.current_tick))
|
||||||
|
events.append(Event(EventKind.CLOSE_POSITION, position.id, ss.current_tick))
|
||||||
|
|
||||||
|
return state, events
|
Loading…
Reference in New Issue
Block a user