.. | _main | _trailmove | _tick |
The timing of startup depends on the setting of candlestick chart minute-hour information. Parameters are currency pair symbol to be processed.
mst_symbol.xml (symbol information) | :target="1" |
mst_rate.xml (candlestick chart minute-hour information) | :target="1" AND isimpl="1" |
tst_implparam.xml (execution plans) | :isimpl="1" |
Called when the floating profit and loss (P/L) of an opened position changes.
tradeid | :position ID |
trailmove | :method name of adjusting trailing stop/limit |
Called when the market data changes. Parameters are currency pair symbol to be processed.
mst_symbol.xml (symbol information) | :target="1" |
.. | _CDate | _CFile | _Log | _Params | PriceBar | Tag | Trade |
Create an instance of _CDate that represents the date and time.
// When no parameters are provided, the newly-created _CDate object represents the current date and time. var date = new _CDate(); // The parameter "time" specifies seconds starting from 1970-01-01 00:00 (UTC). var date = new _CDate(946652399); // The parameter "dateString" specifies the string representing the date. var date = new _CDate("2019"); var date = new _CDate("2019/5/31"); var date = new _CDate("2019-5-31 17:50:10");
// Set or get seconds starting from 1970-01-01 00:00 (UTC). date.time = 946652399; _Print(date.time); // Get the year, month, day, hour, minute, second, week of the local time. // All are read-only properties. // Weeks are from 0 (Sunday) to 6 (Saturday), and months are from 1 (January) to 12 (December). _Print("%d-%d-%d %d:%d:%d [%d]", date.year, date.month, date.day, date.hour, date.min, date.sec, date.week);
// Returns the sum of "100" and second of the date object. _Print(date.addTime(100)); // Add "100" to second of the date object. date.addinTime(100); // Specify 'UTC'(World Standard Time) or'LOCAL'(Local Time) in the "timeKind". Set or get seconds starting from 1970-01-01 00:00. _Print(date.getTime('UTC')); _Print(date.getTime('LOCAL')); date.setTime(946652399, 'UTC'); date.setTime(946652399, 'LOCAL'); // Specify 'UTC'(World Standard Time) or 'LOCAL'(Local Time) in "calKind". Specify the format string in "format". Return a string representing the date and time. For more information about format, please refer to the format description of the C-Language function "strftime". _Print(date.toString()); _Print(date.toString('UTC')); _Print(date.toString('LOCAL')); _Print(date.toString('UTC', '%Y-%m-%d %H:%I:%S')); _Print(date.toString('LOCAL', '%Y-%m-%d %H:%I:%S'));
Create an instance of _CFile on file input and output.
// Specify a file name or path. var file = new _CFile("m5.csv");
// Read from file. file.open(); var content = file.read(); file.close(); // Read each line of the file. file.open(); while (line = file.readline()) { _Print(line); } file.close(); // Write file. file.open('w'); file.write('Hello!'); file.close(); // Write file using append mode. file.open('a'); file.write('Hello!'); file.close();
Static class for log output.
// Set or get four output levels, including "error", "warn", "info" and "debug". _Log.level = "error"; _Print(_Log.level);
// Output log at "info" level. _Log.info("open position.");
A map object that handles [key, value] as strings.
// Set and get values for the 'a' and 'b' keys. It will remain unless it is updated. _Params['a'] = 123.01; _Params.b = '456'; _Print("%s %d %f %s %s\n", _Params.a, _Params.a, _Params.a, _Params['b']); ↓↓↓ The following results are output. ↓↓↓ 123.01 123 123.010000 456
Historical data object (Read Only).
// Get historical data objects to reference properties. var pb = _GetPriceBarByPos("EUR/USD", "m5", 0); _Print("%s %d %s %f", pb.symbol, pb.startdate, pb.period, pb.askClose);
Tag object
// Get label name and label value. var implparam = _GetImplParam("EUR/USD"); _Print("Tag Name:%s, Tag Value:%s", implparam.name, implparam.value);
// Confirm whether the tag has a specified property. Print(implparam.hasAttr("name") ? "OK" : "NG"); // Get the value of specified property from the tag. Print(implparam.getAttr("name")); // Set tag property. implparam.setAttr("name", "order_open"); // Find the subtag that matches the specified name and value from the tag tree of the current tag. var tag = implparam.getTag("method", ""); // Find the subtag that matches the specified name sequence from the tag tree of the current tag. var tag = implparam.getTagByNames("method", "property"); // Find the subtag that matches the specified name, property (key, value) from the tag tree of the current tag. var tag = implparam.getTagByAttr("property", "periods", "m5");
Position object (Read Only)
// Get an opened position object to reference properties. var trade = _GetTrade("70043871"); _Print("%s %s %f %f", trade.symbol, trade.isbuy ? "buy" : "sell", trade.amount, trade.open);
Set the stop of an opened position.
// Specify the position ID to set stop. var ret = _ChangeStopLoss("70043871", 118.9); _Print(ret == 0 ? "succeeded" : "failed");
Set the limit of an opened position.
// Specify position ID to set limit. var ret = _ChangeTakeProfit("70043871", 120.5); _Print(ret == 0 ? "succeeded" : "failed");
Close all positions.
var count = _CloseAllTrade(); _Print("%d positions were closed.", count);
Close an opened position.
// Specify ID and amount of an opened position to close this position. var ret = _CloseTradeById("70043871", 1000); _Print(ret == 0 ? "succeeded" : "failed");
Close an opened position.
// Specify symbol and order type ('B':buy, 'S':sell) of an opened position to close this position. var ret = _CloseTradesBySymbol("EUR/USD", 'S'); _Print(ret == 0 ? "succeeded" : "failed");
Get the number of historical data.
// Specify symbol and period (m5, m15, m30, H1, H4, D1) to get the number of historical data. var count = _GetBarCount("EUR/USD", "m5"); _Print("The number of historical data [%s %s] is %d.", "EUR/USD", "m5", count);
Get a list of closed positions.
// Specify symbol to get a list of closed positions. var lst = _GetClosedTradeListBySymbol("EUR/USD"); for (var i = 0; i < lst.length; i++) { _Print("%s: %f", lst[i].id, lst[i].amount); }
Get the execution plan object.
// Specify symbol to get the execution plan object. var implparam = _GetImplParam("EUR/USD"); if (implparam !== undefined) { _Print("Name of implparam is %s.", implparam.getAttr("name")); }
Get a list of historical data from local.
// Specify symbol and period to obtain 100 historical data after 2018/5/1 8:00:00 from local. var pbList = _GetLaterPriceBarFromLocal("EUR/USD", "m5", "2018/5/1 8:00:00", 100); _Print("%d historical data were returned.", plList.length);
Get the market closing time (seconds starting from 1970-01-01 00:00 (UTC)).
// Close all positions one hour before the market closes. var dtNow = _CDate(); if (dtNow.time >= _GetMarketCloseTime(dtNow.time) - 3600) { _CloseAllTrade(); }
Get the method object.
// Specify symbol and method name to get the properties of the method. var loadMethod = _GetMethod("EUR/USD", "LoadContext"); if (loadMethod !== undefined) { _Print("Vaule of property[orderstop_from_wday] is %s.", loadMethod.getAttr("orderstop_from_wday")); }
Get a list of method objects.
// Specify symbol to get all the defined object of method. var orderMethodList = _GetMethods("EUR/USD"); if (orderMethodList !== undefined) { _Print("%d methods were defined.", orderMethodList.length); }
Get exchange rate information.
// Get the current market price the symbol can be bought at. var ask = _GetOffer("EUR/USD", "Ask"); // Get the current market price the symbol can be sold at. var bid = _GetOffer("EUR/USD", "Bid"); // Get the highest Ask price (buy price) of the symbol for the current trading day. var high = _GetOffer("EUR/USD", "High"); // Get the lowest Bid price (sell price) of the symbol for the current trading day. var low = _GetOffer("EUR/USD", "Low");
Get the PointSize.
// Get the PointSize of EUR/USD (0.0001). var pointsize = _GetPointSize("EUR/USD"); _Print("PointSize of %s is %f.", "EUR/USD", pointsize);
Get a list of price.
// Specify symbol and period to get the open price of the ask bar. var askopen = _GetPriceArray("EUR/USD", "m5", "AskOpen"); // You can also specify symbol and period, first to obtain the historical data from the local, and then get the open price of the ask bar from the list of historical data. var pbList = _GetLaterPriceBarFromLocal("EUR/USD", "m5", "2018/5/1 8:00:00", 100); var askopen = _GetPriceArray(pbList, "AskOpen"); // Specify symbol and period to get the close price of the ask bar. var askclose = _GetPriceArray("EUR/USD", "m5", "AskClose"); // Specify symbol and period to get the highest price of the ask bar. var askhigh = _GetPriceArray("EUR/USD", "m5", "AskHigh"); // Specify symbol and period to get the lowest price of the ask bar. var asklow = _GetPriceArray("EUR/USD", "m5", "AskLow"); // Specify symbol and period to get the open price of the bid bar. var bidopen = _GetPriceArray("EUR/USD", "m5", "BidOpen"); // Specify symbol and period to get the close price of the bid bar. var bidclose = _GetPriceArray("EUR/USD", "m5", "BidClose"); // Specify symbol and period to get the highest price of the bid bar. var bidhigh = _GetPriceArray("EUR/USD", "m5", "BidHigh"); // Specify symbol and period to get the lowest price of the bid bar. var bidlow = _GetPriceArray("EUR/USD", "m5", "BidLow");
Get historical data from local.
// Specify symbol and period to get 100 local historical data before 2018/5/1 8:00:00. var pbList = _GetPriceBarFromLocal("EUR/USD", "m5", "2018/5/1 8:00:00", 100); _Print("%d historical data were returned.", plList.length);
Get the spread.
// Get the spread of "EUR/USD". var spread = _GetSpread("EUR/USD"); _Print("Spread of %s is %f.", "EUR/USD", spread);
Get the standard time (seconds starting from 1970-01-01 00:00 (UTC).
Get the seconds of period.
// Get the seconds of "m5". var seconds = _GetTimeByPeriod("m5"); _Print("Seconds of %s is %d.", "m5", seconds);
Get the object of opened position.
// Specify the position ID to get the object of opened position. var trade = _GetTrade("70043871"); _Print("%s %s %f %f", trade.symbol, trade.isbuy ? "buy" : "sell", trade.amount, trade.open);
Get a list of opened positions.
// Specify the symbol to get a list of opened positions. var lst = _GetTradeListBySymbol("EUR/USD"); for (var i = 0; i < lst.length; i++) { _Print("%s: %f", lst[i].id, lst[i].amount); }
Get the name of trade method.
// Specify an order ID to get the name of trade method. var name = _GetTradeMethodName("50043871"); _Print("Name of TradeMethod is %s.", name);
Get the period of trade method.
// Specify an order ID to get the period of trade method. var period = _GetTradeMethodPeriod("50043871"); _Print("Period of TradeMethod is %s.", period);
Get the name of trailing move.
// Specify an order ID to get the name of trailing move. var name = _GetTrailMoveTsName("50043871"); _Print("Name of TrailMove is %s.", name);
Load the execution plan from the database.
// Load the execution plan according to the specified parameter name, and then join the symbol name to obtain the parameter value. _LoadImplPlan("ImplKbn", "Amount", "OpenMethod1", "OpenMethod2", "OpenMethod3"); _Print("ImplKbn:%s, Amount:%s, OpenMethod1:%s, OpenMethod2:%s, OpenMethod3:%s", _Params["EUR/USD" + "ImplKbn"], _Params["EUR/USD" + "Amount"], _Params["EUR/USD" + "OpenMethod1"], _Params["EUR/USD" + "OpenMethod2"], _Params["EUR/USD" + "OpenMethod3"]);
Create a directory.
// Create a directory with relative paths. var out_dir = "dat/USD-JPY"; var ret = _Mkdir(out_dir); if (ret == -1) { _Print("Directory [%s] creation failed.\n", out_dir); } else if (ret == 0) { _Print("Directory [%s] creation succeeded.\n", out_dir); } else if (ret == 1) { _Print("Directory [%s] already exists.\n", out_dir); }
Convert numbers to strings.
// Convert a number to a string. var num = 12.54 _Print("%f => %s\n", num, _Num2Str(num)); // Convert an array of numbers to an array of strings. var num_ary = [12.54, 78.66, 43]; var str_ary = _Num2Str(num_ary); for (var i = 0; i < num_ary.length; i++) { _Print("%f => %s\n", num_ary[i], str_ary(i)); }
Open position at market price.
// Specify the buy, amount, stop and limit, to open position at market price. var orderId = _OpenMarketOrder("EUR/USD", "B", 1000, 1.1168, 1.1390); _Print("Market order [%s] is opened.", orderId);
Output historical data to standard output.
// Specify the symbol and period to output historical data to standard output. _OutPriceBar("EUR/USD", "m5");
Output to standard output according to the format string.
// Output in decimal, hexadecimal, octal format. _Print("%d %x %o", 123, 123, 123); // Output in floating point and exponent format. _Print("%f %e", 0.0123, 0.0123); // Output as a string. _Print("%s\n", "Close a position.");
Set the name of trade method.
// Specify an order ID to set the name of trade method. _SetTradeMethodName("50043871", "MacdCross");
Set the period of trade method.
// Specify an order ID to set the period of trade method. _SetTradeMethodPeriod("50043871", "m5");
Set the name of trailing move.
// Specify an order ID to set the name of trailing move. _SetTrailMoveTsName("50043871", "trailmove_eqdiff?40:1:20:10");
Set the context of trade method. [C1-CN] can specify up to 40.
// Specify an order ID to set the context of trade method. var title = "INDEX_1,SCORE_1,INDEX_2,SCORE_2,INDEX_3,SCORE_3"; var pred = [2, 0.992, 0, 0.005, 1, 0.003]; _SetTsContext("50043871", 'S', "", title, pred);
Set the trailing move in context of trade method.
// Specify an order ID to set the trailing move in context of trade method. _SetTsContextTrail("50043871", 'S', "m5", "MacdCross", "", "", "trailmove_eqdiff?40:1:20:10");
Create technical indicators.
// Specify symbol and period to create SMA technical indicators. var sma = _TA("SMA", "EUR/USD", "m5", "AskClose", 200); _Print("The number of SMA is %d.", sma.real.length); // You can also specify symbol and period, first to obtain historical data from the local, and then create technical indicators from the list of historical data. var pbList = _GetLaterPriceBarFromLocal("EUR/USD", "m5", "2018/5/1 8:00:00", 360); var sma = _TA("SMA", pbList, "AskClose", 200); // Specify symbol and period to create MINUS_DM technical indicators. var minus_dm = _TA("MINUS_DM", "EUR/USD", "m5", "AskClose", 14); _Print("The number of MINUS_DM is %d.", minus_dm.real.length); // Specify symbol and period to create MINUS_DI technical indicators. var minus_di = _TA("MINUS_DI", "EUR/USD", "m5", "AskClose", 14); _Print("The number of MINUS_DI is %d.", minus_di.real.length); // Specify symbol and period to create PLUS_DM technical indicators. var plus_dm = _TA("PLUS_DM", "EUR/USD", "m5", "AskClose", 14); _Print("The number of PLUS_DM is %d.", plus_dm.real.length); // Specify symbol and period to create PLUS_DI technical indicators. var plus_di = _TA("PLUS_DI", "EUR/USD", "m5", "AskClose", 14); _Print("The number of PLUS_DI is %d.", plus_di.real.length); // Specify symbol and period to create DX technical indicators. var dx = _TA("DX", "EUR/USD", "m5", "AskClose", 14); _Print("The number of DX is %d.", dx.real.length); // Specify symbol and period to create ADX technical indicators. var adx = _TA("ADX", "EUR/USD", "m5", "AskClose", 14); _Print("The number of ADX is %d.", adx.real.length); // Specify symbol and period to create ADXR technical indicators. var adxr = _TA("ADXR", "EUR/USD", "m5", "AskClose", 14); _Print("The number of ADXR is %d.", adxr.real.length); // Specify symbol and period to create EMA technical indicators. var ema = _TA("EMA", "EUR/USD", "m5", "AskClose", 14); _Print("The number of EMA is %d.", ema.real.length); // Specify symbol and period to create WMA technical indicators. var wma = _TA("WMA", "EUR/USD", "m5", "AskClose", 14); _Print("The number of WMA is %d.", wma.real.length); // Specify symbol and period to create MACD technical indicators. var macd = _TA("MACD", "EUR/USD", "m5", "AskClose", 12, 26, 9); _Print("The number of MACD is %d %d %d.", macd.macd.length, macd.signal.length, macd.hist.length); // Specify symbol and period to create RSI technical indicators. var rsi = _TA("RSI", "EUR/USD", "m5", "AskClose", 14); _Print("The number of RSI is %d.", rsi.real.length); // Specify symbol and period to create CMO technical indicators. var cmo = _TA("CMO", "EUR/USD", "m5", "AskClose", 14); _Print("The number of CMO is %d.", cmo.real.length); // Specify symbol and period to create SAR technical indicators. var sar = _TA("SAR", "EUR/USD", "m5", "AskClose", 0.02, 0.2); _Print("The number of SAR is %d.", sar.real.length); // Specify symbol and period to create STOCH technical indicators. // Option: [Ask, Bid] [TA_MAType_SMA, TA_MAType_EMA, TA_MAType_WMA] var stoch = _TA("STOCH", "EUR/USD", "m5", "Ask", 73, 19, "TA_MAType_EMA", 19, "TA_MAType_EMA"); _Print("The number of STOCH is %d %d.", macd.slowk.length, macd.slowd.length); // Specify symbol and period to create BBAND technical indicators. var bband = _TA("BBAND", "EUR/USD", "m5", "AskClose", 14, 2.0, 2.0, "TA_MAType_EMA"); _Print("The number of BBAND is %d.", bband.upperband.length, bband.middleband.length, bband.lowerband.length); // Specify symbol and period to create MIN technical indicators. var min = _TA("MIN", "EUR/USD", "m5", "AskClose", 14); _Print("The number of MIN is %d.", min.real.length); // Specify symbol and period to create MAX technical indicators. var max = _TA("MAX", "EUR/USD", "m5", "AskClose", 14); _Print("The number of MAX is %d.", max.real.length); // Specify symbol and period to create MOM technical indicators. var mom = _TA("MOM", "EUR/USD", "m5", "AskClose", 14); _Print("The number of MOM is %d.", mom.real.length); // Specify symbol and period to create ROC technical indicators. var roc = _TA("ROC", "EUR/USD", "m5", "AskClose", 14); _Print("The number of ROC is %d.", roc.real.length); // Specify symbol and period to create ROCP technical indicators. var rocp = _TA("ROCP", "EUR/USD", "m5", "AskClose", 14); _Print("The number of ROCP is %d.", rocp.real.length); // Specify symbol and period to create ROCR technical indicators. var rocr = _TA("ROCR", "EUR/USD", "m5", "AskClose", 14); _Print("The number of ROCR is %d.", rocr.real.length); // Specify symbol and period to create APO technical indicators. var apo = _TA("APO", "EUR/USD", "m5", "AskClose", 12, 26, "TA_MAType_EMA"); _Print("The number of APO is %d.", apo.real.length); // Specify symbol and period to create PPO technical indicators. var ppo = _TA("PPO", "EUR/USD", "m5", "AskClose", 12, 26, "TA_MAType_EMA"); _Print("The number of PPO is %d.", ppo.real.length); // Specify symbol and period to create STDDEV technical indicators. var stddev = _TA("STDDEV", "EUR/USD", "m5", "AskClose", 14, 1.5); _Print("The number of STDDEV is %d.", stddev.real.length); // Specify symbol and period to create ATR technical indicators. var atr = _TA("ATR", "EUR/USD", "m5", "AskClose", 14); _Print("The number of ATR is %d.", atr.real.length);
Predict buy-and-sell signals based on the pre-trained models.
// The two-dimensional array bitmap generated by technical indicators is passed to CNN model to predict buy-and-sell signals. var pred = _TfClassify("MacdCrossCNN", bitmap, 224, 224, 1, 3); if (pred) { for (var i = 0; i < pred.length; i+=2) { _Print("index:%d score:%f\n", pred[i], pred[i+1]); } }
Output strings to file, which will be saved in the ./tmp directory.
// Save strings in file opened with a new mode. _WriteFile("servertime", "2019/5/28 14:05:10", "1"); // Save strings in file opened with a append mode. _WriteFile("servertime", "2019/5/28 14:05:10", "2");
Create an image file in PNG format.
// Create a monochrome image. var image_height = 224; var image_width = 224; var canvas = []; for(var i = 0; i < image_height; i++){ canvas[i] = []; for(var j = 0; j < image_width; j++){ canvas[i][j] = 0; } } var bitmap = Array.prototype.concat.apply([], canvas); _WritePng('test.png', bitmap, image_height, image_width, 1);