Duplicate elements in all_trades w/ Entry + EntryTime limits

Posted By: GPEngine

Duplicate elements in all_trades w/ Entry + EntryTime limits - 10/20/14 02:37

This simple Strategy does enterLong unconditionally at every bar. Then in EXITRUN it prints to file one line for every value in the all_trades list. I ran it against EUR/USD using official data. Then I analyzed the output, counting the occurrences of Bar# in the file.

Observed:
Repeated values for Bar # in the output, each with different values for TradeProfit.

Expected:
No more than one value per bar in the simulation, with an reliable & final value for TradeProfit..
Code:
#define BUFFSIZE_S 300
char value[BUFFSIZE_S];

function write_line() {
  sprintf(value, "%u,%f\n", TradeBarOpen, (var)TradeUnits);
  file_append("all_trades.txt", value);
}

function run() {
  StartDate = 20140801;
  EndDate = 20140901;
  EntryTime = 2;

  var atrx = ATR(200);
  Entry = atrx * 1.0;
  Stop = atrx * 3.5;

  if (is(INITRUN)) {
    file_delete("all_trades.txt");
  }

  enterLong();

  if (is(EXITRUN)) {
    for (all_trades) {
      write_line();
    }
  }
}

Code:
$ cut -d',' -f1 all_trades.txt | sort | uniq -c | sort -rn | head
      3 807
      2 993
      2 990
      2 985
      2 972
      2 957
      2 953
      2 921
      2 836
      2 783

Code:
$ grep 807 all_trades.txt 
807,772.900024,0.000000
807,772.900024,-2.858410
807,772.900024,-2.866854



What's happening, here?
Posted By: jcl

Re: Duplicate elements in all_trades w/ Entry + EntryTime limits - 10/22/14 17:23

Hm, can you explain the problem in more detail? Trades usually have different profits - that's due to the asset price differences at entry and exit. And there can be several trades at the same bar. What did you expect instead and why?
Posted By: GPEngine

Re: Duplicate elements in all_trades w/ Entry + EntryTime limits - 10/23/14 04:07

How can there be three trades opened at bar 807?

There is only one entry command per bar and EntryTime = 2
so there can be at most 2 pending trades in a given bar.
In bar 807 there were at least 3. How is that possible?
Posted By: jcl

Re: Duplicate elements in all_trades w/ Entry + EntryTime limits - 10/23/14 10:44

EntryTime = 2 can delay a trade by 0, 1, or 2 bars. After 2 bars the trade is cancelled. I have not examined the pending machanism in detail, so I can only make an educated guess here, but I suppose the 3 trades on bar 807 come from the current, the previous, and the previous-plus-one bar. At least that's what I normally would expect from my understanding of pending trades.

You can easily test this by setting EntryTime = 1 - you should then have only 2 trades on bar 807.
Posted By: GPEngine

Re: Duplicate elements in all_trades w/ Entry + EntryTime limits - 10/23/14 14:23

Good guess, but no.
With EntryTime = 1, there is only at most 1 trade opened per bar, including 807.
Code:
$ grep 807 all_trades.txt 
807,772.900024,-2.866854

According to your description, with one enter* command per bar,
  • With EntryTime = 2, at a given bar opened trades could come from the current, the previous, and the previous-plus-one bar
  • With EntryTime = 1, at a given bar opened trades could come from the current, and the previous bar

This implies that
  • With EntryTime = 0, at a given bar opened trades could come from the current bar

But a simple test proves that EntryTime = 0 makes Zorro NEVER enter any trades.
Posted By: jcl

Re: Duplicate elements in all_trades w/ Entry + EntryTime limits - 10/23/14 14:56

Your code can indeed not open a trade at the same bar because you're using an entry distance. This adds something to the price. So the price can never met this condition immediately and enter a trade.

For opening 3 trades at one bar, you had to use an absolute entry limit, not a distance. Your current code can only open 2. The 3 trades on bar 807 are apparently caused by your enumeration code that also writes never entered trades. Write only open and closed trades -> see TradeIsOpen and TradeIsClosed.
Posted By: GPEngine

Re: Duplicate elements in all_trades w/ Entry + EntryTime limits - 10/24/14 03:35

I see. Sorry for wasting your time on this.

First, I was once again confused by TradeBarOpen. I will try to understand and remember that it is the time the trade is Opened, not the time the trade is Entered, and that there is no such thing as TradeBarEntry. I guess if I want to record the Entry bar number for each trade, for later tracking, I will have to store it myself in one of the TradeVar positions.

Then, as you figured out, I was confused about EntryTime. I had never seen EntryTime = 0 produce any trades, but clearly it can. I wonder why the default is 1 and not 0.
Posted By: jcl

Re: Duplicate elements in all_trades w/ Entry + EntryTime limits - 10/24/14 12:57

The default is 1 because that's what you normally want - at 0, the trade would either enter or not, but would not wait.
© 2024 lite-C Forums