En la carga de los recursos... Cargando...

Hedge de plataformas bilaterales para ejercitarse en el intercambio de puntos de referencia

El autor:el rey, Fecha: 2017-09-02 15:14:53
Las etiquetas:PythonCubierta

La estrategia de cobertura de las plataformas de Python


import time
import numpy as np

def reject_outliers(arr, m=2):
    final_list = [x for x in arr if (x > np.mean(arr) - 2 * np.std(arr))]
    final_list = [x for x in final_list if (x < np.mean(arr) + 2 * np.std(arr))]
    return final_list

def main():
    '''SetErrorFilter("canceled")'''
     
    LogReset()
    LogProfitReset()
    
    global sellTrue 
    global buyTrue 
    sellTrue = 0 
    buyTrue = 0
    initStocks = 0.0
    initBalance = 0.0
    lastTradeTime = 0
    lastTradeErrExchange = ''
    accountsCache = []
    depthCache=[]
    names = []
    buyPrice=0
    sellPrice=0
    dealtransactioned = False

    for e in exchanges:
        names.append(e.GetName())
        account = _C(e.GetAccount)
        initStocks += account.Stocks
        initBalance += account.Balance
        Log("Switch", e.GetLabel(), "To", e.IO("websocket"))
        
    Log("Total Currency:", _N(initBalance), "Total Asset", _N(initStocks), 'Python:', __import__('sys').version)
    while (True):
        LogStatus(_D())
        if not accountsCache:
            accountsCache = [_C(e.GetAccount) for e in exchanges]
        Sleep(LoopInterval)
        depthA = exchanges[0].GetDepth()
        depthCache.append(depthA)
        if not depthA:
            continue
        depthB = exchanges[1].GetDepth()
        depthCache.append(depthB)
        if not depthB:
            continue
        
                
        '''trade part'''
        
        diffA = _N(depthA.Bids[0].Price - depthB.Asks[0].Price, 3)
        diffB = _N(depthB.Bids[0].Price - depthA.Asks[0].Price, 3)
        LogStatus("A平台币",exchanges[0].GetAccount().Stocks,"A平台钱",exchanges[0].GetAccount().Balance,"B平台币",exchanges[1].GetAccount().Stocks,"B平台钱",exchanges[1].GetAccount().Balance)
        
        if diffA > 0:
            buytarget = depthB.Asks[0]
            opAmount=min(depthA.Bids[0].Amount,buytarget.Amount)
            buyAmount=min(opAmount,exchanges[1].GetAccount().Balance/buytarget.Price)
            sellAmount=min(buyAmount,exchanges[0].GetAccount().Stocks)
            
            if diffA > (buyAmount*depthB.Asks[0].Price*0.2/100+sellAmount*depthA.Bids[0].Price*0.2/100)*p:
                Log('buy from B sell at A')
                Log("A平台币",exchanges[0].GetAccount().Stocks,"A平台钱",exchanges[0].GetAccount().Balance,"B平台币",exchanges[1].GetAccount().Stocks,"B平台钱",exchanges[1].GetAccount().Balance)
                tradeAmount=min(buyAmount,sellAmount)     
                buyorder =exchanges[1].Buy(buytarget.Price,tradeAmount)
                sellorder = exchanges[0].Sell(depthA.Bids[0].Price,tradeAmount)
                canceller = 0
                    
                    
                while True:
             
                    
                    
                    if (exchanges[1].GetOrder(buyorder).Status == 2) and (exchanges[0].GetOrder(sellorder).Status == 2):
                        Log('deal transactioned')
                        buyPrice = exchanges[1].GetOrder(buyorder).Price
                        sellPrice = exchanges[0].GetOrder(sellorder).Price
                        LogProfit(exchanges[0].GetOrder(sellorder).Amount*exchanges[0].GetOrder(sellorder).Price-exchanges[1].GetOrder(buyorder).Amount*exchanges[1].GetOrder(buyorder).Price)
                        dealtransactioned = True
                        break
                    elif ((exchanges[1].GetOrder(buyorder).Status == 1) or (exchanges[0].GetOrder(sellorder).Status == 1)):
                        buyPrice = exchanges[1].GetOrder(buyorder).Price
                        sellPrice = exchanges[0].GetOrder(sellorder).Price
                        canceller += 1
                        dealtransactioned = True
                        Sleep(200)
                    elif ((exchanges[1].GetOrder(buyorder).Status == 0) and (exchanges[0].GetOrder(sellorder).Status == 0)):
                        Sleep(200)
                        canceller += 1
                    if canceller == 5:        
                        exchanges[1].CancelOrder(buyorder)
                        exchanges[0].CancelOrder(sellorder)
                        Log('deal cancelled')       
                        break    
                
                    

        elif diffB > 0:
            
            opAmount=min(depthB.Bids[0].Amount,depthA.Asks[0].Amount)
            buyAmount=min(opAmount,exchanges[0].GetAccount().Balance/depthA.Asks[0].Price)
            sellAmount=min(buyAmount,exchanges[1].GetAccount().Stocks)
            canceller = 0
            if diffB > (buyAmount*depthA.Asks[0].Price*0.2/100+sellAmount*depthB.Bids[0].Price*0.2/100)*p:
                Log('buy from A sell at B')
                
                tradeAmount=min(buyAmount,sellAmount)
                buyorder=exchanges[0].Buy(depthA.Asks[0].Price,tradeAmount)
                sellorder=exchanges[1].Sell(depthB.Bids[0].Price,tradeAmount)
                canceller =0
                
                while True:
                       
                    if (exchanges[0].GetOrder(buyorder).Status == 2) and (exchanges[1].GetOrder(sellorder).Status == 2):
                        Log('deal transactioned')
                        buyPrice=exchanges[0].GetOrder(buyorder).Price
                        sellPrice=exchanges[1].GetOrder(sellorder).Price
                        LogProfit(exchanges[1].GetOrder(sellorder).Amount*exchanges[1].GetOrder(sellorder).Price-exchanges[0].GetOrder(buyorder).Amount*exchanges[0].GetOrder(buyorder).Price)
                        dealtransactioned = True
                        break
                    elif ((exchanges[0].GetOrder(buyorder).Status == 1) or (exchanges[1].GetOrder(sellorder).Status == 1)):
                        
                        buyPrice=exchanges[0].GetOrder(buyorder).Price
                        sellPrice=exchanges[1].GetOrder(sellorder).Price
                        canceller += 1
                        dealtransactioned = True
                        Sleep(200)
                    elif ((exchanges[0].GetOrder(buyorder).Status == 0) and (exchanges[1].GetOrder(sellorder).Status == 0)):
                        Sleep(200)
                        canceller += 1
                    if canceller == 5:        
                        exchanges[0].CancelOrder(buyorder)
                        exchanges[1].CancelOrder(sellorder)
                        Log('deal cancelled')       
                        break 
              
        '''balance part'''
        for i in [0,1]:
            if dealtransactioned:
                if(exchanges[i].GetAccount().Stocks > (initStocks/2)* q and exchanges[i].GetAccount().Balance < (initBalance/2)*q):           
                    sellwait = 1
                    Log('ready to sell')
                    loopbreaker = 0
                    while (sellwait):
                        
                        if (_N(depthCache[i].Bids[0].Price,3)> buyPrice*r):
                            sellwait = 0

                            break
                        else:
                            Sleep(1000)
                            Log('sellwait')
                            loopbreaker += 1
                        if loopbreaker == 600:
                            '''break'''
                        
                    

                    sellTrue = 1

                    while(sellTrue):
                        dealprice = depthCache[i].Bids[0].Price
                        Log('insufficient money, sell some')
                        idealamount = initBalance/8
                        availamount = exchanges[i].GetAccount().Stocks/2
                        dealamount = min(idealamount,availamount)
                        balancesell = exchanges[i].Sell(depthCache[i].Bids[0].Price, dealamount)
                        Sleep(200)
                        while( exchanges[i].GetOrder(balancesell).Status not in [1,2] ):
                            exchanges[i].CancelOrder(balancesell)
                            if( dealprice > buyPrice):
                                dealprice -= 1
                            balancesell = exchanges[i].Sell(dealprice,dealamount)
                            Sleep(200)
                        '''buyPrice=[]'''
                        Log("Sell Balance finished")
                        sellTrue=0


                if(exchanges[i].GetAccount().Balance > (initBalance/2)*q and exchanges[i].GetAccount().Stocks < (initStocks/2)*q):
                    buywait = 1
                    loopbreaker = 0
                    while (buywait):
                        
                        if(_N(depthCache[i].Asks[0].Price,3)< sellPrice*r):

                            buywait=0

                            break
                        else:
                            Sleep(1000)
                            Log(_N(depthCache[i].Asks[0].Price,3),sellPrice*r)
                            loopbreaker +=1
                        if loopbreaker == 600:
                            '''break
                        break'''
                            



                    buyTrue = 1
                    while(buyTrue):
                        Log('buyTrue started')
                        dealprice = depthCache[i].Asks[0].Price
                        Log('insufficient stocks, buy some')
                        idealamount = initStocks/8
                        availamount = exchanges[i].GetAccount().Balance/dealprice/2
                        dealamount = min(idealamount,availamount)
                        balancebuy = exchanges[i].Buy(dealprice,dealamount)
                        Sleep(200)
                        while(exchanges[i].GetOrder(balancebuy).Status not in [1,2]):

                            exchanges[i].CancelOrder(balancebuy)
                            if (dealprice < sellPrice):
                                dealprice += 1
                            balancebuy = exchanges[i].Buy(dealprice,dealamount)
                            Sleep(200)
                        '''sellPrice=[]'''
                        Log("Buy Balance finished")
                        buyTrue = 0
                        break
                else:         
                    continue
                break
            else:
                break
        



Relacionados

Más.

¿Qué es esto?¿Cómo puedo hacerte amigo?

El hombre del cielo.El disco físico tiene que soportar el procesamiento de errores.

El Dr. Liu¿Puede añadir notas para facilitar el aprendizaje y la comunicación?

el reyPor favor, no utilicen esta táctica en el disco real. Soy un programador nuevo, sólo para el intercambio de aprendizaje, por favor, pidan orientación.

el reyBien! Voy a añadir comentarios poco a poco, gracias por su atención, vamos a intercambiar y progresar juntos.