Strategy based on days before earnings


Category:
0
0

Pete

Would you write the code that, using the AUTO BUY function in TOS Backtester, that buys (near the close) 7 days (calendar) and AUTO SELL 1 day before earnings. I have tried multiple times but been unsuccessful.  Thanks much

Marked as spam
Posted by (Questions: 10, Answers: 11)
Asked on June 26, 2019 3:22 pm
285 views
0
Holy cow that question title is a mouthful: "In Tos backtester: How to Auto buy 7 days before earnings and auto sell 1 day before earning" I am changing it to something other viewers will be able to quickly identify in the search results: "Strategy based on days before earnings" This will also break the URL. I will send you an email notifying you of the new URL to this post.
( at June 27, 2019 12:10 pm)
0
Private answer

Ok let's begin by correcting some of the terms you used. Need to make sure we don't start people off on the wrong foot by using terms that don't really exist.

"AUTO BUY Function":

There is no such thing. There is something called an AddOrder() statement in the Thinkscript language. And this statement is actually a method. It takes several parameters, one of which is the order type. You can get full details here:

http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AddOrder.html

The AddOrder() statement is applied to a chart analysis tool that Thinkorswim refers to as a Strategy. In most cases we use the order types of "OrderType.BUY_TO_OPEN" for an entry order. Then we use the order type "OrderType.SELL_TO_CLOSE" for the exit order. The only time you would use the other order types is if you are building a type of strategy that is always in a position. These are called "Stop-and-Reverse" strategies. For these, you would use the following order types:

"OrderType.BUY_AUTO" and "OrderType.SELL_AUTO"

"TOS Backtester":

Once again, there is no such thing. As mentioned above, Thinkorswim refers to this as a Chart Strategy. The Chart Strategies simply plot theoretical buys and sells on the chart. Then it makes available a report you can export for further evaluation. It is this report, once exported, that forms the basis for backtesting the strategy.

The Code:

Now that we have these little items cleared up for our viewers, let's got on with the solution to your request. Here it is, just two lines of code:

AddOrder(OrderType.BUY_TO_OPEN, HasEarnings()[-7], close[-1], 100, Color.CYAN, Color.CYAN, "Buy On Earnings");
AddOrder(OrderType.SELL_TO_CLOSE, HasEarnings()[-1], close[-1], 100, Color.MAGENTA, Color.MAGENTA, "Sell On Earnings");

Screenshot below shows the results.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 29, 2019 1:48 pm
0
Thanks Pete, sending a contriubtion
( at June 29, 2019 4:19 pm)