♥ 1 |
I am unsure why this is happening. Anyone have a fix or is this just a data feed issue? See image for details. The code is below.
input tradeSize = 1; input accelerationFactor = 0.1; Assert(accelerationFactor > 0, “‘acceleration factor’ must be positive: ” + accelerationFactor); def state = {default init, long, short}; switch (state[1]) { plot parSAR = Floor(SAR / TickSize()) * TickSize(); AddOrder(OrderType.BUY_AUTO, low > parSAR and open[-1] > parSAR, open[-1], tradeSize, Color.BLUE, Color.LIGHT_GREEN, ” Long @ ” +open()[-1]); #and open[-1] > parSAR AddOrder(OrderType.SELL_TO_CLOSE, longExit[-1], Min(open, parSAR[0]), tradeSize, Color.PINK, Color.PINK,”PSAR @ ” + min(open, parSAR[0])); AddOrder(OrderType.SELL_AUTO, high < parSAR[0], open[-1], tradeSize, Color.PINK, Color.PINK, “Short @ ” + open[-1]); AddOrder(OrderType.BUY_TO_CLOSE, shortExit[-1], Max(open, parSAR[0]), tradeSize, Color.BLUE, Color.LIGHT_GREEN, “PSAR @ ” + max(open, parSAR[0])); RESOLVED
Marked as spam
|
Private answer
We’ll need a bit more detail as to exactly what specifications you are trying to use for your strategy entries and exits. I don’t want to assume to know this just from looking at your code. In the mean time I will offer a suggestion. Based on what I saw in your AddOrder() statements, I see some alignment issues. Correcting the alignment may do the trick. So what do I mean by alignment? Well according to the Thinkorswim reference on strategies the condition we select triggers the order on the following bar. http://toslc.thinkorswim.com/center/howToTos/tutorials/Basic/Chapter-7---Creating-Strategies.html It can get very confusing when trying to line up what we expect to happen with what actually happens. In order to explain, I built a simple strategy that buys and sells based on a day of the week. I believe this is a great exercise for anyone wanting to learn what makes these things tick. The goal is to build a strategy that buys at the open on Monday and sells the open on Friday. So we start with this:
Reference the first screenshot below for the result. On the bottom of the chart I plot the day of week. In Thinkorswim, Monday = 1 and Sunday = 7. You can see that the code creates a buy signal on day 1, but the order is placed on day 2. This is exactly what we should expect, but how do we get it to place the orders on Monday and Friday? And hold on a minute, the prices are not matching at all. The entry prices are picked up from the previous bar, the bar that produced the signal. Let see if we can get that corrected in the next step, before we move forward to the final solution.
Reference the second screenshot to see the result. Ok, good. We now have the entry prices matching the opening price of each bar. We did this by adding brackets and a minus 1 “[-1]” after each buyPrice and sellPrice in the AddOrder() statements. This is what I mean by “alignment”. We have properly aligned the entries and exits with the prices. So let’s see if we can finish up our project and get the buy and sell signals to occur on Monday and Friday like we have specified.
Reference the third screenshot to see the result. Well, everything looks good. We have orders taking place at Monday’s open and Friday’s open. And the prices are properly aligned. But how did we do this? We added a [-1] to the entry and exit signal within the AddOrder() statements. And we left the [-1] on the entry and exit prices. That doesn’t really make sense, does it? That’s the real challenge when working on strategies. In order to properly align your signals and the prices takes a bit of practice. Things get even more complicated when we want our entry and exit signals to be triggered based on a value from a previous bar, such as a trailing stop. So once we understand the specification, we should be able to offer some guidance on the code posted by the author of this question. Marked as spam
|
|
Private answer
Following your example…here’s what I came up with. Those entries and exits did fix themselves. Thank you!!!!
def longExit = low < parSAR[1]; AddOrder(OrderType.SELL_TO_CLOSE, longExit[-1], Min(open, parSAR[-1]), tradeSize, Color.PINK, Color.PINK,”PSAR @ ” + min(open, parSAR[-1])); AddOrder(OrderType.SELL_AUTO, sellSignal[-1], open[-1], tradeSize, Color.PINK, Color.PINK, “Short @ ” + open[-1]); AddOrder(OrderType.BUY_TO_CLOSE, shortExit[-1], Max(open, parSAR[-1]), tradeSize, Color.BLUE, Color.LIGHT_GREEN, “PSAR @ ” + max(open, parSAR[-1])); Marked as spam
|
Please log in to post questions.
Got a little ahead of myself here. The shift fixed itself but the entry and exit are either happening on the open or close, not on the cross are what I need.
I received your email requesting professional assistance. Looking forward to helping you get the rest of this dialed for you. This has been a great post and I hope it helps many other viewers who are learning to build advanced Strategies.