mirror of
https://github.com/Ichinga-Samuel/aiomql.git
synced 2026-08-24 01:08:07 +00:00
Update tests and docs across core, lib, and contrib modules
This commit is contained in:
@@ -1 +1 @@
|
||||
from .track import close_after
|
||||
from .track import close_after, hedge_position, track_hedges
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
from logging import getLogger
|
||||
from datetime import datetime
|
||||
|
||||
@@ -16,3 +17,27 @@ async def close_after(open_pos: OpenPosition, /, *, duration: int, start: float
|
||||
_, res = await open_pos.close_position()
|
||||
if res.retcode == 10009:
|
||||
logger.info("%s, %d closed", pos.symbol, pos.ticket)
|
||||
|
||||
|
||||
async def hedge_position(pos: OpenPosition, /, *, hedge_amount: float = -2, close_hedge_amount: float = 0,
|
||||
order_params: dict = None):
|
||||
is_open = await pos.update_position()
|
||||
position = pos.position
|
||||
if not (is_open and pos.is_hedged is False and position.profit < 0):
|
||||
return
|
||||
|
||||
if position.profit <= hedge_amount:
|
||||
ok, order = await pos.hedge_position(order_params=order_params)
|
||||
if not ok:
|
||||
logger.error("Could not hedge %s:%d", pos.symbol, pos.ticket)
|
||||
|
||||
|
||||
async def track_hedges(pos: OpenPosition, close_hedge_amount: float = -10):
|
||||
res = await pos.update_position()
|
||||
if not res:
|
||||
return
|
||||
hedges = list(pos.hedges.values())
|
||||
await asyncio.gather(*[hedge.update_position() for hedge in hedges], return_exceptions=True)
|
||||
hedges = [hedge for hedge in hedges if hedge.is_open]
|
||||
await asyncio.gather(*[hedge.close_position() for hedge in hedges if hedge.position.profit <= close_hedge_amount],
|
||||
return_exceptions=True)
|
||||
Reference in New Issue
Block a user