Help with error in AddOrder statement


Category:
0
0

Hi Pete, hope you can guide me on this I am sure this is a simple one.

I thought my label is correct but I get a error.

Here is the code.

input length = 10;

def N = reference ATR(length = length);

def trailStopPrice = close + (2*N);

#def trailStopPrice = price + trailStop * mult;

addOrder(OrderType.BUY_TO_CLOSE, high >= trailStopPrice, tickColor = GetColor(9), arrowColor = GetColor(9), “Out”);

When I add the “Out” I get the following error

Parameter already defined: tickcolor
Incompatible parameter: “Out” at 20:5
Expected class com.devexperts.tos.thinkscript.data.CustomColor at 24:1

It is the same as this which works fine.

 

AddOrder(OrderType.BUY_TO_CLOSE, ShortExit, open, tradeSize, tickColor = GetColor(7), arrowcolor = GetColor(7), “TSClose”);

 

Marked as spam
Posted by (Questions: 2, Answers: 2)
Asked on February 20, 2018 9:58 pm
1441 views
0
Private answer

Well that original question title had to go. Please make sure the question title accurately reveals the context of the question so other visitors can search for a find solutions. By the way, this is a great question. I love it when folks include their code. Because it prompts me to take extra time to explain things in detail and teach everyone what went right and what when wrong.

You can get all the details for the AddOrder() statement here: http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AddOrder.html

The AddOrder Statement

Ok, the add order statement takes 7 arguments (or parameters). If you place them all in the correct order you do not need to label them. Such as “tickColor = GetColor(7)”. If you enter all 7 parameters and list them all in the correct order you only need to provide the values.

Very Important Lesson

This is very important so pay attention. For SOME statements, there are optional parameters. Let’s make an example using close(). Anywhere in your code you can use the word close to grab the current bar’s close from the current symbol and time frame. Did you see that? There three parameters for the close() statement. They are: Symbol, Period and PriceType. All of them are optional. If not present, a default value is used. If you wanted to get the current bar’s close from another symbol you would provide that as a parameter: close(symbol = "AAPL"). Because we are not providing all three parameters we must use a label so the statement knows which of the three optional parameters we are passing. If we provide all three parameters and list them in correct order, no labels are required.

Ok, got it? Make sure to read that paragraph again if this next section leaves you scratching your head.

Back to the AddOrder Statement

So, let’s count the number of parameters passed in this statement:

AddOrder(OrderType.BUY_TO_CLOSE, ShortExit, open, tradeSize, tickColor = GetColor(7), arrowcolor = GetColor(7), “TSClose”);

Seven, I count 7 parameters. And since they are all present and listed in order we can shorten it to:

AddOrder(OrderType.BUY_TO_CLOSE, ShortExit, open, tradeSize, GetColor(7), GetColor(7), “TSClose”);

Ok, that was the statement that was working fine. Know let’s take a look at the statement that is generating the error:

addOrder(OrderType.BUY_TO_CLOSE, high >= trailStopPrice, tickColor = GetColor(9), arrowColor = GetColor(9), "Out”);

So, how many parameters do we have in this one?

Five, I count only 5 parameters in this one. We have two missing.

Which ones are missing? Parameters 4 and 5 are missing. (entry price and trade size). Both of those two parameters are optional. You can leave them out and the system will provide default values. Your issue begins right there. Remember that labels are not required when all parameters are listed and they are all listed in the correct order. But once you leave out one or more optional parameters, everything that follows must be labeled.

The Correction

We can correct this in one of two ways. We can provide those other two parameters as they have been done in your previous AddOrder statement. We can use the variable names open and tradeSize to do the job. Like this:

addOrder(OrderType.BUY_TO_CLOSE, high >= trailStopPrice, open[-1], tradeSize, tickColor = GetColor(9), arrowColor = GetColor(9), "Out”);

Please take note of the [-1] after the entry price parameter. This is absolutely essential for strategy orders unless you have previously shifted the test for entry bar. Which is an advanced method and requires very careful attention to detail. Your code does not shift the entry bar, so make sure you use open[-1] for your entry price. Otherwise your strategy will create theoretical orders that are completely impossible to achieve in live trading. This is another lesson entirely and if you are interested you can get the mind-blowing details here: https://www.hahn-tech.com/ans/parablic-sar-signal-not-firing-in-tos-strategy/

Ok, on to the second way to correct this. We can leave those optional parameters out, but we must apply a label to every single parameter that follows. Like this:

addOrder(OrderType.BUY_TO_CLOSE, high >= trailStopPrice, tickColor = GetColor(9), arrowColor = GetColor(9), name = "Out”);

Simple eh? Now I could have simply typed that out as the answer and hit the submit button. But because you took the time to provide your source code I decided to turn this into a lesson. I hope I covered all the nooks and crannies. Writing Strategies is a very challenging endeavor. There are many ways to screw things up, and create back-test results that are completely impossible to achieve.

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on February 21, 2018 9:36 am