rirera 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/rirera +12 -35
  3. data/lib/rirera.rb +108 -35
  4. data/rirera.gemspec +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7973831868ff54f96186e2cd90a24b22cb64c1fd
4
- data.tar.gz: e0e1d7990d241c974d863d9deb71fe23c83ede10
3
+ metadata.gz: f7864353d497ead1533b9e403625700aa480d894
4
+ data.tar.gz: 7169d644d6f4e1cc9ccd3b9981356da04ef2944d
5
5
  SHA512:
6
- metadata.gz: cfbffd518d3b5adccf9bd8037bfe7a2b296ff7fdd827ccf185b3d8c7251aa84382e76f6f90555d94cb9b1e43b4512f95877d7a5aeabce517c009fc8547e27955
7
- data.tar.gz: d9f8260888e6a7cbb459e689d22ef184fb55baa948d0be08277277ecb81cbfd4488f9ef01d343815c6cc933eb5f219dfabc77e6a58f0c8f71b9881f1458f40ae
6
+ metadata.gz: 31d521763cebfa87463d3fac829d9d9032fe5794d359c59ba55c30d3220124120af0c31b0242b71f71f445a4ff3fab099467f510d6a483c59cd20694c75cb78e
7
+ data.tar.gz: 7abca528b85ffe87e200f266dcd37a6e4e53832b0c27db05a27439c0df3c760ca53b4328d40ac0a7461a06b4eac0bafd65b633f5de44989ab5ba7fc38268c8cc
data/bin/rirera CHANGED
@@ -1,40 +1,17 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'rirera'
4
- require 'yaml'
5
4
 
6
- obj = RRR.new(ARGV[0])
7
- conf = YAML.load_file(File.join(File.expand_path("..", File.dirname(__FILE__)),"conf/rirera.yml"))
8
- broker = obj.get_broker(ARGV[0], conf) || usage
9
-
10
- system "clear"
11
- print "Volume: "
12
- volume = $stdin.gets.chomp.to_f
13
-
14
- while true do
15
- print "Target price: "
16
- target_price = $stdin.gets.chomp.gsub(",",".").to_f
17
- print "Price: "
18
- actual_price = $stdin.gets.chomp.gsub(",",".").to_f
19
- print "Stop Loss: "
20
- stop_loss = $stdin.gets.chomp.gsub(",",".").to_f
21
-
22
- obj.test_input(actual_price, stop_loss)
23
-
24
- chance = target_price - actual_price
25
- risk = actual_price - stop_loss
26
- rrr = (chance/risk).round(1)
27
- amount = (volume/actual_price).round
28
-
29
- total_commission = obj.get_total_commission(amount, actual_price, target_price, conf, broker)
30
- max_gain = ((amount*target_price) - volume - total_commission).round(2)
31
- loss = ((volume-(amount*stop_loss)) + total_commission).round(2)
32
- break_even_price = obj.get_break_even(total_commission, volume, amount)
33
-
34
- obj.print_rrr(rrr)
35
-
36
- obj.print_result(volume, amount, max_gain, loss, total_commission, break_even_price)
37
-
38
- exit if $stdin.gets.chomp == "n"
39
- system "clear"
5
+ begin
6
+ while true do
7
+ system "clear"
8
+ rrr = Rirera.new(ARGV[0])
9
+ rrr.print_result
10
+ puts ""
11
+ puts "Again? (y/n)"
12
+ exit if $stdin.gets.chomp == "n"
13
+ end
14
+ rescue Interrupt => e
15
+ puts ""
16
+ puts "Exit..."
40
17
  end
data/lib/rirera.rb CHANGED
@@ -1,24 +1,26 @@
1
- # # # # # # #
2
- # Class RRR #
3
- # # # # # # #
4
1
  require 'colorize'
2
+ require 'yaml'
3
+
4
+ class Rirera
5
+ attr_accessor :volume, :target, :actual, :stop
5
6
 
6
- class RRR
7
7
  def initialize(broker)
8
- @broker = broker
8
+ print_title
9
+ @conf = load_config
10
+ @broker = get_broker(broker)
11
+ @volume, @target, @actual, @stop = input
9
12
  end
10
13
 
11
14
  def usage
12
- puts "Usage:"
13
- puts "./rirera broker_name"
14
- exit
15
+ "Usage: ./rirera broker_name"
15
16
  end
16
17
 
17
- def get_broker(broker, conf)
18
- unless conf['broker'][@broker].nil?
19
- @broker
18
+ def get_broker(broker)
19
+ unless @conf['broker'][broker].nil?
20
+ broker
20
21
  else
21
- abort "Not a valid broker"
22
+ puts "Not a valid broker"
23
+ abort usage
22
24
  end
23
25
  end
24
26
 
@@ -28,54 +30,125 @@ class RRR
28
30
  end
29
31
  end
30
32
 
31
- def print_rrr(rrr)
33
+ def print_title
34
+ puts "€$€$€$€$€$€$€$€$€$€$€"
35
+ puts "€ Risk Reward Ratio €"
36
+ puts "€$€$€$€$€$€$€$€$€$€$€"
37
+ puts "| / "
38
+ puts "| _ /\\_ / "
39
+ puts "| / \\/ \\/ "
40
+ puts "| /\\/ "
41
+ puts "|_/ "
42
+ puts "L___________________ "
43
+ puts ""
44
+ sleep 1
45
+ system "clear"
46
+ end
47
+
48
+ def print_rrr
32
49
  decorate
33
50
  print "\n# RRR: "
34
- print "#{rrr}".yellow
51
+ print "#{get_rrr}".yellow
35
52
  puts " #"
36
53
  decorate
37
54
  end
38
55
 
39
- def print_result(volume, amount, max_gain, loss, total_commission, break_even_price)
56
+ def print_result
57
+ print_rrr
40
58
  puts ""
41
59
  puts ""
42
- puts "Volume: #{volume}"
43
- puts "Amount: #{amount}"
44
- puts "Gain: #{max_gain}".green
45
- puts "Loss: #{loss}".red
46
- puts "Commission: #{total_commission}".magenta
47
- puts "Break Even: #{break_even_price}".blue
48
- puts ""
49
- puts "Again? (y/n)"
60
+ puts "Volume: #{@volume}"
61
+ puts "Amount: #{get_amount}"
62
+ puts "Gain: #{get_max_gain}".green
63
+ puts "Loss: #{get_loss}".red
64
+ puts "Commission: #{get_total_commission}".magenta
65
+ puts "Break Even: #{get_break_even}".blue
66
+ end
67
+
68
+ def load_config
69
+ YAML.load_file(File.join(File.expand_path("..", File.dirname(__FILE__)),"conf/rirera.yml"))
50
70
  end
51
71
 
52
72
  def test_input(actual_price, stop_loss)
53
- abort "Stop Loss has to be lower than Price" unless stop_loss < actual_price
73
+ if stop_loss > actual_price
74
+ puts "Stop Loss has to be lower than Price"
75
+ false
76
+ else
77
+ true
78
+ end
79
+ end
80
+
81
+ def sanity_check(num)
82
+ # only allow int and float
83
+ unless is_numeric?(num)
84
+ puts "Wrong input"
85
+ abort
86
+ else
87
+ num.to_f
88
+ end
54
89
  end
55
90
 
56
- def get_total_commission(amount, actual_price, target_price, conf, broker)
91
+ def is_numeric?(s)
92
+ !!Float(s) rescue false
93
+ end
94
+
95
+ def get_rrr
96
+ chance = @target - @actual
97
+ risk = @actual - @stop
98
+ (chance/risk).round(1)
99
+ end
100
+
101
+ def get_total_commission
57
102
  # calculate order pricing
58
- basic_price = conf['broker'][broker]['basic_price']
59
- commission_rate = conf['broker'][broker]['commission_rate']
60
- volume_rate_buy = (amount * actual_price * commission_rate).round(2)
61
- volume_rate_sell = (amount * target_price * commission_rate).round(2)
103
+ basic_price = @conf['broker'][@broker]['basic_price']
104
+ commission_rate = @conf['broker'][@broker]['commission_rate']
105
+ volume_rate_buy = (get_amount * @actual * commission_rate).round(2)
106
+ volume_rate_sell = (get_amount * @target * commission_rate).round(2)
62
107
  total_commission = 0.0
63
108
 
64
109
  [volume_rate_buy, volume_rate_sell].each do |vr|
65
- if (vr + basic_price) > conf['broker'][broker]['min_rate']
66
- if (vr + basic_price) > conf['broker'][broker]['max_rate']
67
- total_commission += conf['broker'][broker]['max_rate']
110
+ if (vr + basic_price) > @conf['broker'][@broker]['min_rate']
111
+ if (vr + basic_price) > @conf['broker'][@broker]['max_rate']
112
+ total_commission += @conf['broker'][@broker]['max_rate']
68
113
  else
69
114
  total_commission += (basic_price + vr)
70
115
  end
71
116
  else
72
- total_commission += conf['broker'][broker]['min_rate']
117
+ total_commission += @conf['broker'][@broker]['min_rate']
73
118
  end
74
119
  end
75
120
  total_commission.round(2)
76
121
  end
77
122
 
78
- def get_break_even(total_commission, volume, amount)
79
- ((volume + total_commission) / amount).round(2)
123
+ def get_amount
124
+ (@volume/@actual).round
125
+ end
126
+
127
+ def get_max_gain
128
+ ((get_amount * @target) - @volume - get_total_commission).round(2)
129
+ end
130
+
131
+ def get_loss
132
+ ((@volume-(get_amount*@stop)) + get_total_commission).round(2)
133
+ end
134
+
135
+ def get_break_even
136
+ ((@volume + get_total_commission) / get_amount).round(2)
137
+ end
138
+
139
+ def input
140
+ print "Volume: "
141
+ volume = sanity_check($stdin.gets.chomp)
142
+ print "Target price: "
143
+ target = sanity_check($stdin.gets.chomp.gsub(",","."))
144
+ print "Price: "
145
+ actual = sanity_check($stdin.gets.chomp.gsub(",","."))
146
+ stop = 0.0
147
+ loop do
148
+ print "Stop Loss: "
149
+ stop = sanity_check($stdin.gets.chomp.gsub(",","."))
150
+ break if test_input(actual.to_f, stop.to_f)
151
+ end
152
+ [volume, target, actual, stop]
80
153
  end
81
154
  end
data/rirera.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rirera'
3
- s.version = '0.1.2'
3
+ s.version = '0.1.3'
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
  s.summary = "Risk Reward Ratio"
6
6
  s.description = "A simple Risk Reward Ratio calculator"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rirera
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maximilian Meister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-08 00:00:00.000000000 Z
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize