TS code for next day MOO exit using 5min data

Discussion in 'Data Sets and Feeds' started by Jock, Feb 21, 2007.

  1. Jock


    I have been using TradeStation for several years, but have only just started testing on 5 minute data. Previously all my coding was for daily data.

    I’m having a problem writing code to exit a trade at tomorrow’s open. For example the following code buys at today’s open plus 0.5 x yesterday’s true range. I then wish to exit MOO tomorrow; however this code always exits at the open of tomorrow’s “second” five minute bar rather than the first, i.e. If market opens at 9.00am the code exits at 9.05am.

    Any thoughts on where I’m going astray would be much appreciated.

    {Data1 is 5 minute data, Data2 is daily data.}

    Vars: dATR(0), ent(0);

    {Start calculations on first 5 minute bar of the day}
    If Date <> Date[1] then begin
    dATR = AvgTrueRange(1) of data2; {Yesterday’s TR}
    ent = Open + 0.5*dATR;

    Buy at ent stop;

    If Date <> Date[1] then ExitLong at Open;
  2. The line "If Date <> Date[1] then ExitLong at Open;" only evalutes on the bar where the date has changed. This means the first bar of the new trading day. Any orders are then issued at the end of this first bar.

    You may want to use time instead, like Sess1EndTime or LastCalcTime. Note that this is in 24 hour format.

    Try "If Time = Sess1EndTime Then ExitLong At Open;" assuming that session time has been correctly set up in the GlobalServer if you are using GlobalServer data.
  3. Jock


    Thanks ChoSingKum, your suggestion works EXCEPT if the preceding day was either a holiday or a half-day, in which case the system exits "two" days later.

    I'd appreciate it if you or anyone else may have any further advice on this.
  4. The right way to do this, if you are using a fixed close to your sessions, such as close of stocks at 4PM EST or futures at 4:15PM EST is this:

    If Time = 1600 then begin
    sell next bar at market;
    buytocover next bar at market;

    In other words, find the time of the last bar of the day.

    This is for Tradestation 8.1 or 8.2 or whatever. Not the original. Hope that helps...
  5. Re: "If Time = Sess1EndTime Then ExitLong At Open;"
    Yes, I overlooked the half-day part.

    I think the holiday should not be a problem because the order should still be generated at the sess1endtime bar for the next bar, even if the next bar is after a holiday.

    Another way is to try this (can only be used in a Signal):
    "If Date < Date of Next Bar then ... ;"

    Note if you do this, the EasyLanguage codes will not be evaluated at the end of every bar but delayed until the appearance of the next bar when the "Date of Next Bar" becomes known. In theory, it might exitlong at the Open but in practice, the Open price is already missed.

    To test this part out, add this to your EZL Signal:
    "Print(Date:8:0," ", Date of Next Bar:8:0);"

    You will see that this line (meaning the entire EZL codes as well) is not evaluated on the last bar on chart. It waits for the next bar to appear first.
  6. This will not make the EZL code universal for all symbols. Since Jock is testing on 5-min bars, it will not work on symbols that close at 4:15 (1615). His position will be exited on the open of the 1605 bar, 15 mins before the close.

    You will then need to have different EZL Signal/Strategy files for the vast array of symbols to track due to the different closing time.
  7. Which is why I said,

    " IF you are using a FIXED close to your sessions..."

    You can simply make the close of your session an INPUT, and you will not need "different EZL Signal/Strategy files for the vast array of symbols to track due to the different closing time."

    To the OP, oftentimes you will see people who spend days trying to find the perfect code solution to a problem which is unnecessary and will be wrong a lot of the time anyway. My solution will solve your problem...Trust me, I use it.
  8. Jock


    TrueStory, thanks for your suggestion. Unfortunately I have an earlier version of TS, so “buytocover” is not recognized.

    If I understand correctly, “buytocover” is effectively the same as “exitshort”. If that is the case, then it seems that I would still have the problem of exiting correctly following a half-day, i.e. if the market closed earlier than the normal 1600.
  9. Jock


    Thanks ChoSingKum, your suggestion does indeed exit correctly at tomorrow’s open.

    However when you say “can only be used in a Signal”, I assume you are referring to the fact that “Next Bar” cannot be used within the system code if more than one dataset is used, i.e. I can’t use “Next Bar” if I also wish to refer to data2 in the same system code.

    Therefore instead of using data2 (daily data) to calculate the yesterday’s TR, I have written a function to calculate yesterday’s TR from the data1 (5-minute data) …

    {Function : Daily True Range}
    vars: t1(0), t2(0), t3(0);
    t1 = highd(0) - lowd(0);
    t2 = closed(1) - lowd(0);
    t3 = highd(0) - closed(1);
    DATR = MaxList(t1, t2, t3);

    If I now refer to this function within the system coding I am able to use your suggested … "If Date < Date of Next Bar then ... ;"

    Thanks again.
  10. I am happy for you. The time you spend on finding your (perfect?) code solution, which is independent on fixed close time, that overcome a necessary programming problem is well worth it. The solution is universal for any day-only instrument, meaning open and close on the same day.

    I am learning at the same time as I am showing you. Yes, you have found out on your own that you cannot use multi-data when you use this statement. We all learn something along the way.

    What I mean by “can only be used in a Signal” is this. The EZL statement "If Date < Date of Next Bar then ... ;" can only be used in the Signal analysis type, cannot be used in Indicators, ShowMe, PaintBar, etc. Signal analysis type is where you write your Buy and Sell orders.

    There are two more you can use in similar way, examples:

    "If Price < Open of Next Bar Then... ;"
    "If Time > Time of Next Bar Then... ;"

    There is also something else you have to consider. Since evalution of the EZL codes is suspended until the Date of Next Bar is known, literally meaning until the next day has already started, you cannot use any order you want executed on the Close of the current bar.
    #10     Feb 26, 2007