This commit is contained in:
Ichinga Samuel
2024-01-18 02:26:27 +01:00
parent 4b5f267509
commit 63f22fc081
43 changed files with 387 additions and 545 deletions
+24 -24
View File
@@ -1,39 +1,39 @@
from datetime import time
import logging
from aiomql.lib import FingerTrap
from aiomql import Bot, Account, ForexSymbol, Session, Sessions
from aiomql import Bot, ForexSymbol, FingerTrap, Session, Sessions, RAM, SimpleTrader, TimeFrame
logging.basicConfig(level=logging.INFO)
def build_bot():
# Either initialize an account here with your login details here or set them in the aiomql.json file.
# acc = Account(login=1234567, password='*******', server='Broker-Server')
bot = Bot()
# Prebuilt strategy from the library.
# Disclaimer: These strategy is only for demonstration purposes.
# The author of this library is not responsible for any losses incurred from using this strategy.
# create sessions for the strategies
london = Session(name='London', start=8, end=time(hour=15, minute=30), on_end='close_all')
new_york = Session(name='New York', start=13, end=time(hour=20, minute=30))
tokyo = Session(name='Tokyo', start=23, end=time(hour=6, minute=30))
# using trade sessions is optional. the strategy will run with a default session of 24 hours if not specified.
# session start and end times are in UTC. Make sure to convert to UTC if you are in a different timezone.
# sessions can be used to close positions at the end of a trading session.
sess = Session(name='London', start=8, end=time(hour=15, minute=30), on_end='close_all')
sess2 = Session(name='New York', start=13, end=time(hour=20, minute=30))
sess3 = Session(name='Tokyo', start=23, end=time(hour=6, minute=30))
allsess = Session(name='All', start=0, end=23, on_end='close_all')
sessions = Sessions(sess, sess2, sess3, allsess)
# configure the parameters and the trader for a strategy
params = {'trend_candles_count': 500, 'fast_period': 8, 'slow_period': 34, 'entry_timeframe': TimeFrame.M5}
gbpusd = ForexSymbol(name='GBPUSD')
st1 = FingerTrap(symbol=gbpusd, params=params,
trader=SimpleTrader(symbol=gbpusd, ram=RAM(risk=0.05, risk_to_reward=2)),
sessions=Sessions(london, new_york))
# configurable parameters for the strategy
params = {'trend_candles_count': 500, 'fast_period': 8}
st1 = FingerTrap(symbol=ForexSymbol(name='GBPUSD'), params=params, sessions=sessions)
st3 = FingerTrap(symbol=ForexSymbol(name='AUDUSD'), params=params, sessions=sessions)
st4 = FingerTrap(symbol=ForexSymbol(name='USDCAD'), params=params, sessions=sessions)
st5 = FingerTrap(symbol=ForexSymbol(name='USDJPY'), params=params, sessions=sessions)
st6 = FingerTrap(symbol=ForexSymbol(name='EURGBP'), params=params, sessions=sessions)
bot.add_strategies([st1, st3, st4, st5, st6])
# use the default for the other strategies
st2 = FingerTrap(symbol=ForexSymbol(name='AUDUSD'), sessions=Sessions(tokyo, new_york))
st3 = FingerTrap(symbol=ForexSymbol(name='USDCAD'), sessions=Sessions(new_york))
st4 = FingerTrap(symbol=ForexSymbol(name='USDJPY'), sessions=Sessions(tokyo))
st5 = FingerTrap(symbol=ForexSymbol(name='EURGBP'), sessions=Sessions(london))
# sessions are not required
st6 = FingerTrap(symbol=ForexSymbol(name='EURUSD'))
# add strategies to the bot
bot.add_strategies([st1, st2, st3, st4, st5, st6])
bot.execute()
build_bot()
# run the bot
build_bot()
+18 -10
View File
@@ -1,25 +1,32 @@
import asyncio
from aiomql import Symbol, TimeFrame, Account
from aiomql import Symbol, TimeFrame, Account, Candle, Candles
async def main():
"""Example of using the Candle and Candles classes.
The candle class is a single price bar. Holding the OHLCV data for a single price bar.
The Candles class is a container of Candle objects. It is an Iterable of Candle objects.
It is sliceable and indexable. It can also be accessed with keywords.
It is a wrapper around a pandas DataFrame. Which is what it uses to store the data.
"""
async with Account():
# create a symbol
sym = Symbol(name="AUDUSD")
sym = Symbol(name="EURUSD")
# Get EURUSD price bars for the past 48 hours
candles = await sym.copy_rates_from_pos(timeframe=TimeFrame.H1, count=48, start_position=0)
candles: Candles = await sym.copy_rates_from_pos(timeframe=TimeFrame.H1, count=48, start_position=0)
# get size of candles
print(len(candles)) # 48
# get the latest candle by accessing the last one.
last = candles[-1] # A Candle object
last: Candle = candles[-1] # A Candle object
print(type(last))
print(last.time)
print(last.Index)
# get the last five hours
last_five = candles[-5:] # A Candles object.
print(type(last_five))
print(last_five)
# slicing returns a Candles object
half = candles[24:]
print(type(half))
print(len(half))
close = candles['close'] # close price of all the candles as a pandas series
print(type(close))
@@ -32,6 +39,7 @@ async def main():
# use talib to compute crossover. This returns a series object that is not part of the candles object.
closeXema = candles.ta_lib.cross(candles.close, candles.ema)
# add to the candles
candles['closeXema'] = closeXema
print(candles)
+3 -3
View File
@@ -7,7 +7,7 @@ async def main():
async with Account():
# create a symbol
sym = ForexSymbol(name="EURUSD")
sym = ForexSymbol(name="EURUSD-T")
# Confirm the symbol is available for this account and initialize with default values.
res = await sym.init()
@@ -15,7 +15,7 @@ async def main():
# I want to place a market buy order, risk only 2usd, and target 10 pips in this trade.
# The ForexSymbol object has a compute_volume method that can be used to compute the volume
# given a target pips and amount.
volume = await sym.compute_volume(amount=2, pips=10)
volume = await sym.compute_volume(amount=2, points=100)
# a risk to reward ratio of 1:2
# get the price tick of the symbol
@@ -34,4 +34,4 @@ async def main():
print(res)
asyncio.run(main())
asyncio.run(main())
+18 -15
View File
@@ -1,25 +1,27 @@
import logging
import asyncio
from datetime import datetime
from aiomql import ForexSymbol, Account, Positions, History, Trader, OrderType, RAM
from aiomql import ForexSymbol, Account, Positions, History, SimpleTrader as Trader, OrderType, RAM
logging.basicConfig(level=logging.INFO, filemode='w', filename='example.log', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
async def main():
# Account details are in the aiomql.json file
async with Account():
# get start time using local timezone
tz = datetime.now().astimezone().tzinfo
start = datetime.now(tz=tz)
# get start time
start = datetime.now()
# create two symbols and initialize them
sym1 = ForexSymbol(name="EURUSD")
sym2 = ForexSymbol(name="GBPUSD")
sym1 = ForexSymbol(name="EURUSD-T")
sym2 = ForexSymbol(name="GBPUSD-T")
await sym1.init()
await sym2.init()
# Risk Assets Management instance
# fix the amount to be risked at 2 USD. USD is the account currency.
ram = RAM(amount=2)
ram = RAM(amount=2, points=100)
# Create two traders instance
trd = Trader(symbol=sym1, ram=ram)
@@ -38,22 +40,23 @@ async def main():
# close all open positions
await pos.close_all()
end = datetime.now(tz=tz)
end = datetime.now()
# get the number of open positions
total = await pos.positions_total()
print(f'{total} Open positions') # 0
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')
start = datetime(day=start.day-1, month=start.month, year=start.year, hour=start.hour, minute=0, second=0)
his = History(date_from=start.timestamp(), date_to=end.timestamp())
# get the number of order
orders = await his.orders_total()
print(f'{orders} orders')
# get the number of deals
# total_deals = await his.deals_total()
# print(f'{total_deals} Deals')
asyncio.run(main())
asyncio.run(main())
+4 -2
View File
@@ -1,10 +1,12 @@
import asyncio
from datetime import datetime
from aiomql import ForexSymbol, Symbol, TimeFrame, Account
from aiomql import ForexSymbol, TimeFrame, Account, Config
config = Config()
async def main():
async with Account():
sym = ForexSymbol(name="EURUSD")
sym = ForexSymbol(name="EURUSD-T")
res = await sym.init()
if not res:
print('Symbol not available')