Login Page - Create Account

Support Board


Date/Time: Fri, 10 May 2024 17:45:20 +0000



[Programming Help] - backtest with manual looping, sc.GetTradePosition() doesn't update?

View Count: 825

[2019-05-16 21:53:10]
uM8137 - Posts: 180
I had sc.GetTradePosition() making sense to me under automatic looping. But now
that I'm changing over to manual looping, it doesn't seem to work anymore... Is
there some trick?

example: Given this little function to encapsulate GetTradePosition():

int getCurrentPos(SCStudyInterfaceRef sc) {
s_SCPositionData pos;
sc.GetTradePosition(pos);
int curPos = pos.PositionQuantity;
return curPos;
}

then I'm backtesting with sc.AutoLoop = 0; (manual looping) over index k, and after a

int beforePos = getCurrentPos(sc);
int sharesBought = (int)sc.BuyEntry(NewOrder, k);
int afterPos = getCurrentPos(sc);
if (sharesBought > 0) {
// here, both beforePos *and* afterPos can be 0.
// Which makes no sense, since sharesBought was >0,
// so afterPos should be beforePos + sharesBought...
// Or is it just a bug?
}

Thanks! (on version 1915)

p.s. addendum: also with k = 0, any valid order is rejected with an odd "Order entry skipped because invalid bar index specified".
Date Time Of Last Edit: 2019-05-16 22:21:22
[2019-05-19 00:56:24]
uM8137 - Posts: 180
Hello! Just checking back.

Any progress on a bug fix or API docs on how to get the current trade position while manual looping?

Thanks!
[2019-05-19 18:16:57]
uM8137 - Posts: 180
Figured it out.

Sometimes you will get an sc.IsFullRecalculation==1 invocation, and yet
your orders will all result in tons of this in the log:

"Order entry skipped because full recalculation"

Now you may think to yourself: ugh! I can't read any of the log any more. It is
completely spammed by tons of these messages. Since there doesn't seem to be a way
of avoiding them, I know, I will check that flag and
not place any orders if sc.IsFullRecalculation is true.

Now this seems logical, but it would be a mistake. Because there is a very important other time,
when doing a backtest, where the one sc.IsFullRecalculation==1 with sc.StartUpdateIndex==0 pass is
your *only* opportunity to place orders.

If you wait until sc.StartUpdateIndex goes
past the bar where you want to place a simulated order,
you will get the following typical complaint, which really
should mention that specifically your order's index was not between sc.UpdateStartIndex
and sc.ArraySize-1:

"Order entry skipped because invalid bar index specified"

So the root of the problem was the spam that obscures most of the log, which is a total red
herring anyway and should eliminated since it serves only to steer you in the wrong direction.

And, last but very much not least, the devious thing about the
"Order entry skipped because full recalculation" message is that
it will subvert the API and tell you that your order succeeded, but publish that
complaint to the log AND then mess with the API in that you won't see any actual trade position change happen
due to your filled order. However your order will get filled under backtest, so you
just have to ignore it.
Date Time Of Last Edit: 2019-05-19 18:20:57
[2019-05-20 03:38:17]
Sierra Chart Engineering - Posts: 104368
You need to add this to your code just below the sc.SetDefaults code block :
  if (sc.IsFullRecalculation)
    return;

Because there is a very important other time,
when doing a backtest, where the one sc.IsFullRecalculation==1 with sc.StartUpdateIndex==0 pass is
your *only* opportunity to place orders.
sc.IsFullRecalculation is only true once on the first bar during a back test.

And, last but very much not least, the devious thing about the
"Order entry skipped because full recalculation" message is that
it will subvert the API and tell you that your order succeeded, but publish that
complaint to the log AND then mess with the API in that you won't see any actual trade position change happen
due to your filled order. However your order will get filled under backtest, so you
just have to ignore it.
None of what you are saying is true at all. When that error occurs, the order is rejected. It is that straightforward and simple.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2019-05-20 03:41:26

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account