代码中如何根据资金情况限定Buy的Size?

Discussion in 'Wealth-Lab Developer' started by installman, Oct 20, 2009.

  1. 太痛苦了,新学。
    请指点一下,您一句话可以让我少华10几个钟头,把精力放在策略的优化上来,谢谢。
     
  2. Size Property
    double Size

    Returns the dollar size of the Position

    查了手册,position的size返回的竟然是价钱。

    我的意思是,限定一次买多少手,比如海龟法则有要求,根据波动情况和风险控制额度限定买入的数量,谢谢。
     
  3. 目前只能在Symbol栏设置,其他请看稍详细点的内容:
    In portfolio simulation mode, all trades are pre-executed using 1 share per Position, and then position sizing is applied after the fact. So the Size property will always be based on 1 share while the Strategy is executing.
    The Size property is always available to Performance Visualizers, which execute after the position sizing has been applied.
    耐心等待5.6版的posSizer()功能的释放吧。
     
  4. 非常感激!
    我在Symbol栏设置Start capital=20000,percent of equity=25%,发现所有信号出现的情况下,也只买到75%的仓位,还25%的现金没有进行买卖,为什么会这样呢?
    代码如下:
    //本策略使用.NET框架System命名空间中的Math类,WL5本身不再提供有关数学,字符串,文件操作方法,分别将以.NET框架Math,StreamReader, StreamWrite,String等类来代替。
    using System;
    //本策略使用.NET框架System.Drawing命名空间中相关的类。WL5部分绘制方法的颜色和字体参数需要此命名空间。
    using System.Drawing;
    //本策略使用WealthLab命名空间中相关的类,WealthLab命名空间包含WL5本身提供发展策略所需类(每个成员的详细说明参考WealthScript QuickRef文档)如Alert Object,Bars Object,ChartPane Object,DataSeries Object,FundamentalItem Object,Position Object,SymbolInfo Object和WealthScript基类以及其它高级开发的基类等等。
    using WealthLab;
    //WL5本身提供了包含超过100多个技术指标,使用指标需要包含此命名空间。
    using WealthLab.Indicators;

    namespace WealthLab.Strategies
    {
    //WL5中每个策略定义为类并且都从WealthScript基类继承所有方法与属性,WealthScript基类定义了WealthScript QuickRef参考文档罗列的Common Signals,Cosmetic Chart,Data Access,Fundamental Data,Math,Position Management,System,Time Frames,Trading类别中的所有方法与属性和虚方法Execute。
    public class MyStrategy : WealthScript
    {

    StrategyParameter strategyParameter1;
    StrategyParameter strategyParameter2;
    public MyStrategy()
    {
    strategyParameter1 = CreateParameter("avglineterm", 60, 20, 60, 10);
    strategyParameter2 = CreateParameter("stopfactor", 1, 1, 2, 0.5);
    }

    double N,L1, L2, L3, L4, LE, LS, Tick ;

    //WL5每个策略需要重定义继承来自WealthScript基类虚方法Execute,它是策略的主体也是执行点。
    protected override void Execute()
    {
    int LongTerm, ATRParam, StopTerm;
    //时间参数设置 
    LongTerm = strategyParameter1.ValueInt ;
    ATRParam = 20 ;
    StopTerm = strategyParameter2.ValueInt ;
    //绘制止损与退出点 
    PlotStops();
    Tick = Bars.SymbolInfo.Tick;
    //绘制通道指标
    PlotSeries(PricePane,SMA.Series(Close,LongTerm),Color.Blue,LineStyle.Solid,2);

    SMA smaFast = SMA.Series(Close, LongTerm);

    //WL5中每个交易系统或者策略都有一个主体循环
    for(int bar = LongTerm; bar < Bars.Count; bar++)
    {
    //计算系统一多头退出价,跌破60日均线日退出?
    LE = SMA.Series(Close, LongTerm)[bar]-Tick ;
    //计算波动幅度
    N = ATR.Series(Bars,ATRParam)[bar];
    //头次交易,交易次数为零时的进场
    if (Positions.Count == 0 )
    {
    L1 = SMA.Series(Close, LongTerm)[bar];
    L2 = L1 + 0.5 * N ;
    L3 = L1 + 1.0 * N ;
    L4 = L1 + 1.5 * N ;
    BuyAtStop( bar+1,L1, "L1" );
    BuyAtStop( bar+1,L2, "L1" );
    BuyAtStop( bar+1,L3, "L1" );
    BuyAtStop( bar+1,L4, "L1" );
    }
    else//Positions.Count的else
    //加仓
    {
    if (ActivePositions.Count==3 )
    {
    BuyAtStop( bar+1,L4, "L4" );
    }
    if(ActivePositions.Count==2 )
    {
    BuyAtStop( bar+1,L3, "L3" );
    BuyAtStop( bar+1,L4, "L4" );
    }
    if (ActivePositions.Count==1 )
    {
    BuyAtStop( bar+1,L2, "L2" );
    BuyAtStop( bar+1,L3, "L3" );
    BuyAtStop( bar+1,L4, "L4" );
    }
    //不是头次交易,清空头寸之后的再次进场
    if (ActivePositions.Count==0 )
    {
    //重新计算进场价
    L1 = SMA.Series(Close, LongTerm)[bar];
    //设置加码仓价
    L2 = L1 + 0.5 * N ;
    L3 = L1 + 1.0 * N ;
    L4 = L1 + 1.5 * N ;
    BuyAtStop( bar+1,L1, "L1" );
    BuyAtStop( bar+1,L2, "L1" );
    BuyAtStop( bar+1,L3, "L1" );
    BuyAtStop( bar+1,L4, "L1" );
    }
    }//结束Positions.Count的else


    //设置头寸止损价格
    //WL5新特征:也可用for循环搭配ActivePositions形式,直接以活动头寸介入,运行效率更高,下同
    foreach( Position p in Positions )
    {
    if ( p.Active )
    if(p.PositionType==PositionType.Long)
    LS=p.EntryPrice-StopTerm*N;
    }
    //头寸止损与退出条件
    foreach( Position p in Positions )
    {
    if ( p.Active )
    if(p.PositionType==PositionType.Long)
    if(p.EntryBar < bar+1)
    SellAtStop( bar+1,p, LS, "LongStop");
    if(p.PositionType==PositionType.Long)
    if(p.EntryBar < bar+1)
    SellAtStop( bar+1, p, LE, "LongExit");
    }
    }//结束系统主循环
    }
    }
    }
     
  5. 基本上解决了,看来是手续费的原因,导致最后一个买入资金不够25%以致不能执行,呵呵。
    改成24.x%就可以了。