def foo(config): print('Running...') asset = EURUSD(generate_data(100000)) account = Account(initial_balance=1000) slow_period = 45 fast_period = 35 sma = np.zeros((asset.data.shape[0],2)) sma[(slow_period-1):,0] = moving_average(asset.data[:,1],slow_period) sma[(fast_period-1):,1] = moving_average(asset.data[:,1],fast_period) risk_man = ConstantRate(config['lot_size'],on='free_margin') strategy = MACross(RiskManagement=risk_man,rate=config['rate']) strategy = asset.register(strategy) result = BackTest(account,strategy).run(assets=asset,exog=sma) tune.track.log(mean_accuracy=result.Account.balance)
for stock in self.last_selected.index: # for each last chosen stocks if stock not in selected.index: self.selected_to_close.append(stock) del self.past_data[0] return order.asset_id in self.selected_to_close # close if the order is selected for close else: return False else: return order.asset_id in self.selected_to_close # close if the order is selected for close def decide_short_close(self, order, spot_price, timestamp, Account, exog): return False stocks = [ Stock(generate_data(10000, freq='1h'), name=f'stock_{i}', short_name=f'STCK_{i}') for i in range(20) ] # randomly generate stock prices account = Account( initial_balance=1000) # initialize an account with 1000 USD balance risk_man = ConstantRate(0.05) # constant lot size with 5% of balance each time strategy = CustomAllocation(RiskManagement=risk_man, id=45450, name='custom_buyer') for stock in stocks: strategy = stock.register( strategy) # allow strategy to use all of them, either one or multiple sim = BackTest(Account=account, Strategy=strategy).run(stocks)
import numpy as np import pandas as pd sys.path.insert(0, "../") from strategy_tester.account import Account from strategy_tester.asset import Stock from strategy_tester.strategy import Strategy from strategy_tester.risk_management import ConstantLots from strategy_tester.simulate import BackTest from strategy_tester.utils import generate_data, ROI, sharpe_ratio import numpy as np import time aapl = Stock(price=generate_data(100000), base="AAPL") account = Account(max_allowed_risk=None, max_n_orders=10) risk_man = ConstantLots(0.1) strategy = Strategy(RiskManagement=risk_man, id=23030, name="noise_trader") strategy = aapl.register(strategy) sim = BackTest( Account=account, Strategy=strategy, track=[ROI, sharpe_ratio] ).run(aapl) # print(sim.tracked_results) # print(sim.Account.balances) # print(sim.Account.equities) # print(sim.Account.free_margins) # print(sim.Account.navs) # print(sim.Account.n_inactive_orders)
import sys import numpy as np import pandas as pd sys.path.insert(0, '../') from strategy_tester.account import Account from strategy_tester.asset import AAPL from strategy_tester.strategy import Strategy from strategy_tester.risk_management import ConstantLots from strategy_tester.simulate import BackTest from strategy_tester.utils import generate_data, ROI, sharpe_ratio import numpy as np import time aapl = AAPL(generate_data(100000)) account = Account(initial_balance=1000, max_allowed_risk=None, max_n_orders=10) risk_man = ConstantLots(0.1) strategy = Strategy(RiskManagement=risk_man, id=23030, name='noise_trader') strategy = aapl.register(strategy) start = time.time() sim = BackTest(Account=account, Strategy=strategy, track=[ROI, sharpe_ratio]).run(aapl) end = time.time() print(f'{end-start} seconds elapsed.') print(sim.tracked_results) # print(sim.Account.balances) # print(sim.Account.equities) # print(sim.Account.free_margins) # print(sim.Account.navs)
# TO DO: simplify the code class Random(ParamSearch): def __init__(self,q,*args,**kwargs): super().__init__(*args,**kwargs) if not 0 < q < 1: raise ValueError('q must be in (0,1)') self.q = q def run(self,*args,**kwargs): params = list(dict_product(self._paramset)) size = len(params) self.n = int(size*self.q) idxs = np.random.randint(0,size,self.n,dtype=int) params = [params[i] for i in idxs] list(map(lambda s: self._run(s,*args,**kwargs),params)) return self if __name__ == '__main__': from strategy_tester.utils import generate_data, dict_product from strategy_tester.account import Account from strategy_tester.strategy import Strategy from strategy_tester.risk_management import ConstantRate data = generate_data(100000) account = Account(balance=1000) paramset = {'RiskManagement':[ConstantRate(0.05),ConstantRate(0.1),ConstantRate(0.2)]} sim = Random(0.7,account,Strategy,paramset).run(data.values) print(sim.output)
import sys import numpy as np import pandas as pd sys.path.insert(0, '../') from strategy_tester.account import Account from strategy_tester.asset import EURUSD from strategy_tester.strategy import Strategy from strategy_tester.risk_management import ConstantRate, KellyCriterion from strategy_tester.simulate import BackTest from strategy_tester.utils import generate_data eurusd = EURUSD(generate_data(100000)) account = Account(initial_balance=1000) strategy_1 = Strategy(RiskManagement=ConstantRate(0.05), id=23030, name='noise_trader_1') strategy_2 = Strategy(RiskManagement=KellyCriterion(n=20), id=23031, name='noise_trader_2') strategy_1, strategy_2 = eurusd.register(strategy_1, strategy_2) sim = BackTest(Account=account, Strategy=[strategy_1, strategy_2]).run(eurusd) print(sim.Account.balances)
import sys import numpy as np import pandas as pd sys.path.insert(0,'../') from strategy_tester.account import Account from strategy_tester.asset import EURUSD, GBPUSD from strategy_tester.strategy import Strategy from strategy_tester.risk_management import ConstantRate from strategy_tester.simulate import BackTest from strategy_tester.utils import generate_data eurusd = EURUSD(data=generate_data(100000)) gbpusd = GBPUSD(data=generate_data(100000)) account = Account(initial_balance=1000) risk_man = ConstantRate(0.1) strategy_1 = Strategy(RiskManagement=risk_man,id=23030,name='noise_trader') strategy_2 = Strategy(RiskManagement=risk_man,id=23031,name='noise_trader2') strategy_1,strategy_2 = eurusd.register(strategy_1,strategy_2) strategy_1 = gbpusd.register(strategy_1) sim = BackTest(Account=account,Strategy=[strategy_1,strategy_2]).run(assets=(eurusd,gbpusd)) print(sim.Account.balances)
import sys import numpy as np import pandas as pd sys.path.insert(0, "../") from strategy_tester.account import Account from strategy_tester.asset import Currency # EURUSD, GBPUSD from strategy_tester.strategy import Strategy from strategy_tester.risk_management import ConstantRate from strategy_tester.simulate import BackTest from strategy_tester.utils import generate_data eurusd = Currency(price=generate_data(1000000), base="EUR", quote="USD") gbpusd = Currency(price=generate_data(1000000), base="GBP", quote="USD") account = Account() risk_man = ConstantRate(0.1) strategy_1 = Strategy(RiskManagement=risk_man, id=23030, name="noise_trader") strategy_2 = Strategy(RiskManagement=risk_man, id=23031, name="noise_trader2") strategy_1, strategy_2 = eurusd.register(strategy_1, strategy_2) strategy_1 = gbpusd.register(strategy_1) sim = BackTest(Account=account, Strategy=[strategy_1, strategy_2]).run( assets=(eurusd, gbpusd) ) print(sim.Account.balances)