Python Grid testing

Author: The Gospel for Small-Fund Users, Created: 2018-10-12 11:53:06, Updated: 2018-10-12 11:59:21

'''backtest
start: 2018-09-30 00:00:00
end: 2018-10-11 19:00:00
period: 15m
exchanges: [{"eid":"OKCoin_EN","currency":"ETC_USD"}]
'''
	
def main():
	first()
	while True:
		get_information = _C(exchange.GetTicker)
		#get_order = exchange.GetOrder() 
		btc_price_last = get_information.Last
		#account = exchange.GetAccount()
		
		for j in range(0,len(Grid_price_list)):
			if(Grid_price_list[j-1] < btc_price_last < Grid_price_list[j]):   # 判断价格所在的网格区间
				if(btc_price_last < exchange.GetOrder(buy_id).AvgPrice and btc_price_last <= Grid_price_list[j-1]): # !!!需要进一步完善针对卖单情况的逻辑!!!,现价小于买入价,且现价小于等于网格下值,继续买入。
					buy()
				else:
					main()
				if(btc_price_last > exchange.GetOrder(buy_id).AvgPrice and btc_price_last >= Grid_price_list[j]): # !!!需要进一步完善针对卖单情况的逻辑!!!,现价大于买入价,且现价大于等于网格上值,继续卖出。
					sell()
				else:
					main()
			
		order_stock_rate = 0.05 * j # 仓位比例
		
	Sleep(300 * 1000)
	
def buy():
	buy_price = Grid_price_list[j-1]
	buy_id = exchange.Buy(buy_price, 500 * (j - 1) / buy_price)

def sell():
	sell_price = Grid_price_list[j]
	sell_id = exchange.Sell(sell_price, 500 * j / sell_price)


def first():

	get_information = _C(exchange.GetTicker)
	#get_order = exchange.GetOrder() 
	btc_price_last = get_information.Last
	global account
	account = _C(exchange.GetAccount)

	global Grid_price_list
	Grid_price_list = [] # 给最低网格价格赋值,算出每个价格
	btc_price_bottom = 7
	for i in range(1,20):
		btc_price_bottom = btc_price_bottom * 1.05
		Grid_price_list.append(btc_price_bottom)
	Log(Grid_price_list)

	global j
	for j in range(0,len(Grid_price_list)):
		if(Grid_price_list[j-1] < btc_price_last < Grid_price_list[j]):   # 判断价格所在的网格区间
			buy()
			Log("初始买入",exchange.GetOrder(buy_id)
		#order_stock_rate = 0.05 * j # 仓位比例

More

The Gospel for Small-Fund UsersPlease blind me.

The Gospel for Small-Fund UsersTraceback (most recent call last): File "", line 1292, in Run File "", line 63 if 'init' in locals() and callable(init): ^ SyntaxError: invalid syntax

The Little Dream\>_<, this mistake is normal, don't bother.

The Little DreamYou can see that the order information Status is 0 and is still in the pending order state, i.e. the order has not been settled, so the number of coins has not changed.

The Gospel for Small-Fund UsersIf you look at the code and the log below, the information in exchange.GetOrder ((buy_id) is amount is 368.3512, and the information in Stocks is the same as the value I set back to 20. Log (("Initial purchase", exchange.GetOrder ((buy_id), account.Stocks) is the name of the log file. 2018-09-30 00:00:00 Information Initial purchase {'Status': 0L, 'Price': 10.8592, 'AvgPrice': 0.0, 'Amount: 368.3512, 'DealAmount': 0.0, 'ContractType': 'ETC_USDT_Huobi', 'Type': 0L, 'Id: 1L} 20.0 This is the first time that a contract has been entered into with a customer. 2018-09-30 00:00:00 Information 1 20.0 This is a list of all the different ways I've written this column in the past. 2018-09-30 00:00:00 Huobi has purchased 10.8592 368.351259761308

The Little DreamYou should pay with Balance and sell with Stocks.

The Gospel for Small-Fund Users 还请教一下,回测系统里面account.Stocks的值不会变吗?怎么下了买入单,Balance减小了,Stocks没变呢?

The Little Dream### Please call me DEBUG Master~ What's up? global j for j in range ((0, len ((Grid_price_list)): if ((Grid_price_list[j-1] < btc_price_last < Grid_price_list[j]: # Determines the grid range in which the price is located Buy this. Log (("Initial purchase", exchange.GetOrder ((buy_id) # This place is a little less written ) oooh, T_T. This kind of coding problem is to be watched!!!!!! #order_stock_rate = 0.05 * j # ratio of positions What's up?