I'd like to modify my exising study so that if the time is between 11:30 and 14:00 (deadzone) that it will pass on taking any trades. Any ideas? Thx!!
Announcement
Collapse
No announcement yet.
Dead Zone Trading
Collapse
X
-
Geoff
Here is one way to do it. First create a variable called (for example) AllowTrading and set it initially to true.
Then create a variable called Time which will return the bar time in an easy to use format
PHP Code:var Time = (hour(0)*100)+minute(0);
//Time returns a value expressed as 930, 1045, 1420, etc
PHP Code:if(Time >=1130 && Time <1400){
AllowTrading = false;
}
Shown below is the complete example. For the purpose of illustrating how this works I have included a command to paint the background in yellow when the conditions do allow trading.
Alex
PHP Code:function main(){
var AllowTrading = true;
var Time = (hour(0)*100)+minute(0);
if(Time >=1130 && Time <1400){
AllowTrading = false;
}
if(AllowTrading){
//your trading code here
setBarBgColor(Color.yellow);//used for example
}
return;
}
-
Skipping DZ Trades
A,
This is what I ended up with (workin!). Its bascially a hack up of a few stuidies. If there is anything nasty that jumps out (besides the overal poor structure layout) then please yell. I'm running it on bar replay now and it seems to be working!!
It was not working at first but I had to use { } around my other if statements inside of if(AllowTrading). Here is the code if anyone wants to snag it:
PHP Code:/*****************************************
Description : 10 EMA/10 MA
Crossover Strategy
Modifications: Will exclude trades in deadzone (1130-2PM)
*****************************************/
var ma = new MAStudy(10, 0, "Close", MAStudy.SIMPLE);
function preMain() {
setPriceStudy(true);
setStudyTitle("DZ No Trading");
setCursorLabelName("MA 10",0);
setDefaultBarFgColor(Color.red,0);
setCursorLabelName("EMA 10 (MA 10)",1);
setDefaultBarFgColor(Color.blue,1);
setColorPriceBars(true);
setDefaultPriceBarColor(Color.grey);
}
var EMA_1 = 0;
function main() {
var K = 2 / (10 + 1);
var EMA = K * ma.getValue(MAStudy.MA) + (1 - K) * EMA_1;
var AllowTrading = true;
var Time = (hour(0)*100)+minute(0);
if(Time >=1130 && Time <1400){
AllowTrading = false; }
if(AllowTrading){
if (getBarState() == BARSTATE_NEWBAR)
EMA_1 = EMA;
if(ma.getValue(MAStudy.MA) > EMA && !Strategy.isLong()) {
Strategy.doLong("Long", Strategy.MARKET, Strategy.THISBAR);
Alert.addToList(getSymbol(), "Skyscraper @ "+close(), Color.RGB(0,0,0), Color.blue); }
if(ma.getValue(MAStudy.MA) < EMA && !Strategy.isShort()) {
Strategy.doShort("Short", Strategy.MARKET, Strategy.THISBAR);
Alert.addToList(getSymbol(), "Midget @ "+close(), Color.RGB(0,0,0), Color.red); }
setBarBgColor(Color.green); } // YELLOW SCARES ME
if(Strategy.isLong())
setPriceBarColor(Color.blue);
if(Strategy.isShort())
setPriceBarColor(Color.red);
return new Array(ma.getValue(MAStudy.MA),EMA);
}
Comment
-
Geoff,
Just wanted to thank you for posting that code. Found it very interesting and actually never ran across that particular moving average idea before. Hope you don't mind my butting in.
I have a program I've been working on which I am attaching in the hope that you may find it useful and maybe save you some time and hope I don't mess you up with the post. BTW I don't take credit for any of it, it was "stolen" from other examples in this forum or documentation
The portions that are relevent here are related to time processing. I actual check for start time and end time, and when the study is loaded or back tested you get prompted and can pass the time periods as parameters which prevents you from having to change the EFS code each time.
I have and end of day time also specified for closing out trades, which can be different from the end time. You may want to initiate a trade before 11 for example and keep it until EOD.
I am going to try, and I'm not a programmer and merge your time check with mine, basically looking to trade during the prime time market hours excluding the lunchtime period.
Hope you find it useful and feel free to improve upon my code enough to make it profitable and give me a shout.
There is also alot of money mgmt code included, profit targets, trailing stops which may or may not be on your agenda, also stolen from here and there which you may find useful.
Thanks again....there are SO FEW pieces of code published out of the thousands posted here that show any profit potential whatsoever...that I had to thank you and clumsiley try to return the favor.
glen [email protected]
ps feel free to share anymore ideas because I could use it.
Attached FilesLast edited by demarcog; 08-28-2006, 03:09 PM.Glen Demarco
[email protected]
Comment
-
Geoff,
I hope someone at esignal has made you aware that MARKET.THISBAR really means OPEN.THISBAR and I've been told to use MARKET.NEXTBAR, which is really the open of the next bar.
That killed the profit on alot of shorter term 1-5 minute systems.
It's a major issue for me, and cannot imagine why this situation has not been corrected.
One more question.
Just curious is the formula you are applying to the SMA to get the EMA different then using the esignal EMA function?
var vEMA10 = new MAStudy(10, 0, "Close", MAStudy.EXPONENTIAL);Last edited by demarcog; 08-28-2006, 05:05 PM.Glen Demarco
[email protected]
Comment
-
If you are working with an intraday strategy back testing WITHOUT LOOK INSIDE BAR is worthless! You are just wasting your time, IMHO.
If you are trading a swing strategy (daily/weeky) then you can use 1 minute bars.
Most people don't understand how to backtest.
Originally posted by demarcog
Geoff,
I hope someone at esignal has made you aware that MARKET.THISBAR really means OPEN.THISBAR and I've been told to use MARKET.NEXTBAR, which is really the open of the next bar.
That killed the profit on alot of shorter term 1-5 minute systems.
It's a major issue for me, and cannot imagine why this situation has not been corrected.
One more question.
Just curious is the formula you are applying to the SMA to get the EMA different then using the esignal EMA function?
var vEMA10 = new MAStudy(10, 0, "Close", MAStudy.EXPONENTIAL);
Comment
-
Originally posted by buzzhorton
If you are working with an intraday strategy back testing WITHOUT LOOK INSIDE BAR is worthless! You are just wasting your time, IMHO.
If you are trading a swing strategy (daily/weeky) then you can use 1 minute bars.
Most people don't understand how to backtest.
If you know a way of doing that in esignal please share.
I agree that backtesting certainly has it's problems and limitations which is why I'm in this post right now trying to resolve.
The irony of you using IMHO I'm sure didn't get past you Avery LOLGlen Demarco
[email protected]
Comment
-
Thanks for the input buzz and demarcog. I hate to admit it but your right. I just started back testing and have no idea what I'm doing. I see huge profits and am getting all giddy. I changed to the NEXTBAR strategy and now most of the studies show negative PNL. Ahh my mood has declined by 5%.
I need to add to more things to that 'no deadzone' trade script. Wanted it to close out any trade that was open at 3:58pm and I wanted it to close or place a stop on any trade that was open going in to deadzone. Just been very busy and haven't had time to look in to that part.
Do you use NEXTBAR or do you compensate by upping the slippage amount in the analyzer settings? I'm thinking about getting some sleep. Ttyl!
Comment
-
Geoff
There are a few errors in the code you posted. If you run the script in Tick Replay you should see the following:
a) during the cutoff time (ie the time in which trades are not allowed) the EMA is not updating correctly (see following image)
This is because the section of code that transfers the value of EMA to EMA_1 at BARSTATE_NEWBAR is enclosed inside the AllowTrading conditional statement hence never executes when AllowTrading is false.
b) during the times in which trading is allowed the EMA is also calculating incorrectly albeit in a less obvious way. To verify this run the script for a while then Pause the Tick Replay and Reload the efs and you will see the plot change. This is because the transfer of values between EMA and EMA_1 occurrs at BARSTATE_NEWBAR after the EMA has updated its current value whereas it should happen before. What this means is that on a new bar the value that gets transferred is the value at the first tick of a new bar instead of the last one of the bar that just closed. The EMA also needs to be declared as a global variable instead of a local variable so that it retains its value during each iteration of the efs.
Also, Strategy.MARKET should be changed to Strategy.CLOSE (or LIMIT or STOP) if the trade is to be executed on the same bar that triggers it.
Strategy.MARKET corresponds to the Open and should be used in conjunction with Strategy.THISBAR only when the conditions are based on the prior bar eg. "IF the Close of the prior bar was above the value of the moving average at the prior bar THEN Buy at Market (ie Open) of This bar".
If instead the conditions are based on the current bar then Strategy.MARKET should be used with Strategy.NEXTBAR eg "IF the Close of this bar is above the value of the moving average on this bar THEN Buy at Market (ie Open) of the Next bar".
Using Strategy.MARKET on the same bar that triggers the trade will return unrealistic results.
Lastly the Strategy Object should be used only for back testing and not for real time. For the latter you should create your own variables with which you track the status of the strategy.
Enclosed below is a revision of your code (complete with comments on the changes made) that corrects the errors described above.
Alex
PHP Code:var ma = new MAStudy(10, 0, "Close", MAStudy.SIMPLE);
function preMain() {
setPriceStudy(true);
setStudyTitle("DZ No Trading");
setCursorLabelName("MA 10",0);
setDefaultBarFgColor(Color.red,0);
setCursorLabelName("EMA 10 (MA 10)",1);
setDefaultBarFgColor(Color.blue,1);
setColorPriceBars(true);
setDefaultPriceBarColor(Color.grey);
}
var EMA = 0;//variable needs to be global
var EMA_1 = 0;
function main() {
var K = 2 / (10 + 1);
//the following needs to happen before current value of EMA is calculated
if (getBarState() == BARSTATE_NEWBAR){//moved from original location
EMA_1 = EMA;//moved from original location
}
EMA = K * ma.getValue(MAStudy.MA) + (1 - K) * EMA_1;//removed var and declared variable globally
var AllowTrading = true;
var Time = (hour(0)*100)+minute(0);
if(Time >=1130 && Time <1400){
AllowTrading = false;
}
if(AllowTrading){
//if (getBarState() == BARSTATE_NEWBAR)//moved to new location
//EMA_1 = EMA;//moved to new location
if(ma.getValue(MAStudy.MA) > EMA && !Strategy.isLong()) {
Strategy.doLong("Long", Strategy.CLOSE, Strategy.THISBAR);//replaced MARKET with CLOSE
Alert.addToList(getSymbol(), "Skyscraper @ "+close(), Color.RGB(0,0,0), Color.blue);
}
if(ma.getValue(MAStudy.MA) < EMA && !Strategy.isShort()) {
Strategy.doShort("Short", Strategy.CLOSE, Strategy.THISBAR);//replaced MARKET with CLOSE
Alert.addToList(getSymbol(), "Midget @ "+close(), Color.RGB(0,0,0), Color.red);
}
setBarBgColor(Color.cyan);
}
if(Strategy.isLong()){
setPriceBarColor(Color.blue);
}
if(Strategy.isShort()){
setPriceBarColor(Color.red);
}
return new Array(ma.getValue(MAStudy.MA),EMA);
}
Comment
-
Hello demarcog/glay,
Originally posted by demarcog
Geoff,
I hope someone at esignal has made you aware that MARKET.THISBAR really means OPEN.THISBAR and I've been told to use MARKET.NEXTBAR, which is really the open of the next bar.
That killed the profit on alot of shorter term 1-5 minute systems.
It's a major issue for me, and cannot imagine why this situation has not been corrected.
Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
Comment
-
Jason,
As always thanks for the valuable assistance and the link to the useful doc, looks like a brand new or recently updated doc.
Last thing I want to do is give you grief about MARKET.THISBAR
as you and Alexis have been invaluable and hope you don't take it personally.Glen Demarco
[email protected]
Comment
Comment