Strategy based on Opening Bar Color
Author: Nikko
Creation Date: 11/26/2014 2:56 PM
profile picture

Nikko

#1
I am just getting started in Wealth Lab. Can anyone help me with a simple script for intraday equity trading?

This is an intraday trading script that goes long if the day's opening bar is green and goes short if the opening bar is red. Entry is not on the opening bar, but upon the close of the first retracement bar after the opening bar. Exit is on a fixed point goal or at the end of the day.


Bar = 15 minutes (this can change)
If the close of the opening bar is greater than the open of the opening bar, go long. Enter on the first subsequent bar after the opening bar that is negative (where that bar's close is less than the open of that bar)

If the close of the opening bar is less than the open of the opening bar, go short. Enter on the first subsequent bar after the opening bar that is positive (where the close is greater than the open of that bar)

Exit with an X point gain or else exit at the end of the day



Thanks for your help! Looking forward to this first step as a way for me to start programming my ideas.
profile picture

Eugene

#2
Welcome to the forums. Here you go:

CODE:
Please log in to see this code.
profile picture

Nikko

#3
Wow that was fast. Thanks a bunch. I will try it now.

profile picture

Nikko

#4
Try it on IWM over the last 2 years.
profile picture

Nikko

#5
It appears that the strategy will open an additional trade during the day if the first one closes on goal. Actually, this should not happen with this rule. Correct me if I am reading the results incorrectly on this.

Also, I get an error message when I open the strategy and change the time scale. "Unhandled exception has occurred in your application." Should this be a concern? The message is below.

Any help you can provide with this is greatly appreciated.


Nikko



************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at WealthLabPro.ChartForm.a(BarDataScale A_0)
at WealthLabPro.ChartForm.w()
at WealthLabPro.ChartForm.RunOnAllSymbols()
at WealthLabPro.MainForm.ChangeScale(BarScale scale, Int32 barInterval)
at WealthLabPro.MainForm.ao(Object A_0, EventArgs A_1)
at WealthLabPro.ScaleSelecter.a(Object A_0, EventArgs A_1)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

profile picture

Eugene

#6
QUOTE:
Also, I get an error message when I open the strategy and change the time scale. "Unhandled exception has occurred in your application."

Could not reproduce. If you're concerned, please provide the missing details to help us reproduce:

1. Single symbol or Multi-symbol mode, Position Sizing, and Data Loading settings
2. Symbol(s) and Data provider
3. Is "Update data on demand" activated in the Data Manager?
4. The complete step by step procedure to reproduce the ecxeption

QUOTE:
Actually, this should not happen

This should happen because you did not state the opposite in your system rules. Here's a tweaked version that stops for the rest of the day after reaching the goal:

CODE:
Please log in to see this code.
profile picture

Nikko

#7
Thanks for the new script. It seems to be working well.

I will work through the questions you sent regarding the error messages and get back to you.

I appreciate all your help.

profile picture

Nikko

#8
Error message seems to happen when I change the time scale but have not yet entered a stock symbol upon opening the program. The other settings you questioned would be default settings the program comes loaded with.

Error message seems to be benign, but I thought I would mention it. Hope this helps.
profile picture

Eugene

#9
Oh, this is not an issue. Just don't do that. ;)
profile picture

Nikko

#10
If a goal is not met during the day, exactly what time will the position close for an EOD "End of Day" exit? (I do not want the possibility of incurring overnight margin interest which will kick in after the extended session closes at 8PM EST with Fidelity.) Thanks.
profile picture

Nikko

#11
I have been running this script live for a few days. Today it did not hit the goal and so should have exited at end of day with a market order. It produced and EOD order and an EOD signal. But no trade. Order status is "Active". I waited until 8 PM EST until extended hours ended but still no trade. Can you check the code or is there a setting that is wrong somewhere else in the platform?

Automatically stage orders from strategy alerts is checked.
Platform is connected to Fidelity, as the entry trade was executed. Just not the exit at EOD.

Thanks for your help.



profile picture

Eugene

#12
If these 3 conditions are all true:

+ the trade's exit signal was marked "EOD Alert" (as opposed to "EOD" which won't produce a tradeable alert)
+ "Auto-Trading" option is enabled in the Orders tool for your auto-staged order to become auto-placed
+ trading took place in a real money Fidelity account (as opposed to a PaperTrading account)

Then it could have been a Fidelity order problem. (?) Please report Fidelity data problems and order/login issues directly into Fidelity phone support. Being a 3rd party, we can not check your order status. Thanks.
profile picture

Nikko

#13
Trade signal was EOD.
Autotrading was indeed activated.
Trading is in a real Fidelity account.
The order created states "Active", but it does not fill and Fidelity does not have a record of the order.
As I said, intraday goals will fill. But EOD orders do not fill.

Could it be that perhaps your software is sending a regular market order during extended hours? If this is the case, then Fidelity may not recognize it for trading until regular market hours reopen.

How can we change the setup so that EOD market orders get filled before 4 PM EST?


profile picture

Eugene

