惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

博客园 - liuym

三 ICE开发初级研究 二 示例程序一 - liuym - 博客园 一 VC2008环境中ICE的配置 无法定位序数 3109 于LIBEAY32.dll - liuym 转:Virtual List的使用方法 转:VC++编程之CListCtrl控件的使用 2 转:VC++编程之CListCtrl控件的使用 好用 转:删除,修改注册表中需要设置权限的项 转:软件能够修复硬盘吗?―硬盘损坏全分析 转:CWnd 对象怎么和 HWND 窗口句柄相互转化 VC 多文档窗口 子窗口最大化时切换窗口 窗口没有最大化显示的问题 转:Windows VC6编译安装Boost库 转:送给那些一心想要传送文件的朋友(TCP协议).cpp-from CSDN 转:如何去了解、熟悉一个已经开发完的项目 进行维护、二次开发或者升级 递归创建文件夹 DLL中资源和主程序资源冲突 - liuym - 博客园 转:一种巧妙的删除程序自己的方法 转:双缓冲图形刷新技术 和 WGF双缓冲绘图框架 软件保护建议(转)
TB 编程整理
liuym · 2012-06-22 · via 博客园 - liuym

索引:

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可以满足即时发单,止损止盈具体算法需要用户自己写