Stata daily return compounded to monthly - return

I have a panel datast of daily stock returns. For each stock, I need to calculate its compound monthly return (say 30 days):
(1+r_1)*(1+r_2)*...*(1+r_30) - 1
Stock identifier is permno, dm is year and month indicator. I use the following Stata code:
gen gross_ret = 1+ret
bys permno dm: gen prod = sum(ln(gross_ret))
by permno dm: replace prod = exp(prod[_N])
gen mret = prod - 1
I randomly pick permno dm combinations to verify the results, and they seem to be right. However, I do see extreme values such as mret = 26. I guess the reason is that some gross_ret is near 0, so ln(gross_ret) is very high. Then I double check using CRSP monthly return data, I found 99% of differences between compounding return (calculated by the code above) and CRSP monthly return smaller than 0.0007, which is acceptable. But the largest absolute difference is 3.24, which is too big and might affect my final result (I have been trouble shooting for two whole days, and this might be my last resort).
Is my way of calculating monthly return wrong? If so, please suggest a better way.

Perhaps you might achieve better accuracy by avoiding the logarithims, along with using double for intermediate results, as Nick recommends.
bys permno dm: gen double prod = (1+ret) if _n==1
by permno dm: replace prod = (1+ret)*prod[_n-1] if _n>1
by permno dm: gen mret = prod[_N] - 1
With that said, if you have a daily date variable (either year-month-day or just day), you could include it in the sort so your data is in the order you expect it after this process.
bys permno dm (day): gen double prod = (1+ret) if _n==1
by permno dm: replace prod = (1+ret)*prod[_n-1] if _n>1
by permno dm: gen mret = prod[_N] - 1

Related

why mql4 show error 130 when we use Stoploss in OrderSend function

