在云那方

首页 新随笔 联系 订阅 管理
索引:
1) 图表的第一根k线,或者是新的一天
2) 求当天第一根Bar到现在的BAR数
3) TB的时间表示
4) 收盘平仓的例子
5) 限制连续建仓
6) 主动的加仓示例
7) CurrentContracts函数 获得当前持仓的持仓合约数。
8) 止损止盈的编写
9) buy,sell函数注意事项
 
内容:
1) 图表的第一根k线,或者是新的一天
If(CurrentBar == 0 || Date != Date[1])
 
2) 求当天第一根Bar到现在的BAR数
// 使用普通变量
Vars
  Numeric TodayBars;
Begin
  TodayBars = 0;
  While ( CurrentBar > TodayBars and 
    date[TodayBars] == date[TodayBars+1] )
  {
    TodayBars = TodayBars + 1;
  }
  Commentary("TodayBars = " + text(TodayBars));
End
 
// 使用序列变量
Vars
  NumericSeries ReBars;
Begin
  If(CurrentBar == 0 || Date != Date[1])
  {
    ReBars = 0;
  }Else
  {
    ReBars = ReBars + 1;
  }
  Return ReBars;
End
 
3) TB的时间表示
Time()函数表示当前公式应用商品在当前Bar的时间, 如果当前时间为11:34:21.356,Time返回值为0.113421356
函数中传递时间的时候可以传个整形,然后乘以一个小数, 如:
Numeric TradeEndTime(145500);
if (Time <= RangeEndTime * 0.000001)
 
4) 收盘平仓的例子
// 收盘平仓
If ((Date[-1]!=InvalidInteger && Date!=Date[-1])||(Date[-1]==InvalidInteger
&& Date < CurrentDate))   //代码中将消失的信号补上
{
   Sell(0,Close);
   BuyToCover(0,Close);
}
Else If (Date==CurrentDate && Time==0.1455 && CurrentTime>=0.1459)
{
  Sell(0,Close); 
  BuyToCover(0,Close); 
}
 
5) 限制连续建仓
MarketPosition获得当前持仓状态。
-1 当前位置为持空仓
0 当前位置为持平
1 当前位置为持多仓
 
// 在买卖指令的条件中加入MarketPosition条件, 以限制连续建仓
If (MarketPosition <> 1 and MA1[1] > MA2[1])
{
  Buy(Lots,Open);
If (MarketPosition <> -1 and MA1[1] < MA2[1])
{
  SellShort(lots,Open); 
}   
 
6) 主动的加仓示例
//加仓部分代码
If (MarketPosition <> 1 and MA1[1] > MA2[1])  //限制连续建仓
{
  Buy(Lots,Open);
} else if (marketposition == 1 and MA1[1] > MA2[1] and high > High[1]) //主动的加仓
{
  Buy(Lots,Max(Open,High[1]));
}
 
If (MarketPosition <> -1 and MA1[1] < MA2[1])  //限制连续建仓
{
  SellShort(lots,Open); 
} Else if (marketposition == -1 and MA1[1] < MA2[1] and low < Low[1])  //主动的加仓
{
  SellShort(Lots,Min(Open,Low[1]));
}
 
7) CurrentContracts函数 获得当前持仓的持仓合约数。如:当前空头持仓2手,CurrentContracts则返回-2。
 
8) 止损止盈的编写
固定跳数的止盈或止损的写法:
// EntryPrice() 获得当前持仓的第一个建仓价格。
// PriceScale() 当前公式应用商品的计数单位。
// MinMove() 当前公式应用商品的最小变动量。
if (MarketPosition==1)
{
  TargetPrice = EntryPrice + TakeProfit * MinMove * PriceScale;
  StopPrice = EntryPrice – Stoploss * MinMove * PriceScale;
  if (High >= TargetPrice) Sell(0, Max(Open, TargetPrice));
  Else if ( Low <= StopPrice) Sell(0, Min(Open, StopPrice));
}
 
//价格百分比的止盈或止损的写法:
  TargetPrice = EntryPrice * (1+ TakeProfit * 0.01);
  StopPrice = EntryPrice * (1 – Stoploss * 0.01);
 
9) buy,sell函数注意事项
buy,sell函数如果有止损止盈指令就必须限制在开仓bar不平仓,当然也会导致在开仓bar可能导致损失较大,
否则,就会有在开仓bar同时执行开平仓。
这没办法,buy,sell是通过图表信号去驱动指令的,当图表上一条K线即满足开仓、平仓时它就同时执行。
开仓后就止损止盈平仓指令用A_SendOrder发单, A_SendOrder可以满足即时发单,止损止盈具体算法需要用户自己写
posted on 2013-03-20 15:43  Rich.T  阅读(553)  评论(0编辑  收藏  举报