Files
zszq-trs/YLErpWeb/wwwroot/Scripts/app/UserDefinedStructure.js
T
2024-05-09 14:06:26 +08:00

282 lines
11 KiB
JavaScript

// 查询自定义结构
function queryStructure() {
var name = $("#UserDefineStructure :selected").text();
main.post("/UserDefinedStructure/QueryUserDefinedStructure", { structureName: name }).done(function (res) {
if (res != null) {
$("#StructureName").val(res.StructureName);
$("#StructureCode").val(res.StructureCode);
$("#PricingMethod").val(res.PricingMethod);
$("#userDefinedFields").empty();
var li = "";
var selectId = "";
for (var i = 0; i < res.Fields.length; ++i) {
selectId = "fieldDefSelect" + res.Fields[i].FieldName;
li = "";
li += "<li><input type='text' style='width:35%' value='"
li += res.Fields[i].FieldDisplayName;
li += "'/><input type='text' style='width:35%' value='";
li += res.Fields[i].FieldName;
li += "'/><select id='";
li += selectId;
li += "' style='width:20%'><option value='Bool'>Bool</option><option value='Double'>Double</option><option value='String'>String</option><option value='Date'>Date</option></select>";
li += "<a href='#' class='delete'><span class='glyphicon glyphicon-remove'></span></a>";
li += "</li>";
$("#userDefinedFields").append(li);
$("#" + selectId).get(0).selectedIndex = res.Fields[i].FieldType;
}
// 设置payoff源代码的显示
$("#PayoffDescription").text(res.PayoffDescription);
editor.getDoc().setValue(res.PayoffDescription);
editor.refresh();
$("#PriceDistribution").val(res.PriceDistribution);
$(".delete").click(function () {
$(this).parent().remove();
});
// 刷新“定价测试”区域的自定义字段
refreshNewFieldInPricingTest(res.Fields);
$("#StructureName").attr("disabled", true);
}
});
}
// 新建一个结构
function createStructure() {
$("#StructureName").attr("disabled", false);
$("#StructureName").val("");
$("#StructureCode").val("");
$("#PricingMethod").val("无");
$("#userDefinedFields").empty();
var sampleCode = "\
/// \n\
/// 预处理蒙特卡洛模拟的参数,以获得更高的模拟效率。\n\
/// 默认仅生成每一个观察日对应的时间序列\n\
/// \n\
/// valueDate: 估值日期\n\
/// r: 无风险利率\n\
public override float[] PrepareMC(Date valueDate, double r)\n\
{\n\
return base.PrepareMC(valueDate, r);\n\
}\n\
\n\
/// \n\
/// 蒙特卡洛模拟中对单一价格路径的处理函数\n\
/// \n\
/// st: 价格序列\n\
/// r: 无风险利率\n\
public override float DiscountedPayoffForOnePath(float[] st, float r)\n\
{\n\
return (float)0.0;\n\
}";
editor.getDoc().setValue(sampleCode);
editor.refresh();
}
// 结构定义新增字段
function addNewField() {
var li = "<li><input type='text' style='width:35%'/><input type='text' style='width:35%'/>";
li += "<select style='width:20%'><option value='Bool'>Bool</option><option value='Double'>Double</option><option value='String'>String</option><option value='Date'>Date</option></select>";
li += "<a href='#' class='delete'><span class='glyphicon glyphicon-remove'></span></a></li>"
$("#userDefinedFields").append(li);
$(".delete").click(function () {
$(this).parent().remove();
});
}
// 从当前页面获取自定义结构信息
function getCurrentStructure() {
var structure = {};
structure.StructureName = $("#StructureName").val();
structure.StructureCode = $("#StructureCode").val();
structure.PricingMethod = $("#PricingMethod option:selected").val();
structure.FieldDefinitions = getUserDefinedFields();
structure.PayoffDescription = editor.getDoc().getValue();
structure.PriceDistribution = $("#PriceDistribution option:selected").val();
return structure;
}
// 保存自定义结构
function saveStructure() {
var structure = getCurrentStructure();
var saveNew = !$("#StructureName").prop("disabled");
main.post("/UserDefinedStructure/SaveUserDefinedStructure", { req: structure, saveNew: saveNew }).done(function (res) {
window.location.reload();
});
}
// 删除自定义结构
function deleteStructure() {
var name = $("#StructureName").val();
main.confirm("确认删除" + name + "吗?",
function () {
main.post("/UserDefinedStructure/DeleteStructureByName", { name: $("#StructureName").val() }).done(function (res) {
window.location.reload();
});
}
);
}
// 获取自定义字段列表
function getUserDefinedFields() {
var fields = [];
$("#userDefinedFields").find("li").each(function () {
var field = {};
field.FieldDisplayName = $(this).find("input").eq(0).val();
field.FieldName = $(this).find("input").eq(1).val();
field.FieldType = $(this).find("select").val();
fields.push(field);
});
return JSON.stringify(fields);
}
// 在定价测试中刷新结构字段
function refreshFields() {
//var name = $("#UserDefineStructure :selected").text();
var name = $("#StructureName").val();
main.post("/UserDefinedStructure/QueryUserDefinedStructure", { structureName: name }).done(function (res) {
refreshNewFieldInPricingTest(res.Fields);
});
}
// 在定价测试中刷新结构字段
function refreshNewFieldInPricingTest(fields) {
$("#divTestOptionFields").empty();
var html = "";
var inputId = "";
for (var i = 0; i < fields.length; ++i) {
inputId = "Test" + fields[i].FieldName;
html += "<div class='search-group'><label class='search-label'>";
html += fields[i].FieldDisplayName;
html += "</label>";
if (fields[i].FieldType == 3) {
html += "<input id='";
html += inputId;
html += "' class='search-input datepicker' type='text'></div>";
} else if (fields[i].FieldType == 0) {
html += "<select id='"
html += inputId;
html += "'><option value='true'>是</option><option value='false'>否</option></select></div>";
} else {
html += "<input id='";
html += inputId;
html += "' class='search-input' type='text'></div>";
}
}
$("#divTestOptionFields").append(html);
ResetDatePicker();
}
// 编译自定义的结构
function compileSourceCode() {
var structure = getCurrentStructure();
main.post("/UserDefinedStructure/CompileUserDefineSourceCode", { req: structure }).done(function (res) {
});
}
// 试定价
function calcStructure() {
var structure = getCurrentStructure();
var request = {};
request.req = structure;
request.baseFieldStr = getBaseFieldsForPricingTest();
request.customizedFieldStr = getCustomizedFieldsForPricingTest(structure.FieldDefinitions);
request.valueDate = "2020-07-28";
request.spot = 1;
request.r = 0.05;
request.q = 0.05;
request.vol = 0.25;
request.calcGreeks = $("#checkBoxCalcGreeks").is(":checked");
main.post("/UserDefinedStructure/PricingTestUserDefineSourceCode", request).done(function (res) {
$("#resultPv").text(isNaN(res.Pv) ? "0.00" : res.Pv.toFixed(2));
$("#resultDelta").text(isNaN(res.Delta) ? "0.00" : res.Delta.toFixed(2));
$("#resultGamma").text(isNaN(res.Gamma) ? "0.00" : res.Gamma.toFixed(2));
$("#resultVega").text(isNaN(res.Vega) ? "0.00" : res.Vega.toFixed(2));
$("#resultTheta").text(isNaN(res.Theta) ? "0.00" : res.Theta.toFixed(2));
$("#resultRho").text(isNaN(res.Rho) ? "0.00" : res.Rho.toFixed(2));
});
}
// 为试定价获取基础字段
function getBaseFieldsForPricingTest() {
var fields = {};
fields.StartDate = $("#TestStartDate").val();
fields.MaturityDate = $("#TestMaturityDate").val();
fields.ObservationDateStr = $("#setObservationDatesBtn").data("observationdates");
return JSON.stringify(fields);
}
// 为试定价获取自定义字段
function getCustomizedFieldsForPricingTest(defStr) {
var fields = [];
var fieldDefs = JSON.parse(defStr);
fieldDefs.forEach(function (val, i) {
var field = {};
field.Name = val.FieldName;
field.Type = val.FieldType;
field.Value = $("#Test" + val.FieldName).val();
fields.push(field);
});
return JSON.stringify(fields);
}
// 发布结构
function publishStructure() {
var structure = getCurrentStructure();
main.post("/UserDefinedStructure/PublishUserDefinedStructure", { req: structure }).done(function (res) {
window.location.reload();
});
}
// 设置观察日
function setObservationDates(thisobj, title1 = "", title2 = "", isTitle1Percent = false, isTitle2Percent = false, defaultTitle1Value = 0, defaultTitle2Value = 0) {
var startTime = $("#TestStartDate").val();
var endTime = $("#TestMaturityDate").val();
if (main.isEmpty(startTime)) {
main.message("请输入成交日期");
return false;
}
if (main.isEmpty(endTime)) {
main.message("请输入到期日期");
return false;
}
var observationDates = $(thisobj).data("observationdates");
var observationNum = $(thisobj).data("observationnum");
var observationUnit = $(thisobj).data("observationunit");
var observationHolidayType = $(thisobj).data("observationholidaytype");
var alignEnd = true;
var btnId = $(thisobj).attr('id');
sessionStorage.setItem(btnId + "_observationdates", observationDates);
layer.open({
type: 2,
title: "设置自定义观察日",
shadeClose: false,
shade: 0.4,
area: ['500px', '560px'],
content: "/trade/tradeObservationDates?observationNum=" + observationNum + "&observationUnit=" + observationUnit + "&observationHolidayType=" + observationHolidayType + "&startTime=" + startTime + "&endTime=" + endTime + "&alignEnd=" + alignEnd + "&btnId=" + btnId + "&title1=" + title1 + "&title2=" + title2 + "&isTitle1Percent=" + isTitle1Percent + "&isTitle2Percent=" + isTitle2Percent + "&defaultTitle1Value=" + defaultTitle1Value + "&defaultTitle2Value=" + defaultTitle2Value
});
}
// 设置观察日窗口返回的函数
function getObservationDatesSetting(observationNum, observationUnit, observationHolidayType, observationDates, alignEnd, btnId) {
$("#" + btnId).data("observationdates", observationDates);
$("#" + btnId).data("observationnum", observationNum);
$("#" + btnId).data("observationunit", observationUnit);
$("#" + btnId).data("observationholidaytype", observationHolidayType);
$("#" + btnId).data("alignend", alignEnd);
}
// 重置页面上所有日期控件
function ResetDatePicker() {
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
showOtherMonths: true,
selectOtherMonths: true
});
};