sample main and strategy

This commit is contained in:
Giulio De Pasquale 2020-11-30 14:38:46 +00:00
parent c7a582e6c5
commit 5d5fbc91e1

48
main.py
View File

@ -493,8 +493,10 @@ from bfxbot import BfxBot
import dotenv import dotenv
import os import os
import bfxapi import bfxapi
from bfxbot.symbolstatus import Strategy, SymbolStatus, PositionState, Event
from bfxbot.utils import TAKER_FEE from bfxbot.currency import Symbol
from bfxbot.models import Strategy, SymbolStatus, PositionState, Event, EventKind
from bfxbot.utils import TAKER_FEE, net_pl_percentage
dotenv.load_dotenv() dotenv.load_dotenv()
@ -511,7 +513,38 @@ class TrailingStopStrategy(Strategy):
} }
def position_on_tick(self, position: Position, ss: SymbolStatus) -> (PositionState, List[Event]): def position_on_tick(self, position: Position, ss: SymbolStatus) -> (PositionState, List[Event]):
return 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")
@ -527,14 +560,17 @@ async def main():
bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET) bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET)
strategy = TrailingStopStrategy() strategy = TrailingStopStrategy()
bot.set_strategy(Symbol.BTC, strategy)
bot.set_strategy("tBTCUSD", strategy) await bot.start()
eh = bot.event_handler(Symbol.BTC)
while True:
print("WAITING...")
await bot.update() await bot.update()
return
if __name__ == '__main__': if __name__ == '__main__':
asyncio.run(main()) asyncio.run(main())