2方向トレンド追跡移動平均クロスオーバー戦略

作者: リン・ハーンチャオチャン,日付: 2023年11月23日 15:26:25
タグ:

img

概要

この戦略は,自動化されたトレンド追跡クロスオーバー取引戦略を実施するために,トレンド指標と組み合わせて両方向のスムーズ移動平均を計算します.この戦略は,長期的トレンドを追跡し,強いトレンド中により高い収益を達成することを目的としています.

戦略の論理

  1. 資産価格の両方向の滑らかな移動平均を計算し,開く価格移動平均と閉じる価格移動平均を含む.
  2. コレレーション係数とATR指標を使用して価格傾向指標を計算する.
  3. 価格動向指標を両方向移動平均値と組み合わせると,全体的な動向判断が得られます.
  4. オープン価格移動平均値が閉じる価格移動平均値を超えるとロング; 閉じるポジションが低くなると.
  5. トレンドインジケーターが上向きを示したときにだけロングで,トレンドインジケーターが下向きを示したときにだけショートで

利点

  1. 双方向移動平均値は 傾向をよりスムーズで安定的に追跡します
  2. トレンドインジケーターは トレンドの方向性をより良く判断し,間違った取引を避けるのに役立ちます.
  3. クロスオーバーはより明確な取引信号を提供します.
  4. パラメータ調整により,より多くの市場環境に合わせて調整できるスムーズ性
  5. 複数の指標のクロスバリダーションは 誤った信号を避けるのに役立ちます

リスク

  1. トレンドターニングポイントで逆転を逃すリスクは,移動平均期間の調整によって軽減できます.
  2. 移動平均値自体には遅延があります トレンドインジケーターから検証が必要です
  3. 適正でない移動平均期間の場合,過取引または良きエントリが欠落する可能性があります.
  4. 幅広いパラメータの最適化が必要で,異なる時間枠と市場に対応する必要があります.

改善

  1. 移動平均の種類をテストする
  2. 他のタイプのトレンドインジケーターを試してください.
  3. 移動平均値やトレンド指標のパラメータを最適化する
  4. 変動率などの他の指標も含まれます
  5. ストップ・ロスの戦略を追加します

結論

パラメータ調整により,この多次元トレンド戦略は,比較的低いリスクで長期的トレンドを安定的に追跡することができます.しかし,オーバーフィットに注意してください.全体として,制御可能なリスクを持つ長期的トレンド追跡を提供し,さらなる研究と適用に値します.


