Class: ChockABlock::AlgoTrader
- Inherits:
-
Object
- Object
- ChockABlock::AlgoTrader
- Includes:
- Errors
- Defined in:
- lib/chock_a_block/algo_trader.rb
Constant Summary collapse
- TOTAL_AMOUNT =
The total amount of stocks to buy
100_000
Instance Method Summary collapse
- #basic_strategy ⇒ Object
-
#cancel_orders_and_reset_bidding_price(orders) ⇒ Object
cancel all orders after certain ticks to avoid pushing the market into the higher price.
-
#initialize(api_key, account, venue, symbol) ⇒ AlgoTrader
constructor
A new instance of AlgoTrader.
-
#update_status(orders) ⇒ Object
calculate the sum of currently bought stocks from orders and print out the current progress.
Constructor Details
#initialize(api_key, account, venue, symbol) ⇒ AlgoTrader
Returns a new instance of AlgoTrader.
8 9 10 11 12 13 |
# File 'lib/chock_a_block/algo_trader.rb', line 8 def initialize(api_key, account, venue, symbol) @currntly_bought = 0 @last_bidding_price = Float::INFINITY @stock = Stock.new(venue, symbol) @broker = Broker.new(api_key, account, venue, symbol) end |
Instance Method Details
#basic_strategy ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/chock_a_block/algo_trader.rb', line 15 def basic_strategy # Use a ticker to reset orders every 5 times # to avoid sticking with very low bidding price ticker = 0 success_ticker = 0 while TOTAL_AMOUNT > @currntly_bought begin # Get best bidding price/qty from stock orderbook bid = @stock.best_bidding # reset every 10_000 attempts to avoid stalling for too long # NOTE: this is not important and the game can be finished without it # NOTE: but it increases the success rate of the algorithm ticker += 1 if ticker > 10_000 ticker = 0 orders = @broker.get_orders update_status(orders) cancel_orders_and_reset_bidding_price(orders) end # Place order if price market price is reasonable # and the current bidding price offered is lower than our last price next if @last_bidding_price < bid['price'] @broker.place_order(bid['price'] + 1, bid['qty']) @last_bidding_price = bid['price'] sleep 2 # Get Status about current orders count orders = @broker.get_orders update_status(orders) # reset every 10 successful attempts to avoid flooding bidding size # NOTE: this is not important and the game can be finished without it # NOTE: but it increases the success rate of the algorithm success_ticker += 1 cancel_orders_and_reset_bidding_price(orders) if success_ticker % 10 == 0 rescue ChockABlock::OrderError => e p 'Something went wrong while placing order' p e. p 'Retrying' retry rescue ChockABlock::StockNotFoundError, ChockABlock::VenueNotFoundError => e # when the game ends the Venue and Stock are removed so # any request done after that return p 'Sorry, The game ended before we finish, :(' p '--------------------------------------------------------' return end end p 'Congratulations, We successfully finished :D' p '--------------------------------------------------------' end |
#cancel_orders_and_reset_bidding_price(orders) ⇒ Object
cancel all orders after certain ticks to avoid pushing the market into the higher price. and set the last bidding price high to avoid stucking with a very low bidding price.
82 83 84 85 |
# File 'lib/chock_a_block/algo_trader.rb', line 82 def cancel_orders_and_reset_bidding_price(orders) @last_bidding_price = Float::INFINITY orders.each{|order| @broker.cancel_order(order['id'])} end |
#update_status(orders) ⇒ Object
calculate the sum of currently bought stocks from orders and print out the current progress
72 73 74 75 76 |
# File 'lib/chock_a_block/algo_trader.rb', line 72 def update_status(orders) @currntly_bought = orders.map{|order| order['totalFilled']}.inject :+ # print progress p "Progress: #{(@currntly_bought.to_f / TOTAL_AMOUNT.to_f * 100.0).round(1)}%" end |