I am trying to create a EA in mql4, but in OrderSend function, when i use some value instead of zero it show ordersend error 130. Please help to solve this problem
Code line is
int order = OrderSend("XAUUSD",OP_SELL,0.01,Bid,3,Bid+20*0.01,tp,"",0,0,Red);
error number 130 means Invalid stops.
so that means there is a problem with the stops you set with the ordersend function.
I suggest you set it like that:
int order = OrderSend("XAUUSD",OP_SELL,0.01,Bid,3,Bid+20*Point,tp,"",0,0,Red);
so you could use Point instead of hard coding it.
and to check what is the error number means. I think you could refer to: https://book.mql4.com/appendix/errors
You should know that there exists a minimum Stop Loss Size (mSLS) given in pips. "mSLS" changes with the currency and broker. So, you need to put in the OnInit() procedure of your EA a variable to get it:
int mSLS = MarketInfo(symbol,MODE_STOPLEVEL);
The distance (in pips) from your Order Open Price (OOP) and the Stop-Loss Price (SLP) can not be smaller than mSLS value.
I will try to explain a general algorithm I use for opening orders in my EAs, and then apply the constrain on Stop-Loss level (at step 3):
Step 1. I introduce a flag (f) for the type of operation I will open, being:
f = 1 for Buy, and
f = -1 for Sell
You know that there are mql4 constants OP_SELL=1 and OP_BUY=0 (https://docs.mql4.com/constants/tradingconstants/orderproperties).
Once I have defined f, I set my operation type variable to
int OP_TYPE = int(0.5((1+f)*OP_BUY+(1-f)*OP_SELL));
that takes value OP_TYPE=OP_BUY when f=1, while OP_TYPE=OP_SELL when f=-1.
NOTE: Regarding the color of the orders I put them in an array
color COL[2]= {clrBlue,clrRed};
then, having OP_TYPE, I set
color COLOR=COL[OP_TYPE];
Step 2. Similarly, I set the Order Open Price as
double OOP = int(0.5*((1+f)*Ask+(1-f)*Bid));
which takes value OOP=Ask when f=1, while OOP=Bid when f=-1.
Step 3. Then, given my desired Stop Loss in pips (an external POSITIVE parameter of my EA, I named sl) I make sure that sl > SLS. In other words, I check
if (sl <= mSLS) // I set my sl as the minimum allowed
{
sl = 1 + mSLS;
}
Step 4. Then I calculate the Stop-Loss Price of the order as
double SLP = OOP - f * sl * Point;
Step 5. Given my desired Take Profit in pips (an external POSITIVE parameter of my EA, I named tp) I calculate the Take-Profit Price (TPP) of the order as
double TPP = OOP + f * tp * Point;
OBSERVATION: I can not affirm, but, according to mql4 documentation, the minimum distance rule between the stop-loss limit prices and the open price also applies to the take profit limit price. In this case, a "tp" check-up needs to be done, similar to that of the sl check-up, above. that is, before calculating TPP it must be executed the control lines below
if (tp <= mSLS) // I set my tp as the minimum allowed
{
tp = 1 + mSLS;
}
Step 5. I call for order opening with a given lot size (ls) and slippage (slip) on the operating currency pair (from where I get the Ask and Bid values)
float ls = 0.01;
int slip = 3; //(pips)
int order = OrderSend(Symbol(),OP_TYPE,ls,OOP,slip,SLP,TPP,"",0,0,COLOR);
Note that with these few lines it is easy to build a function that opens orders of any type under your command, in any currency pair you are operating, without receiving error message 130, passing to the function only 3 parameters: f, sl and tp.
It is worth including in the test phase of your EA a warning when the sl is corrected for being less than the allowed, this will allow you to increase its value so that it does not violate the stop-loss minimum value rule, while you have more control about the risk of its operations. Remember that the "sl" parameter defines how much you will lose if the order fails because the asset price ended up varying too much in the opposite direction to what was expected.
I hope I could help!
Whilst the other two answers are not necessarily wrong (and I will not go over the ground they have already covered), for completeness of answers, they fail to mention that for some brokers (specifically ECN brokers) you must open your order first, without setting a stop loss or take profit. Once the order is opened, use OrderModify() to set you stop loss and/or take profit.

Finding the last digit of certain number raised to any power

I'm trying to find the last digit of the result of any number raised to any power, using binomial theorem, not modulus or something. Please explain me why last digit of a number's unit number raised to a power is same as the original number raised to the same power using binomial theorem.
Ex. XV^Y = V^Y
Also, I found out that each integer each its cyclicity and I understand that. But I'm confused since:
17^8 = 7^8 = 7^4 since 8 is a multiple of 4.
But why not 7^2 = 7^8 as well? 8 is also a multiple of 2.
It's because of the last digit that you are raising to a power several times and not about the power.
7^1=...7 <=
7^2=...9
7^3=...3
7^4=...1
7^5=...7 <=
7^6=...9
7^7=...3
7^8=...1
7^9=...7 <=
Say you have a number x=t*10+u, where t is the "tens" and u is the units, so e.g. 1234=123*10+4. The binomial theorem states: x^n = sum{k=0,...,n} (t*10)^(n-k)*u^k. As long as (n-k)>0, the summand will be a multiple of 10. You should be able to figure it out from there.

Having trouble with "simple math"

I have an assignment where ?I need to take a gross pay that is paid in cash and display how many 100s,50s,20s,10s,5s,1s the employee is paid. I'm sure its just my math that is not working but here is my code
COMPUTE W02-PAY-100S = W02-GROSS-PAY / 100
COMPUTE PAY = W02-GROSS-PAY - (W02-PAY-100S * 100)
COMPUTE W02-PAY-50S = PAY / 50
COMPUTE PAY = PAY - (W02-PAY-50S * 50)
COMPUTE W02-PAY-20S = PAY / 20
COMPUTE PAY = PAY - (W02-PAY-20S * 20)
COMPUTE W02-PAY-10S = PAY / 10
COMPUTE PAY = PAY - (W02-PAY-10S * 10)
COMPUTE W02-PAY-5S = PAY / 5
COMPUTE PAY = PAY - (W02-PAY-5S * 5)
COMPUTE W02-PAY-1S = PAY / 1
.
What ends up happening is the first 100s are displayed as 3, then everything else is 0's.
I'd suspect PAY is defined as PIC 9 rather than PIC 9(7) (or whatever), or that there is some problem with the definitions of the other fields you are using (like no integer parts).
However, you're going about it the "wrong" way for COBOL, by making use of the fact that COMPUTE is like an assignment statement in other languages.
Instead COBOL offers more nuance with verbs for maths. Have a look at DIVIDE with GIVING and REMAINDER.
DIVIDE W02-GROSS-PAY BY 100
GIVING NOTES-100
REMAINDER W-RUNNING-BALANCE-LESS-100S
DIVIDE W-RUNNING-BALANCE-LESS-100S
BY 50
GIVING NOTES-50
REMAINDER W-RUNNING-BALANCE-LESS-50S
DIVIDE W-RUNNING-BALANCE-LESS-50S
BY 20
GIVING NOTES-20
REMAINDER W-RUNNING-BALANCE-LESS-20S
DIVIDE W-RUNNING-BALANCE-LESS-20S
BY 10
GIVING NOTES-10
REMAINDER W-RUNNING-BALANCE-LESS-10S
DIVIDE W-RUNNING-BALANCE-LESS-10S
BY 05
GIVING NOTES-05
REMAINDER COINS-01
If you get your COMPUTEs working, you don't need the last one (divide by one gives you as the answer the original number). With the COMPUTEs you are doing a lot more work than necessary in COBOL.
Now that you know about DIVIDE, also look at MULTIPLY, ADD and SUBTRACT.
To complete the picture, look at the intrinsic functions like INTEGER and MOD. The MOD and more, you have above. The INTEGER is unnecessary because a simple MOVE to a field without a fractional part will get there. A MOVE to a field without an integer part will get you the fraction.
Don't, necessarily, look for something which allows you to do it as you would in another language. You may "pass" your COBOL module that way, but you won't really learn COBOL.

seed random numbers Objective-C

I'm working on a game in Xcode 6 and need to generate a new random number each time 2 specific objects touch each other. I have tried using srand() at the start of my application but it seems that the values remain the same as if it isn't seeding a new value each time the objects collide.
here is the code
if((CGRectIntersectsRect(Stickman.frame, Box1.frame))) {
xRan = arc4random()%11;
if(xRan<=3){
Spike1 = true;
[self SpikeCall];
}
//Gold
if (xRan==10) {
G1 = true;
}
Box1.center = CGPointMake(0,278);
Box1SideMovement = 5;
}
The problem is that after the Stickman hits the Box1 when it comes back on screen it still holds the same value in xRan except for certain scenarios where it will between 1-3 then it makes Spike1 true. I'd like it to be so that each time the object Box1 intersects with Stickman the xRan seeds a new number between 1-10 so that there is a 1 in 10 chance of G1 becoming true & if xRan is 1-3 it will make Spike1 true.
This is more of a comment than an answer, but it's too long for a comment.
There are a couple of problems with your approach here. First, srand does not seed the arc4random function. It seeds the rand function, which is a different pseudo-random number generator with somewhat worse properties than arc4random. There is no explicit seeding function for arc4random. Second, if you want a random number between 1 and 10 you should not use the % 11 approach. That gives you a random number between 0 and 10 (and I think you don't want zero), but also it probably does not give you a uniform distribution. Even if arc4random is good at providing a uniform distribution in its whole range it may not provide a uniform distribution of the least significant bits.
I suggest you use something like:
arc4random_uniform(10) + 1
arc4random_uniform(10) will return a number between 0 and 9, and will do a good job of providing a uniform distribution in that range. The +1 just shifts your interval so you get numbers between 1 and 10 instead of between 0 and 9.

How can I specify specific comparisons in the Lifetest Procedure in SAS?

I would like to make sure I have properly adjusted for multiple testing, which I am doing through permutation. I do not want to over-adjust, so I only want to include comparisons that were specified a priori. Here is a dataset that is similar in construction to the one I am analyzing:
data test;
call streaminit(1234);
do a=1 to 5;
do b=1 to 2;
do i = 1 to 8;
censored = rand('BERNOULLI',0.5);
if censored = 0 then ttd = rand('NORMAL',10);
else ttd = 30;
id = 16*(A-1)+8*(B-1)+i;
output;
end;
end;
end;
drop i;
run;
If I were interested in comparing each level of a and b to every other level, I would run the following:
proc lifetest data=test;
time ttd * censored(1);
strata a b / diff=all adjust=simulate(nsamp=1000000 seed=1234);
run;
Instead, I would like to compare each level of a to every other level of a within b (10 comparisons at 2 levels makes 20 comparisons). On top of that, I would like to compare the two levels of b within each level of a (5 comparisons). So in all that is 25 comparisons I have to adjust for, but adjusting for all pairwise comparisons as I have done in the above code accounts for 45 comparisons. Is there a way in proc lifetest to just specify the 25 pairwise comparisons I am interested in? If not, is there some way around this so I can adjust post hoc just for the 25 comparisons I am interested in?
While this is a very statistics-related question, the primary question is about problems specific to programming in SAS and not about which methods to use or the appropriateness of the chosen method, so I am posting it here and not on Cross Validated, though I admit that a post hoc adjustment outside of proc lifetest may be the way to go and so would be willing to let Cross Validated have a shot at it as well.

Resources