Exit strategy order next bar after entry


Category:
0
0

Great resource, keep up the great work!

I’m trying to set up a simple study (and to backtest with FloatingPL) to buy whenever there’s X (let’s assume 3) bars of consecutive lows, on the 3rd consecutive low, I want to buy just before the end of the day, and immediately sell the next day.

I have the following code:

def x = open is less than close from 1 bars ago and close from 1 bars ago is less than close from 2 bars ago and close from 2 bars ago is less than close from 3 bars ago;

AddOrder(OrderType.BUY_TO_OPEN, x is true, low, 100, Color.YELLOW, Color.YELLOW);

…. The problem is that I don’t know how to setup the sell order for the immediate next day close after the buy, and I’d like this process to the continue for whatever period the chart is set to. I would really appreciate the help! Thanks!

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on October 27, 2019 5:28 pm
480 views
0
Ok, so this is a Chart Strategy and not a Chart Study. I will move this out of the Chart Studies topic and into the Strategy Guide topic. I will also update the title of the question. Because your original title: "Count number of days" Has absolutely nothing to do with your request. Your code is already counting the number of days of consecutive lows, yes? So what you are really asking for is how to exit on the next day after entry. I'll update all this tomorrow morning before posting my solution.
( at October 27, 2019 5:44 pm)
1
Private answer

First off, I need to let you know there was a problem with your AddOrder statement that would report the entry price at the low of the bar instead of the close. In practice we all know it's impossible to enter a trade at the dead low of a candle. If it were, we all would be rich beyond our wildest dreams.

So the first step is to correct your entry price to the close of the signal bar. That is certainly possible. However in practice you will find there is some slippage because you can't be the last person to place a trade on a consistent basis. When constructing chart strategies we must always ensure the entry and exit prices are achievable in live trading.

Here's how we do that for your particular strategy:

AddOrder(OrderType.BUY_TO_OPEN, buySignal[-1], close[-1], 100, Color.YELLOW, Color.YELLOW);

Be very careful here. Because we are telling the strategy engine to read one bar into the future. If we are not careful here we can create what is referred to as a "future leak". Which is when the strategy must know what takes place in the future in order to make a trade. In this case we are entering the trade at the close. So that is the end of the bar and does not create a future leak.

If we had chosen the open instead "open[-1]". Well that would be impossible to trade in a live market because we would not now this signal bar is valid until it closes. So we can either enter on the close of the signal bar or the open on the bar that follows the signal bar.

But you want to exit the trade on the following bar so we enter on the close of the signal bar so we can keep the following bar free and clear for closing the trade. How do we do that?

AddOrder(OrderType.SELL_TO_CLOSE, buySignal, close[-1], 100, Color.WHITE, Color.WHITE);

Here we are checking if the current bar is a buy signal. Then exit at the close of the bar that follows.

Screenshot below shows the result. Here is the full code that includes your original:

def buySignal = open is less than close from 1 bars ago and close from 1 bars ago is less than close from 2 bars ago and close from 2 bars ago is less than close from 3 bars ago;
AddOrder(OrderType.BUY_TO_OPEN, buySignal[-1], close[-1], 100, Color.YELLOW, Color.YELLOW);
AddOrder(OrderType.SELL_TO_CLOSE, buySignal, close[-1], 100, Color.WHITE, Color.WHITE);

Notice we are no longer using "x" as a variable name. Your variable names should fully describe what they contain. Single character variable names are nonsense. Avoid them like the plague they truly are. Just because you see other people using them does not mean it's safe.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4079)
Answered on October 28, 2019 1:09 pm
0
Thanks so much for your help Pete, greatly appreciate it. Quick question, does this only generate the indicator at the end of the close? Or if the volume meets the criteria throughout the day it will trigger? Goal would be to have it fire off before end of day if criteria is met. Thanks.
( at February 22, 2020 4:22 pm)
0
This is a chart strategy. Chart Strategies are set to execute either at the close of the bar that completes the signal or at the open of the bar that follows it. This is not a chart study. So this does not generate alerts or notifications. This is strictly used for back testing a trading setup.
( at February 22, 2020 4:50 pm)