Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How_To_ Introduce_TradingView

英 谷口
October 25, 2017
790

How_To_ Introduce_TradingView

英 谷口

October 25, 2017
Tweet

Transcript

  1. ؔ਺໊ ࣮ߦλΠϛϯά ֓ཁ PO3FBEZ ىಈ࣌ શମઃఆσʔλऔಘ HFU4FSWFS5JNF PO3FBEZ࣮ߦޙ αʔό࣌ࠁऔಘ TFBSDI4ZNCPMT#Z/BNF

    γϯϘϧมߋ࣌ γϯϘϧݕࡧ݁Ռσʔλऔಘ SFTPMWF4ZNCPM γϯϘϧܾఆ࣌ γϯϘϧৄࡉσʔλऔಘ DBMDVMBUF)JTUPSZ%FQUI HFU#BST࣮ߦલ σʔλಡΈࠐΈظؒઃఆ HFU#BST σʔλදࣔ࣌ PIMDσʔλσʔλऔಘ TVCTDSJCF#BST ॳճHFU#BST࣮ߦޙ ϦΞϧλΠϜσʔλऔಘ VOTVCTDSJCF#BST γϯϘϧมߋલ ϦΞϧλΠϜσʔλऔಘղআ HFU.BSLT HFU#BST࣮ߦޙ νϟʔτϚʔΫઃఆ HFU5JNFTDBMF.BSLT HFU#BST࣮ߦޙ νϟʔτԼ෦ϚʔΫઃఆ σʔλϑΟʔυඞਢϝιουҰཡ
  2. ίϯετϥΫλ ˣ PO3FBEZ ˣ HFU4FSWFS5JNF ˣ SFTPMWF4ZNCPM ˣ DBMDVMBUF)JTUPSZ%FQUI ˣ

    HFU#BST ˣ TVCTDSJCF#BST ˣ HFU.BSLT ˣ HFU5JNFTDBMF.BSLT ྫॳظදࣔ
  3. TFBSDI4ZNCPMT#Z/BNF ˣ SFTPMWF4ZNCPM ˣ VOTVCTDSJCF#BST ˣ DBMDVMBUF)JTUPSZ%FQUI ˣ HFU#BST ˣ

    TVCTDSJCF#BST ˣ HFU.BSLT ˣ HFU5JNFTDBMF.BSLT ྫγϯϘϧ੾Γସ͑
  4. ίϯετϥΫλྫ ZaifChart = function() {
 this._datafeedURL = location.protocol + '//'

    + location.hostname + '/zaif_chart_api/v1/';
 this._configuration = {};
 this._callbacks = {};
 this._initialize = function(){
 var that = this;
 this._send(this._datafeedURL + 'config', {})
 .done(function(response) {
 $.each( JSON.parse(response), function( key, value ) {
 that._configuration[key] = value;
 });
 that._fireEvent('configuration_ready');
 })
 .fail(function(reason) {
 that.logMessage(reason);
 throw new Error(reason)
 });
 }; this._initialize(); };
  5. \ TVQQPSUFE@SFTPMVUJPOT< %  %  %  8 >

     TZNCPMT@UZQFT< \ OBNFશ௨՟  WBMVF ^  \ OBNFओཁ௨՟  WBMVFlNBJO@DVSSFODJFT ^  \ OBNFτʔΫϯ  WBMVFUPLFOT ^ > ^ ؆୯ͳશମઃఆͷྫ
  6. σʔλϑΟʔυ಺෦ؔ਺ͦͷ̍ɿϦΫΤετૹ৴ this._send = function(url, params) {
 var request = url;


    if (params) {
 for (var i = 0; i < Object.keys(params).length; ++i) {
 var key = Object.keys(params)[i];
 var value = encodeURIComponent(params[key]);
 request += (i === 0 ? '?' : '&') + key + '=' + value;
 }
 }
 this.logMessage('request url:' + request);
 return $.ajax({
 type: 'GET',
 url: request,
 contentType: 'text/plain'
 });
 };
  7. σʔλϑΟʔυ಺෦ؔ਺ͦͷ̎ɿΠϕϯτൃՐ this._fireEvent = function(event, argument) {
 if (this._callbacks.hasOwnProperty(event)) {
 var

    callbacksChain = this._callbacks[event];
 for (var i = 0; i < callbacksChain.length; ++i) {
 callbacksChain[i](argument);
 }
 this._callbacks[event] = [];
 }
 };
  8. PO3FBEZྫ ZaifChart.prototype.onReady = function(callback) {
 var that = this;
 if

    (this._configuration) {
 setTimeout(function() {
 callback(that._configuration);
 }, 0);
 } else {
 this._on('configuration_ready', function() {
 callback(that._configuration);
 });
 }
 };
  9. TFSDI4ZNCPM#Z/BNFྫ 
 ZaifChart.prototype.searchSymbolsByName = function(userInput, exchange, symbolType, onResultReadyCallback) {
 this._send(this._datafeedURL

    + 'search', {
 user_input: userInput,
 symbol_type: symbolType
 })
 .done(function(response) {
 var data = JSON.parse(response);
 onResultReadyCallback(data);
 })
 .fail(function(reason) {
 this.logMessage('searchSymbolsByName error:’ + reason);
 onResultReadyCallback([]);
 });
 };
  10. < \  UZQFNBJO@DVSSFODJFT   TZNCPM#5$@+1:   UJDLFSCUD@KQZ

      EFTDSJQUJPOϏοτίΠϯ೔ຊԁऔҾ   GVMM@OBNF#5$@+1: ^  \  UZQFUPLFO   TZNCPM1&1&$"4)@+1:   UJDLFSQFQFDBTI@KQZ   EFTDSJQUJPO1&1&$"4)೔ຊԁऔҾ   GVMM@OBNF1&1&$"4)@+1: ^  ɾɾɾɾɾɾɾ > ؆қγϯϘϧ৘ใϦετͷྫ
  11. SFTPMWF4ZNCPMྫ 
 ZaifChart.prototype.resolveSymbol = function(symbolName, onCallback, onErrorCallback) {
 this._send(this._datafeedURL +

    'symbols', {symbol: symbolName})
 .done(function(response) {
 var data = JSON.parse(response);
 onCallback(data);
 })
 .fail(function(reason) {
 this.logMessage('resolveSymbol error:’ + reason);
 onErrorCallback('unknown_symbol');
 });
 };
  12. \ UJNF[POF"TJB5PLZP  TFTTJPO  lOBNFCUD@KQZ  UJDLFSCUD@KQZ  UZQFNBJO@DVSSFODJFT

     TVQQPSUFE@SFTPMVUJPOT<  % 8 .> ^ ৄࡉ͸↓ https://github.com/tradingview/charting_library/wiki/Symbology#symbolinfo-structure ৄࡉγϯϘϧ৘ใͷྫ
  13. HFU#BSTྫ 
 ZaifChart.prototype.getBars = function(symbolInfo, resolution, from, to, onCallback, onErrorCallback)

    {
 var that = this;
 this._send(this._datafeedURL + 'history', {
 symbol: symbolInfo.name,
 resolution: resolution,
 from: from,
 to: to
 })
 .done(function(response) {
 var data = JSON.parse(response);
 var noData = data.ohlc_data.length === 0;
 var bars = [];
 var barsCount = noData ? 0 : data.ohlc_data.length;
 for (var i = 0; i < barsCount; ++i) {
 var barValue = {
 time: data.ohlc_data[i].time,
 close: data.ohlc_data[i].close,
 open: data.ohlc_data[i].open,
 high: data.ohlc_data[i].high,
 low: data.ohlc_data[i].low,
 volume: data.ohlc_data[i].volume
 };
 bars.push(barValue);
 }
 onCallback(bars, { noData: noData});
 })
 .fail(function(reason) {
 that.logMessage('getBars error:'+ reason);
 onErrorCallback(‘network error');
 });
 };
  14. දࣔ͢Δ0)-$σʔλΛऔಘ ɹɾҾ਺ ɹɹɹTZNCPM*OGPɿγϯϘϧ৘ใ ɹɹɹSFTPMVUJPOɿִؒ ͱ͔%ͱ͔  GSPNɿσʔλ։࢝࣌ࠁ VOJYUJNF  UPɿσʔλओྗ࣌ࠁ

    VOJYUJNF  ɹɹɹPO$BMMCBDLɿ஋ઃఆ ɹɹɹPO&SSPS$BMMCBDLɿΤϥʔϝοηʔδ ɹɾҾ਺͸ඵ୯Ґ͕ͩɺPO$BMMCBDLʹ͸ ɹɹϛϦඵ୯ҐͰUJNFΛઃఆ͢Δඞཁ͋Γ
  15. \  PIMD@EBUB<  \   IJHI  

     MPX    UJNF    DMPTF    PQFO  ^   \   IJHI    MPX    UJNF    DMPTF    PQFO  ^  > ^
  16. TVCTDSJCF#BSTྫ //ίϯετϥΫλͰνϟʔτσʔλΛߋ৽͢ΔΫϥεΛ࢓ࠐΜͰ͓͘
 ZaifChart = function() { ɾɾɾɾɾɾ
 this._barsPulseUpdater = new

    ZaifChartDataPulseUpdater(this, 1000); ɹɹɾɾɾɾɾɾ }; 
 ZaifChart.prototype.subscribeBars = function(symbolInfo, resolution, onRealtimeCallback, listenerGUID, onResetCallback) {
 this._barsPulseUpdater.subscribeDataListener(symbolInfo, resolution, onRealtimeCallback, listenerGUID, onResetCallback);
 };
  17. ZaifChartDataPulseUpdater = function(datafeed, interval) {
 this._datafeed = datafeed;
 this._subscribers =

    {};
 this._requestsPending = false;
 setInterval(_update, interval);
 };
 
 ZaifChartDataPulseUpdater.prototype.subscribeDataListener = function(symbolInfo, resolution, newDataCallback, listenerGUID) {
 this._datafeed.logMessage('Subscribing ' + listenerGUID);
 
 if (!this._subscribers.hasOwnProperty(listenerGUID)) {
 this._subscribers[listenerGUID] = {
 symbolInfo: symbolInfo,
 resolution: resolution,
 lastBarTime: NaN,
 listeners: []
 };
 }
 
 this._subscribers[listenerGUID].listeners.push(newDataCallback);
 };

  18. ZaifChartDataPulseUpdater = function(datafeed, interval) {
 var that = this;
 var

    _update = function() {
 for (var listenerGUID in that._subscribers) {
 var subscriptionRecord = that._subscribers[listenerGUID];
 var resolution = subscriptionRecord.resolution;
 (function(_subscript) {
 that._datafeed.getBars(_subscript.symbolInfo, resolution, null, null, function(bars, updateBarsFlg) {
 that._requestsPending = false;
 var lastBar = bars[bars.length - 1];
 var subscribers = _subscript.listeners;
 var isNewBar = lastBar.time > _subscript.lastBarTime;
 if (isNewBar) {
 var previousBar = bars[bars.length - 2];
 for (var i = 0; i < subscribers.length; ++i) {
 subscribers[i](previousBar);
 }
 }
 _subscript.lastBarTime = lastBar.time;
 for (var i = 0; i < subscribers.length; ++i) {
 subscribers[i](lastBar);
 }
 },
 function() {
 }, null);
 })(subscriptionRecord);
 }
 };
 };
  19. VOTVCTDSJCF#BSTྫ 
 
 ZaifChart.prototype.subscribeBars = function(listenerGUID) {
 this._barsPulseUpdater.unsubscribeDataListener(listenerGUID);
 };
 


    ZaifChartDataPulseUpdater.prototype.subscribeDataListener = function(listenerGUID) {
 delete this._subscribers[listenerGUID];
 };

  20. ͦͷଞ //औಘ͢ΔOHLCσʔλͷൣғΛมߋ͢Δ ZaifChart.prototype.calculateHistoryDepth = function(resolution, resolutionBack, intervalBack) {
 };
 //νϟʔτϚʔΫදࣔ


    ZaifChart.prototype.getMarks = function(symbolInfo, startDate, endDate, onDataCallback, resolution) {
 onDataCallback([]);
 };
 //νϟʔτԼ෦ϚʔΫදࣔ
 ZaifChart.prototype.getTimescaleMarks = function(symbolInfo, startDate, endDate, onDataCallback, resolution) {
 onDataCallback([]);
 };
 ৄࡉ͸↓ https://github.com/tradingview/charting_library/wiki/JS-Api#getmarkssymbolinfo-startdate-enddate- ondatacallback-resolution-optional
  21. (function() {
 TradingView.onready(function () { //४උ͕Ͱ͖͔ͯΒ࣮ߦ͞ΕΔ
 new TradingView.widget({ //֤ΦϓγϣϯΛઃఆ
 fullscreen:

    true,
 symbol: ‘btc_jpy’,
 interval: 'D',
 container_id: 'trading-view-area',
 datafeed: new ZaifChart(),
 library_path: '/js/charting_library/',
 locale: 'ja',
 disabled_features: ["use_localstorage_for_settings"],
 enabled_features: ["study_templates"],
 charts_storage_url: 'http://saveload.tradingview.com',
 charts_storage_api_version: "1.1",
 client_id: 'tradingview.com',
 user_id: 'public_user_id'
 });
 });
 })(); ৄࡉ͸Լه↓ https://github.com/tradingview/charting_library/wiki/Widget-Constructor ؔ਺Λ࣮ߦ