무작위 숫자에 기초한 양적 거래 전략

저자:차오장, 날짜: 2023-12-07 17:14:20
태그:

img

전반적인 설명

이 전략의 핵심 아이디어는 긴 또는 짧은 포지션을 결정하기 위해 무작위 숫자를 사용하여 동전 던지기 및 주사위를 던지는 것과 같은 확률 이벤트를 시뮬레이션하는 것입니다. 따라서 무작위 거래를 구현합니다. 이러한 유형의 거래 전략은 시뮬레이션 테스트와 더 복잡한 전략 개발을위한 기본 프레임워크로 사용될 수 있습니다.

전략 원칙

  1. 사용flip무작위 이벤트를 시뮬레이션 하 고coinLabel무작위 숫자 크기

  2. 사용risk그리고ratio스톱 로스를 설정하고 수익 라인을 취합니다.

  3. 설정된 최대 주기 수에 따라 다음 거래 신호를 무작위로 트리거합니다.

  4. 클로즈 포지션 상자를plotBox variable.

  5. stoppedOut그리고takeProfit변수들은 스톱 로스 또는 수익을 감지하는 데 사용됩니다.

  6. 전략 수행을 테스트하기 위한 역 테스트 기능을 제공해야 합니다.

이점 분석

  1. 코드의 구조는 명확하고 이해하기 쉽고 2차적으로 개발됩니다.

  2. UI 상호 작용은 친화적이며 그래픽 인터페이스를 통해 다양한 매개 변수를 조정할 수 있습니다.

  3. 무작위성이 강하고 시장 변동에 영향을 받지 않으며 높은 신뢰성을 가지고 있습니다.

  4. 매개 변수 최적화를 통해 더 나은 투자 수익을 얻을 수 있습니다.

  5. 다른 전략에 대한 시범 또는 테스트로 사용될 수 있습니다.

위험 분석

  1. 무작위 거래는 시장을 판단할 수 없고 수익 위험이 있습니다.

  2. 최적의 매개 변수 조합을 결정할 수 없으므로 반복 테스트가 필요합니다.

  3. 너무 밀도가 높은 무작위 신호로 인해 발생할 수 있는 슈퍼 상관관계의 위험이 있습니다.

  4. 리스크를 제어하기 위해 스톱 로스 및 수익 메커니즘을 사용하는 것이 좋습니다.

  5. 거래 기간을 적절하게 연장함으로써 위험을 줄일 수 있습니다.

최적화 방향

  1. 더 복잡한 요소를 포함해서 무작위 신호를 생성합니다.

  2. 시험 범위를 넓히기 위해 거래 품종을 늘려라

  3. UI 상호 작용을 최적화하고 전략 제어 기능을 높입니다.

  4. 매개 변수 최적화를 위한 더 많은 테스트 도구와 지표를 제공합니다.

  5. 다른 전략에 추가되는 거래 신호 또는 스톱 손실 수익 요소로 사용될 수 있습니다.

요약

이 전략의 전반적인 틀은 완전하며, 높은 신뢰성으로 무작위 이벤트에 기반한 거래 신호를 생성합니다. 동시에 매개 변수 조정, 백테스팅 및 차트 기능을 제공합니다. 초보자 전략 개발을 테스트하는 데 사용할 수 있으며 다른 전략의 기본 모듈로도 사용할 수 있습니다. 적절한 최적화를 통해 전략 성능을 더욱 향상시킬 수 있습니다.


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

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © melodicfish

//@version=4
strategy("Coin Flipper Pro",overlay=true,max_bars_back=100)

// ======= User Inputs variables=========

h1=input(title="------- Trade Activity -------",defval=false)
maxBars=input(25.0,title="Max Bars between Coin Filps",step=1.0,minval=4.0)

h2=input(title="------- Position Settings -------",defval=false)
risk=input(defval=5.0,title="Risk in % ",type=input.float, minval=0.001 ,step=0.1)
ratio= input(defval=1.5,title="Risk to Reward Ratio x:1 ",type=input.float, minval=0.001,step=0.1)

h3=input(title="------- Plot Options -------",defval=false)
showBox=input(defval=true, title="Show Position Boxes")

h4=input(title="------- Back Testing -------",defval=false)
runTest=input(defval=true, title="Run Strategy Back Test")
customTime=input(defval=false, title="Use Custom Date Range for back test")


