循环两个R数据帧以创建第三个数据帧

时间:2019-06-20 16:05:22

标签: r dataframe for-loop

我正在尝试回溯交易策略。

我正在使用R 3.6进行编码

数据在两个数据帧中。首先是五年的每日价格活动(即每五天一次)。第二个数据帧是同一五年内的价格活动,仅在一分钟级别(即五分钟)内。该数据包括低,高,打开和关闭会话。

我的策略是遍历分钟数据帧的各个日期,同时参考并填充每日数据帧中的数据以确定以下内容:

  1. 保证开仓的条件(即多头或空头)。
  2. 是否已达到目标价或止损价。
  3. 在第三个数据框(ORBOorders)上记录“交易”。

嵌入式循环无法按我认为的方式工作,我不知道为什么。

我知道我可以使代码更简单,但是它会不断被抽出,所以我可以弄清楚哪里出了问题。

我知道在循环中追加向量不是处理向量的首选方法,但是我不知道程序完成后向量的大小。我很想听听任何想法。

这是一种在价格超出一天前五分钟确立的范围时寻求价格的策略。

这是用于识别最初5分钟后价格走势的代码。计数器不工作。目的是了解在五分钟数据集(即今天的日期)内特定事件的索引;我将在下面的下一批代码中使用它。第六分钟计数器从6开始。输出永远不会高于7,因此,它不会在循环的每次迭代中计数。我不知道为什么。

for (i in 1:length(fiveyrdaily$Date)){
    todaysdate <- subset(fiveyrminutes, fiveyrdaily$Date[i] == fiveyrminutes$Date) # subset so I'm only seeing the respective days in the minutes dataframe
    counter3 <- 6  # start a counter so I can track which position I'm in within the minutes dataframe, start after ORBO
    for (t in counter:length(todaysdate$Date)){  #loop over the minutes dataframe
         if ((fiveyrdaily$FiveHighClose[i] == 0) & (todaysdate$Close[t] > fiveyrdaily$FiveMinuteHigh[i])) {
             fiveyrdaily$FiveHighClose[i] <- todaysdate$Close[t]
             fiveyrdaily$FiveHighAfterClose[i] <- todaysdate$High[t] #record the session's high
             counter3 <- counter3 + 1
             fiveyrdaily$counterhigh[i] <- counter3 # record the position of this event within the subset of the minute dataframe
          }
          else if ((fiveyrdaily$FiveLowClose[i] == 0) & (todaysdate$Close[t] < fiveyrdaily$FiveMinuteLow[i])) {
               fiveyrdaily$FiveLowClose[i] <- todaysdate$Close[t]
               fiveyrdaily$FiveLowAfterClose[i] <- todaysdate$Low[t] # record the session's low
               counter3 <- counter3 + 1
               fiveyrdaily$counterlow[i] <- counter3
          }
          counter3 <- counter + 1
     }
 }

这是“ for”循环,用于确定是否购买并记录结果。

#Open a long position
for (i in 1:length(fiveyrdaily$Date)){
     if ((fiveyrdaily$counterhigh[i] < fiveyrdaily$counterlow[i]) & openposition < 2) {

          #entryprice is the price predetermined ticks above the high
          entryprice <- fiveyrdaily$FiveHighAfterClose[i] + (openticksaway * tickvalue)
          #create stoploss
          stoplossprice <- entryprice - stoplossvalue
          #uncover target closest to five minute high
          if (possibletargets > fiveyrdaily$FiveHighAfterClose[i]){ fiveyrdaily$ORBOtarget[i] <- min(possibletargets)}

          poscounter <- fiveyrdaily$counterhigh[i]
          beginforloop <- todaysdate[poscounter]

          for (c in beginforloop:length(todaysdate$Date)){ #see where to open position starting from the occurence of the high
               if ((entryprice > todaysdate$Low[c]) & (entryprice < todaysdate$High[c]) & (openposition < 2)){ #determine if conditions warrant entry
                    openposition <- openposition + 1 # open a position
                    if (openposition > 0){ #trade management
                         openpos <- 1
                         if ((stoplossprice < todaysdate$high[c]) & (stoplossprice > todaysdate$Low[c])){ # determine if stoploss has been hit
                              orderdate <- c(orderdate, todaysdate[c]) #enter data into orders dataframe
                              orderstrategy <- c(orderstrategy, "ORBO")
                              ordertype <- c(ordertype, "Long")
                              ordersymbol <- c(ordersymbol, "ES")
                              orderentry <- c(orderentry, entryprice)
                              orderclose <- c(orderclose, stoplossprice)
                              orderprofit <- c(orderprofit, abs((entryprice - stoplossprice) * pointvalue))
                              openpos <- 0
                         }
                         else if ((fiveyrdaily$ORBOtarget[i] < todaysdate$high[c]) & (fiveyrdaily$ORBOtarget[i] > todaysdate$Low[c])){ # determine if target has been hit
                              orderdate <- c(orderdate, todaysdate[c]) #enter data into orders dataframe
                              orderstrategy <- c(orderstrategy, "ORBO")
                              ordertype <- c(ordertype, "Long")
                              ordersymbol <- c(ordersymbol, "ES")
                              orderentry <- c(orderentry, entryprice)
                              orderclose <- c(orderclose, stoplossprice)
                              orderprofit <- c(orderprofit, abs((fiveyrdaily$ORBOtarget[i] - entryprice) * pointvalue))
                              openpos <- 0
                         }
                    }
               }
          }
    }
}

以下是每日数据框中的数据-每日五次:

> tail(fiveyrdaily)
           Date  Time    Open    High     Low   Close     Vol      OI UpperBand
1254 06/12/2019 16:15 2883.50 2889.75 2875.25 2881.00 1205406 2495060   2919.12
1255 06/13/2019 16:15 2894.75 2900.50 2886.75 2898.50  523312  448119   2925.39
1256 06/14/2019 16:15 2893.00 2899.75 2884.25 2894.75 1318938  951568   2927.99
1257 06/17/2019 16:15 2895.50 2902.75 2892.00 2896.25 1649621 1595842   2932.71
1258 06/18/2019 16:15 2914.00 2936.50 2910.25 2926.25 2257843 2093571   2944.19
1259 06/19/2019 16:15 2925.50 2936.75 2915.25 2933.50 1639495 2093571   2954.61
     LowerBand MidLine      PP   RSI OverBot OverSld SlowK SlowD OverBot.1
1254   2751.13 2835.13 2892.58 56.24      70      30 86.82 87.54        80
1255   2749.21 2837.30 2882.00 59.06      70      30 87.60 88.11        80
1256   2748.24 2838.11 2895.25 58.19      70      30 89.01 87.81        80
1257   2746.94 2839.82 2892.92 58.46      70      30 91.79 89.47        80
1258   2743.68 2843.94 2897.00 63.41      70      30 92.63 91.14        80
1259   2740.02 2847.31 2924.33 64.51      70      30 95.20 93.21        80
     OverSld.1  Volume Momentum ZeroLine   InOrOut GapUpOrDown TypeOfDay PPTrend
1254        20 1205406    49.25        0 Disregard   Disregard      Bear Uptrend
1255        20  523312    93.50        0 Disregard      Gap Up      Bull Uptrend
1256        20 1318938   114.75        0 Disregard   Disregard      Bull Uptrend
1257        20 1649621   105.75        0 Disregard   Disregard      Bull Uptrend
1258        20 2257843   173.75        0 Disregard      Gap Up      Bull Uptrend
1259        20 1639495   184.00        0 Disregard   Disregard      Bull Uptrend
     FiveMinuteLow FiveMinuteHigh FiveMinHtoL DollarFiveHtoL FiveHighClose
1254       2881.25        2889.75        8.50          425.0          0.00
1255       2892.00        2895.75        3.75          187.5       2896.00
1256       2886.25        2893.75        7.50          375.0       2894.75
1257       2892.00        2897.00        5.00          250.0       2897.25
1258       2910.25        2915.50        5.25          262.5       2920.75
1259       2923.25        2927.25        4.00          200.0       2930.00
     FiveHighAfterClose FiveLowClose FiveLowAfterClose ORBOtarget counterhigh
1254               0.00      2881.00           2880.75          0           0
1255            2896.00      2891.75           2891.00          0           6
1256            2894.75      2886.00           2885.00          0           7
1257            2897.50         0.00              0.00          0           7
1258            2921.75         0.00              0.00          0           7
1259            2931.50      2922.50           2922.25          0           7
     counterlow DollarOpentoHigh DollarOpentoClose DollarOpentoLow
1254          7            312.5             125.0           412.5
1255          7            287.5             187.5           400.0
1256          7            337.5              87.5           437.5
1257          0            362.5              37.5           175.0
1258          0           1125.0             612.5           187.5
1259          7            562.5             400.0           512.5

这是分钟数据帧中的数据-五分钟:

Date  Time    Open    High     Low   Close   Up Down UpperBand
509796 06/19/2019 16:10 2932.25 2932.50 2932.25 2932.50  717  430   2935.66
509797 06/19/2019 16:11 2932.25 2932.50 2932.25 2932.25  125  276   2935.46
509798 06/19/2019 16:12 2932.25 2932.75 2932.25 2932.75  612  604   2934.95
509799 06/19/2019 16:13 2932.50 2933.25 2932.50 2933.00  830  153   2934.66
509800 06/19/2019 16:14 2933.25 2933.25 2932.75 2933.00  676  376   2934.26
509801 06/19/2019 16:15 2932.75 2934.00 2932.75 2933.25 2929 2026   2933.90
       LowerBand MidLine      PP   RSI OverBot OverSld SlowK SlowD OverBot.1
509796   2930.27 2932.96 2932.42 47.94      70      30 45.45 40.66        80
509797   2930.24 2932.85 2932.42 46.22      70      30 53.70 46.21        80
509798   2930.45 2932.70 2932.33 50.07      70      30 63.83 54.33        80
509799   2930.56 2932.61 2932.58 51.92      70      30 72.73 63.42        80
509800   2930.76 2932.51 2932.92 51.92      70      30 83.33 73.30        80
509801   2930.97 2932.44 2933.00 53.90      70      30 85.00 80.35        80
       OverSld.1 Volume Momentum ZeroLine
509796        20   1147    -0.50        0
509797        20    401    -1.00        0
509798        20   1216     1.75        0
509799        20    983     1.75        0
509800        20   1052     2.00        0
509801        20   4955     1.75        0

这是订单数据框的输出,请注意它是空的-ORBOorders:

ORBOorders
  orderdate orderstrategy ordertype ordersymbol orderentry orderclose
1                                                        0          0
  orderprofit
1           0

这些是问题所在:

-counter3无法正常工作(以找到应该购买的点)

-第二批代码给出此错误:

  

beginforloop:length(todaysdate $ Date)中的错误:长度为0的参数   另外:警告消息:如果((可能的目标>   Fiveyrdaily $ FiveHighAfterClose [i]){:条件的长度> 1   并且只会使用第一个元素

-ORBOorder数据框中绝对没有数据。

感谢您的任何帮助!

0 个答案:

没有答案