The class mid_class () :
Def __init__ (self, this_exchange) :
"'
Initialize the data
To get information by using exchange
"'
The self. The exchange=this_exchange
The self. The name=self. Exchange. GetName ()
The self. The current=self. Exchange. GetCurrency ()
Def get_account_info (self) :
"'
Account information
"'
The self. The Balance='___'
Self. Stocks='___'
Self. FrozenBalance='___'
Self. FrozenStocks='___'
Try:
The self. The account=self. Exchange. GetAccount ()
The self. The Balance=self. The account [' Balance ']
The self. The Amount=self. The account [' Stocks']
Self. FrozenBalance=self. The account [' FrozenBalance]
Self. FrozenStocks=self. The account [' FrozenStocks]
Return True
Except:
Return False
Def get_ticker (self) :
"'
Get the price information
"'
Self. High='___'
Self. Low='___'
Self. Sell='___'
Self. Buy='___'
The self. The Last='___'
The self. The Volume='___'
Try:
The self. The ticker=self. Exchange. GetTicker ()
Self. High=self. Ticker [' High ']
Self. Low=self. Ticker [' Low ']
Self. Sell=self. Ticker [' Sell ']
Self. Buy=self. Ticker [' Buy ']
The self. The Last=self. The ticker [' Last ']
The self. The Volume=self. The ticker [' Volume ']
Return True
Except:
Return False
Def get_record_info (self, period=PERIOD_M5) :
"'
K line information
"'
Self. Record=self. Exchange. GetRecords (period)
Def make_order (self, order_type, price, amount) :
"'
Post an order
"'
If order_type=='Buy' :
Try:
Order_id=self. Exchange. Buy (price, amount)
Return 'hang pay success'
Except:
Return 'hang check failure'
Elif order_type=='Sell' :
Try:
Order_id=self. Exchange. Sell (price, amount)
Return 'hang sale success'
Except:
Return 'hang sell single failure'
Def cancel_order (self) :
"'
To cancel an order
"'
Return the self. Exchange. CancelOrder (order_Id)
Def refresh_data (self) :
"'
All information refresh
"'
If not the self. The get_account_info () :
Return 'false_get_account'
If not the self. Get_ticker () :
Return 'false_get_ticker'
Try:
Self. Get_record_info ()
Except:
Return 'false_get_record_info'
Return 'refresh_data_finish!!!!!! '
The class juncang_class () :
Def __init__ (self, mid_class) :
Self. Jys=mid_class
Self. Last_time=time. Time ()
Self. Last_trade_price=self. Jys. Last
Def make_need_account_info (self) :
Self. Jys. Refresh_data ()
Self. B=self. Jys. Amount
Self. Money=self. Jys. Balance
Now_price=self. Jys. Last
self. Total_money=self. B * now_price + self. Money
Self. Half_money=self. Total_money/2
Self. Need_buy=(self. Half_money - self. B * now_price)/now_price
Self. Need_sell=(self. Half_money - self. Money)/now_price
Def do_juncang (self) :
If self. Need_buy & gt; 0.001:
Self. Jys. Make_order (' Buy 'self. Jys. Low, self. Need_buy)
Elif self. Need_sell & gt; 0.001:
Self. Jys. Make_order (' Sell 'self. Jys. High, the self. The need_buy)
Def if_need_trade (self, condition, param) :
The if condition=='time:
If time. Time () - self. Last_time & gt; Param:
Self. Do_juncang ()
Self. Last_time=time. Time ()
The if condition=='price:
If self. Jys. Last - self. Last_trade_price & gt; Param:
Self. Do_juncang ()
Self. Jys. Last=self. Last_trade_price
Def the main () :
Test_mid=mid_class (exchange)
The Log (test_mid refresh_data ())
Test_juncang=juncang_class (test_mid)
While (True) :
Test_juncang. Make_need_account_info ()
Test_juncang. If_need_trade (' time ', 1000 * 60)