Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何为我的背部测试仪实现美元跟踪停止?_Python 3.x_Quantitative Finance_Algorithmic Trading_Trading_Back Testing - Fatal编程技术网

Python 3.x 如何为我的背部测试仪实现美元跟踪停止?

Python 3.x 如何为我的背部测试仪实现美元跟踪停止?,python-3.x,quantitative-finance,algorithmic-trading,trading,back-testing,Python 3.x,Quantitative Finance,Algorithmic Trading,Trading,Back Testing,现在,我的回溯测试仪测试了一个策略买入,即获利回吐0.025%,止损回吐0.048%。我正试图在超过27美元后,激活高于27美元的回吐利润。正在努力构建此代码,非常感谢您的帮助,请参阅下面的代码 BTC_TICKER = 'BTC' END_DATE = dt.datetime(2019, 11, 1, 0, 0, 0) START_DATE = dt.datetime(2018, 1, 1, 0, 0, 0) # get ohlc OHLC_BTC = get_ohlc_cryptocom

现在,我的回溯测试仪测试了一个策略买入,即获利回吐0.025%,止损回吐0.048%。我正试图在超过27美元后,激活高于27美元的回吐利润。正在努力构建此代码,非常感谢您的帮助,请参阅下面的代码

BTC_TICKER = 'BTC'

END_DATE = dt.datetime(2019, 11, 1, 0, 0, 0)
START_DATE = dt.datetime(2018, 1, 1, 0, 0, 0)

# get ohlc
OHLC_BTC = get_ohlc_cryptocompare(BTC_TICKER, USD_TICKER, START_DATE,
                                  end_date=END_DATE, interval_key='hour')
OHLC_BTC = hlp.correct_ohlc_df(OHLC_BTC, frequency=TIMEFRAME)

# create dfs in format that Strategy requires
closes_df = pd.concat([OHLC_BTC['Close']],
                      axis=1, keys=[BTC_TICKER])

# use imported indicator to create weights
macd_df = technicals.macd(OHLC_BTC[['Close']])
weights_df = closes_df.copy()


`TAKE_PROFIT = 0.025
STOP_LOSS = -0.048
pnl = 0
pnl_dollars = 0

i = 0
for index, row in macd_df.iterrows():
    if i < 3:
        weights_df.at[index, BTC_TICKER] = 0

    # no positions cases
    elif weights_df.values[i-1] == 0:
        if (macd_df['MACDdiff_6_23'].values[i-2] < 0
                and macd_df['MACDdiff_6_23'].values[i-1] > 0):
            weights_df.at[index, BTC_TICKER] = 1
        else:
            weights_df.at[index, BTC_TICKER] = 0

    # exit from a position cases
    elif weights_df.values[i-1] == 1:
        if pnl <= STOP_LOSS:
            weights_df.at[index, BTC_TICKER] = 0
        elif pnl >= TAKE_PROFIT:
            weights_df.at[index, BTC_TICKER] = 0
        else:
            weights_df.at[index, BTC_TICKER] = 1

    # update pnl
    if weights_df.values[i] == 0:
        pnl = 0
    else:
        pnl += (OHLC_BTC['Close'].values[i] -
                OHLC_BTC['Close'].values[i-1])/OHLC_BTC['Close'].values[i-1]

    i += 1


# create strategy
s = Strategy(closes_df, weights_df, cash=100)

# run backtest, robust tests, calculate stats
s.run_all(delay=2, verify_data_integrity=True, instruments_drop=None,
          commissions_const=0, capitalization=False)

BTC\u股票代码='BTC'
结束日期=日期时间(2019,11,1,0,0,0)
开始日期=日期时间(2018,1,1,0,0,0)
#获得ohlc
OHLC\u BTC=获取OHLC\u加密比较(BTC\u股票代码、美元股票代码、开始日期、,
结束日期=结束日期,间隔时间=小时)
OHLC\U BTC=hlp。纠正OHLC\U df(OHLC\U BTC,频率=时间范围)
#以战略要求的格式创建dfs
closes_df=pd.concat([OHLC_BTC['Close']],
轴=1,键=[BTC_TICKER])
#使用导入的指示器创建权重
macd_df=technicals.macd(OHLC_BTC[['Close']]
权重\u df=关闭\u df.copy()
`获取利润=0.025
停止损失=-0.048
pnl=0
pnl_美元=0
i=0
对于索引,macd_df.iterrows()中的行:
如果i<3:
[索引,BTC股票代码]处的权重=0
#无职位个案
elif权重值[i-1]==0:
if(macd_df['MACDdiff_6_23'])值[i-2]<0
和macd_df['MACDdiff_6_23']。值[i-1]>0:
[索引,BTC股票代码]处的权重=1
其他:
[索引,BTC股票代码]处的权重=0
#退出某一职位
elif权重值[i-1]==1:
如果pnl=获取利润:
[索引,BTC股票代码]处的权重=0
其他:
[索引,BTC股票代码]处的权重=1
#更新pnl
如果权重值[i]==0:
pnl=0
其他:
pnl+=(OHLC_BTC['Close'].值[i]-
OHLC_BTC['Close']值[i-1])/OHLC_BTC['Close']值[i-1]
i+=1
#创建策略
s=策略(收盘价、权重、现金=100)
#运行回溯测试、稳健测试、计算统计数据
s、 运行所有(延迟=2,验证数据完整性=真,仪器下降=无,
佣金(常量=0,大写=False)

我们需要检查当前BTC价格是否至少为27美元。如果这就是你想要的,请告诉我

# exit from a position cases
elif weights_df.values[i-1] == 1:
    if pnl <= STOP_LOSS:
        weights_df.at[index, BTC_TICKER] = 0
    elif TAKE_PROFIT * OHLC_BTC['Close'].values[i-1] > 27:            
        weights_df.at[index, BTC_TICKER] = 0            
    else:

        weights_df.at[index, BTC_TICKER] = 1
#从职位退出
elif权重值[i-1]==1:
如果pnl 27:
[索引,BTC股票代码]处的权重=0
其他:
[索引,BTC股票代码]处的权重=1