Skip to content

Instantly share code, notes, and snippets.

@Ichinga-Samuel
Last active April 29, 2024 12:43
Show Gist options
  • Save Ichinga-Samuel/6ded822bd068e9d6c835b844233db1da to your computer and use it in GitHub Desktop.
Save Ichinga-Samuel/6ded822bd068e9d6c835b844233db1da to your computer and use it in GitHub Desktop.
import asyncio
from datetime import datetime
from pytz import timezone
from aiomql import ForexSymbol, Account, Positions, History, Order, OrderType, RAM, TradeAction
async def main():
async with Account():
# get start time using local timezone
tz = timezone('UTC')
start = datetime.now(tz=tz)
start = start.replace(minute=start.minute - 1)
# create two symbols and initialize them
sym1 = ForexSymbol(name="BTCUSD")
sym2 = ForexSymbol(name="ETHUSD")
await sym1.init()
await sym2.init()
# create first order
amount = 2
volume = sym1.volume_min
points = sym1.compute_points(amount=amount, volume=volume)
tick = await sym1.info_tick()
points_value = points * sym1.point
sl = tick.ask - points_value
tp = tick.ask + 2 * points_value
order = Order(symbol=sym1.name, type=OrderType.BUY, volume=volume, action=TradeAction.DEAL,
price=tick.ask, sl=sl, tp=tp)
# create second order
amount = 2
volume = sym2.volume_min
points = sym2.compute_points(amount=amount, volume=volume)
tick = await sym2.info_tick()
points_value = points * sym2.point
sl = tick.bid + points_value
tp = tick.bid - 2 * points_value
order2 = Order(symbol=sym2.name, type=OrderType.SELL, volume=volume, action=TradeAction.DEAL,
price=tick.ask, sl=sl, tp=tp)
# Place Trades
res = await order.send()
res2 = await order2.send()
print(res.comment, res2.comment)
await asyncio.sleep(5)
# Create a Positions object
pos = Positions()
# get the number of open positions
total = await pos.positions_total()
print(f'{total} Open positions') # 2
# close all open positions
await pos.close_all()
end = datetime.now(tz=tz)
# get the number of open positions
total = await pos.positions_total()
print(f'{total} Open positions')
# get historical trades
his = History(date_from=start, date_to=end)
# get the number of deals
total_deals = await his.deals_total()
print(f'{total_deals} Deals')
# get the number of order
orders = await his.orders_total()
print(f'{orders} orders')
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment