Correcting the AddOrder Buy/Sell Date for Crossovers

Snowthunder

New member
VIP
I have been working for sometime on a crossing signal using a calculated line from Heikin Ashi candles and EMAs. Still working out a lot of variables mainly using Excel spreadsheets and hope to share if I can tune things in some more. However, using Excel spreadsheets and processing hundreds of thousands of data points and calculations is tedious and slow. Errors creep in. Would love to switch to using TOS strategies profit/loss as it would allow me to do a lot of manipulations quickly. Indeed, have done so to get relative data but would love to use the report specific data but there are issues using it. The main one being is that the date recorded by the report is typically two periods off the specific crossing.

To better illustrate and to simplify the situation to try and sort it out, I used the TDAmeritrade included strategy “MovAvgTwoLinesStrat”. I copied it to be able to manipulate the code to check variables as well as changed to my AddOrder style. Regardless, it illustrates the issues I’m trying to work through and would love to resolve. I picked 3 ema crossing 8 ema as the signal to generate more signals for evaluation (not a specific endorsement of the pair) and have zoomed in on a couple months (5/25/22 to 7/25/22) to provide examples.

Here's the code for reference:
Code:
# 12 EMA Cross Above 26 EMA Strategy
#AddOrder changes by Snowthunder to demonstrate issues

input fastLength = 12;
input slowLength = 26;

def fastMA = ExpAverage(close, fastLength);
def slowMA = ExpAverage(close, slowLength);

def buySignal = fastMA crosses above slowMA;
def sellSignal = fastMA crosses below slowMA;

#######Original Buy/Sell order type from TD Ameritrade##########
#AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA, tickColor = GetColor(1), arrowColor = GetColor(1), name = "MovAvgTwoLinesStratLE");
#AddOrder(OrderType.SELL_AUTO, FastMA crosses below SlowMA, tickColor = GetColor(2), arrowColor = GetColor(2), name = "MovAvgTwoLinesStratSE");

AddOrder(OrderType.BUY_TO_OPEN, buySignal, open[-1] ,100 , Color.GREEN, Color.GREEN,name = ”BUY”);
AddOrder(OrderType.SELL_TO_CLOSE, sellSignal, open[-1] ,100 , Color.RED, Color.RED, name = ”SELL");



1683821758488.png


The resulting Reports table:

1683821779210.png


This data is using the standard included AddOrders. The first obvious issue is that using the “BUY_AUTO” order type generates both an open and close order for each buy and sell signal. Simple to fix by using “BUY_TO_OPEN” and “SELL_TO_CLOSE”

But the main issue is that if you look at the chart, the actual EMA crosses are 1-2 periods PRIOR to the trade Arrow/Name. For example, the first “Buy” by the chart is the EMA crossing on 5/27/22 but the AddOrder registers it as 5/31/22 and uses the Opening price for that day. All the orders follow that pattern.

How can I get the AddOrder signal to match the actual EMA crossing??

I do know I can manipulate the data by pulling in earlier prices from the days before, but I’m taking years worth of data, moving it to excel to manipulate the data so that I can do other calculations such as Percentage P/L (not the accumulative P/L buying set number of shares as in TOS), average duration, max duration, Whipsaw, etc. I need to filter the data by the specific date of the lines crossing. Even if I change the price used to get more accurate P/L data, it still uses the wrong date. Any suggestions?



I trade mainly at the end of day and many of these crosses are clear in the last 10-30 minutes and I’ll trade the closing price the day of the cross. I acknowledge that there is a lot of variability in that, but after looking at thousands of these and similar crosses, I have settled on saying I’ll buy the closing of the cross and sell the open of the next day of the actual cross. Makes a difference:

1683821903530.png


1683821913761.png


Looking for any thoughts, recommendations, etc. Thanks in advance
 
Last edited by a moderator:
@Snowthunder

[mod note:]
Just wanted to clarify something about the strategies posted to the forum.

To make sure they work properly, we do require that they use the very next bar which is open[-1] to trigger. This is really important, since anything else does not reflect realistic results.

Your code has the order being placed on open / close (not attainable; an order cannot be placed as the close / open is being formed). Your post was edited to the proper syntax.

This will not provide you with the results that you are looking for.
You want to remove the lag that is built into all moving averages by moving the signal to the moment it is starting to form. But that is not realistic. An order cannot be submitted until the signal has been created.

Yes, you can write a strategy that repaints back to that point; but it does not reflect reality.
Which is why the only strategies posted to the forum must comply with the use of the very next bar which is open[-1].

Retail traders are attracted to moving average strategies. They are very pretty.
But moving averages strategies lag; repainting the signal won't change that reality.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

I understand what you are saying, but you're missing my point. I'm not trying to post a strategy, I'm trying to post a question about using strategies. The actual chart related cross over doesn't match to one used by strategies, likely in all cases. The chart cross overs match in TOS, Stockcharts, Barcharts and with Excel calculations.


Tried to post a chart highlighting the problem, but can't get it to load?? Because you're put in as the solution??

 
At least I finally found a link in here that illustrates and defines the issue better:

https://usethinkscript.com/threads/...-fatal-flaw-of-thinkscript.13475/#post-113259

Nice to know I'm not totally crazy. Just couldn't find this post when I searched in multiple different ways. At least I know and can easily write a correction formula for when I move the data to Excel.

Thanks

are you asking about this?

calc the actual price , when 2 lines cross, between 2 bars
https://usethinkscript.com/threads/...section-of-two-drawn-lines.12361/#post-106384


posting a link to an image
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/page-3#post-98334


i agree, searching on this site rarely finds what you want.
unfortunetely, this site uses an OR search, finding results that match any 1 of the words, not all of the words.

go to google , and type in
usethinkscript ( and your words)
usethinkscript cross average
it will do an AND search, and use all the words.

try both methods and compare the results.
 
are you asking about this?

calc the actual price , when 2 lines cross, between 2 bars
https://usethinkscript.com/threads/...section-of-two-drawn-lines.12361/#post-106384


posting a link to an image
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/page-3#post-98334


i agree, searching on this site rarely finds what you want.
unfortunetely, this site uses an OR search, finding results that match any 1 of the words, not all of the words.

go to google , and type in
usethinkscript ( and your words)
usethinkscript cross average
it will do an AND search, and use all the words.

try both methods and compare the results.

Thanks for the links. The link I referenced was specifically what I was trying to understand. TOS has a glitch in that there is a gap before the strategies AddOrder reacts. It is at least a period and it is time that is absolutely realistic in play for trading in real life. Now that I know, I can adapt and modify my backtesting. I'm verifying my steps with excel in addition, it was just such a gross difference I had to sort it out.

The two lines scripts looks interesting. It's in my list to explore, thank you.

Searching this site is really an embarrassment of riches. There are so, so many fantastic ideas and scripts that it's difficult to sort. My problem I think was more just not knowing the right words to search. And yes, I try google routinely as you suggested. It does work better.....so long as you are searching the right words!

I kept getting an error that my imgur BB link was expired (don't remember the exact words) yet I could post it in another board (did as a test). I'll retry in the near future to check. Got backtesting to do!!

But Thank You for your reply and ideas, they are are appreciated.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
312 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top