Module: Coinbot::OrderBook::WeightedAvg
- Defined in:
- lib/coinbot/order_book/weighted_avg.rb
Class Method Summary collapse
-
.help ⇒ Object
Display Usage for this Module.
-
.status(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::OrderBook::WeightedAvg.status( order_book: 'required - order_book data structure' ).
Class Method Details
.help ⇒ Object
Display Usage for this Module
148 149 150 151 152 153 154 |
# File 'lib/coinbot/order_book/weighted_avg.rb', line 148 public_class_method def self.help puts "USAGE: weighted_avg_indicator_hash = #{self}.status( order_book: 'required - order_book data structure' ) " end |
.status(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::OrderBook::WeightedAvg.status(
order_book: 'required - order_book data structure'
)
13 14 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/coinbot/order_book/weighted_avg.rb', line 13 public_class_method def self.status(opts = {}) order_book = opts[:order_book] order_history = order_book[:order_history] indicator_status = opts[:indicator_status] invested = opts[:invested] indicator_hash = {} indicator_hash[:invested] = invested buy_orders_done = order_history.select do |orders| orders[:side] == 'buy' && orders[:status] == 'done' && orders[:done_reason] == 'filled' end total_buy_orders_done = buy_orders_done.length avg_buy_exec = format('%0.7f', 0.00).to_f # total_buy_executed = 0.00 # total_invested = 0.00 # buy_fees = 0.00 if total_buy_orders_done.positive? # Calculate Weighted Avg. of Completed Buy Orders weighted_buy_prices_arr = [] buy_orders_done.each do |order| weighted_hash = {} executed_value = order[:executed_value].to_f filled_size = order[:filled_size].to_f weighted_hash[:price] = (executed_value / filled_size).to_f weighted_hash[:filled_size] = filled_size weighted_hash[:price_times_filled_size] = weighted_hash[:price] * filled_size weighted_buy_prices_arr.push(weighted_hash) end price_times_filled_size = weighted_buy_prices_arr.sum do |orders| orders[:price_times_filled_size].to_f end this_filled_size = weighted_buy_prices_arr.sum do |orders| orders[:filled_size] end calc_avg_buy_exec = price_times_filled_size / this_filled_size avg_buy_exec = format('%0.7f', calc_avg_buy_exec).to_f executed_value = buy_orders_done.sum do |orders| orders[:executed_value].to_f end # total_buy_executed = format('%0.7f', executed_value).to_f # fill_fees = buy_orders_done.sum do |orders| # orders[:fill_fees].to_f # end # buy_fees = format('%0.7f', fill_fees).to_f # calc_total_invested = total_buy_executed.to_f - buy_fees.to_f # total_invested = format('%0.7f', calc_total_invested).to_f end order_book[:avg_buy_exec] = avg_buy_exec sell_orders_done = order_history.select do |orders| orders[:side] == 'sell' && orders[:status] == 'done' && orders[:done_reason] == 'filled' end total_sell_orders_done = sell_orders_done.length avg_sell_exec = format('%0.7f', 0.00).to_f # total_sell_executed = format('%0.7f', 0.00).to_f # total_return = format('%0.7f', 0.00).to_f # sell_fees = format('%0.7f', 0.00).to_f if total_sell_orders_done.positive? # Calculate Weighted Avg. of Completed Sell Orders weighted_sell_prices_arr = [] sell_orders_done.each do |order| weighted_hash = {} executed_value = order[:executed_value].to_f filled_size = order[:filled_size].to_f weighted_hash[:price] = (executed_value / filled_size).to_f weighted_hash[:filled_size] = filled_size weighted_hash[:price_times_filled_size] = weighted_hash[:price] * filled_size weighted_sell_prices_arr.push(weighted_hash) end price_times_filled_size = weighted_sell_prices_arr.sum do |orders| orders[:price_times_filled_size].to_f end this_filled_size = weighted_sell_prices_arr.sum do |orders| orders[:filled_size] end calc_avg_sell_exec = price_times_filled_size / this_filled_size avg_sell_exec = format('%0.7f', calc_avg_sell_exec).to_f executed_value = sell_orders_done.sum do |orders| orders[:executed_value].to_f end # total_sell_executed = format('%0.7f', executed_value).to_f # fill_fees = sell_orders_done.sum do |orders| # orders[:fill_fees].to_f # end # sell_fees = format('%0.7f', fill_fees).to_f # calc_total_made = total_sell_executed.to_f - sell_fees.to_f # total_return = format('%0.7f', calc_total_made).to_f end order_book[:avg_sell_exec] = avg_sell_exec if avg_buy_exec < avg_sell_exec indicator_hash[:color] = :green weight_op = '<' elsif avg_buy_exec > avg_sell_exec indicator_hash[:color] = :red weight_op = '>' else indicator_hash[:color] = :yellow weight_op = '==' end weighted_avg_status_out = "VWAPB #{weight_op} VWAPS" order_book[:candles].last[:weighted_avg_status] = weighted_avg_status_out indicator_hash[:status] = weighted_avg_status_out # indicator_hash[:order_book] = order_book indicator_status.weighted_avg = indicator_hash # indicator_hash rescue StandardError => e raise e end |