// Strategy main loop
while (true) {
// Obtain the K-line data for current trading pairs and contracts
var r = _C(exchange.GetRecords)
// Obtain the length of the K-line data, i.e. l
var l = r.length
// Judge the K-line length that l must be greater than the indicator period (if not, the indicator function cannot calculate valid indicator data), or it will be recycled
if (l < Math.max(ema1Period, ema2Period)) {
// Wait for 1,000 milliseconds, i.e. 1 second, to avoid rotating too fast
Sleep(1000)
// Ignore the code after the current if, and execute while loop again
continue
}
// Calculate ema indicator data
var ema1 = TA.EMA(r, ema1Period)
var ema2 = TA.EMA(r, ema2Period)
// Drawing chart
$.PlotRecords(r, 'K-Line') // Drawing the K-line chart
// When the last BAR timestamp changes, i.e. when a new K-line BAR is created
if(preTime !== r[l - 1].Time){
// The last update of the last BAR before the new BAR appears
$.PlotLine('ema1', ema1[l - 2], r[l - 2].Time)
$.PlotLine('ema2', ema2[l - 2], r[l - 2].Time)
// Draw the indicator line of the new BAR, i.e. the indicator data on the current last BAR
$.PlotLine('ema1', ema1[l - 1], r[l - 1].Time)
$.PlotLine('ema2', ema2[l - 1], r[l - 1].Time)
// Update the timestamp used for comparison
preTime = r[l - 1].Time
} else {
// When no new BARs are generated, only the indicator data of the last BAR on the chart is updated
$.PlotLine('ema1', ema1[l - 1], r[l - 1].Time)
$.PlotLine('ema2', ema2[l - 1], r[l - 1].Time)
}
// Conditions for opening long positions, turning points
var up = (ema1[l - 2] > ema1[l - 3] && ema1[l - 4] > ema1[l - 3]) && (ema2[l - 2] > ema2[l - 3] && ema2[l - 4] > ema2[l - 3])
// Conditions for opening short positions, turning points
var down = (ema1[l - 2] < ema1[l - 3] && ema1[l - 4] < ema1[l - 3]) && (ema2[l - 2] < ema2[l - 3] && ema2[l - 4] < ema2[l - 3])
To be continued...
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
If you enjoyed what you read here, create your account today and start earning FREE STEEM!