tsYear = input(2021,minval=1000,maxval=9999,title= "Test Start Year")
tsMonth = input(1,minval=1,maxval=12,title= "Test Start Month")
tsDay = input(1,minval=1,maxval=31,title= "Test Start Day")
start = timestamp(tsYear,tsMonth,tsDay,0,0)

teYear = input(2021,minval=1000,maxval=9999,title=  "Test Stop Year")
teMonth = input(5,minval=1,maxval=12,title=  "Test Stop Month")
teDay = input(1,minval=1,maxval=31,title=  "Test Stop Day")
end = timestamp(teYear,teMonth,teDay,0,0)

// ======= variables =========
var barsBetweenflips=25
var coinFlipResult=0.0
var flip=true
var coinLabel=0.0
var stoppedOut= true
var takeProfit=true
var posLive=false
var p1=0.0
var p2=0.0
var p3=0.0
var plotBox=false
var posType=0
long=false
short=false


// ===== Functions ======

getColor() => 
    round(random(1,255))


// ===== Logic ========
if barssince(flip==true)>barsBetweenflips and posLive==false
    flip:=true
    coinLabel:=random(1,10)

    // Candle Colors   
candleColor= flip==true and flip[1]==false and barstate.isconfirmed==false?color.rgb(getColor(),getColor(),getColor(),0):flip==false and close>=open?color.green:color.red
candleColor:= barstate.ishistory==true and close>=open?color.green: barstate.ishistory==true and close<open? color.red:candleColor 
barcolor(candleColor)

if flip[1]==true and posLive==false
    flip:=false
    barsBetweenflips:=round(random(3,round(maxBars)))
    posLive:=true
    
long:= flip[1]==true and coinLabel[1]>=5.0
short:= flip[1]==true and coinLabel[1]<5.0


    // Calculate Position Boxes
if long==true and posType!=1 

    riskLDEC=1-(risk/100) 
    p1:= close[1]*(1+((risk/100)*ratio)) // TargetLine
    p2:=close[1]
    p3:= close[1]*riskLDEC // StopLine
    plotBox:=true
    posType:=1
  
if short==true and posType!=-1 

    riskSDEC=1-((risk*ratio)/100)
    p1:= close[1]*riskSDEC   // TargetLine
    p2:=close[1]
    p3:= close[1]*(1+(risk/100)) // StopLine
    plotBox:=true
    posType:=-1

    
    // Check Trade Status 
stoppedOut:= posType==1 and long==false and low<= p3? true: posType==-1 and short==false and high>=p3? true: false  
takeProfit:= posType==1 and long == false and high>= p1? true: posType==-1 and short==false and low<=p1? true: false  
if stoppedOut==true or takeProfit==true
    posType:=0
    plotBox:=false
    posLive:=false


// ====== Plots ========
plot1=plot(plotBox and showBox? p1:na,style=plot.style_linebr,color=color.white, transp= 100)
plot2=plot(plotBox and showBox? p2:na,style=plot.style_linebr,color=color.white, transp= 100)
plot3=plot(plotBox and showBox? p3:na,style=plot.style_linebr,color=color.white, transp= 100)
fill(plot1,plot2,color= color.green)
fill(plot2,plot3,color= color.red)
plotshape(flip==true and flip[1]==false and coinLabel>=5.0,style=shape.labelup,location=location.belowbar, color=color.green,size=size.tiny,title="short label",text="Heads",textcolor=color.white)
plotshape(flip==true and flip[1]==false and coinLabel<5.0,style=shape.labeldown,location=location.abovebar, color=color.red,size=size.tiny,title="short label",text="Tails",textcolor=color.white)
if stoppedOut==true
    label.new(bar_index-1, p3, style=label.style_xcross, color=color.orange)
if takeProfit==true
    label.new(bar_index-1, p1, style=label.style_flag, color=color.blue)
    
    

if runTest==true and customTime==false or runTest==true and customTime==true and time >= start and time <= end 
    strategy.entry("Sell", strategy.short,when=short==true)
    strategy.close("Sell", comment="Close Short", when=stoppedOut==true or takeProfit==true)
    strategy.entry("Long", strategy.long,when=long==true)
    strategy.close("Long",comment="Close Long", when= stoppedOut==true or takeProfit==true )


   
    

더 많은