Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 945 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
>1000% AR? #482631
03/09/21 20:42
03/09/21 20:42
Joined: May 2020
Posts: 27
Germany
M
Morris Offline OP
Newbie
Morris  Offline OP
Newbie
M

Joined: May 2020
Posts: 27
Germany
Here is a relatively simple pair trading script. This uses actual 5 sec Oanda data for BCO and WTICO (the two types of oil). It does not trade based on bars, but rather with each tick. A position is entered when the other asset moves by more than the spread, and it is always closed with the next tick.

Code
#define ASSET_0 "WTICO/USD"
#define ASSET_1 "BCO/USD"
#define SPREAD_0 0.003     // Actual Oanda spread
#define SPREAD_1 0.003

var tick_price[2] = {1, 1};
var prev_tick_price[2] = {1, 1};

void tick() {
    // Determine for which asset this tick arrived
    int asset_id = -1;
    if(strcmp(Asset, ASSET_0) == 0) {
        asset_id = 0;
    } else if(strcmp(Asset, ASSET_1) == 0) {
        asset_id = 1;
    }

    // Store this and the prior tick's price
    prev_tick_price[asset_id] = tick_price[asset_id];
    tick_price[asset_id] = priceClose();

    // Close position after one tick
    exitLong("**");
    exitShort("**");

    // If current tick changes the price by more than the spread,
    // buy (resp. sell) the other asset if the price goes up (resp. down)
    var price_change = tick_price[asset_id] / prev_tick_price[asset_id] - 1;
    if(abs(price_change) >= max(SPREAD_0, SPREAD_1)) {
        // Trade the asset which did *not* receive the current tick
        if(asset_id == 0) {
            asset(ASSET_1);
        } else {
            asset(ASSET_0);
        }
        // Buy if uptick, sell if downtick
        if(price_change > 0) {
            enterLong();
        } else {
            enterShort();
        }
    }
}

function run() {
    if(is(INITRUN)) {
        set(TICKS, LOGFILE);

        StartDate = 2015;
        EndDate = 2021;

        BarPeriod = 60;
        LookBack = 2;
        Lots = 1000;

        asset(ASSET_0);
        Spread = SPREAD_0;
        asset(ASSET_1);
        Spread = SPREAD_1;
    }	
}


Running this script in Test mode yields a rather astounding performance (see attached chart):
Code
Monte Carlo Analysis... Median AR 5516%
Win 1436370€  MI 19389€  DD 1385€  Capital 4535€
Trades 32051  Win 65.5%  Avg +5.3p  Bars 1
AR 5130%  PF 3.40  SR 14.71  UI 0%  R2 0.76


Now, not to disappoint anyone -- unfortunately, it doesn't look nearly as lucrative in real trading (imagine if it did)...

So, what's going on here?

The problem appears to be with Zorro's backtesting ticks: When a tick arrives, it updates the asset price accordingly, but the other assets still have their previous prices. So, when choosing a different asset, enterLong/enterShort will actually trade at a previous tick's price.

Since oil prices, like many currency prices, move more or less in unison, receiving a tick for one type of oil tells you more often than not what the other oil will do, and Zorro lets you trade at a price from the past.

What I would expect Zorro to do when testing in tick mode is to enter or exit at the next tick's price -- as you would in live trading --, as opposed to the previous one's.

(Let me know if you would like the Oanda data to reproduce this.)

Attached Files zorrotest_ticks_BCOUSD.png
Re: >1000% AR? [Re: Morris] #482635
03/11/21 12:22
03/11/21 12:22
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
That's right. Assume historical price ticks for assets A and B have the same time stamp. The tick() function runs first with asset A, then with asset B. In the first run asset A has the current price and asset B the previous price. This allows you to snoop the next price of B when the assets are related.

Thus, for trading with related assets in a tick function, check the timestamps and only trade when all assets are up to date. You can store the timestamp - wdate(0) - in any tick function, and trade when the stored timestamps match or are less than TickTime apart. This will be mentioned in the manual.

Re: >1000% AR? [Re: Morris] #482645
03/12/21 11:29
03/12/21 11:29
Joined: May 2020
Posts: 27
Germany
M
Morris Offline OP
Newbie
Morris  Offline OP
Newbie
M

Joined: May 2020
Posts: 27
Germany
Thanks for the follow-up, jcl. I see what you are suggesting, and that would potentially work.

However, wouldn't it be preferable to fix this in Zorro, instead of working around it in code?
  • For one, the historical price ticks might not have exactly the same time stamp (in which case the workaround becomes more complicated)
  • Also, backtesting and live trading code would be different in this scenario, when one of Zorro's strengths is that you basically just click "Trade" on your backtesting code to go live, without the need to change it


I understand this may not be an easy fix, but logically, I would have thought enterLong/enterShort should trade at the next tick's price...

Re: >1000% AR? [Re: Morris] #482646
03/12/21 15:18
03/12/21 15:18
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
The problem: to really fix it we had to process ticks in 2 steps, first updating all assets and then in a second step running all tick functions. This would unfortunately have a severe impact on execution time.

We will think of another way, not fixing the problem but making it easier to handle in the script. This will be implemented in a future version.


Moderated by  jcl, Nems, Spirit, Tobias 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1