Index Futures Scalping System for ThinkorSwim

MerryDay

MerryDay

Active member
VIP
@barbaros
I am not a programmer. Is there a way to set backtesting to a specific number of points?
This is a backtest of his study without any modifications.

In real life, I use his entry signals but as I said, I use my own exit points.
The Thursday and Friday signals were extremely profitable.
I skipped Monday and Today's as my strategy signaled they were not good setups.
 
rad14733

rad14733

Well-known member
VIP
@MerryDay Are you looking for a user-selectable take-profit indicator...???
 
B

barbaros

Active member
@MerryDay I don't have details of the script you are using, but you can set your specific exit target and stoploss target with something like this. Caution, I have not tested the code below. Idea is when you put your actual order in, it would execute at the next bar open with OCO. Since the OCO targets can get hit within that bar, you can execute the close targets in that bar looking forward. If this doesn't work, you can take out the [-1] and try. Again, I have not tested this and writing it from my iPad :)

Python:
# Profit Target and Stoploss for long
addOrder(OrderType.SELL_TO_CLOSE, close[-1] >= EntryPrice()[-1] + 1, name = "Close Long");
addOrder(OrderType.SELL_TO_CLOSE, close[-1] <= EntryPrice()[-1] - 4, name = "Close Long Stop");

# Profit Target and Stoploss for short
addOrder(OrderType.BUY_TO_CLOSE, close[-1] <= EntryPrice()[-1] - 1, name = "Close Short");
addOrder(OrderType.BUY_TO_CLOSE, close[-1] >= EntryPrice()[-1] + 4, name = "Close Short Stop");
 
Last edited:
MerryDay

MerryDay

Active member
VIP
@barbaros Wow! Thanks! This will totally change how I backtest my strategies
 
rad14733

rad14733

Well-known member
VIP
@MerryDay I don't have details of the script you are using, but you can set your specific exit target and stoploss target with something like this. Caution, I have not tested the code below. Idea is when you put your actual order in, it would execute at the next bar open with OCO. Since the OCO targets can get hit within that bar, you can execute the close targets in that bar looking forward. If this doesn't work, you can take out the [-1] and try. Again, I have not tested this and writing it from my iPad :)

Python:
# Profit Target and Stoploss for long
addOrder(OrderType.SELL_TO_CLOSE, close[-1] >= EntryPrice()[-1] + 1, name = "Close Long");
addOrder(OrderType.SELL_TO_CLOSE, close[-1] <= EntryPrice()[-1] - 4, name = "Close Long Stop");

# Profit Target and Stoploss for short
addOrder(OrderType.BUY_TO_CLOSE, close[-1] <= EntryPrice()[-1] - 1, name = "Close Short");
addOrder(OrderType.BUY_TO_CLOSE, close[-1] >= EntryPrice()[-1] + 4, name = "Close Short Stop");
To the best of my knowledge, EntryPrice() is EntryPrice() and shouldn't require nor should it accept [-1]... And, unless I'm mistaken, it doesn't work with backtesting... Do you have information to the contrary...???
 
B

barbaros

Active member
To the best of my knowledge, EntryPrice() is EntryPrice() and shouldn't require nor should it accept [-1]... And, unless I'm mistaken, it doesn't work with backtesting... Do you have information to the contrary...???

Now that I am in front of my computer so I run it for errors, you are correct that you cannot reference the function without dereferencing the result into a variable.

Here is a sample test. It exits if it hits the target or the stop loss in either direction.

Python:
def sma9 = reference SimpleMovingAvg(close, 9);
def entryPrice = EntryPrice();

# Long entry
addOrder(OrderType.BUY_TO_OPEN, close crosses above sma9);

# Profit Target and Stoploss for long
addOrder(OrderType.SELL_TO_CLOSE, close[-1] >= entryPrice[-1] + 1, name = "Close Long", price = entryPrice[-1] + 1);
addOrder(OrderType.SELL_TO_CLOSE, close[-1] <= entryPrice[-1] - 4, name = "Close Long Stop", price = entryPrice[-1] - 4);

# Short entry
addOrder(OrderType.SELL_TO_OPEN, close crosses below sma9);

# Profit Target and Stoploss for short
addOrder(OrderType.BUY_TO_CLOSE, close[-1] <= entryPrice[-1] - 1, name = "Close Short", price = entryPrice[-1] - 1);
addOrder(OrderType.BUY_TO_CLOSE, close[-1] >= entryPrice[-1] + 4, name = "Close Short Stop", price =  entryPrice[-1] + 4);

For @MerryDay's reference, it becomes

Code:
def entryPrice = EntryPrice();

# Profit Target and Stoploss for long
addOrder(OrderType.SELL_TO_CLOSE, close[-1] >= entryPrice[-1] + 1, name = "Close Long", price = entryPrice[-1] + 1);
addOrder(OrderType.SELL_TO_CLOSE, close[-1] <= entryPrice[-1] - 4, name = "Close Long Stop", price = entryPrice[-1] - 4);

# Profit Target and Stoploss for short
addOrder(OrderType.BUY_TO_CLOSE, close[-1] <= entryPrice[-1] - 1, name = "Close Short", price = entryPrice[-1] - 1);
addOrder(OrderType.BUY_TO_CLOSE, close[-1] >= entryPrice[-1] + 4, name = "Close Short Stop", price =  entryPrice[-1] + 4);

There is a catch with this though. There are cases where the stoploss is too small and price movement is highly volatile, it could have hit the stoploss in the candle before turning around and hitting the profit target. The order of the exit entries favor the profit target.
 
Last edited:
rad14733

rad14733

Well-known member
VIP
@barbaros Looks good...!!! (y) I had to look back at a few of my Strategies to remember that I had used EntryPrice() in a few AddOrder() calls... 🧐 I stand corrected... ;) I hope old age isn't setting in... :cautious:
 

Similar threads

Top