Support Board
Date/Time: Mon, 10 Mar 2025 06:56:24 +0000
[Programming Help] - Backtesting with sc.AllowOnlyOneTradePerBar = TRUE
View Count: 761
[2022-03-19 03:53:02] |
User409050 - Posts: 22 |
Hi SC support team, I have a question about how the option sc.AllowOnlyOneTradePerBar works, I run a Bar Based Back Testing for a custom study with these options sc.AllowMultipleEntriesInSameDirection = TRUE; sc.AllowOnlyOneTradePerBar = TRUE; sc.MaximumPositionAllowed = 4; to place BuyEntry when some criteria are met. For example the day is "2022-02-22" and the bar is not the last bar on the chart (sc.Index < sc.ArraySize - 1). In the documentation, it mentions that if one uses "Bar Based Back Testing" then a custom study is calculated 4 times for each OHLC price. But when I run the study, it is calculated only once for the "2022-02-22 bar. If I remove the option (sc.Index < sc.ArraySize - 1) then It runs perfectly. but here is another problem. I wanted to exclude the last bar in the first place because if I didn't and the last bar happens to meet the criteria then when running "Bar Based Back Testing", the study will be calculated for the last bar first to some extent like 2-3 orders then it will be calculated for the other bars in the past which the order is not correct. Thanks |
[2022-03-19 11:46:43] |
1+1=10 - Posts: 270 |
Hi User, I’m a fellow customer that writes a lot of ACSIL. I wanted to exclude the last bar in the first place because if I didn't and the last bar happens to meet the criteria then when running "Bar Based Back Testing", the study will be calculated for the last bar first to some extent like 2-3 orders then it will be calculated for the other bars in the past which the order is not correct.
Could you restate this section to be a bit clearer? Alternatively, would you be willing to paste your code (using the Code button to make it easier to read)? Date Time Of Last Edit: 2022-03-19 11:46:54
|
[2022-03-19 11:49:56] |
1+1=10 - Posts: 270 |
Oh, are you expecting trades to be triggered only at the close of the current bar? If so, you need to also use: ACSIL Interface Members - Functions: sc.GetBarHasClosedStatus()
|
[2022-03-19 19:24:07] |
User409050 - Posts: 22 |
Hi thank you for your response, I'm sorry i have not made it clear enough. 1) are you expecting trades to be triggered only at the close of the current bar? No I'm not expecting trades to be triggered at the close, I'm expecting to see trades triggered at each Open/High/Low/Close Price because that the behavior mentioned in the documentation when one uses "Bar Based Backtesting". but it didn't when I added the option to exclude the last bar out of calculation. Here is my example: //1 input reference and 1 subgraph reference to draw arrows on every bar that meets the criteria SCInputRef date_to_place_orders = sc.Input[0]; SCSubgraphRef up_arrow = sc.Subgraph[0]; ... //for sc.defaults settings //some parts skipped because I don't to make the post too long //for autoloop sc.AutoLoop = 1; //setup unmannered trading in SC sc.AllowMultipleEntriesInSameDirection = TRUE; //stop when total position + working order = 10 sc.MaximumPositionAllowed = 10; sc.CancelAllOrdersOnEntriesAndReversals = false; sc.AllowEntryWithWorkingOrders = true; sc.MaintainTradeStatisticsAndTradesData = true; //allow > 1 trade for each bar sc.AllowOnlyOneTradePerBar = 0; sc.AllowOppositeEntryWithOpposingPositionOrOrders = TRUE; return; //------------------------------------end settings-------------- s_SCNewOrder test_order; test_order.OrderQuantity = 1; test_order.OrderType = SCT_ORDERTYPE_MARKET; test_order.TextTag = "Simple Order"; SCDateTime curDate = sc.BaseDateTimeIn[sc.Index]; if (curDate < date_to_place_orders.GetDate()) { return; } //Criteria //if the current date > the chosen date place as many order as it can //expected to see 4 orders for each bar when backtesting using "Bar replay" but stop when it reaches to 10 if (curDate > date_to_place_orders.GetDate()) { sc.BuyEntry(test_order); up_arrow[sc.Index] = sc.High[sc.Index]; } //ended up with the number of open position > maximum allowed position (10) //the reason is on the last bar, Sierra Chart cleared all the trade data and runs once more time for the last bar but //the last bar happens to meet the criteria and due to the fact that all openning positions have been cleared so, //it successfully placed another order and led to the "Trade Quantity" in Trades tab > Allowed number of position //Fixed by adding 1 more criteria //ended up with now using bar replay, each bar only processed once instead of 4 for each OHLC price if (curDate > date_to_place_orders.GetDate() && sc.Index < sc.ArraySize - 1) I can understand why it placed another order on the last bar, but I don't know why excluding the last bar out of calculation causes the "Bar Based Backtesting" runs only once for each bar instead of 4 times as before? Thanks Date Time Of Last Edit: 2022-03-19 19:34:12
|
[2022-03-20 02:07:05] |
1+1=10 - Posts: 270 |
Sorry for the delay in my response. I actually work on the weekend. I can't answer your questions exactly, as while it is easy to predict how SC behaves in live/delayed ACSIL trading, the backtesting portion, especially bar-based backtesting has its quirks as it is a convenience to make backtesting quicker when you don't need a super accurate backtesting environment. Having said that, here are some thoughts: 1. I think in some sense the idea of having multiple entries per bar and using bar based backtesting don't really fit together. Bar based backtesting only looks at a bar's prices when that bar has closed so you don't know anything about the state of the bar as it developed. Hence, your ACSIL strategy should also only be looking at the bars when they are closed, using sc.GetBarHasClosedStatus(). 2. If you want to develop an ACSIL strategy that can enter multiple times on the same bar then you need to use Replay Backtesting -- Auto Trade System Back Testing: Replay Back Testing - Automatic -- which actually replays all the data feed updates that it took to build your bar and calls your strategy for every update -- this could be 100s or 1000s of times for the same bar. This is how an ACSIL strategy would work in live/delayed trading. 3. Once you choose option 1 or option 2, then you can see if sc.MaximumPostionAllowed work as it should and you can also get more clarity about how your trading system will function if you exclude the most recent bar added to the chart. Hopefully this helps. Date Time Of Last Edit: 2022-03-20 02:07:48
|
To post a message in this thread, you need to log in with your Sierra Chart account: