Bad price on long exit

Hey everyone, I'm running a backtest on imported CSV data. All the trades offhand look normal, except one trade in the "Result list" in the analysis window is showing an exit at a price of over 200k on a long trade. To top it off, the trade is actually labeled as "Long (max loss)" despite earning a huge profit.

I've looked at my raw data and there is nothing close to that price anywhere in the file.

Has anyone seen similar behavior or have an idea what might be causing it?

Thanks!

@rc1111 I suggest to take a look at the Detailed Log to better understand that trade.

1 Like

Thanks, @beppe. I didn't know about that feature. Unfortunately, it isn't telling anything I didn't already know, which is that AB think's it is exiting the trade at a price that never happened.

Here is the detailed info, do you see what might be causing it?

Screen Shot 2021-04-23 at 11.15.43 AM

@rc1111, just to be sure, did you verify the correctness of your imported prices in the Quote editor (from main menu Symbol/Quote editor...)?

1 Like

@beppe very cool, another useful feature you are teaching me! Unfortunately, again, it isn't showing anything unusual :frowning:

I've checked my raw data file as well, and the highest value anywhere in the file is 58,000 (even including volume).

Screen Shot 2021-04-23 at 11.32.31 AM

Here is a sample from the CSV file around that time:

2020-03-12,10:30,7200.13,7200.13,7116.68,7135.09,206.44
2020-03-12,10:31,7135.09,7156.08,7064.77,7064.77,176.09
2020-03-12,10:32,7062.68,7065.92,7008.11,7045.58,417.65
2020-03-12,10:33,7043.69,7091.92,7043.69,7084.34,222.19
2020-03-12,10:34,7089.57,7113.69,7053.27,7066.00,274.10
2020-03-12,10:35,7072.20,7072.20,7010.56,7011.67,88.08
2020-03-12,10:36,7000.00,7000.03,6900.00,6901.15,331.89
2020-03-12,10:37,6876.01,6895.49,6721.00,6740.32,373.82
2020-03-12,10:38,6723.51,6755.43,6600.00,6643.00,314.14
2020-03-12,10:39,6639.74,6730.00,6626.23,6716.15,188.85
2020-03-12,10:40,6723.16,6730.00,6643.90,6655.04,140.83
2020-03-12,10:41,6655.04,6674.46,6600.00,6620.22,137.47
2020-03-12,10:42,6608.28,6635.12,6320.00,6350.90,341.18
2020-03-12,10:43,6350.88,6475.73,6300.00,6387.47,274.66
2020-03-12,10:44,6387.44,6411.97,6200.00,6206.97,270.63
2020-03-12,10:45,6150.00,6236.69,6048.01,6064.69,264.80
2020-03-12,10:46,6064.11,6092.61,5843.00,5932.99,238.67
2020-03-12,10:47,5950.00,5987.88,5600.00,5689.88,243.51
2020-03-12,10:48,5675.59,5752.57,5578.00,5749.94,259.55
2020-03-12,10:49,5739.37,6294.31,5649.03,6294.10,316.26
2020-03-12,10:50,6274.99,6400.00,6000.00,6249.51,257.07
2020-03-12,10:51,6328.32,6699.99,6228.50,6529.33,197.74
2020-03-12,10:52,6526.37,6664.38,6302.72,6426.98,179.39
2020-03-12,10:53,6420.21,6519.31,6335.81,6498.19,141.07
2020-03-12,10:54,6498.18,6800.00,6494.34,6775.28,161.16
2020-03-12,10:55,6775.26,6800.00,6550.00,6671.85,237.26
2020-03-12,10:56,6671.85,6682.19,6476.11,6550.96,115.60
2020-03-12,10:57,6550.95,6599.99,6179.76,6380.72,201.84
2020-03-12,10:58,6380.70,6412.00,6088.34,6219.99,213.81
2020-03-12,10:59,6220.00,6349.99,5999.99,6099.97,227.00
2020-03-12,11:00,6099.97,6099.97,5776.71,5949.42,212.02

You have not provided any reproducible full code.

If the price does not exist then you seem to have PriceBoundChecking field of SetOption function being turned off (False).

https://www.amibroker.com/guide/afl/setoption.html

And you seem to manipulate trade prices (BuyPrice/SellPrice) going outside of H-L range as result.
So if H-L bound check is off turn it on by setting PriceBoundChecking to TRUE.

Picture?

Detailed log says +profit.

1 Like

The prices shown in the detailed log for the trades do not seem to coincide with those that appear in the quote editor.

In addition to the valuable suggestions by @fxshrat , by any chance, did you inverted months/days during the import? (Some dates can be ambiguous, but considering the traded instrument this does not seems the case).

1 Like

thanks @fxshrat. Here is a picture of that trade in the result list (highlighted).

Screen Shot 2021-04-23 at 11.53.03 AM
I will try the

PriceBoundChecking

option you mention now.

As for code, which statements would be useful without my sharing my entire system? I have one buy and 3 stops (for loss, profit target and Nbars).

@beppe, I don't believe I inverted the months and days during import, especially since the chart looks normal and every other trade I've spot checked makes sense so far.

@fxshrat Unfortunately using this code at the beginning:

SetOption("PriceBoundChecking", True);

had no effect on that erroneous trade price :frowning:

@rc111, just an additional check that may help other users to provide an explanation of what you are seeing.

Do you have some particular settings for the symbol and in the Preferences about "Trade using FX conversion" (as, for example, displayed in this recent thread)?

Re the code, I see that the problematic trade (and others marked as max-loss) are opened and closed on the same bar. Show at least how you set the stops and the BuyPrice/SellPrice variables.

1 Like

Thanks, @beppe. I have not altered the currencies setting, and have tried to treat it as if it were a stock for the moment. I've only just started with AB and trying to get down the basics. Could that be causing it?

Screen Shot 2021-04-23 at 12.37.31 PM

As for trades that are opened and closed on the same bar, I believe it is executing as intended. For example, a signal is generated, it gets in on the open of the following bar, which also ends up going below the stop, so it exits on that bar as well.

@fxshrat Here are my buy and sell (essentially):

Buy = buyCondition;
Sell = 0;
ExRem(Buy, Sell);
ExRem(Sell, Buy);

stop_amount_long = Open - (Ref(Low, -1) * .998);

ApplyStop(stopTypeLoss, stopModePoint, stop_amount_long, True, False);
ApplyStop(stopTypeProfit, stopModePoint, Open * (1+ target_pct), 1, False);
ApplyStop(stopTypeNBar, stopModeBars, max_bars_in_trade, True, False);

And here are some backtesting settings that may explain better having 2 executions within the same bar:

Screen Shot 2021-04-23 at 12.42.16 PM

This one are incorrect code lines.
ExRem returns array. It does not return nothing.
Correct use of Exrem with example is in manual.
http://www.amibroker.com/guide/afl/exrem.html

In addition ExRem should not be used in analysis.
So rather remove those lines completely.


This line

may become Null (which could explain profit at "Long (max loss)").

So modify to e.g.

stop_amount_long = Nz(BuyPrice - Ref(Low, -1) * 0.998, 1);

or some other valueifnull ...

In addition take a look at KB link below to overcome cases where difference is smaller than ticksize.


That one is unknown variable!

That one is unknown variable!


Overall your provided code is still an incomplete example.


Why price is overridden to 200k is still unclear to me. I can only explain with pricebound check being off.


If you use ApplyStop in code and do not want to get applied any other UI stop settings then rather set scenario to "Custom" and disable all stops there.

1 Like

Thank you @fxshrat :pray:

It was the minimum tick size! The open of the bar it is buying was actually lower than the stop price, so I guess that is where the issue was. I added:

TickSize = .01;

stop_amount_long = Max(TickSize, Nz(BuyPrice - (Ref(Low, -1) * .998)));

And it made the difference. What a relief.

Is there a more sophisticated way to prevent buying if it is already below the stop price when it would enter?

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.