Question: thinkScript entry price

SeattleTrader20

New member
Good morning! Happy labor day! Quick question, has anybody ran into this issue with thinkscript:

I'm trying to AddOrder 1% below the daily open but when I do:

AddOrder(OrderType.BUY_AUTO, (open * 0.99));

It shows that I enter at the open price instead of entering 1% below the open.

However, when I do almost the same thing in studies:

plot line = (open * 0.99); the line gets plotted at the correct price; 1% below the open.

Does anybody know what the issue is? How can I get in 1% below the open?
 
Solution
@rad14733 Thank you for your answer and quick response. Any many hours of research even prior to making this post, I ended up finding a work around this issue. I hope this helps somebody who comes to check on this thread in the future.

Here is the lines of code which allowed me to buy at 1% below the open:

def discount = 0.99;
def buy_price = open * 0.99;
def entry_condition = (open * discount) <= high && (open * discount) >= low;
AddOrder(OrderType.BUY_AUTO, entry_condition, buy_price[-1]);

Yes, the proper logic is to buy at the open of the next candle after the confirmation candle... You can streamline your logic to the following which requires less calculations and thus improves performance...

Ruby:
def discount =...

rad14733

Well-known member
VIP
@SeattleTrader20 What you are seeing is an inherent flaw within the AddOrder() Strategy functionality of Thinkorswim... Orders only fill after the candle closes, not based on price action during the candles formation... This is a limitation for which there is no real remedy, unfortunately... The best we can do is to make adjustments to the conditions, entry price, and which candle that entry takes place on...
 

SeattleTrader20

New member
@rad14733 Thank you for your answer and quick response. Any many hours of research even prior to making this post, I ended up finding a work around this issue. I hope this helps somebody who comes to check on this thread in the future.

Here is the lines of code which allowed me to buy at 1% below the open:

def discount = 0.99;
def buy_price = open * 0.99;
def entry_condition = (open * discount) <= high && (open * discount) >= low;
AddOrder(OrderType.BUY_AUTO, entry_condition, buy_price[-1]);
 

rad14733

Well-known member
VIP
@rad14733 Thank you for your answer and quick response. Any many hours of research even prior to making this post, I ended up finding a work around this issue. I hope this helps somebody who comes to check on this thread in the future.

Here is the lines of code which allowed me to buy at 1% below the open:

def discount = 0.99;
def buy_price = open * 0.99;
def entry_condition = (open * discount) <= high && (open * discount) >= low;
AddOrder(OrderType.BUY_AUTO, entry_condition, buy_price[-1]);

Yes, the proper logic is to buy at the open of the next candle after the confirmation candle... You can streamline your logic to the following which requires less calculations and thus improves performance...

Ruby:
def discount = 0.99;
def buy_price = open * discount;
def entry_condition = buy_price <= high && buy_price >= low;
AddOrder(OrderType.BUY_AUTO, entry_condition, buy_price[-1]);
 
Solution

SeattleTrader20

New member
@rad14733 Thank you, I added that to my code but it looks like there's an issue. The code is trying to buy and sell at a price outside of the body of the candle.. do you have any advice/ resources that can help me fix this. Attached below is my full code and a picture of what I'm talking about. Thank you so much, your help is invaluable!

Photo:
Code:
#Adjustable Variables
    input discount = 0.99;

#Static Variables
    def entry_price = EntryPrice();
    def buy_price = open * 0.99;
    def sell_price = entry_price * 1.01;
    def exit_condition = sell_price <= high && sell_price >= low;
    def entry_condition = buy_price <= high && buy_price >= low;

#Orders
    AddOrder(OrderType.BUY_AUTO, entry_condition, buy_price[-1]);
    AddOrder(OrderType.SELL_TO_CLOSE, exit_condition, sell_price);

Basically what I want it to do is buy when the price goes down 1% in 1 day and sell whenever it goes back up 1%.
 

rad14733

Well-known member
VIP
@SeattleTrader20 Unfortunately, we can't realistically buy at a price that doesn't exist during the candle and that is what is happening... The issue is that AddOrder() won't wait for your desired price, it only trades based on the Condition provided, at the Price you state, on the candle you tell it to enter on... In order to only buy at your desired price you need to add that as part of your Condition... And that means less entry opportunities... Otherwise you need to buy at the open of the next candle... We have to live within the limitations that the platforms backtesting capabilities provide us...
 

Similar threads

Top