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:
The resulting Reports table:
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:
Looking for any thoughts, recommendations, etc. Thanks in advance
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");
The resulting Reports table:
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:
Looking for any thoughts, recommendations, etc. Thanks in advance
Last edited by a moderator: