Dear All,
I am trying to write a programme to automatize the trading between two assets. At a certain point the programme is suppose to generate a new variable, "position", which will be different, depending on an option being specified.
Specifically:
[CODE]
capture program drop trading
program define trading, rclass
version 15
syntax varlist(min=2 max=2 numeric) [if], [stop(numlist int max=1)]
tokenize `varlist'
local var1="`1'"
local var2="`2'"
if "`stop'" != "." {
tokenize `stop'
local S="`1'"
}
gen spread = ln(`var1') - ln(`var2')
su spread
gen z_score = (spread - r(mean)) / r(sd)
local long_threshold = 1.96
local short_threshold = -1.96
gen position = .
replace position = 1 if z_score > `long_threshold'
replace position = -1 if z_score < `short_threshold'
if "`stop'" == "." {
replace position = 0 if abs(z_score) < 0.05 | sign(z_score) != sign(z_score[_n-1])
}
else if "`stop'" != "." {
replace position = 0 if abs(z_score) < 0.05 | sign(z_score) != sign(z_score[_n-1]) | `stop' > = `S'
}
end
[\CLOSE]
So if the option stop is not specified, position should be determined by the portion in red. Instead, if the option stop is specified, position should be defined according to the portion of code in green. However I keep receiving an error message, saying that stop is not found. Do you have any suggestion about how to fix this?
Thanks for any help you may provide.
Dario
I am trying to write a programme to automatize the trading between two assets. At a certain point the programme is suppose to generate a new variable, "position", which will be different, depending on an option being specified.
Specifically:
[CODE]
capture program drop trading
program define trading, rclass
version 15
syntax varlist(min=2 max=2 numeric) [if], [stop(numlist int max=1)]
tokenize `varlist'
local var1="`1'"
local var2="`2'"
if "`stop'" != "." {
tokenize `stop'
local S="`1'"
}
gen spread = ln(`var1') - ln(`var2')
su spread
gen z_score = (spread - r(mean)) / r(sd)
local long_threshold = 1.96
local short_threshold = -1.96
gen position = .
replace position = 1 if z_score > `long_threshold'
replace position = -1 if z_score < `short_threshold'
if "`stop'" == "." {
replace position = 0 if abs(z_score) < 0.05 | sign(z_score) != sign(z_score[_n-1])
}
else if "`stop'" != "." {
replace position = 0 if abs(z_score) < 0.05 | sign(z_score) != sign(z_score[_n-1]) | `stop' > = `S'
}
end
[\CLOSE]
So if the option stop is not specified, position should be determined by the portion in red. Instead, if the option stop is specified, position should be defined according to the portion of code in green. However I keep receiving an error message, saying that stop is not found. Do you have any suggestion about how to fix this?
Thanks for any help you may provide.
Dario
Comment