就实现GRID TRADING请教TS玩家

Discussion in 'TradeStation' started by johnyj, Sep 17, 2005.

  1. 如果你在开仓的时候设定了买卖单的名称,例如:buy ("Condition1") nCrtLots Contracts this bar on close;
    那么你就可以指定对这笔交易进行平仓,例如:ExitLong from entry ("Condition1") this bar on close;
     
  2. 多谢,很有启发。

    还有个问题,我从来不是用收盘价市价单进入市场的,都是用限价单在前一根K线挂上。这样一来就有很多困难,因为不知道TS在下一根K线穿越限价单的时候是在什么时候将这张单填上的,是在所有语句执行之前么?怎么样使得这个填单动作触发我某些变量的状态变化呢?
     
  3. 同意。基本上收盘和开盘市价挂单没有实用性,一般有实战价值的交易系统都是采用止损或者限价挂单。johnyj兄这里的限价单指的是限价挂单(即TS中的Limit orders)还是外汇中的止盈单?
     
  4. 不好意思,回晚了。

    没有完全看明白你的意思。TS里面在指令触发方面有个“接近原则”,意思就是如果你在下一根K线中可能有不止一笔的条件单都满足了触发条件,那么TS将会假定接近开盘价的那个条件单先得到触发,而假定接近收盘价的那个条件单最后得到触发的。
    假如是系统交易的话,一般情况下系统发出指令就认为是应该立即成交的。您说的限价单是否可以理解为行情触发的条件单,即满足了K线穿越限价的时候触发这笔单子发出?如果是这样的话我的理解是这样的:当你设置了条件单(stop,limit)等等,系统会不断地以一个时间间隔来测算这个条件是否满足(一般是1分钟也有一个tick测算间隔的,可在strategy里面设置),如果满足了条件那么这笔委托单立即发出。也就是说如果你用了stop等限价、止损单等,就已经把这一部分的控制权交给TradeStation了,我相信TradeStation的指令模块应该能给很好地处理你提到的这些问题,只是我们都没有这个模块因而无法测试和验证我们的想法。
    不过我们完全可以自己来接管TradeStation在这方面的控制,比如你可以在你的signal里面自己定义一个条件,假如是condition1=close cross above nYourLimitPrice,然后你就可以捕捉到这个填单动作(if condition1 then...)来自己处理自己的变量了。
    我这里是自己开发的期货委托交易模块,本来是想完全依赖TS的,只是在实践中发现至少在TS5里面委托指令这部分TS是有很多缺陷的,部分缺陷是无法弥补的(可能是TS封装在它的指令模块里了),我这里的经验只能是不得不接管TS对指令和持仓的控制,完全自己控制和跟踪指令与持仓,反而非常方便了。
     
  5. 下面是TS5帮助里面对指令等方面的说明,你可以在BackTesting里面查到,不知是否可以对你有些帮助:

    Time-based bars include the open, high, low, and close for the specified time period. When you work with historical data, TradeStation doesn't know the chronological order of the transactions that make up the bar. The only transactions for which the chronology is known are the open, which occurred first, and the close, which occurred last. With time-based bars based on historical data there is no way to know whether the market opened and then went down, or the market opened and then went up.

    However, because the order of ticks can be important, Omega Research extensively studied the movement of various markets to discover a way to determine the most likely chronological order of the high and low ticks. After research, two general rules were established about price movement and the chronological order in which ticks occur.
    Assumption 1: The order in which prices on a bar are reached relates to the proximity of the open to the low and to the high

    When the open is closer to the low than to the high, TradeStation assumes that the low was reached first. Likewise, if the open is closer to the high, TradeStation assumes that the high was reached first. For example, say you have a buy order at 100. You protect yourself against losses at 97 points or lower, and want to exit the position at 101 or higher. The open is 99 points. The high of the day is 103 and the low is 92, so the open is closer to the high. In this case, your buy order is filled at 100 and your strategy will generate your profit-taking exit order at 101, thus recording a profit of 1 point.

    However, it's possible that in reality the price climbed to 100, at which point your entry order was filled, and then it dipped, hitting 99, before climbing back up to 101. In this case, you would actually have taken a loss of 1 point instead of a profit of 1 point. So, looking at historical data, and based on the market assumption described earlier, TradeStation would record this trade as a winner, when it would in fact have been a loser. The opposite could happen also, where a trade could be recorded as a loser when it was actually a winner.

    To more closely simulate market activity in these cases, TradeStation developed what is known as Bouncing Ticks? When enabled, TradeStation automatically sends the data down by a percentage (the TradeStation default is 10%) immediately after a buy or sell order, and then comes back up to the next item on the bar (high, low, or close). This action more closely resembles what happens using real-time data.
    Using this 10% setting on the example above, the buy would have happened at 100, and would have dropped immediately by 10% to 99 (to more closely resemble real-time results), your exit would be recorded at 99, and the bar would more correctly reflect the losing trade that would have happened if you had been using real-time data.

    Assumption 2: Symbols Trade at Every Price Along the Bar

    The second assumption is that a symbol trades at every price along the bar. That may not always be an accurate assumption. If, using the example above again, you had a buy order at 100, and the symbol actually traded at 99 and then jumped to 101, the strategy would record your buy at 100, even though you actually were filled at 101. If your exit was at 110 then, the strategy would record a profit of 10 points, even though your true profit would only be 9 points. This assumption might cause a strategy to appear more or less profitable than it actually was.

    TradeStation allows you to avoid both of these assumptions by back-testing to the tick level.

    The order of the transactions on the bar becomes very important when you apply a trading strategy to time-based bars based on historical data. For example, if you had no market position and the entry and exit criteria of the trading strategy were both triggered on the same bar, knowing which signal was triggered first would be important.

    If the exit criteria of the strategy were triggered first, TradeStation would ignore the exit signal (because there was no market position to exit) and, when the entry signal was triggered on the same bar, TradeStation would issue the entry order, showing that a new position was opened.

    The open price of a bar is always number 1. The close price of the bar is always number 4. The numbers 3 and 4 are used to designate the most likely chronological order of the high and low ticks.

    Rarely does the market strictly follow the 1, 2, 3, 4, order. Instead, the market bounces up and down. For example, even on a day when the market climbs in a relatively steady manner, there are brief dips followed by still higher highs.

    When you apply a trading strategy to a chart based on historical data, a special TradeStation innovation called Bouncing Ticks?simulates the irregular up and down way the market really moves, even during an up-trend or a down-trend.

    The Bouncing Ticks function works as follows: When market conditions trigger the entry signal of your trading strategy (either long or short), TradeStation bounces back by a certain percentage that you specify. If, within that percentage, there is a price that would trigger the exit criteria of your strategy, TradeStation exits the position on the same bar on which you entered the position.

    Here is the major reason that TradeStation simulates market activity in this way: During actual market activity, chances are extremely high that once the entry signal of your trading strategy is triggered the market would turn against you by a certain percentage. When testing a trading strategy on historical data, you need to take this normal bouncing market activity into account, particularly when you design your strategy's exit criteria. For example, in real-time market conditions, when the market turns against you, you have no way of knowing if the market will rebound in your direction. Your strategy should be constructed in such a way that it exits your position before you sustain losses that you cannot afford.

    When testing a trading strategy on a time-based chart based on historical data, it can signal a flaw in your trading strategy if the strategy repeatedly enters and exits on the same bar. In such a case, we recommend that you re-examine the rules of your strategy and redesign the entry and exit criteria so that the exit criteria of the strategy liquidates your open position only when the market takes a significant turn against you.

    For instructions on enabling the Bouncing Ticks option, see Setting the Bouncing Ticks Option.

    { ** ?1987, 1999 Omega Research, Inc. ** }
     
  6. 多谢黑马兄,你说的办法我也想到过,只是怕麻烦,呵呵。。。

    因为TS是依次评价每一根K线的,所以只有在该K线绘制完成(CLOSE被定下来)的时候才可能评价这根K线。

    那么在实战中就是:TS在评价每一根K线的时候都把符合条件的希望成交的限价单挂一遍,然后等到下一根K线完成之后观察是否成交了。所以我认为限价单是否成交应该是在一根K线绘制完毕后最先评价的。这个评价之后才开始进行EASY LANGUAGE代码的评价。所以这个对于限价单是否成交的评价应该返回一个值,可惜这部份在TS中好象无法操控,结果弄得只好自己写CODE来评价,做了重复工作,就象你说的一样。
     
  7. 不知道johnyj兄提到的限价单是不是指由TS系统控制的行情触发的委托单,如止损、止盈单等等,而不是指由显式的buy/sell/exitlong/exitshort等指令主动发出的委托单?
    显式的委托单是在每次当前周期的K线完成后才会获得一次计算和执行机会的,而TS系统控制的触发单可以在比当前的周期更小的周期里面获得测算和执行机会的(可以设置的),我在使用一个5分钟的交易系统中看到,每隔1分钟CPU会出现一个峰值,如果估计的没错(因为没有去验证),这每一分钟就是给TS控制的触发单一个测算和执行的机会。
    因为TS控制的触发单的执行暂时无法在EL代码中捕获而去执行交易,在实际的交易中干脆就全部用显式的指令了,在周期较小的情况下基本上可以达到使用的要求。
     
  8. 我是这么理解的,你看对不对?

    比方说我交易日线,今天是10月17日,欧元的收盘价格是1.2001,我的EL代码中有一行是:
    Buy 1 contract next bar at 1.2050 stop;

    代码是在10月17日的日K线产生以后(也就是说收盘以后)才开始运行的,那么就是说在17日收盘后才会产生这么一个限价单的指令,我此时就在OANDA挂上一张限价单:价格突破1.2050后买入1手。

    这个next bar 显然是指10月18日了,那么在10月18日收盘完成的时候,TS才可能进行EL代码的评价。但在18日收盘之前,如果价格已到达了1.2050,我在前一天(17日)收盘后挂在场内的限价买单就已经成交了。这个成交TS在18日收盘时评价18日那根日K线的时候能察觉到,并将信号画在18日的K线。但怎么让这个成交触发我某个变量的变化,就是我搞不清的了。

    你说的TS控制的行情触发单是怎么回事,我怎么从来没听说过。。。
     
  9. 从您的例子看来,我说的TS控制的行情触发单可能就是你说的限价单。
    就您这个例子,对TS委托交易我的理解是这样的,当TS在17日收盘执行到
    Buy 1 contract next bar at 1.2050 stop;这条指令的时候,在18日交易的时候TS会每隔一个时间间隔去测算一下看看价格是否已经突破1.2050了,之所以我说它是TS控制的由行情触发的指令单,就是因为TS对这部分封装了而无法控制。
    至于到18日收盘后EL代码再一次得到执行的机会,只要当天的行情确实突破了1.2050那么这个指令肯定已经成交了,那么此时EL代码中的MarketPosition肯定与前一根K线(17日)的MarketPosition不一样了,跟踪比较这个数值应该就能获得触发你定义的某个变量的可能性了吧?另外也可以参考一下EntriesToday()这个函数,或许有些帮助。不知道我理解的对不对,老兄有了结果告诉我一下。
    另外老兄说:“此时就在OANDA挂上一张限价单:价格突破1.2050后买入1手。”不是指你手工下单而不是由TS自动控制吧?
     
  10. 我觉得TS应该是在下一根K线的数据完全获得之后才进行评价的。因为做历史数据测试的时候,TS能用来计算的数据只有那些已经画在图上的K线。如果我让TS测试4小时线,那么TS显然只有4小时线的数据可用来计算。

    而MarketPosition只有三种状态,而对于网格交易来说,在一个方向上通常有几十个仓位,就无法体现出区别了。

    我现在是希望在某一个价位的仓位有一个状态开关,当这个仓位被开立后这个开关被置于1,这样在这个相同的价位就不再开立新单。而在该仓位被平仓以后,状态就置0,于是又在这个价位再开立新单。

    另一个角度来说,是随时检查当前价位上下一格有没有已经成交却还未平仓的单子,如果已经有,那么就不做动作,如果没有,就开单。由于TS把所有的单子都混在一起,就很难借助TS本身的现有功能来解决这个问题。
     
  11. 我们用个简单的例子来说吧

    {Test the johnyj's thought of Positions
    2005-10-20}
    Variables: Mkt(0);

    value1=Highest(high,10)[1];
    value2=Lowest(low,10)[1];
    value3=Highest(high,20)[1];

    buy("B1") 2 contracts next bar at value1 stop;
    sell("S1") 3 contracts next bar at value2 stop;
    buy("B2") 3 contracts next bar at value3 stop;

    Mkt=MarketPosition*CurrentContracts;

    if Mkt<>Mkt[1] then begin
    if Mkt= 2 then Print(date,time," B1 trigered");
    if Mkt=-3 then Print(date,time," S1 trigered");
    if Mkt> 2 then Print(date,time," B2 trigered");

    {Here you can set your variables based on the difference between Mkt and Mkt[1]}

    end;

    输出如下:
    1051012.00 1035.00 B1 trigered
    1051012.00 1055.00 S1 trigered
    1051012.00 1125.00 B2 trigered
    1051013.00 1000.00 S1 trigered
    1051013.00 1040.00 B1 trigered
    1051013.00 1110.00 B2 trigered
    1051014.00 905.00 S1 trigered
    1051014.00 1015.00 B1 trigered
    1051014.00 1100.00 B2 trigered
    1051014.00 1405.00 S1 trigered
    1051017.00 1015.00 B1 trigered
    1051017.00 1045.00 B2 trigered
    1051017.00 1405.00 S1 trigered
    1051017.00 1450.00 B1 trigered
    1051017.00 1455.00 B2 trigered
    1051018.00 910.00 S1 trigered
    1051018.00 1030.00 B1 trigered
    1051018.00 1105.00 B2 trigered
    1051018.00 1335.00 S1 trigered
    1051019.00 1035.00 B1 trigered
    1051019.00 1125.00 S1 trigered
    1051019.00 1405.00 B1 trigered
    1051019.00 1430.00 S1 trigered
    1051020.00 905.00 B2 trigered
    1051020.00 1000.00 S1 trigered
    1051020.00 1120.00 B1 trigered
    1051020.00 1355.00 S1 trigered


    上面的结果可以说明我们可以通过跟踪Mkt数量的变化来确定曾经触发了哪些交易,如果系统中设置的买卖指令数量是有规律的话。
    具体到网格计算,或许还可以通过价格差距配合持仓差来判别哪些指令曾经触发了,以此来设置一个状态开关。
    其实TradeStation系统是自己维护一个指令列表的,保存了所有关于指令持仓的资料,老兄可以在Universe目录下的Alert.mdb文件中查到些线索,只是它被封装用户无法使用,造成许多不便。因此我在用TS程序自动交易时,干脆就完全使用显式的交易指令,接管TS对持仓的控制,因为只要TimeFrame足够小,TS控制的行情触发单同样可以通过显式交易指令来实现。
     
  12. 多谢指点,你的想法是比较巧妙的,而且你使用PRINT的手法也很值得学习!

    因为双向操作有时既有多单也有空单,我仍然觉得有必要每个单子分开处理,比较符合人的理解,而且以后才好对每一仓位都分别加上有效的技术分析方法来加强网格的效能。

    看我在CHFJPY和EURJPY上拉的网,目前完全是机械的,但仍然效果不错。
     
  13. johnyj兄看来对外汇是行家,以后多多指教了!
    这个图是什么软件做的?也是可以类似ts/wld构建交易系统的么?
     
  14. 这是OANDA的JAVA平台,没法使用TS或者WLD,只能用JAVA API,月费600美元。

    网格交易法的每一格都可以使用技术分析手段,成功了就是获利,而失败了就变成拉网,因此根本没法用系统交易方法来实现模拟,我只能开个真实帐户来测试这种方法。
     
  15. 都是高手,向你们学习,。
     
  16. 典型的网格交易,你要是用MT编制一个自动交易,你用历史数据测试一下你就会发现,总有一天暴仓的时候
     
  17. 够狠。
     
  18. 说得没错,我现在手里就有这么系统的mt4版,曲线并不好看。