@@ -76,6 +76,7 @@ import { PineTS } from "pinets";
7676//import { PineTS, } from '../../../../../PineTS/src/PineTS.class'
7777
7878const dev = false
79+ //const dev = true
7980
8081type Props = {
8182 width : number ,
@@ -248,118 +249,123 @@ class KlineViewContainer extends Component<Props, State> {
248249
249250 const scripts = this . scripts || this . getSelectedIncicators ( )
250251
251- return fetchData ( source , baseSer , ticker , tframe , tzone , startTime , limit ) . then ( async latestTime => {
252- let start = performance . now ( )
253-
254- if ( ! this . state . isLoaded ) {
255- // reinit xc to get correct last occured time/row, should be called after data loaded to baseSer
256- console . log ( "reinit xc" )
257- xc . reinit ( )
258- }
252+ return fetchData ( source , baseSer , ticker , tframe , tzone , startTime , limit )
253+ . catch ( ex => {
254+ console . error ( ex . message )
255+ throw ex
256+ } )
257+ . then ( async latestTime => {
258+ let start = performance . now ( )
259259
260- // console.log(kvar.toArray().filter(k => k === undefined), "undefined klines in series");
260+ if ( ! this . state . isLoaded ) {
261+ // reinit xc to get correct last occured time/row, should be called after data loaded to baseSer
262+ console . log ( "reinit xc" )
263+ xc . reinit ( )
264+ }
261265
262- const provider = new TSerProvider ( kvar )
263- const fRuns = scripts . filter ( ( { script } ) => script !== undefined ) . map ( async ( { scriptName, script } ) => {
264- const pineTS = new PineTS ( provider , ticker , tframeToPineTimeframe ( tframe ) ) ;
266+ // console.log(kvar.toArray().filter(k => k === undefined), "undefined klines in series");
265267
266- return pineTS . ready ( ) . then ( ( ) =>
267- pineTS . run ( script ) . then ( result => ( { scriptName, result } ) )
268- . catch ( error => {
269- console . error ( error ) ;
268+ const provider = new TSerProvider ( kvar )
269+ const fRuns = scripts . filter ( ( { script } ) => script !== undefined ) . map ( async ( { scriptName, script } ) => {
270+ const pineTS = new PineTS ( provider , ticker , tframeToPineTimeframe ( tframe ) ) ;
270271
271- return { scriptName, result : undefined }
272- } ) )
273- } )
272+ return pineTS . ready ( ) . then ( ( ) =>
273+ pineTS . run ( script ) . then ( result => ( { scriptName, result } ) )
274+ . catch ( error => {
275+ console . error ( error ) ;
276+ throw error ;
277+ //return { scriptName, result: undefined }
278+ } ) )
279+ } )
274280
275- return Promise . all ( fRuns ) . then ( results => {
276- console . log ( `Scripts run in ${ performance . now ( ) - start } ms` ) ;
277-
278- start = performance . now ( ) ;
279-
280- const init = { overlayIndicators : [ ] , stackedIndicators : [ ] } as { overlayIndicators : Indicator [ ] , stackedIndicators : Indicator [ ] }
281-
282- const { overlayIndicators, stackedIndicators } =
283- results . reduce ( ( { overlayIndicators, stackedIndicators } , { scriptName, result } , n ) => {
284- if ( result ) {
285- // should use identity var name, here we use `${scriptName}_${n}`
286- const tvar = baseSer . varOf ( `${ scriptName } _${ n } ` ) as TVar < PineData [ ] > ;
287- const size = baseSer . size ( ) ;
288- const indicator = result . indicator ;
289- const plots = Object . values ( result . plots ) as Plot [ ] ;
290- const data = plots . map ( ( { data } ) => data ) ;
291- try {
292- for ( let i = 0 ; i < size ; i ++ ) {
293- const vs = data . map ( v => v ? v [ i ] : undefined ) ;
294- tvar . setByIndex ( i , vs ) ;
281+ return Promise . all ( fRuns ) . then ( results => {
282+ console . log ( `Scripts run in ${ performance . now ( ) - start } ms` ) ;
283+
284+ start = performance . now ( ) ;
285+
286+ const init = { overlayIndicators : [ ] , stackedIndicators : [ ] } as { overlayIndicators : Indicator [ ] , stackedIndicators : Indicator [ ] }
287+
288+ const { overlayIndicators, stackedIndicators } =
289+ results . reduce ( ( { overlayIndicators, stackedIndicators } , { scriptName, result } , n ) => {
290+ if ( result ) {
291+ // should use identity var name, here we use `${scriptName}_${n}`
292+ const tvar = baseSer . varOf ( `${ scriptName } _${ n } ` ) as TVar < PineData [ ] > ;
293+ const size = baseSer . size ( ) ;
294+ const indicator = result . indicator ;
295+ const plots = Object . values ( result . plots ) as Plot [ ] ;
296+ const data = plots . map ( ( { data } ) => data ) ;
297+ try {
298+ for ( let i = 0 ; i < size ; i ++ ) {
299+ const vs = data . map ( v => v ? v [ i ] : undefined ) ;
300+ tvar . setByIndex ( i , vs ) ;
301+ }
302+
303+ } catch ( error ) {
304+ console . error ( error , data )
295305 }
296306
297- } catch ( error ) {
298- console . error ( error , data )
299- }
307+ // console.log(result)
308+ console . log ( scriptName + ' data\n' , data )
309+ console . log ( scriptName + ' options\n' , plots . map ( x => x . options ) )
300310
301- // console.log(result)
302- console . log ( scriptName + ' data\n' , data )
303- console . log ( scriptName + ' options\n' , plots . map ( x => x . options ) )
311+ const isOverlayIndicator = indicator !== undefined && indicator . overlay
304312
305- const isOverlayIndicator = indicator !== undefined && indicator . overlay
313+ // plot1, plot2 from fill function
314+ const outputs = plots . reduce ( ( [ overlay , stacked ] , { title, plot1, plot2, options } , atIndex ) => {
315+ const style = options . style
316+ const location = options . location
306317
307- // plot1, plot2 from fill function
308- const outputs = plots . reduce ( ( [ overlay , stacked ] , { title, plot1, plot2, options } , atIndex ) => {
309- const style = options . style
310- const location = options . location
318+ //console.log(plot1, plot2)
311319
312- //console.log(plot1, plot2)
320+ const isOverlayOutput = ( style === 'shape' || style === 'char' )
321+ && ( location === 'abovebar' || location === 'belowbar' )
313322
314- const isOverlayOutput = ( style === 'shape' || style === 'char' )
315- && ( location === 'abovebar' || location === 'belowbar' )
323+ const output = { atIndex, title, plot1, plot2, options }
316324
317- const output = { atIndex, title, plot1, plot2, options }
325+ if ( isOverlayOutput || isOverlayIndicator ) {
326+ overlay . push ( output )
318327
319- if ( isOverlayOutput || isOverlayIndicator ) {
320- overlay . push ( output )
328+ } else {
329+ stacked . push ( output )
330+ }
321331
322- } else {
323- stacked . push ( output )
324- }
332+ return [ overlay , stacked ]
325333
326- return [ overlay , stacked ]
334+ } , [ [ ] , [ ] ] as Output [ ] [ ] )
327335
328- } , [ [ ] , [ ] ] as Output [ ] [ ] )
336+ if ( outputs [ 0 ] . length > 0 ) {
337+ overlayIndicators . push ( { scriptName, tvar, outputs : outputs [ 0 ] } )
338+ }
329339
330- if ( outputs [ 0 ] . length > 0 ) {
331- overlayIndicators . push ( { scriptName, tvar, outputs : outputs [ 0 ] } )
332- }
340+ if ( outputs [ 1 ] . length > 0 ) {
341+ stackedIndicators . push ( { scriptName, tvar, outputs : outputs [ 1 ] } )
342+ }
333343
334- if ( outputs [ 1 ] . length > 0 ) {
335- stackedIndicators . push ( { scriptName, tvar, outputs : outputs [ 1 ] } )
344+ console . log ( "overlay:" , overlayIndicators . map ( ind => ind . outputs ) , "\nstacked:" , stackedIndicators . map ( ind => ind . outputs ) )
336345 }
337346
338- console . log ( "overlay:" , overlayIndicators . map ( ind => ind . outputs ) , "\nstacked:" , stackedIndicators . map ( ind => ind . outputs ) )
339- }
347+ return { overlayIndicators, stackedIndicators }
340348
341- return { overlayIndicators , stackedIndicators }
349+ } , init )
342350
343- } , init )
351+ this . latestTime = latestTime ;
344352
345- this . latestTime = latestTime ;
353+ return this . updateState (
354+ {
355+ isLoaded : true ,
356+ updateEvent : { type : 'chart' , changed : this . state . updateEvent . changed + 1 } ,
357+ overlayIndicators,
358+ stackedIndicators,
359+ } ,
360+ ( ) => {
361+ if ( latestTime !== undefined && source === Source . binance ) {
362+ this . reloadDataTimeoutId = setTimeout ( ( ) => { this . currentLoading = this . fetchData_runScripts ( latestTime , 1000 ) } , 5000 )
363+ }
364+ } )
346365
347- return this . updateState (
348- {
349- isLoaded : true ,
350- updateEvent : { type : 'chart' , changed : this . state . updateEvent . changed + 1 } ,
351- overlayIndicators,
352- stackedIndicators,
353- } ,
354- ( ) => {
355- if ( latestTime !== undefined && source === Source . binance ) {
356- this . reloadDataTimeoutId = setTimeout ( ( ) => { this . currentLoading = this . fetchData_runScripts ( latestTime , 1000 ) } , 5000 )
357- }
358- } )
366+ } )
359367
360368 } )
361-
362- } )
363369 } )
364370
365371
@@ -767,13 +773,13 @@ class KlineViewContainer extends Component<Props, State> {
767773 clearTimeout ( this . reloadDataTimeoutId ) ;
768774 }
769775
770- return new Promise < void > ( ( resolve ) => {
776+ return new Promise < void > ( ( resolve , reject ) => {
771777 this . setState (
772778 { selectedIndicatorTags } ,
773779 ( ) => {
774- this . currentLoading = this . fetchData_runScripts ( this . latestTime , 1000 ) . then ( ( ) => {
775- resolve ( ) ;
776- } )
780+ this . currentLoading = this . fetchData_runScripts ( this . latestTime , 1000 )
781+ . catch ( ex => reject ( ex ) )
782+ . then ( ( ) => resolve ( ) )
777783
778784 return this . currentLoading
779785 } )
@@ -876,13 +882,13 @@ class KlineViewContainer extends Component<Props, State> {
876882 // NOTE When you call setState multiple times within the same synchronous block of code,
877883 // React batches these calls into a single update for performance reasons.
878884 // So we set isLoaded to false here and use callback.
879- return new Promise < void > ( ( resolve ) => {
885+ return new Promise < void > ( ( resolve , reject ) => {
880886 this . setState (
881887 { isLoaded : false } ,
882888 ( ) => {
883- this . currentLoading = this . fetchData_runScripts ( undefined , 1000 ) . then ( ( ) => {
884- resolve ( ) ;
885- } )
889+ this . currentLoading = this . fetchData_runScripts ( undefined , 1000 )
890+ . catch ( ex => reject ( ex ) )
891+ . then ( ( ) => resolve ( ) )
886892
887893 return this . currentLoading
888894 } )
@@ -900,14 +906,14 @@ class KlineViewContainer extends Component<Props, State> {
900906 this . kvar = this . baseSer . varOf ( KVAR_NAME ) as TVar < Kline > ;
901907 this . xc = new ChartXControl ( this . baseSer , this . width - ChartView . AXISY_WIDTH ) ;
902908
903- return new Promise < void > ( ( resolve ) => {
909+ return new Promise < void > ( ( resolve , reject ) => {
904910 console . log ( "runScripts ..." )
905911 this . setState (
906912 { isLoaded : false } ,
907913 ( ) => {
908- this . currentLoading = this . fetchData_runScripts ( undefined , 1000 ) . then ( ( ) => {
909- resolve ( ) ;
910- } )
914+ this . currentLoading = this . fetchData_runScripts ( undefined , 1000 )
915+ . catch ( ex => reject ( ex ) )
916+ . then ( ( ) => resolve ( ) )
911917
912918 return this . currentLoading
913919 } )
@@ -925,14 +931,14 @@ class KlineViewContainer extends Component<Props, State> {
925931 this . kvar = this . baseSer . varOf ( KVAR_NAME ) as TVar < Kline > ;
926932 this . xc = new ChartXControl ( this . baseSer , this . width - ChartView . AXISY_WIDTH ) ;
927933
928- return new Promise < void > ( ( resolve ) => {
934+ return new Promise < void > ( ( resolve , reject ) => {
929935 this . setState (
930936 {
931937 isLoaded : false ,
932938 } , ( ) => {
933- this . currentLoading = this . fetchData_runScripts ( undefined , 1000 ) . then ( ( ) => {
934- resolve ( ) ;
935- } )
939+ this . currentLoading = this . fetchData_runScripts ( undefined , 1000 )
940+ . catch ( ex => reject ( ex ) )
941+ . then ( ( ) => resolve ( ) )
936942
937943 return this . currentLoading
938944 } )
0 commit comments