vendredi 2 avril 2021

Need some help writing a strategy with different positions depending on three 3 signal variables

Hi I'm brand new in coding, and I am getting stuck every new line of code I try to write but hey its a learning process.

I'm doing a strategy based on the MACD variables. -MACD_Line is either positive or negative. -Signal_Line is either positive or negative. -Histogram is either positive or negative.

Based on the historical prices there are 6 possibilities of combined signals either: ---, -++, --+, +--, ++- or +++.

What I want to do is pre-set different position sizes depending on these 6 possible output signals.

So for example: if "---" then short 50% of equity, if "+++" then long 100% of equity, if "-++" then short 25% of equity.

Therefore the equity position would change after one of the initial 3 variables changes.

My attempt:

strategy("HIST", overlay= false, initial_capital = 1, default_qty_type= strategy.percent_of_equity, default_qty_value= 100 )

//time inputs
startDate = input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer, defval=2014, minval=1800, maxval=2100)

endDate = input(title="End Date", type=input.integer, defval=29, minval=1, maxval=31)
endMonth = input(title="End Month", type=input.integer, defval=3, minval=1, maxval=12)
endYear = input(title="End Year", type=input.integer, defval=2021, minval=1800, maxval=2100)

inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and
     (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))

//variable
ema26= ema(close,26)
ema12= ema(close,12 )
macdl= ema12-ema26
signal= ema(macdl, 9)
hist= macdl-signal


enterLong  =  crossover(macdl,0)
enterShort =  crossunder(macdl,0)

s000 = if (hist <= 0 and macdl <= 0 and signal <=0)
s001 = if (hist > 0 and macdl <= 0 and signal <= 0)
s011 = if (hist > 0 and macdl > 0 and signal <= 0) 
s111 = if (hist > 0 and macdl > 0 and signal > 0)
s011 = if (hist <= 0 and macdl > 0 signal > 0)
s001 = if (hist <= 0 and macdl <= 0 signal > 0)




if (inDateRange and s111)
    strategy.entry(id="+", long=true)

if (inDateRange and s000)
    strategy.entry(id="-", long=false)

if (not inDateRange)
    strategy.close_all()
 

Aucun commentaire:

Enregistrer un commentaire