How to know when there is a state change on a custom study


Category:
0
0

I’m setting up a script that displays a buy when the heikin ashi candle is green if the previous is red. And then to sell at the first red candle.

However, at the beginning of let’s say a 1 year daily chart that starts with a green candle, I will get a sell when it turns red, but I had no associated buy (because it would have triggered before the 1 year chart started). I’m doing some further counting and PL forecasts with the buy and sell.

I can’t figure out how to set up a variable that says I’m in a Buy and I would use that on my sell statement as “AND inBuy” or “AND isLong”

Here are the two statements that initiate the buy and sell. I was trying to use switch and set the state as long but I cannot figure it out.

def buy = haOpen[0] < haClose[0] and haOpen[1] > haClose[1] ;
def sell = haOpen[0] > haClose[0] and haClose[1] >= haOpen[1];

then I post a bubble on the daily chart to show the buy and the closing price for that day and then the sale bubble on the sell day.   There are some other calcs in between where I need to know when I’m in the “buy/long state”

AddChartBubble(buy, low, Concat(“B:”, close), Color.GREEN, no);
AddChartBubble(sell, high, Concat(“S:”, close), Color.RED, yes);

Many thanks, Joe

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on October 2, 2018 5:16 am
71 views
0

Hi Pete
Thank you for taking the time to respond.

I did try this as a strategy but the Buys would occur on the next (candle) day and the sells the next day as well. I could not figure out how to get them to occur at the close of the day, on the day that the signal is set.

There are also some other items I am calculating such as average days in the buy, PL between the buy and sell, etc. All of it works except when i get a sale before the buy.

( at October 3, 2018 5:33 am)
0

Take some time to review the answer to this post.
https://www.hahn-tech.com/ans/parablic-sar-signal-not-firing-in-tos-strategy/

The point you want to grasp from this is how to ensure your buys and sells are aligned on the correct bar. Also take note of the importance that entry and exit prices occur within the traded range of the bar (below the high and above the low). This is a bit tricky and you have understand the offset that the strategy employs behind the scenes. This post should give you the tools to be able to run your own experiments and learn how strategies work.

As to the rest of it. Pretty sure you can compute what you need by exporting the Strategy report to an Excel spreadsheet, where you can compute most anything you want. Since you are trying to do this manually, using the code, I assume you have not viewed our video series on Strategies.

https://www.hahn-tech.com/thinkorswim-strategy-guide/

https://www.hahn-tech.com/tos-strategy-analysis/

https://www.hahn-tech.com/thinkorswim-strategy-analysis/

If you should ever require our professional services you can get all the details here: https://www.hahn-tech.com/about/

( at October 3, 2018 8:02 am)
0
Private answer

What you are trying to accomplish here is mostly the job of a Chart Strategy. Not a Chart Study. The Chart Strategy automatically keeps track of the “state”, in a position or flat. So that theoretical orders are only plotted where the current “state” permits.

If you are already familiar with this and have decided to do this in a Chart Study anyway, I have to assume you have specific goals that you have not been able to achieve using a Chart Strategy.

Having said all that. There is very little I am able to provide in the free Q&A forum. This topic is far too advanced and would take me far too much time to work out for you.

The only tip I can give you is to study how this works in the ATRTrailingStop and the ParabolicSAR Chart Studies of Thinkorswim. Those studies maintain “state” so that the direction of the trailing stop can be switched at the appropriate time and held until the next change.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on October 2, 2018 7:06 am