I'm in the middle of creating a small lua program. In the program I want to calculate the percentage of hits (numPercent) from the number of hits (numHits) and the number of misses (numMiss).
For instance, if I were to hit the target 5 times and missed 0, it would show the percentage as 100% Hits
How would I formulate this problem?
This is what I got so far, which as you can see is completely incorrect.
if ( numHit > numMiss) then --calculates percentage
numPercent = numHit / numMiss * 100/2
else
numPercent = numMiss / numHit * 100/2
end
Could I get some guidance in formulating it correctly?
numPercent = 100 * numHit / (numHit + numMiss)
You may need to test if "numHit + numMiss" is zero before that, and return whatever you want in that case.
Related
I'm currently trying to learn Swift for iOS app development. I'm fairly new to programming in general but do have some experience in other languages
I've just learnt about the modulo operator, or the remainder operator depending on what you call it. (%)
It should return the remainder of an equation, right?
var equation = 500 % 30
When I print equation, it writes '20' but I can't figure out why? 30 does not fit into 500 20 times, it's 16.6. The / operator would return 16 so I expected % to return whatever the remainder is?
Please tell me if I'm being stupid but I can't figure it out.
a % b does not determine how many times b fits into a. The modulo operator determines what remains when you subtract the largest multiple of b that is smaller than a from a. In this case, the largest multiple of 30 that is smaller than 500 is 480, and 500 - 480 = 20, so 500 % 30 = 20.
I feel stupid. Sorry.
480 is the highest number 30 can perfectly fit into, which leaves 20 remaining.
I am a little bit new in this language, but I have the basics.
What I want: open a position with stop loss and take profit.
I want to place an order with 100 euro, and I want to set the stop loss to 10 euros, and set the take profit to 5 euros. But as I see the OrderSend method requires lots for placing the order, and levels for stop loss and take profit.
And my problem is: how to calculate these values based on the euro amounts I want to set?
I searched for some lot-pip-etc calculation on the web, but after all what I tried did not work. This is how I wanted to calculate:
double AmountToTradeInEuro = 100;
double AmountToTakeInEuro = 5;
double AmountToMaxLossInEuro = 10;
double Lots = AmountToTradeInEuro / MarketInfo(Symbol(), MODE_TICKVALUE);
double StopLossLevel = AmountToTakeInEuro / MarketInfo(Symbol(), MODE_TICKVALUE);
double TakeProfitLevel = AmountToMaxLossInEuro / MarketInfo(Symbol(), MODE_TICKVALUE);
OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, StopLossLevel, TakeProfitLevel);
Basically I would like to know how to calculate the Lot for 100 euro and how to calculate the Levels for stoploss and takeprofit.
And are the stoploss and takeprofit levels also lots? Or are they different units?
Welcome to MQL4!
First question is about the account currency - if it is USD (or something else not EUR) - you have to make such convertion. Ok, let me describe what to do with the EUR balance.
You can compute lot size based on the stoploss - in such a case you can get tick value using MarketInfo(_Symbol,MODE_TICKVALUE). But you must know the price level where to exit (stoploss), whether it is 1 pips or 100 pips. Let us think that is 100 ticks (which is equal to 10 pips of 5-digit broker). Then, your lot size is
double lot = AmountToMaxLoss / (MarketInfor(_Symbol, MODE_TICKVALUE) * stoploss), then you have to normalize the result:
double lot_step=MarketInfo(_Symbol, MODE_LOTSTEP);
double result = lot_step * NormalizeDouble(lot / lot_step, 0);
then check that result > MarketInfo(_Symbol, MODE_MINLOT).
About takeprofit - it might be a strange approach to wait for your takeprofit target in the currency instead of the price level but if you need - the way is same.
im new to Hyperion and i have a Problem with some Data.
I do not want to cumulatively calculate data instead I would like to use the differences of the two consecutive values to make evaluations.
Example:
Start: 100
200
300
The result should be 200 and not 600 is this possible?
And if yes how?
Thanks!
My Hyperion Version: 11.1.2.0000
It sounds like this is what you're looking for:
Value Difference strDifference Output strOutput
100 0 0 0 0
200 100 200-100 = 100 100 0+100 = 100
300 100 300-200 = 100 200 100+100 = 200
150 -150 150-300 = -150 50 200+(-150) = 50
So, the formula for the column Difference is:
if(Prior(Difference)==null) {0}
else {Value-Prior(Value)}
And, the formula for the column Output is:
if(Prior(Difference)==null) {0}
else {Prior(Difference)+Difference}
Unless you want the total to be the Output in which case it's more simple:
Sum(Difference)
Sort order matters, obviously.
This is highly inefficient; if your dataset is large, Hyperion will take a long time to process the section, if it finishes at all.
Ok, here it goes another Euler problem question.
I've started to learn Lua by solving Euler project problems and got stuck on Euler problem 12.
It looks to me very straightforward and I don't understand why is my result incorrect?
Here is my solution so far:
-- return triangular number of the specified number
function get_tri_num(num)
local n = 0
for i=1, num do
n = n + i
end
return n
end
-- return all factors of the specifeid number
function factors(num)
local factors = {}
for i=1, num/2 do
if num%i == 0 then
factors[#factors+1] = i
end
end
factors[#factors+1] = num
return factors
end
-- get the first triangle number with >500 divisors
function euler12()
local n = 0
local trinum = 1
while true do
n = n + 7
trinum = get_tri_num(n)
if #factors(trinum) > 500 then break end
end
print(trinum, n)
end
euler12()
This problem is computation intensive, well, at least the way I am solving it, so I use luajit.
time luajit euler12.lua
103672800 14399
real 3m14.971s
user 3m15.033s
sys 0m0.000s
First, I try this solution on the toy example provided in the problem description. Changing the line of euler12() to if #factors(trinum) > 5 then break end, I get:
28 7
Which corresponds to the results shown in the problem example.
Second, after I see that the toy example is working I run euler12() with >500 condition. According to my solution the answer is 103672800 and yes, if I separately check the number of divisors for this result is >500:
print(#factors(103672800))
648
But...
The problem is here:
while true do
n = n + 7
Why does n increaments 7 each time? That doesn't make sense, change it to 1, and you could get the correct answer.
However, the performance is still poor. Several places that could be improved:
Every time the function get_tri_num is called, it's calculating
from scratch, that's not necessary.
You don't need the factors of a number, you only need the number of
factors of a number, so why return a table in factors?
for i=1, num/2 do is not necessary. Iterating to the square root of
num is enough to get the number of factors.
Refer to my code for the same problem.
I have a panel data set for which I would like to calculate moving averages across years.
Each year is a variable for which there is an observation for each state, and I would like to create a new variable for the average of every three year period.
For example:
P1947=rmean(v1943 v1944 v1945), P1947=rmean(v1944 v1945 v1946)
I figured I should use a foreach loop with the egen command, but I'm not sure about how I should refer to the different variables within the loop.
I'd appreciate any guidance!
This data structure is quite unfit for purpose. Assuming an identifier id you need to reshape, e.g.
reshape long v, i(id) j(year)
tsset id year
Then a moving average is easy. Use tssmooth or just generate, e.g.
gen mave = (L.v + v + F.v)/3
or (better)
gen mave = 0.25 * L.v + 0.5 * v + 0.25 * F.v
More on why your data structure is quite unfit: Not only would calculation of a moving average need a loop (not necessarily involving egen), but you would be creating several new extra variables. Using those in any subsequent analysis would be somewhere between awkward and impossible.
EDIT I'll give a sample loop, while not moving from my stance that it is poor technique. I don't see a reason behind your naming convention whereby P1947 is a mean for 1943-1945; I assume that's just a typo. Let's suppose that we have data for 1913-2012. For means of 3 years, we lose one year at each end.
forval j = 1914/2011 {
local i = `j' - 1
local k = `j' + 1
gen P`j' = (v`i' + v`j' + v`k') / 3
}
That could be written more concisely, at the expense of a flurry of macros within macros. Using unequal weights is easy, as above. The only reason to use egen is that it doesn't give up if there are missings, which the above will do.
FURTHER EDIT
As a matter of completeness, note that it is easy to handle missings without resorting to egen.
The numerator
(v`i' + v`j' + v`k')
generalises to
(cond(missing(v`i'), 0, v`i') + cond(missing(v`j'), 0, v`j') + cond(missing(v`k'), 0, v`k')
and the denominator
3
generalises to
!missing(v`i') + !missing(v`j') + !missing(v`k')
If all values are missing, this reduces to 0/0, or missing. Otherwise, if any value is missing, we add 0 to the numerator and 0 to the denominator, which is the same as ignoring it. Naturally the code is tolerable as above for averages of 3 years, but either for that case or for averaging over more years, we would replace the lines above by a loop, which is what egen does.
There is a user written program that can do that very easily for you. It is called mvsumm and can be found through findit mvsumm
xtset id time
mvsumm observations, stat(mean) win(t) gen(new_variable) end