Strategy thinks price is $1


New member
I have a strategy script I'm writing and the code periodically thinks the price is $1. It uses two criteria to place a buy order. I've tested a script before that uses two criteria so I don't think that is causing the problem. However, I'm new to this so forgive me it probably has lots of mistakes. The variables seem to be affecting what TOS thinks the price is.

Here is the code, it includes williams alligator and MACD. The buy order should execute an open when price close is above teeth and macd diff is greater than 0. Close is simply when macd diff is below 0. Code works fine if I remove one of the two conditions, so for some reason together they aren't working. Any help appreciated!

input length = 14;
input pricealli = hl2;
input jawLength = 13;
input teethLength = 8;
input lipsLength = 5;
input jawDisplace = -8;
input teethDisplace = -5;
input lipsDisplace = -3;
input averageType = AverageType.WILDERS;
input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
input averageTypemac = AverageType.EXPONENTIAL;   
input price = close;

def diff = reference MACD(fastLength, slowLength, macdLength, averageTypemac).Diff;
def Jaw = MovingAverage(averageType, pricealli[-jawDisplace], jawLength);
def Teeth = MovingAverage(averageType, pricealli[-teethDisplace], teethLength);
def Lips = MovingAverage(averageType, pricealli[-lipsDisplace], lipsLength);

AddOrder(OrderType.BUY_TO_OPEN, diff crosses above 0, price is greater than teeth, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "BUYalli");
AddOrder(OrderType.SELL_TO_CLOSE, diff crosses below 0, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Sellalli");

In the chart I scaled SPY to show how it's marking a buy order at $1, and also not sure if relevant but it seems to have all the variables showing there including both average types for the different studies together.



I think I know what is wrong. In an AddOrder() function, if you don't use a specifier for price (price =______) in a strategy, it assumes the statement after the second comma to be the price at which the simulated order gets filled. This is because of the structure of the AddOrder() function.
So internally TOS evaluates the statement 'price is greater than teeth' to be equal to '1' when True. But we don't need to concern ourselves with that practice.

Basically in your AddOrder() functions you don't specify the price at which the order should be executed, which unless you have a strategy that does not send out an order immediately after a bar closes, use price = close.
ALSO, don't use commas for and/or statements, write them out.

Ex (I haven't actually looked at your strategies rules, so the following is just an example):
AddOrder(OrderType.BUY_TO_OPEN, diff crosses above 0 and price is greater than teeth, price = CLOSE, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "BUYalli");
*Also I'm not @ my desktop, so I might have made a syntax error, but i I don't think so.

If you want to just simplify things rather than write out 'price = _____', then take a look at the this, which has a bunch of examples too.

tldr; replace the comma between your two conditions with an 'and'

Hope this was helpful somewhat :)

Similar threads