/*backtest
start: 2022-11-22 00:00:00
end: 2023-11-22 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
//Author Josef Tainsh PhD 6Sept2020


//USE STUDY FOR ALERTS WITH AUTO TRADING BOT
//study(title = "Open Close Crossover for BOT Alerts", shorttitle = "OCC for BOT Alerts",overlay = true)
//USE STRATEGY TO FIT THE MOVING AVERAGE AND THE TREND FITS BY MINIMISING LOSS (OR MAXIMISING PROFITS)
//NOT THAT STRATEGIES RARELY SHOW A PROFIT ALSO THE STRATEGIES USE THE CROSS OVER ON THE MOVING AVERAGE TO ENTER A POSITION
strategy(title = "OCC Trend Combo 1 day BTC Moonflag", overlay = true, initial_capital=1000, commission_type=strategy.commission.percent, commission_value=0.2, default_qty_type = strategy.percent_of_equity, default_qty_value=100, pyramiding=0, calc_on_order_fills=false)
//CalcOnTick = true
//calc_on_every_tick = false

// Function for coders who want to offer their users a repainting/no-repainting version of the HTF data.
// It has the advantage of using only one `security()` call for both.
f_security(_symbol, _res, _src, _repaint) => security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]


/////////////////////////////////////////////////////////////////////////////
// === BASE FUNCTIONS ===
/////////////////////////////////////////////////////////////////////////////
//This function returns true if execution is at the start of a new bar (so the last bar is previous close where alerts are determined)
is_newBar(stratRes) =>
    t = time(stratRes)
    not na(t) and (na(t[1]) or t > t[1])
///////////////////////////////////////////////////////////////////////////////////////////////////
// Returns MA input selection variant, default to SMA if blank or typo.
variant(type, src, len, offSig, offALMA) =>
    v1 = sma(src, len)                                                  // Simple
    v2 = ema(src, len)                                                  // Exponential
    v3 = 2 * v2 - ema(v2, len)                                          // Double Exponential
    v4 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len)               // Triple Exponential
    v5 = wma(src, len)                                                  // Weighted
    v6 = vwma(src, len)                                                 // Volume Weighted
    v7 = na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len    // Smoothed
    v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))   // Hull
    v9 = linreg(src, len, offSig)                                       // Least Squares
    v10 = alma(src, len, offALMA, offSig)                               // Arnaud Legoux
    type=="EMA"?v2 : type=="DEMA"?v3 : type=="TEMA"?v4 : type=="WMA"?v5 : type=="VWMA"?v6 : type=="SMMA"?v7 : type=="HullMA"?v8 : type=="LSMA"?v9 : type=="ALMA"?v10 : v1
/////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////
//SAMPLE SETTINGS FOR THE TREND INDICATOR FUNCTION
//1MIN   31, 0.2 (+-12%)
//timeResForTrend_1min = 1, [bLongs_1min, oscLongs_1min] = functionTrend(timeResForTrend_1min,31, 0.2)
//plot(bLongs1_1min,"Trend Indicator", color=oscLongs_1min == 1 ? color.blue :#e65100,linewidth=3,transp=0)
//OTHER  20, 0.2  5min(LONG -5%, SHORT -4%), 10min(LONG -17%, SHORT -6%), 60min(long +16%, SHORT )
//timeResForTrend_Not1min = XXXX, [bLongs_Not1Min, oscLongs_Not1Min] = functionTrend(timeResForTrend_Not1min,20, 0.2)
//plot(bLongs5_Not1Min,"Trend Indicator", color=oscLongs_Not1Min == 1 ? color.blue :#e65100,linewidth=3,transp=0)
/////////////////////////////////////////////
functionTrend(trendRes_Function,length_Function,sc_Function)=>
    b_Function =0.
    ls_Function = 0.
    src_Function = sc_Function*security(syminfo.tickerid, trendRes_Function, close)+(1-sc_Function)*nz(ls_Function[1],security(syminfo.tickerid, trendRes_Function, close))
    if (is_newBar(trendRes_Function)==false)
        src_Function := src_Function[1]
    er_Function = 1 - abs(change(src_Function,length_Function))/sum(abs(change(src_Function)),length_Function)
    n_Function = cum(1)-1
    a_Function = cum(abs(src_Function - nz(b_Function[1],src_Function)))/n_Function*(1+er_Function)
    b_Function := src_Function > nz(b_Function[1],src_Function) + a_Function ? src_Function : src_Function < nz(b_Function[1],src_Function) - a_Function ? src_Function : nz(b_Function[1],src_Function)
    alpha_Function = fixnan(correlation(src_Function,b_Function,length_Function) * (stdev(src_Function,length_Function)/stdev(b_Function,length_Function)))
    beta_Function = sma(src_Function,length_Function) - alpha_Function*sma(b_Function,length_Function)
    ls_Function := alpha_Function*b_Function+beta_Function
    osc_Function = 0
    osc_Function := b_Function > b_Function[1] ? 1 : b_Function < b_Function[1] ? 0 : osc_Function[1] 
    if (is_newBar(trendRes_Function)==false)
        er_Function := er_Function[1]
        n_Function := n_Function[1]
        a_Function := a_Function[1]
        b_Function := b_Function[1]
        alpha_Function := alpha_Function[1]
        beta_Function := beta_Function[1]
        ls_Function := ls_Function[1]
        osc_Function := osc_Function[1]
    [b_Function, osc_Function]
/////////////////////////////////////////////////////////////////
pine_atr(length,stratRes_atr) =>
    trueRange = max(max(security(syminfo.tickerid, stratRes_atr, high) - security(syminfo.tickerid, stratRes_atr, low), abs(security(syminfo.tickerid, stratRes_atr, high) - security(syminfo.tickerid, stratRes_atr, close[1]))), abs(security(syminfo.tickerid, stratRes_atr, low) - security(syminfo.tickerid, stratRes_atr, close[1]))) 
    sum = 0.0
    sum := (trueRange + (length - 1) * nz(sum[1])) / length
    sum  
//////////////////////////////////////////////////////////////////////////////////////////////


//STOP LOSS AND TAKE PROFIT
//ALSO NEEDS TO BE ACTIVATED IN THE STRATEGY
//long_tp_inp = input(1000, title='Long Take Profit %', step=0.1)/100/// Long Take Profit
//long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100/// Long Stop Loss 
//short_tp_inp = input(75, title='Short Take Profit %', step=0.1)/100/// Short Take Profit
//short_sl_inp = input(5, title='Short Stop Loss %', step=0.1)/100/// Short Stop Loss 
//long_take_level = strategy.position_avg_price * (1 + long_tp_inp)
//long_stop_level = strategy.position_avg_price * (1 - long_sl_inp)
//short_take_level = strategy.position_avg_price * (1 - short_tp_inp)
//short_stop_level = strategy.position_avg_price * (1 + short_sl_inp)
//plot(long_take_level, color=color.green)
//plot(long_stop_level, color=color.red)
//plot(short_take_level, color=color.green)
//plot(short_stop_level, color=color.red)
//////////////////////////////////////////


//oneMinChartTrend = input(title="One Min Chart, TrendRes[ATR(15), Corr(18)]",defval=false)
//fourHourChartTrendLongsOnly = input(title="Four Hour Chart, Longs, Choppy TrendRes[ATR(5), Corr{60)]",defval=false)
//oneHourChartTrendShortsOnly = input(title="One Hour Chart Trend, Shorts, TrendRes[ATR(15), Corr(10)]",defval=false)
//oneMDayChartTrend = input(title="One Day Chart Longs best, Shorts poor,  TrendRes[ATR(25), Corr(60)]",defval=false)

trendRes_ATR   = input(title="ATR Trend Resolution: (mins)", defval="60", options=["1","2","3","5", "7", "9", "10", "11", "12", "15", "20", "25", "30", "45", "60", "90", "120", "180", "240", "1D", "3D"], type = input.resolution)
Length = input(title="ATR Trend Length",defval=15, minval=1)
Multiplier = input(title="ATR Trend Multiplier",defval=6, minval=1)  //avgTR      = wma(atr(1), Length)

trendRes   = input(title="Correlation Trend Resolution: (mins)", defval="120", options=["1","2","3","5", "7", "9", "10", "11", "12", "15", "20", "25", "30", "45", "60", "90", "120", "180", "240", "1D", "3D"], type = input.resolution)
lengthTrend = input(24, "Correlation Trend Length (eg 28 for 1 hour)")
scTrend = input(.2, "Correlation Trend Tuner (0 to 1)", step=0.1)
trendCombination = input(.2, "Trend Combination Tuner (0 to 1): 0=all ATR, 1 = all Corr", step=0.1)




//with sl
//1day longs only (works also with shorts)
//20,10,20,10  ___   25,15,6,60, 24, 0.2, 0.4


//1min chart, long and short -70% but visible trend
//1.5, 1.5, 1.5, 1.5____15,7,2,10,12,0.5,0.4
//if (oneMinChartTrend)
//    trendRes_ATR     := "15"
//    trendRes         := "10"
//    Length           := 7
//    Multiplier       := 2
//    lengthTrend      := 12
//    scTrend          := 0.5
//    trendCombination := 0.4

//choppy region with chart on 4hour
//with sl long only - shorts at -2% could not get a positive result with shorts, only when ma crosses through after a run up
//longs on 4h 8%
//2.7, 2, 2,1,___5,15,6,60,21,0.2, 0.4
//if (fourHourChartTrendLongsOnly)
//    trendRes_ATR     := "5" 
//    trendRes         := "60"
//    Length           := 15
//    Multiplier       := 6
//    lengthTrend      := 21
//    scTrend          := 0.2
//    trendCombination := 0.4

//shorts on 1h -30% but only shorts to catch are when the MA after a long run with lots of green turns red when pops out of back of price action with a long drop
//in this case use the trend indicator to stop the run after a long while
//not sure about sl and tp but
//3.7,2, 3.7,2, _15,11,5,10,17,0.8, 0.3
//if (oneHourChartTrendShortsOnly)
//    trendRes_ATR     := "15"
//    trendRes         := "10"
//    Length           := 11
//    Multiplier       := 5
//    lengthTrend      := 17
//    scTrend          := 0.8
//    trendCombination := 0.3

//1day longs only 
//20,10,20,10 _25,15,6,60, 24, 0.2, 0.4
//if (oneMDayChartTrend)
//    trendRes_ATR     := "25" 
//    trendRes         := "60"
//    Length           := 15
//    Multiplier       := 6
//    lengthTrend      := 24
//    scTrend          := 0.2
//    trendCombination := 0.4
//This shows 111% since start of 2020, and 323% since the start of 2020


//1day longs only
//100,3,?,? _25,15,6,60, 24, 0.2, 0.5 (longs only) (300% back to 2019)
//?,?,60,0.3 _25,15,6,60, 24, 0.2, 0.5 (shorts only) (50% back to 2019)
//A take profit with the shorts worked, but the long had >100% in some runs
//The stop loss on the long did not really have any effect however, with the shorts a stop loss of 0.3% heloed
//However, the algo on the chart was the daily close and not sure if this works in the same way when calculating all the time on new ticks
//if (oneMDayChartTrend)
//    trendRes_ATR     := "25" 
//    trendRes         := "60"
//    Length           := 15
//    Multiplier       := 6
//    lengthTrend      := 24
//    scTrend          := 0.2
//    trendCombination := 0.4
//This shows 37% since the start of 2019, a few big wins with lots of small losses, much more trades than just with longs (46 compared to 2)









//[retA, posA] = trendATR(trendRes_ATR)
//plot(retA, color= color.blue , title="Second Trend Identifier")
//plot(retA, color= color.white , title="ATR Trend")

////////////////////////////////////////////////////////////////////////////////////////////
//THE TREND TRADER OVERLAY WHICH COLOURS THE BARS
avgTR      = wma(pine_atr(1, trendRes_ATR), Length)
highestC   = highest(Length)
lowestC    = lowest(Length)
hiLimit = highestC[1]-(avgTR[1] * Multiplier)
loLimit = lowestC[1]+(avgTR[1] * Multiplier)
closeA =security(syminfo.tickerid, trendRes_ATR, close)
ret=0.
ret := iff(closeA > hiLimit and closeA > loLimit, hiLimit, iff(closeA < loLimit and closeA < hiLimit, loLimit, nz(ret[1], 0)))
pos=0.
pos := iff(closeA > ret, 1, iff(closeA < ret, -1, nz(pos[1], 0))) 
//barcolor(pos == -1 ? color.red: pos == 1 ? color.green : color.blue )
//plot(ret, color= color.white , title="ATR Trend")
////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
// === END BASE FUNCTIONS ===
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////
//SET THE RESOLUTION AND INPUT CHARAGERISTICS FOR THE TREND INDICATOR
/////////////////////////////////////////////////////////////////////////
[bTrend, oscTrend] = functionTrend(trendRes,lengthTrend, scTrend)
cssTrend = oscTrend == 1 ? color.blue :#e65100
//plot(bTrend,"Trend Indicator", color=cssTrend[1],linewidth=3,transp=0)//bgcolor(oscLongs < 0 ? color.green : color.red, transp=82)
//plot(bLongs,"Trend Indicator", color=cssLongs,linewidth=3,transp=0)//bgcolor(oscLongs < 0 ? color.green : color.red, transp=82)
//alertcondition(change(oscTrend)>0,title="New Up Trend",message="New Up Trend")
//alertcondition(change(oscTrend)<0,title="New Down Trend",message="New Down Trend")
////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////
//TREND COMBINATION
///////////////////////////////////////////////////////////////////////////////////////////////
trendCombinationVal = (ret - bTrend) * trendCombination + bTrend//ret := iff(closeA > hiLimit and closeA > loLimit, hiLimit, iff(closeA < loLimit and closeA < hiLimit, loLimit, nz(ret[1], 0)))
posTrendCombination=0.
posTrendCombination := iff(closeA >= trendCombinationVal, 1, iff(closeA < trendCombinationVal, -1, nz(posTrendCombination[1], 0))) 
//barcolor(posTrendCombination[1] == -1 ? color.red: posTrendCombination[1] == 1 ? color.green : color.blue )
posTrendCombinationColor = posTrendCombination == 1 ? color.blue :#e65100
plot(trendCombinationVal, color= posTrendCombinationColor[2] ,linewidth=3 , title="Trend Combo")
barcolor(posTrendCombinationColor[1])
//////////////////////////////////////////////////////////////////////////////////////////////


// === INPUTS ===
//DEFAULT SETTINGS BELOW ARE FOR 15MIN CHART TIMEFRAME, FOR THE DAYILY TIMEFRAME ON CHART AND ALGO USE SMA 29PERIOD
//MA RESOLUTION IS SET TO PREDEFINED VALUES THERE ARE A LOT MISSING WHICH MIGHT BE VERY IMPORTANT CONSIDER HAVING AN INTEGER FOR THE MINUTES
stratRes    = input(defval = "1D", title = "Set MA Resolution ( note run alerts on 1min chart )", type = input.resolution)
basisType   = input(title="MA Type: ", defval="ALMA", options=["SMA", "EMA", "DEMA", "TEMA", "WMA", "VWMA", "SMMA", "HullMA", "LSMA", "ALMA"], type = input.string)
basisLen    = input(defval = 18, title = "MA Period", minval = 1)
offsetSigma = input(defval = 1, title = "Offset for LSMA / Sigma for ALMA (6 is large)", minval = 0)
offsetALMA  = input(defval = 0.88, title = "Alma Offset (between 0 and 1, 0.99 = resposive, 0.01 = smooth)", minval = 0, step = 0.01)
//ignoreSmallCrossOvers    = input(defval = 0, title = "Ignore Small Cross Over if $USD Less Than", minval = 0)
so = security(syminfo.tickerid, stratRes, open, lookahead=barmerge.lookahead_on)
sh = security(syminfo.tickerid, stratRes, high, lookahead=barmerge.lookahead_on)
sl1 = security(syminfo.tickerid, stratRes, low, lookahead=barmerge.lookahead_on)
sc = security(syminfo.tickerid, stratRes, close, lookahead=barmerge.lookahead_on)
br= so != so[1] and sc != sc[1] and sh != sh[1] and sl1 != sl1[1] 
col= so > sc ? color.red : color.green
a1=na(br) ? so : na
a2=na(br) ? sh : na
a3=na(br) ? sl1 : na
a4=na(br) ? sc : na
p1=plot(a1,"MTF Open", color.white, style = plot.style_linebr, transp = 100, editable = false)
p2=plot(a2,"MTF High", color.black, style=plot.style_linebr, transp = 100, editable = false)
p3=plot(a3,"MTF Low", color.black, style=plot.style_linebr, transp = 100, editable = false)
p4=plot(a4, "MTF Close", col, style=plot.style_linebr, transp = 100, editable = false)
fill(p1,p4,col, transp = 100, editable = false)
fill(p2,p3,color.silver, transp = 100, editable = false)
reso(exp, res) => security(syminfo.tickerid, res, exp, lookahead=barmerge.lookahead_on) 
closeSeries = reso(variant(basisType, close, basisLen, offsetSigma, offsetALMA), stratRes)
openSeries  = reso(variant(basisType, open, basisLen, offsetSigma, offsetALMA), stratRes) 


//6HOUR CHART 1 DAY TIMEFRAME BACK TO 2019
//SMA (19) , 360% 300%LONGS 60% SHORTS
//alma (38,,0.86) 560% (400% LONGS, 28%SHORTS)  20TRADES

//If there are any crossovers in a bar, how many are there
//Just one crossover might signify a change in trend for the given timeframe
//Lots of crossovers might signify a sidewards trend which this algo does not work well with
crossOverInBar = crossover(closeSeries, openSeries)
crossUnderInBar = crossunder(closeSeries, openSeries)
crossedOverOrUnderInBar = crossOverInBar or crossUnderInBar

trendState=false
trendState  := closeSeries > openSeries ? true : closeSeries < openSeries ? false : trendState[1]
//closePlot   = plot(closeSeries, title = "Close Line", color = #009900, linewidth = 2, style = plot.style_line, transp = 100, editable = false)
//openPlot    = plot(openSeries, title = "Open Line", color = #CC0000, linewidth = 2, style = plot.style_line, transp = 100, editable = false)
closePlot   = plot(closeSeries, title = "Close Line", color = #009900, linewidth = 2, style = plot.style_line)
openPlot    = plot(openSeries, title = "Open Line", color = #CC0000, linewidth = 2, style = plot.style_line)
closePlotU  = plot(trendState ? closeSeries : na, transp = 100, editable = false)
openPlotU   = plot(trendState ? openSeries : na, transp = 100, editable = false)
closePlotD  = plot(trendState ? na : closeSeries, transp = 100, editable = false)
openPlotD   = plot(trendState ? na : openSeries, transp = 100, editable = false)
fill(openPlotU, closePlotU, title = "MA Up Trend", color = #009900, transp = 0)
fill(openPlotD, closePlotD, title = "MA Down Trend", color = #CC0000, transp = 0)
// === /PLOTTING ===
coLor = closeSeries > openSeries ? color.green : color.red
hclose = plot(closeSeries, title="Close Series", color = coLor, linewidth = 1, transp = 100, editable = false)
hopen = plot(openSeries, title="Open Series", color = coLor, linewidth = 1, transp = 100, editable = false)

//longCond    = crossover(closeSeries, openSeries)
//shortCond   = crossunder(closeSeries, openSeries)


openLongCond = false
openShortCond = false
closeLongCond = false
closeShortCond = false

longCondLastBarClose = false
longCondLastBarClose := nz(longCondLastBarClose[1], false)
longCondThisBarClose = false
crossOver = false
if (is_newBar(stratRes))
    longCondThisBarClose := closeSeries[1] >= openSeries[1] //+ ignoreSmallCrossOvers
    if (longCondLastBarClose != longCondThisBarClose)
        crossOver := true
        longCondLastBarClose := longCondThisBarClose
longCondCrossOver = false
shortCondCrossOver = false
if (crossOver)
    longCondCrossOver := longCondThisBarClose
    shortCondCrossOver := not longCondThisBarClose
openLongCond := longCondCrossOver
openShortCond := shortCondCrossOver
closeLongCond := shortCondCrossOver
closeShortCond := longCondCrossOver

//ACTIVATE THIS TO STRATEGY TEST THE CORRELATION COMBINATION
//CORRELATION TREND
//longCondTrend=change(oscTrend)>0
//shortCondTrend=change(oscTrend)<0
//openLongCond := longCondTrend
//openShortCond := shortCondTrend
//closeLongCond := shortCondTrend
//closeShortCond := longCondTrend

////ACTIVATE THIS TO STRATEGY TEST THE ATR TREND
//longCondATRTrend = pos==1
//shortCondATRTrend = pos==-1
//openLongCond := longCondATRTrend
//openShortCond := shortCondATRTrend
//closeLongCond := shortCondATRTrend
//closeShortCond := longCondATRTrend

//ACTIVATE THIS TO STRATEGY TEST THE TREND COMBINATION
//TREND COMBINATION
//longCondTrendCombination = posTrendCombination==1
//shortCondTrendCombination = posTrendCombination==-1
//openLongCond := longCondTrendCombination
//openShortCond := shortCondTrendCombination
//closeLongCond := shortCondTrendCombination
//closeShortCond := longCondTrendCombination

//posTrendCombinationColor = posTrendCombination == 1 ? color.blue :#e65100


//Only get into a trade when both conditions are satisfied
//Get out of a trade when either conidition is satisfied




///////////////// LONG //////////////////
isEntry_Long = false
isEntry_Long := nz(isEntry_Long[1], false)
isExit_Long = false
isExit_Long := nz(isExit_Long[1], false)
entry_long = not isEntry_Long[1] and openLongCond
exit_long = not isExit_Long and closeLongCond
if (entry_long)
    isEntry_Long := true
    isExit_Long := false
if (exit_long)
    isEntry_Long := false
    isExit_Long := true
entry_long := entry_long 
exit_long := exit_long 
///////////// SHORT ///////////////////////////
isEntry_Short = false
isEntry_Short := nz(isEntry_Short[1], false)
isExit_Short = false
isExit_Short := nz(isExit_Short[1], false)
entry_short = not isEntry_Short[1] and openShortCond 
exit_short = not isExit_Short and closeShortCond 
if (entry_short)
    isEntry_Short := true
    isExit_Short := false
if (exit_short)
    isEntry_Short := false
    isExit_Short := true
entry_short := entry_short 
exit_short := exit_short 
//////////////////////////////////////////////////




//plotshape(series=entry_long, text="OpenLong", style=shape.triangleup, location=location.belowbar, color=color.white, size=size.small)
//plotshape(series=exit_long, text="ExitLong",style=shape.triangledown, location=location.belowbar, color=color.white, size=size.small)
//plotshape(series=entry_short, text="OpenShort", style=shape.triangledown, location=location.abovebar, color=color.white, size=size.small)
//plotshape(series=exit_short, text="ExitShort",style=shape.triangleup, location=location.abovebar, color=color.white, size=size.small)


//Alerts are for the study mode
alertcondition(entry_long, title="Enter Long")
alertcondition(entry_short, title="Enter Short")
alertcondition(exit_long, title="Exit Long")
alertcondition(exit_short, title="Exit Short")

/////////////////////////////////////////////////////////////
//TO TEST THE STRATEGY
///////////////////////////////////////////////////////////
/// PERIOD - This is for the strategy mode/// 
testStartYear = input(2019, "Backtest Start Year") 
testStartMonth = input(1, "Backtest Start Month") 
testStartDay = input(1, "Backtest Start Day") 
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0) 
testStopYear = input(2020, "Backtest Stop Year") 
testStopMonth = input(12, "Backtest Stop Month") 
testStopDay = input(31, "Backtest Stop Day") 
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0) 
testPeriod() =>  true

includeLongTrades=input(title="Include Long Trades", defval = true, type=input.bool)
includeShortTrades=input(title="Include Short Trades", defval = false, type=input.bool)
entry_long := entry_long and includeLongTrades
exit_long := exit_long and includeLongTrades
entry_short := entry_short and includeShortTrades
exit_short := exit_short and includeShortTrades
//if testPeriod() 
//    if (entry_long)
//        strategy.close("ENTRY_SHORT", comment="close short")
//        strategy.entry("ENTRY_LONG", strategy.long, oca_name="oca1",  comment="open long")
//    if (exit_long)
//        strategy.close("ENTRY_LONG", comment="close long")
//    if (entry_short)
//        strategy.close("ENTRY_LONG", comment="close long")
//        strategy.entry("ENTRY_SHORT", strategy.short, oca_name="oca1",  comment="open short")
//    if (exit_short)
//        strategy.close("ENTRY_SHORT", comment="close short")




if testPeriod() 
    if (entry_long)
        strategy.close("ENTRY_SHORT", comment="close short")
        strategy.entry("ENTRY_LONG", strategy.long, oca_name="oca1",  comment="open long")
    if (exit_long)
        strategy.close("ENTRY_LONG", comment="close long")
    if (entry_short)
        strategy.close("ENTRY_LONG", comment="close long")
        strategy.entry("ENTRY_SHORT", strategy.short, oca_name="oca1",  comment="open short")
    if (exit_short)
        strategy.close("ENTRY_SHORT", comment="close short")
//STOP LOSS AND TAKE PROFIT
//if (close  < long_stop_level or close  > long_take_level)
//    strategy.close("ENTRY_LONG", comment="close long SL/TP")
//if (close  > short_stop_level or close  < short_take_level)
//    strategy.close("ENTRY_SHORT", comment="close short SL/TP")


//closeNowA =0.
//closeNow = security(syminfo.tickerid, "1", close)
//closeNowA := closeNow
//if (is_newBar("1")==false)
//    closeNowA := closeNowA[1]
//plot (closeNowA)
////if (openLongCond)
//if (closeNowA  < long_stop_level or closeNowA  > long_take_level)
//    strategy.close("ENTRY_LONG", comment="close long SL/TP")
////if (openShortCond)
//if (closeNowA  > short_stop_level or closeNowA  < short_take_level)
//    strategy.close("ENTRY_SHORT", comment="close short SL/TP")




//if (strategy.position_avg_price)
//strategy.entry(id="Long", long=true, when=entry_long)
//strategy.exit("Take Profit/ Stop Loss","Long", stop=long_stop_level, limit=long_take_level)
//strategy.close(id="Long", when=exit_long, comment = "ExitLong TP/SL")
//strategy.entry(id="Short", short=true, when=entry_short)
//strategy.exit("Take Profit/ Stop Loss","Long", stop=short_stop_level, limit=short_take_level)
//strategy.close(id="Short", when=exit_short, comment = "ExitShort TP/SL")


//if testPeriod() 
//    if (entry_long)
//        strategy.close("ENTRY_SHORT", comment="josef.tainsh@gmail.com_EXIT-SHORT_BINANCE-FUTURES_BTC/USDT_josef.tainsh@gmail.com-BOT-NAME_3M")
//        strategy.entry("ENTRY_LONG", strategy.long, oca_name="oca1",  comment="josef.tainsh@gmail.com_ENTER-LONG_BINANCE-FUTURES_BTC/USDT_josef.tainsh@gmail.com-BOT-NAME_3M")
//    if (exit_long)
//        strategy.close("ENTRY_LONG", comment="josef.tainsh@gmail.com_EXIT-LONG_BINANCE-FUTURES_BTC/USDT_josef.tainsh@gmail.com-BOT-NAME_3M")
//    if (entry_short)
//        strategy.close("ENTRY_LONG", comment="josef.tainsh@gmail.com_EXIT-LONG_BINANCE-FUTURES_BTC/USDT_josef.tainsh@gmail.com-BOT-NAME_3M")
//        strategy.entry("ENTRY_SHORT", strategy.short, oca_name="oca1",  comment="josef.tainsh@gmail.com_ENTER-SHORT_BINANCE-FUTURES_BTC/USDT_josef.tainsh@gmail.com-BOT-NAME_3M")
//    if (exit_short)
//        strategy.close("ENTRY_SHORT", comment="josef.tainsh@gmail.com_EXIT-SHORT_BINANCE-FUTURES_BTC/USDT_josef.tainsh@gmail.com-BOT-NAME_3M")
//    if (close  < long_stop_level or close  > long_take_level)
//        strategy.close("ENTRY_LONG", comment="josef.tainsh@gmail.com_EXIT-LONG_BINANCE-FUTURES_BTC/USDT_josef.tainsh@gmail.com-BOT-NAME_3M")
//    if (close  > short_stop_level or close  < short_take_level)
//        strategy.close("ENTRY_SHORT", comment="josef.tainsh@gmail.com_EXIT-SHORT_BINANCE-FUTURES_BTC/USDT_josef.tainsh@gmail.com-BOT-NAME_3M")



//strategy.cancel(id="ENTRY_LONG")


もっと