‘‘‘backtest
start: 2020-05-01 00:00:00
end: 2021-08-10 00:00:00
period: 15m
exchanges: [{"eid":"Futures_OKCoin","currency":"BTC_USD"}]
‘‘‘
import pandas as pd
import time
def main():
exchange.SetContractType("quarter")
preTime = 0
Log(exchange.GetAccount())
while True:
records = exchange.GetRecords(PERIOD_M15)
if records and records[-2].Time > preTime:
preTime = records[-2].Time
doTicker(records[:-1])
Sleep(1000 *60)
def doTicker(records):
#Log(‘onTick‘,exchange.GetTicker())
M15 = pd.DataFrame(records)
#Factor=3
#Pd=7
M15.columns = [‘time‘,‘open‘,‘high‘,‘low‘,‘close‘,‘volume‘,‘OpenInterest‘]
#HL2
M15[‘hl2‘]=(M15[‘high‘]+M15[‘low‘])/2
#ATR(PD)
length=Pd
M15[‘prev_close‘]=M15[‘close‘].shift(1)
ranges= [M15[‘high‘] - M15[‘low‘],M15[‘high‘]-M15[‘prev_close‘],M15[‘low‘]-M15[‘prev_close‘]]
M15[‘tr‘] = pd.DataFrame(ranges).T.abs().max(axis=1)
alpha = (1.0 / length) if length > 0 else 0.5
M15[‘atr‘]=M15[‘tr‘].ewm(alpha=alpha, min_periods=length).mean()
M15[‘Up‘]=M15[‘hl2‘]-(Factor*M15[‘atr‘])
M15[‘Dn‘]=M15[‘hl2‘]+(Factor*M15[‘atr‘])
M15[‘TrendUp‘]=0.0
M15[‘TrendDown‘]=0.0
M15[‘Trend‘]=1
M15[‘Tsl‘]=0.0
M15[‘linecolor‘]=‘Homily‘
M15 = M15.fillna(0)
for x in range(len(M15)):
M15[‘TrendUp‘].values[x] = max(M15[‘Up‘].values[x],M15[‘TrendUp‘].values[