Buy / Sell / Close positions at Open price
-
Greetings,
I am looking for help on the following two issues. I want to learn how to do the following.
1 Buy at open Price
My strategy is to check the color of the first candle ( 9:15:00 ) , and place a buy or sell order at the
open
price of the next candle ( 9:30:00 ) .
My buy order looks like this:self.o[d] = self.buy_bracket( data=d, size=sz, limitprice=prev_low, price=d.open[0], # trying to use open price of current candle stopprice=weekly_low )
However if you check the logs below, you will see that the order is triggered at 09:30:00 but is completed at 14:00:00 candle which is not what is desired.
How could I fix this issue so that I am able to buy or sell with the open price of current candle.
2 Close at open price of 03:15:00 candle
I am trying to close open positions at the start of the last candle, that is 3:15:00 PM candle however the position is closed at the end of the next day.
The following is my logic I am using to close based on time of candle.
# square off: if str(d.datetime.time(0)) == "15:15:00": # check if dates match print("square off at 3:15") self.close(d)
The following are my logs:
order_place_status : {('HCLTECH', datetime.date(2021, 12, 22)): True, ('HCLTECH', datetime.date(2021, 12, 24)): True} 2021-12-24 09:30:00, Sell, HCLTECH, d.close 1253.9000244140625, eclose 2021-12-24, size 33, target price 1230.699951171875, stop price 1283.800048828125 2021-12-24 09:45:00, Order ref: 4 / Type Sell / Status Submitted / Name HCLTECH / lim_stop 1264.600 / Price 0.000 / Size 0 2021-12-24 09:45:00, Order ref: 5 / Type Buy / Status Submitted / Name HCLTECH / lim_stop 1283.800 / Price 0.000 / Size 0 2021-12-24 09:45:00, Order ref: 6 / Type Buy / Status Submitted / Name HCLTECH / lim_stop 1230.700 / Price 0.000 / Size 0 2021-12-24 09:45:00, Order ref: 4 / Type Sell / Status Accepted / Name HCLTECH / lim_stop 1264.600 / Price 0.000 / Size 0 2021-12-24 09:45:00, Order ref: 5 / Type Buy / Status Accepted / Name HCLTECH / lim_stop 1283.800 / Price 0.000 / Size 0 2021-12-24 09:45:00, Order ref: 6 / Type Buy / Status Accepted / Name HCLTECH / lim_stop 1230.700 / Price 0.000 / Size 0 2021-12-24 14:00:00, Order ref: 4 / Type Sell / Status Completed / Name HCLTECH / lim_stop 1264.600 / Price 1264.600 / Size -33 HCLTECH 2021-12-27 candle is -1 gap is : 0 square off at 3:15 2021-12-28 09:15:00, Order ref: 7 / Type Buy / Status Submitted / Name HCLTECH / lim_stop 1267.000 / Price 0.000 / Size 0 2021-12-28 09:15:00, Order ref: 7 / Type Buy / Status Accepted / Name HCLTECH / lim_stop 1267.000 / Price 0.000 / Size 0 2021-12-28 09:15:00, Order ref: 7 / Type Buy / Status Completed / Name HCLTECH / lim_stop 1267.000 / Price 1273.150 / Size 33 2021-12-28 09:15:00 HCLTECH Closed: PnL Gross -282.15, Net -290.5, HCLTECH 2021-12-28 candle is 1 gap is : 0 2021-12-28 10:00:00, Order ref: 5 / Type Buy / Status Completed / Name HCLTECH / lim_stop 1283.800 / Price 1283.800 / Size 33 2021-12-28 10:00:00, Order ref: 6 / Type Buy / Status Canceled / Name HCLTECH / lim_stop 1230.700 / Price 0.000 / Size 0 square off at 3:15 2021-12-29 09:15:00, Order ref: 8 / Type Sell / Status Submitted / Name HCLTECH / lim_stop 1280.000 / Price 0.000 / Size 0 2021-12-29 09:15:00, Order ref: 8 / Type Sell / Status Accepted / Name HCLTECH / lim_stop 1280.000 / Price 0.000 / Size 0 2021-12-29 09:15:00, Order ref: 8 / Type Sell / Status Completed / Name HCLTECH / lim_stop 1280.000 / Price 1282.950 / Size -33 2021-12-29 09:15:00 HCLTECH Closed: PnL Gross -28.05, Net -36.5
If you could help me with these issues, my strategy will be ready to backtest.
I have done several Google searches for similar issues but i was not able to make anything work.Kindly assist me with this and I will be really grateful.
-
Hello,
Have you looked at cheat on open and cheat on close (coc) features ?
https://www.backtrader.com/docu/cerebro/cheat-on-open/cheat-on-open/
https://www.backtrader.com/docu/broker/#set_coccoc
But those are valid only for backtesting, obviously we cannot "cheat " with live trading. At least not in this way lol