感谢你们的帮助!
我想我会分享我在沙盒环境中实施的最终解决方案。所有这一切都允许更改货币交易脚本中的多行代码,而只需对用户输入进行一次更改。我能够访问有关新货币对的大量数据,而无需在数百行代码中更改对新货币对的每个引用、投资组合余额、起始余额、日志输出等。
PAIR = 'btc_usd' # requires format 'xxx_yyy'
def instruments(pair):
currency_code = PAIR.split('_')[1]
asset_code = PAIR.split('_')[0]
instrument_pair = asset_code + '_' + currency_code
instrument = pairs[instrument_pair]
currency = portfolio[currencies[currency_code]]
assets = portfolio[currencies[asset_code]]
currency_CODE = (currency_code).upper()
asset_CODE = (asset_code).upper()
if info.tick == 0:
storage.initial_currency = currency
storage.initial_assets = assets
return (instrument_pair, instrument, currency_CODE, currency_code, currency,
storage.initial_currency, asset_CODE, asset_code, assets, storage.initial_assets)
def tick():
(instrument_pair, instrument, currency_CODE, currency_code, currency, storage.initial_currency,
asset_CODE, asset_code, assets, storage.initial_assets) = instruments(PAIR)
if info.tick == 0:
buy(instrument, 1)
if info.tick == 1:
log('******************************')
log('User Input......: %s' % PAIR)
log('instrument pair.: %s' % instrument_pair)
log('instrument......: %s' % instrument)
log('CURRENCY CODE...: %s' % currency_CODE)
log('currency code...: %s' % currency_code)
log('currency........: %.2f' % currency)
log('initial currency: %.2f' % storage.initial_currency)
log('ASSET CODE......: %s' % asset_CODE)
log('asset code......: %s' % asset_code)
log('assets..........: %.2f' % assets)
log('initial assets..: %.2f' % storage.initial_assets)
log('******************************')
在我们的沙箱中产生:
[2014-06-09 02:00:00] BUY: 1.00 BTC (at 648.00 USD)
[2014-06-09 02:15:00] ******************************
[2014-06-09 02:15:00] User Input......: btc_usd
[2014-06-09 02:15:00] instrument pair.: btc_usd
[2014-06-09 02:15:00] instrument......: 2000
[2014-06-09 02:15:00] CURRENCY CODE...: USD
[2014-06-09 02:15:00] currency code...: usd
[2014-06-09 02:15:00] currency........: 352.00
[2014-06-09 02:15:00] initial currency: 1000.00
[2014-06-09 02:15:00] ASSET CODE......: BTC
[2014-06-09 02:15:00] asset code......: btc
[2014-06-09 02:15:00] assets..........: 1.00
[2014-06-09 02:15:00] initial assets..: 0.00
[2014-06-09 02:15:00] ******************************
我还得出的另一种解决方案是:
PAIR = 'Btc#@#$^@% _uSd' # will also work with input 'BTCUSD'
def instruments(pair):
p = filter(str.isalpha, pair).lower()
currency_code = ''.join(list((p)[3:6]))
asset_code = ''.join(list((p)[0:3]))
instrument_pair = asset_code + '_' + currency_code
instrument = pairs[instrument_pair]
currency = portfolio[currencies[currency_code]]
assets = portfolio[currencies[asset_code]]
currency_CODE = (currency_code).upper()
asset_CODE = (asset_code).upper()
if info.tick == 0:
storage.initial_currency = currency
storage.initial_assets = assets
return (instrument_pair, instrument, currency_CODE, currency_code, currency,
storage.initial_currency, asset_CODE, asset_code, assets, storage.initial_assets)
def tick():
(instrument_pair, instrument, currency_CODE, currency_code, currency, storage.initial_currency,
asset_CODE, asset_code, assets, storage.initial_assets) = instruments(PAIR)
if info.tick == 0:
buy(instrument, 1)
if info.tick == 1:
log('******************************')
log('User Input......: %s' % PAIR)
log('instrument pair.: %s' % instrument_pair)
log('instrument......: %s' % instrument)
log('CURRENCY CODE...: %s' % currency_CODE)
log('currency code...: %s' % currency_code)
log('currency........: %.2f' % currency)
log('initial currency: %.2f' % storage.initial_currency)
log('ASSET CODE......: %s' % asset_CODE)
log('asset code......: %s' % asset_code)
log('assets..........: %.2f' % assets)
log('initial assets..: %.2f' % storage.initial_assets)
log('******************************')
产生:
[2014-06-09 02:00:00] BUY: 1.00 BTC (at 648.00 USD)
[2014-06-09 02:15:00] ******************************
[2014-06-09 02:15:00] User Input......: Btc#@#$^@% _uSd
[2014-06-09 02:15:00] instrument pair.: btc_usd
[2014-06-09 02:15:00] instrument......: 2000
[2014-06-09 02:15:00] CURRENCY CODE...: USD
[2014-06-09 02:15:00] currency code...: usd
[2014-06-09 02:15:00] currency........: 352.00
[2014-06-09 02:15:00] initial currency: 1000.00
[2014-06-09 02:15:00] ASSET CODE......: BTC
[2014-06-09 02:15:00] asset code......: btc
[2014-06-09 02:15:00] assets..........: 1.00
[2014-06-09 02:15:00] initial assets..: 0.00
[2014-06-09 02:15:00] ******************************