Trade one position each month in Rules
Author: rmpwealth
Creation Date: 10/26/2016 6:02 PM
profile picture

rmpwealth

#1
This seems like a simple strategy, but I am not able to build it w/drag&drop. Can you please help?

I want to have a list of 12-equities, one for each month of the year. I want to buy and sell each month's equity each month. Kind of like a "seasonal" strategy, wherein each "season" is an individual month.

For example: Buy ABC @ open on first trading day of January; Sell @ close on last trading day of January. Buy DEF@ open on first trading day of February; Sell @ close on last trading day of February. Buy GHI @ open on first trading day of March; Sell @ close on last trading day of March. Etc.

Can you please help?

Thank you.

Richard
profile picture

Eugene

#2
Why do you think you cannot do it in Rules? I think it's possible. Please check this out:



Or in code if you wish:

Bar of the Month
profile picture

rmpwealth

#3
Hi, Eugene. Thank you responding.

Regarding the Rules response, I have two question.

1. It looks like in the example, if I Buy an equity at market open on the last trading day of the month and Sell that equity at market close on the last trading day of the month I will buy and sell the equity on the same day. Should the first condition be "first trading day of month"?

2. More importantly, however, how do I assign a specific equity to each specific month (i.e. -- How do I buy ABC at Open on the first trading day of every January, hold ABC during January, sell ABC at Close on the last trading day of January; then buy DEF at Open on the first trading day of every February, hold DEF during February, sell DEF at close on the last trading day of February; then buy GHI at Open on the first trading day of every March, hold GHI during March, etc.)?

If I am not explaining it clearly, let me try this: I want to invest in 12 "seasonal" equities during the year; one "seasonal" equity during each monthly "season". For example: (1) Buy a snowplow stock at the start of every January, hold it during the month of January, sell it at the end of January; then (2) buy a Valentine's Candy stock at the start of every February, hold it during during the month of February, sell it at the end of each February; then (3) buy a Windmill stock at the start of every March, hold it during the month of March, sell it at the end of each March; and so on for each month of the year (ex.: Buy Raincoats during April, Flower Pots during May, Boats during June, etc.). Regarding position sizes, roll all end-of-month amounts over to each following month's purchases.

Thanks again for your help.

Richard
profile picture

Eugene

#4
Hi Richard,

1. Just give it a try. Buy at market generates an alert for the next bar. And what is the next bar after the last trading day of the month?

2. This kind of tasks exceeds the limits of Rule-based strategies in general. You can assign specific stocks to months only in code-based strategies. Just an example to be run on a DataSet of 12 symbols (off the top of my head) listed in the variable "list" in single-symbol mode. Make sure to set the starting date to December 1st:
CODE:
Please log in to see this code.

As you could see the symbol's zero-based index in the list corresponds with the month i.e. 12 = USO, January; 1 = AAPL, February etc. This is only a quick example and clearly not the most intuitve one.
profile picture

rmpwealth

#5
Hi, Eugene,

Thank you for the code.

I loaded it, compiled it, and ran it; and got the following error message:

QUOTE:
Runtime error: Basis price for Position entry cannot be zero


What did I do wrong?

Richard
profile picture

Cone

#6
Make sure you have updated data for all the symbols in the List, i.e., "AAPL", "GOOG", "V", "NFLX", "QQQ", "DIA", "INTC", "GLD", "GE", "MSFT", "JNJ", "USO"

Also, click only 1 symbol (doesn't matter which, but it should also be up-to-date) when running that script.
profile picture

Eugene

#7
Richard, please copy/paste updated code above or simply add this new line on top of BuyAtMarket and recompile:

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

rmpwealth

#8
It works. Thank you very much.

Richard