Advertisement
Guest User

Untitled

a guest
Dec 15th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1. PAIR = info.primary_pair                                              
  2.  
  3. LIVE = False                                                      
  4. try: end = info.end                                              
  5. except: LIVE = True    
  6.  
  7. import random as r
  8. import time as t
  9.  
  10. #####################################################################    
  11. #####################################################################
  12. ''' ICEBERG CONTROLS '''    
  13. #####################################################################
  14. #only backtest two orders at a time else it logs too much
  15. ICEBERG_TEST        = True  #ON/OFF to test
  16. ICEBERG_SIMPLE      = False #True for simple version
  17.  
  18. ICEBERG_MIN         = .01   # (1.5) min order size
  19. ICEBERG_MAX         = .03   # (2.5) max order size
  20. ICEBERG_QTY         = 2.5   # (2.5) max coexisting orders
  21. ICEBERG_WAIT        = 60    # (60) sec delay between orders
  22. ICEBERG_ATTEMPTS    = 180   # (180) cycle loop x times before fail
  23. ICEBERG_ALERT       = 100   # (100) send email after x attempts
  24. #####################################################################
  25. #####################################################################
  26.  
  27. def market_ice_simple(action):
  28.    
  29.     # logical theory is the same in configurable version
  30.     while 1:
  31.         try:
  32.             action(PAIR, amount = 2, timeout=60)
  33.         except TradewaveFundsError:
  34.             try:
  35.                 portfolio.update()
  36.                 action(PAIR)
  37.                 break
  38.             except TradewaveInvalidOrderError:
  39.                 break
  40.             except: pass
  41.         except: pass
  42.         if LIVE: t.sleep(60)
  43.    
  44. def market_ice_banner():
  45.     '''
  46.    market_ice()      
  47.    
  48.    litepresence was here Dec 2015 :D hat tip @ traderjoe, lewk
  49.    
  50.    GOALS:
  51.    Effective as-is on any exchange with 250 btc; retail frequency.
  52.    Randomize Order size
  53.    Maintain coexisting orders on the books
  54.    Log Mean Execution Time and Price
  55.    Log Percent Slippage
  56.    Simple User interface
  57.    Don't Fail
  58.    
  59.    BETA NOT LIVE TESTED - PLEASE REPORT ERRORS *AND* SUCCESS
  60.    
  61.    Contribute to open source iceberg market orders:
  62.    
  63.    https://discuss.tradewave.net/t/looping-iceberg-order-strategy-one-order-at-a-time
  64.    '''
  65.  
  66. def market_ice(action):  
  67.  
  68.     if LIVE or ICEBERG_TEST:  
  69.        
  70.         last = float(data[PAIR].price)
  71.         ice_start_time = int(t.time())
  72.  
  73.         # once at beginning of session announce start time and price
  74.         log('%s @ %.2f Deploying Iceberg Market Orders' %
  75.             (ice_start_time, last))
  76.        
  77.         # create lists to calculate weighted execution
  78.         executions = []
  79.         subtotals = []
  80.         amounts = []    
  81.        
  82.         attempt = 0 # Make 180 Iceberg Attempts
  83.         while attempt < ICEBERG_ATTEMPTS:  
  84.            
  85.             # send email alert on 100th attempt
  86.             if attempt == ICEBERG_ALERT:
  87.                 email(last, subject='ALERT Tradewave ICEBERG ATTEMPT 100')
  88.    
  89.             # each order size random in range [1.5 to 2.5 BTC]
  90.             random_size = r.random()*(ICEBERG_MAX-ICEBERG_MIN) + ICEBERG_MIN
  91.             order_timeout = int(ICEBERG_QTY*ICEBERG_WAIT)
  92.             try:                                
  93.                 # place iceberg order; allow up to 3 to overlap
  94.                 order = action(PAIR, amount=random_size,
  95.                     timeout=order_timeout)
  96.                 # record ice order weighted time, subtotal, amount
  97.                 amount = (order.amount).to_float()
  98.                 price = float(order.price)
  99.                 subtotals.append(amount*price)
  100.                 executions.append(amount*t.time())
  101.                 amounts.append(amount)
  102.                 # if I don't have enough funds, place a final order    
  103.             except TradewaveFundsError:
  104.                 expire_icebergs = order_timeout - ICEBERG_WAIT + 1
  105.                 if LIVE: t.sleep(expire_icebergs)
  106.                 try:
  107.                     portfolio.update()  # refresh holdings
  108.                     order = action(PAIR) # place final order
  109.                     # record the final order weighted time, subtotal, amount
  110.                     amount = (order.amount).to_float()
  111.                     price = float(order.price)
  112.                     subtotals.append(amount*price)
  113.                     executions.append(amount*t.time())
  114.                     amounts.append(amount)
  115.                     break
  116.                 except TradewaveInvalidOrderError as e:
  117.                     break # if final order is too small break
  118.                 except: pass # if anything fails try final order again
  119.                 # don't log anything because too much logging = fail
  120.             except: pass # if anything fails try iceberg again  
  121.             # don't log anything because too much logging = fail
  122.             if LIVE: t.sleep(ICEBERG_WAIT) # delay between orders    
  123.             attempt += 1
  124.            
  125.         # calculate and log weighted session time and price
  126.         coinz_moved = sum(amounts) #quantity
  127.         coinz_value = sum(subtotals) #value
  128.         execution_time = int(sum(executions)/coinz_moved) #weighted time
  129.         mean_price = sum(subtotals)/coinz_moved #weighted price
  130.         delta = mean_price - last # net slippage
  131.         slip = 100 * (delta / last) # percent slippage
  132.         session_time = int((t.time() - ice_start_time)/60)
  133.         ma  = float(data[PAIR].ma(30))
  134.         atr = float(data[PAIR].atr(30))
  135.         std = float(data[PAIR].std(30))
  136.         par = (100*((std+atr)/2)/ma)/2
  137.        
  138.         # once at end of session, on one line, log results
  139.         log('market_ice() moved %.2f assets ' %
  140.                 coinz_moved +
  141.             'valued @ %.2f in %s minutes :: ' %
  142.                 (coinz_value, session_time) +
  143.             'weighted execution: %.2f @ %s ' %
  144.                 (mean_price, execution_time) +
  145.             'delta: %.2f slippage: %.3f par: %.3f' %
  146.                 (delta, slip, par))
  147.                
  148.     else:
  149.         # during backtesting place market orders lump sum
  150.         try:
  151.             action(PAIR)
  152.         except:
  153.             log('Order Failed')  
  154.             pass  
  155.    
  156. def tick():
  157.    
  158.     if info.tick == 0:
  159.         market_ice(sell)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement