# 金融 / 金融工程 / Dev ## `buySample()` 声明:function 买入策略范例,与系统环境变量有关 返回:boolean ### 示例 范例01:结果:0 ```tsl SetSysParam(PN_Stock(), 'SZ000002'); SetSysParam(pn_date(), inttodate(20121017)); return BuySample(); // 输出:0 ``` ## `portfolioCalcSys(sector, init_money, start_date, end_date, commission_charge, scale_restriction, negotiability, buy_c, sell_c, min_sell, min_buy, buy_price, sell_price, value_price, max_buy_rate, max_sell_rate, min_commission_charge, buy_unit, free_ze_rate, base_money, list_days)` 声明:function 交易策略测试(复杂参数,可选条件) | 参数 | 类型 | 说明 | | ----------------------- | ----------- | ----------------------------------------------------------------- | | `sector` | string | 字符串,市场板块 | | `init_money` | real | 整数,初始资金 | | `start_date` | t_date_time | 日期型时间,开始时间 | | `end_date` | t_date_time | 日期型时间,截止时间 | | `commission_charge` | real | 实数,手续费 | | `scale_restriction` | real | 实数,单支证券比例约束(0.1表示10%) | | `negotiability` | real | 实数,流通性(0.05表示5%) | | `buy_c` | expression | 布尔表达式,买入条件 | | `sell_c` | expression | 布尔表达式,卖出条件 | | `min_sell` | integer | 整数,最小卖出股数 | | `min_buy` | integer | 整数,最小买入股数 | | `buy_price` | expression | 用户自定义,表达式,买入价格 | | `sell_price` | expression | 用户自定义,表达式,卖出价格 | | `value_price` | expression | 用户自定义,表达式,计算价值 | | `max_buy_rate` | real | 实数,最大买入占的比例(和实际交易值比,0.1表示10%) | | `max_sell_rate` | real | 实数,最大卖出比例(和真实交易相比,0.1表示10%) | | `min_commission_charge` | real | 实数,最低手续费 | | `buy_unit` | integer | 整数,买入单位 | | `free_ze_rate` | real | 实数,冻结比例(当日卖出得到的资金不准许买入的比例,1表示全部冻结) | | `base_money` | real | 实数,最低操作资金(防止过多操作) | | `list_days` | integer | 整数,起码上市天数 | 返回:table_array ### 示例 范例01:调用函数 ```tsl return portfolioCalcSys( "申万煤炭开采", 1000000, intToDate(20120825), intToDate(20121023), 0, 0.2, 0.05, @(stockZf3()>8), @(stockZf3()<-3), 100, 100, @close(), @close(), @close(), 0.1, 0.1, 5, 100, 1, 10000, 100); ``` ## `selectStockIndex()` 声明:function 策略期间,股票组合的收益情况,等比投资,与周期有关 返回:table_array ### 示例 范例01:调用 SelectStockIndex ```tsl SetSysParam(PN_CYCLE(), cy_day()); ReturnSelectStockIndex("SZ000541;SH600000", inttodate(20120620), inttodate(20120930), @BsByMACD(12, 26, 9, 2), @SsByMACD(12, 26, 9, 2), "SH000001", 1); // 输出:股票组合array(SZ000541;SH600000)在2012-06-20到2012-09-30在给出的买入条件和卖出条件下的等比投资的交易情况 ``` ## `selectStockIndex_call(stock_arr, beg_t, end_t, select_cond, not_is_sell, sell_cond, not_need_stoped)` 声明:function 返回选股结果 | 参数 | 类型 | 说明 | | ----------------- | ---------- | ------------------------ | | `stock_arr` | array | 一维字符串数组,证券代码 | | `beg_t` | datetime | 日期型时间,开始选股时间 | | `end_t` | datetime | 日期型时间,结束选股时间 | | `select_cond` | expression | 布尔表达式,选股条件 | | `not_is_sell` | bool | 真假 | | `sell_cond` | expression | 布尔表达式,卖出股票条件 | | `not_need_stoped` | bool | 真假 | 返回:table_array ### 示例 范例01:调用 SelectStockIndex_Call ```tsl StockArr := array("SH600460", "SZ000920", "SH600718"); BegT := inttodate(20120925); EndT := inttodate(20121017); Return SelectStockIndex_Call(StockArr, BegT, EndT, @BsByMACD(12, 26, 9, 2), true, @SsByMACD(12, 26, 9, 2), false); ``` ## `sellSample()` 声明:function 卖出策略范例,与系统环境变量有关 返回:boolean ### 示例 范例01:调用 SellSample ```tsl SetSysParam(PN_Stock(), 'SZ000002'); SetSysParam(pn_date(), inttodate(20121017)); return sellSample(); // 判断万科A在2012-10-17在卖出策略sellSample()下是否卖出, 结果:0 ``` ## `setRetBuyPrice(price)` 声明:function 设置一个“tmpBuyPrice”自定义的系统变量的值 | 参数 | 类型 | 说明 | | ------- | ---- | ---------- | | `price` | real | 实数,价格 | 返回:integer ### 示例 范例01:调用 SetRetBuyPrice ```tsl SetRetBuyPrice(20); return GetSysParam('tmpBuyPrice'); // 输出:20 ``` ## `setRetSellPrice(price)` 声明:function 设置一个“tmpSellPrice”自定义的系统变量的值 | 参数 | 类型 | 说明 | | ------- | ---- | ---------- | | `price` | real | 实数,价格 | 返回:integer ### 示例 范例01:调用 SetRetSellPrice ```tsl SetRetSellPrice(50); return GetSysParam('tmpSellPrice'); // 输出:50 ``` ## `setStockBuyInfo(id, type, value)` 声明:function 设置Type类型对应的证券代码ID的值 | 参数 | 类型 | 说明 | | ------- | ------ | -------------------------------- | | `id` | string | 字符串,证券代码 | | `type` | string | 字符串,类型参数 | | `value` | real | 实数,Type类型对应的证券代码的值 | 返回:数据表类型数据 ### 示例 范例01:调用函数 ```tsl setStockBuyInfo("SZ000002", 'BuyPrice', 8); return getSysParam('BuyPrice'); ``` ## `setStockBuyTime(id, time)` 声明:function 设置ID的”BuyTime”对应的系统变量值 | 参数 | 类型 | 说明 | | ------ | ----------- | ---------------- | | `id` | string | 字符串,证券代码 | | `time` | t_date_time | 日期型时间 | 返回:数据表类型的数据 ### 示例 范例01:调用函数 ```tsl setStockBuyTime('SZ000002', intToDate(20121023)); return getSysParam("BuyTime"); ``` ## `stockBuyDiff()` 声明:function 现价与买入价的差异比,用于做交易策略用,与当前股票有关 返回:real ### 示例 范例01:调用 StockBuyDiff ```tsl SetStockBuyPrice('SZ000002', 8.04); SetSysParam(PN_Stock(), 'SZ000002'); return StockBuyDiff(); // 输出:4.47761 ``` ## `stockBuyTimeDiff()` 声明:function 返回买入天数,作为交易策略用,与当前股票、时间有关 返回:integer ### 示例 范例01:调用 StockBuyTimeDiff ```tsl SetStockBuyTime('SZ000002', inttodate(20121018)); SetSysParam(pn_date(), inttodate(20121022)); SetSysParam(PN_Stock(), 'SZ000002'); return StockBuyTimeDiff(); // 输出:4 ``` ## `stockHoldingCount()` 声明:function 返回当前默认证券代码设置了'HoldingInfo'的系统变量值 返回:number ### 示例 范例01:调用函数 ```tsl setStockBuyInfo('SZ000002', 'HoldingInfo', 9); setSysParam(Pn_Stock(), 'SZ000002'); return stockHoldingCount(); // 输出:9 ``` ## `tradingByJSZB(stocks, beg_t, end_t, buy_cond, sell_cond, zs_by_df, zy_by_zf, mc_by_ts)` 声明:function 个股买卖测试 | 参数 | 类型 | 说明 | | ----------- | ----------- | -------------------------------- | | `stocks` | string | 字符串,以分号分割,股票列表 | | `beg_t` | t_date_time | 日期型时间,个股买卖测试开始时间 | | `end_t` | t_date_time | 日期型时间,个股买卖测试截止时间 | | `buy_cond` | expression | 表达式,个股买入条件 | | `sell_cond` | expression | 表达式,个股卖出条件 | | `zs_by_df` | real | 实数,跌幅止损(%) | | `zy_by_zf` | real | 实数,涨幅止盈(%) | | `mc_by_ts` | integer | 整数,持有N日后自动平仓 | 返回:table_array ### 示例 范例01:调用函数 ```tsl return tradingByJszb( "SZ000002;SH600718;SH600000;SH600001", intToDate(20100125), intToDate(20101017), @isHighOfNDay(0), @close()