AddOrder close to open on same candle

L

LetsGetItDone

New member
I would like to adjust my backtesting strategy to enter an opening order in the opposite direction of my closing simultaneously. The AddOrders trigger when a boolean up / down arrow is triggered. The order fills on the candle after the arrow.

Example of order flow:

Arrow up, next candle BTO (time elapses) Down arrow, next candle STC (time elapses) Down arrow, next candle STO (time elapses) Up arrow, next candle BTC

Goal:
Arrow up, next candle BTO (time elapses) Down arrow, next candle STC + STO (time elapses) Down arrow, no action (time elapses) Up arrow, next candle BTC + BTO.

Code:
def TimeOpen = SecondsFromTime(0950); #function is eastern time zone
def TimeClose = SecondsFromTime(1530); #function is eastern time zone
def AfterClose = SecondsTillTime(1530); #function is eastern time zone

#closing orders
AddOrder(OrderType.SELL_TO_CLOSE, (TimeOpen >= 0 and TimeClose <= 0) and bearishTrigger, HL2, 1, Color.PINK, Color.PINK, "STC" + " " + hl2);
AddOrder(OrderType.BUY_TO_CLOSE, (TimeOpen >= 0 and TimeClose <= 0)and bullishTrigger, HL2, 1, Color.ORANGE, Color.ORANGE, "BTC" + " " + hl2);

#opening orders
AddOrder(OrderType.BUY_TO_OPEN, (TimeOpen >= 0 and TimeClose <= 0) and bullishTrigger, HL2, 1, Color.YELLOW, Color.PINK, "BTO" + " " + hl2);
AddOrder(OrderType.SELL_TO_OPEN, (TimeOpen >= 0 and TimeClose <= 0) and bearishTrigger, HL2, 1, Color.BLUE, Color.YELLOW, "STO" + " " +hl2);

#ending day order
AddOrder(OrderType.Buy_To_Close, (AfterClose <= 0) and bullishTrigger, HL2, 1, Color.YELLOW, Color.YELLOW, "BTC" + " " +hl2);
AddOrder(OrderType.Sell_To_Close, (AfterClose <= 0) and bearishTrigger, Hl2, 1, Color.PINK, Color.PINK, "STC" + " " + hl2);
 
L

LetsGetItDone

New member
This is my first post, have I articulated my question clearly?
 
P

Pensar

Active member
VIP
This is my first post, have I articulated my question clearly?
@LetsGetItDone Welcome to the forum! I believe your question is stated clearly, it may have been overlooked. :) Perhaps try "Buy_Auto" instead of "Buy To Open", and "Sell_Auto" in place of "Sell To Open"? Buy/Sell_Auto strategy orders automatically reverse the trade when placed.
 
L

LetsGetItDone

New member
@LetsGetItDone Welcome to the forum! I believe your question is stated clearly, it may have been overlooked. :) Perhaps try "Buy_Auto" instead of "Buy To Open", and "Sell_Auto" in place of "Sell To Open"? Buy/Sell_Auto strategy orders automatically reverse the trade when placed.
Thank you for your reply. After modifying the AddOrder to Auto and running some tests, I was not able to produce a closing order and an opening order on the same candle.




Code:
def TimeOpen = SecondsFromTime(0950); #function is eastern time zone
def TimeClose = SecondsFromTime(1530); #function is eastern time zone
def AfterClose = SecondsTillTime(1530); #function is eastern time zone

#Market Hour Orders
AddOrder(OrderType.BUY_AUTO, (TimeOpen >= 0 and TimeClose <= 0) and bearishTrigger, HL2, 1, Color.PINK, Color.ORANGE , "Buy" + " " + hl2);
AddOrder(OrderType.SELL_AUTO, (TimeOpen >= 0 and TimeClose <= 0)and bullishTrigger, HL2, 1, Color.ORANGE, Color.WHITE, "Sell" + " " + hl2);

#ending day closing Order
AddOrder(OrderType.Buy_To_Close, (AfterClose <= 0) and bullishTrigger, HL2, 1, Color.YELLOW, Color.PLUM, "BTC" + " " +hl2);
AddOrder(OrderType.Sell_To_Close, (AfterClose <= 0) and bearishTrigger, Hl2, 1, Color.PINK, Color.PLUM, "STC" + " " + hl2);
If you have other suggestion please let me know.
 
rad14733

rad14733

Active member
VIP
Thank you for your reply. After modifying the AddOrder to Auto and running some tests, I was not able to produce a closing order and an opening order on the same candle.
I don't beleive that can be done using a strategy... If it can, you would need separate AddOrder logic for each direction... But even then I'm doubtful... I manually calculate stuff like that... I use a modified version of one of the trend reversal scripts that can plot chart labels with the change in price from price bubble to price bubble and either use a calculator or spreadsheet... I'll check to see which one it is once the markets close...
 
Last edited:
P

Pensar

Active member
VIP
@LetsGetItDone In the picture you linked, doesnt the BUY_AUTO and SELL_AUTO snip show that it is reversing the trade on the same candle? It does look as if the bearishTrigger and bullishTrigger conditions are placed in the wrong order types.

Here is a picture of a simple SMA crossover strategy that buys and sells on the same candle in the same way as your snip shows.

 
Last edited:
L

LetsGetItDone

New member
@Pensar yes, you are correct. I was visually looking for something different. This resolved my question. Thank you.
 
Last edited:

Similar threads

Top