#14
You need to switch to Yahoo! data and enable "Always return data with partial bar" on the Data Manager's Yahoo! tab, and then update the data near the close as per the WealthScript Programming Guide > Programming Trading Strategies > Alerts > How to: Alert for AtClose Signals

QUOTE:
However, if you were able to access partial bar data (Wealth-Lab User Guide: Yahoo! Data Provider), it would be possible to update a Daily bar just prior to the market close to get an estimate of the final closing price with which to execute a trade. The trick, however, is to use an AtMarket order in place of the AtClose order when processing the last bar as demonstrated below.
profile picture

Nikko

#15
Your help in developing this script has been invaluable. I am told by Fidelity I am one of the very few individuals utilizing the WLP platform for live, intraday trading. I utilize this script for daytrading 5-10 symbols per day. I have been handling some of the code's limitations by hand, but it is clumsy to do this with several symbols. It is time to attend to some of the details that I have been hoping to get to.

1) As we started discussing before, the script will not exit positions at the close that have not met goal during the day. A market order is sent from an EOD alert, which is unexecutable by Fidelity until the next day, as it arrives after the regular market session closes. Though you tried to tell me how to fix this problem, I never did figure out how to get it to work. Can you tweak the code so that it will close open positions by the end of the day? Perhaps you can close positions with market orders at 3:59:50 EST, if that makes it easier.

Also, you stated that I should switch from Fidelity to Yahoo streaming data. Will I need a Yahoo premium account and pay for the data feed? Not sure how this works. (A 20 minute quote delay will not work for intraday trading.)

2) The point goal is an optimizable strategy parameter. Can you also make the bar scale an optimizable strategy parameter?

3) Can you put a % stop loss in the code and make it an optimizable strategy parameter, as well?

4) Can you put a filter that deactivates the strategy if the symbol price is a certain percentage away from the previous close? Again, this can be an optimizable strategy parameter.

5) I have noted a few bugs with the platform. In particular, several times per week either an alert is not sent to open a position or to close a position at the goal when the necessary criteria have been met. I can find no pattern with this, but it happens often ... twice today, in fact. The code usually works as it should, but there are instances of erratic performance during normal trading conditions. These erratic occurrences do not leave any permanent error messages. (These are not aberrations caused by order conflicts or unavailability of shortable shares.) Any insight into this would be appreciated.

6) Perhaps there was an update with Windows that affected how fonts are displayed in the application interface On the left hand side of the home page, the parameters "Scale, Data Range, Position Size, Symbol" and even the "Go" over the go-button do not fit into the space allocated to them. Changing the font and the size in the preferences window does not solve the problem.


Your help is greatly appreciated.
profile picture

Eugene

#16
Sorry but our support doesn't cover making repeated tweaks and variations to a custom code. We provide the convenient framework for your own scripting along with guidance and examples, when required.

1 - Please see reply #14 for instructions and examine the sample code in the WS Guide.

Re: Yahoo. Unfortunately, support for Premium accounts has been dropped by Yahoo long ago.

2 - On how to work with bar scales from script, please see "Time Frames" section in the QuickRef and also the WealthScript Programming Guide > Multi-Time Frame Analysis for examples.

On making this optimizable with a strategy parameter, the WealthScript Programming Guide has a great tutorial under Programming Trading Strategies > Strategy Parameters.

3 - This Wiki FAQ suggests to experiment with Strategy from Rules to get a basic trading system working and then convert it into code-based Strategy to see how it works under the hood. Besides the application itself, many examples and clues re: programming a stop loss are provided in the QuickRef, WealthScript Programming Guide, in many downloadable strategies, and also on our forum and the Wealth-Lab Wiki site.

4 - It'd be just trivial in an EOD system but for an intraday script it takes a couple of extra code lines. The Knowledge Base article "Building blocks of Intraday trading strategies" contains a readymade snippet on finding the yesterday's close in an intraday system.

5, 6 - Please do not put every unrelated question or issue in just one thread. This aproach is suboptimal as we keep the forum organized for easy access and context search. Thanks.
profile picture

Nikko

#17
Can you provide the names of some programmers who can help me with this and other Wealth Lab projects?

profile picture

Eugene

#18
QUOTE:
Can you provide the names of some programmers who can help me with this and other Wealth Lab projects?

No but this is an open community forum where anyone interested can stand out and offer assistance.

QUOTE:
1) As we started discussing before, the script will not exit positions at the close that have not met goal during the day. A market order is sent from an EOD alert, which is unexecutable by Fidelity until the next day, as it arrives after the regular market session closes. Though you tried to tell me how to fix this problem, I never did figure out how to get it to work. Can you tweak the code so that it will close open positions by the end of the day? Perhaps you can close positions with market orders at 3:59:50 EST, if that makes it easier.

On second thought, I think you're right. Closing out open positions can be approached with a little tweak. No need in Yahoo! data and no EOD. Assuming 15-minute data, here's the updated script:
CODE:
Please log in to see this code.
profile picture

Nikko

#19
Thank you again for your assistance.