开拓者期货期权程序化系统交易论坛

标题: 请教怎么自动平部分仓? [打印本页]

作者: bigfrog    时间: 2008-10-11 15:30:50     标题: 请教怎么自动平部分仓?

//------------------------------------------------------------------------
// 简称: a1
// 名称:
// 类别: 交易指令
// 类型: 多头建仓
// 输出:
//------------------------------------------------------------------------

Params
        Numeric m1(60);

Vars
        Numeric lots(1);
        Numeric type(0);
    NumericSeries ma1(0);
        Numeric rmp(0);
begin
                rmp = MarketPosition;
                ma1=AverageFC(close,m1);
                type = ContractUnit;
                if (type == 5) //以 1/3仓位开仓
                lots= Trunc(CurrentCapital/Close*2*0.35,0);
                Else
                lots= Trunc(CurrentCapital/Close*0.35,0);
       

        if (rmp == 0)
                {
                        if (CrossOver(close,ma1))// 上穿60均线开仓
                       {
                        Buy(lots,High,True);
                         return;
                        }
                        if (Crossunder(close,ma1)) // 下穿60均线开仓
                       {
                        SellShort(lots,Low,True);
                         return;
                        }

       
                }
       
        if (rmp ==1)
                {
                  If (( MaxPositionProfit/A_CurrentEquity > 0.1) And  (A_BuyPosition > Ceiling(lots/2,1)+1)) //想实现赢利10%平仓一半
                        {
                        Sell(Ceiling(lots/2,1),Low);
                        Return;
                        }
                       
                  If (Close <ma1)
                        {
                        Sell(0,low,True);
                        SellShort(lots,low,true);
                        Return;
                        }
                }

        if (rmp ==-1)
                {
                  If (( MaxPositionProfit/A_CurrentEquity > 0.1) And  (A_SellPosition > Ceiling(lots/2,1)+1)) //
                        {
                        BuyToCover(Ceiling(lots/2,1),Low);
                        Return;
                        }
                       
                  If (Close >ma1)
                        {
                        BuyToCover(0,low,True);
                        Buy(lots,low,true);
                        Return;
                        }
                }

End


//------------------------------------------------------------------------
// 编译版本        GS2004.06.12
// 用户版本        2008/10/11 14:52
// 版权所有        bigfrog
// 更改声明        TradeBlazer Software保留对TradeBlazer平台
//                        每一版本的TrabeBlazer公式修改和重写的权利
//------------------------------------------------------------------------



进出场都简单的
就是当中实现平半仓的功能没办法实现
请管理员帮忙看一下
这里平半仓怎么写?


[ 本帖最后由 bigfrog 于 2008-10-14 13:37 编辑 ]
作者: nopain    时间: 2008-10-11 15:48:23

直接用Sell/BuyToCover,数量设置为仓位的一半就可以了。

您这里的问题在于您在历史中使用A函数,这样是不行的。A函数只能在最后一个Bar使用
作者: bigfrog    时间: 2008-10-11 16:01:39


A函数有点理解了

但我仓位是变化的,是根据资金的1/3来计算仓位的
也是变化的

还有一点
就是条件满足后出了一半后,再次条件满足又会出掉一半
这样仓就给平没了

我想实现就平一次半仓,是为了保护资金用的
请问这里代码应该怎么写?
作者: bigfrog    时间: 2008-10-11 21:04:42

管理员
有办法吗

这里的代码可以怎么改?




欢迎光临 开拓者期货期权程序化系统交易论坛 (http://bbs.tb18.net/) Powered by Discuz! X2