Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PAIR = info.primary_pair
- LIVE = False
- try: end = info.end
- except: LIVE = True
- import random as r
- import time as t
- #####################################################################
- #####################################################################
- ''' ICEBERG CONTROLS '''
- #####################################################################
- #only backtest two orders at a time else it logs too much
- ICEBERG_TEST = True #ON/OFF to test
- ICEBERG_SIMPLE = False #True for simple version
- ICEBERG_MIN = .01 # (1.5) min order size
- ICEBERG_MAX = .03 # (2.5) max order size
- ICEBERG_QTY = 2.5 # (2.5) max coexisting orders
- ICEBERG_WAIT = 60 # (60) sec delay between orders
- ICEBERG_ATTEMPTS = 180 # (180) cycle loop x times before fail
- ICEBERG_ALERT = 100 # (100) send email after x attempts
- #####################################################################
- #####################################################################
- def market_ice_simple(action):
- # logical theory is the same in configurable version
- while 1:
- try:
- action(PAIR, amount = 2, timeout=60)
- except TradewaveFundsError:
- try:
- portfolio.update()
- action(PAIR)
- break
- except TradewaveInvalidOrderError:
- break
- except: pass
- except: pass
- if LIVE: t.sleep(60)
- def market_ice_banner():
- '''
- market_ice()
- litepresence was here Dec 2015 :D hat tip @ traderjoe, lewk
- GOALS:
- Effective as-is on any exchange with 250 btc; retail frequency.
- Randomize Order size
- Maintain coexisting orders on the books
- Log Mean Execution Time and Price
- Log Percent Slippage
- Simple User interface
- Don't Fail
- BETA NOT LIVE TESTED - PLEASE REPORT ERRORS *AND* SUCCESS
- Contribute to open source iceberg market orders:
- https://discuss.tradewave.net/t/looping-iceberg-order-strategy-one-order-at-a-time
- '''
- def market_ice(action):
- if LIVE or ICEBERG_TEST:
- last = float(data[PAIR].price)
- ice_start_time = int(t.time())
- # once at beginning of session announce start time and price
- log('%s @ %.2f Deploying Iceberg Market Orders' %
- (ice_start_time, last))
- # create lists to calculate weighted execution
- executions = []
- subtotals = []
- amounts = []
- attempt = 0 # Make 180 Iceberg Attempts
- while attempt < ICEBERG_ATTEMPTS:
- # send email alert on 100th attempt
- if attempt == ICEBERG_ALERT:
- email(last, subject='ALERT Tradewave ICEBERG ATTEMPT 100')
- # each order size random in range [1.5 to 2.5 BTC]
- random_size = r.random()*(ICEBERG_MAX-ICEBERG_MIN) + ICEBERG_MIN
- order_timeout = int(ICEBERG_QTY*ICEBERG_WAIT)
- try:
- # place iceberg order; allow up to 3 to overlap
- order = action(PAIR, amount=random_size,
- timeout=order_timeout)
- # record ice order weighted time, subtotal, amount
- amount = (order.amount).to_float()
- price = float(order.price)
- subtotals.append(amount*price)
- executions.append(amount*t.time())
- amounts.append(amount)
- # if I don't have enough funds, place a final order
- except TradewaveFundsError:
- expire_icebergs = order_timeout - ICEBERG_WAIT + 1
- if LIVE: t.sleep(expire_icebergs)
- try:
- portfolio.update() # refresh holdings
- order = action(PAIR) # place final order
- # record the final order weighted time, subtotal, amount
- amount = (order.amount).to_float()
- price = float(order.price)
- subtotals.append(amount*price)
- executions.append(amount*t.time())
- amounts.append(amount)
- break
- except TradewaveInvalidOrderError as e:
- break # if final order is too small break
- except: pass # if anything fails try final order again
- # don't log anything because too much logging = fail
- except: pass # if anything fails try iceberg again
- # don't log anything because too much logging = fail
- if LIVE: t.sleep(ICEBERG_WAIT) # delay between orders
- attempt += 1
- # calculate and log weighted session time and price
- coinz_moved = sum(amounts) #quantity
- coinz_value = sum(subtotals) #value
- execution_time = int(sum(executions)/coinz_moved) #weighted time
- mean_price = sum(subtotals)/coinz_moved #weighted price
- delta = mean_price - last # net slippage
- slip = 100 * (delta / last) # percent slippage
- session_time = int((t.time() - ice_start_time)/60)
- ma = float(data[PAIR].ma(30))
- atr = float(data[PAIR].atr(30))
- std = float(data[PAIR].std(30))
- par = (100*((std+atr)/2)/ma)/2
- # once at end of session, on one line, log results
- log('market_ice() moved %.2f assets ' %
- coinz_moved +
- 'valued @ %.2f in %s minutes :: ' %
- (coinz_value, session_time) +
- 'weighted execution: %.2f @ %s ' %
- (mean_price, execution_time) +
- 'delta: %.2f slippage: %.3f par: %.3f' %
- (delta, slip, par))
- else:
- # during backtesting place market orders lump sum
- try:
- action(PAIR)
- except:
- log('Order Failed')
- pass
- def tick():
- if info.tick == 0:
- market_ice(sell)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement