Linear Regression Universal Strategy, how to improve it?
Forums › ProRealTime English forum › ProOrder support › Linear Regression Universal Strategy, how to improve it?
- This topic has 32 replies, 6 voices, and was last updated 2 years ago by
GraHal.
-
-
08/23/2022 at 5:37 PM #199478
Hello everyone! I am new and very impressed by the great potential of Prorealtime in following the market automatically. I am writing a thesis on trading systems. So I would like to share this strategy to improve and experiment with new solutions together.
It is a very simple strategy with stop orders that uses two linear regressions, as entry and exit signals from both long and short, and the Historical volatility indicator to dynamically manage the period of linear regressions.
It works if optimized, on different currency pairs, (I tried it on AUDUSD, EURUSD, GBPUSD, CNHJPY, and potentially on all currency pairs)
I advise you to improve it on CNHJPY to get low drawdowns and given the possibility of testing it live with a low initial capital (margin = $ 80).I’d like to add a machine learning feature to optimize the only three variables automatically.
Thanks to anyone who wants to help me.
123456789101112131415161718192021222324252627282930313233//Linear Regression Universal Strategy// instrument: CNHJPY// timezone : europetime, berlin// timeframe : Daily// Spread: 0.7// created and coded by davidelaferla//————————————————————————-defparam cumulateorders=false//***********************************************************************************************************N = 1//------------------ VARIABILI DEL SISTEMA---------------------------------------//CNHJPY Values: -------------------------------------------- Ottimization infoVolatilityperiod=15// different fixed value for each currency pair: range=3-20, with step=1Regression1period=18// Linear Regression Imput Signal: range=6-100 with step=1Regression2period=1.3//Linear Regression Output Signal: range=1-2, with step=0.01//***********************************************************************************************//------------------ INDICATOR ---------------------------------------hv=HistoricVolatility[VolatilityPeriod](close)hv2=HistoricVolatility[VolatilityPeriod](typicalprice)st=linearregression[ROUND(Regression1Period*hv)](close)lr=linearregression[ROUND(Regression1Period*Regression2Period*hv2)](typicalprice)// Condizioni per uscire da posizioni short e entrare su posizioni longIF st<lr THENBUY n contract AT lr stopENDIF// Condizioni per uscire da posizioni long e entrare su posizioni shortIF st>lr THENSELLshort n contract At st stopENDIF08/24/2022 at 8:41 AM #199505Looks good!
0.7 spread is shown at Line 5, isn’t the spread on CNHJPY 1.8 pips / 180 Yen?
I optimised and set it going on Demo Live, but it keep getting Rejected due to ‘not enough data to calculate etc’. I added ‘Defparam PreLoad bars = 10,000’, but still keeps getting Rejected for same reason.
Any thoughts?
08/24/2022 at 3:49 PM #199528How would you like them to be optimized at runtime?
08/24/2022 at 10:02 PM #199553I get strange results when I optimize the system.
For all optimization parameters I get the same positive result equal to the graph of davidelaferla.
If I keep the spread below 1 then the system is positive, if you get 0.1 point above 1 then the positive result suddenly turns into negative result.
During the optimization, the parameter Regression2period is also not optimized, setting is optimization from 1 to 2 step 0.1 but the parameter remains at 1?
08/25/2022 at 7:57 AM #19955508/25/2022 at 8:22 AM #199556What Timeframe you running on? If Daily then you may get the error message tomorrow?
I was running on 2 min TF, and although the System started okay, at the ‘first changeover of full 2 min’ I got the error message!
I spent a few hours on it trying everything!! Narrowed it down eventually to the ’round’ function. I replaced with ‘ceil’ but got a Rejection after maybe an hour or so; ‘floor’ lasted a few hours but then also was Rejected.
I have one version running since yesterday with no Rejections, but I had to remove ’round’ altogether!
I feel there should be a successful Algo in there somewhere?
EDIT / PS
Ceil and Floor together (somehow?) may work as then the value would be rounded from above and below?
08/25/2022 at 8:45 AM #199558I do use a time frame of a day so the error message will still come…
As you probably know, it uses the ’round’ function because the number, which refers to the period, must be an integer… (and positive)
Very special that you do not get an error message on this …
Strange things are happening here, but I don’t know yet what it’s about…
08/25/2022 at 8:59 AM #19956008/25/2022 at 9:23 AM #199566Try setting up on 1 min or 2 mins TF then you can see if you get Rejected as you only have to wait 1 or 2 mins.
Also the opti may work better? ( I do recall strange happenings trying to recreate David’s results).
I rarely do 1 day TF hence me setting up on M2, also mine is running on DJI.
08/25/2022 at 9:48 AM #199569I now have the system running in Demo on a time frame of two minutes and I do indeed get the same error message about “too little historical data to be able to calculate…”??
What a waste of time…
1 user thanked author for this post.
08/25/2022 at 10:37 AM #199577Hi guys,
I’ve slightly modified this code and since yesterday it’s running in demo without problems. I’m running it with MTF with 5M as basic TF. See below. Any comment is really appreciated. I’ve also added +/- contracts based on profit.
//Linear Regression Universal Strategy
// instrument: VALUTE
// timeframe : Daily
// created and coded by davidelaferlaDEFPARAM CUMULATEORDERS =FALSE
DEFPARAM FLATBEFORE=080000
DEFPARAM FLATAFTER=220000timeframe (default)
//////////////////////// Q.TY CONTRACTS
MS=1
ONCE LongProfit = 0
ONCE ShortProfit = 0
IF StrategyProfit <> StrategyProfit[1] AND BarIndex > 0 THEN
Profitto = StrategyProfit – StrategyProfit[1]
IF LongOnMarket[1] THEN
LongProfit = LongProfit + Profitto
ELSIF ShortOnMarket[1] THEN
ShortProfit = ShortProfit + Profitto
ELSE
p1 = 1
p2 = 2
IF OnMarket THEN
p1 = 2
p2 = 3
ENDIF
IF TradePrice(p1) > TradePrice(p2) THEN
IF Profitto > 0 THEN
LongProfit = LongProfit + Profitto
ELSE
ShortProfit = ShortProfit + Profitto
ENDIF
ELSIF TradePrice(p1) < TradePrice(p2) THEN
IF Profitto < 0 THEN
LongProfit = LongProfit + Profitto
ELSE
ShortProfit = ShortProfit + Profitto
ENDIF
ELSE
LongProfit = LongProfit + (Profitto / 2)
ShortProfit = ShortProfit + (Profitto / 2)
ENDIF
ENDIF
ENDIF/////////////////// for LONG trades
once LotSizeL = 1
Once MinSizeL = 1 //minimum size required
Once LotSizeL = MinSizeL //starting size (can be bigger than minimum)
Once LotStepL = LL200 //250 //increase/decrease this money step
LotSizeL = max(MinSizeL,round((MinSizeL/MS),1) + (MinSizeL * round((LongProfit / LotStepL),1)))
if LotSizeL>MU then
LotSizeL=MU-MMUU
endif
myLotL=LotSizeL///////////////////// for SHORT trades
once LotSizeS = 1
Once MinSizeS = 1 //minimum size required
Once LotSizeS = MinSizeS //starting size (can be bigger than minimum)
Once LotStepS = LS200 //250 //increase/decrease this money step
LotSizeS = max(MinSizeS,round((MinSizeS/MS),1) + (MinSizeS * round((ShortProfit / LotStepS),1)))
if LotSizeS>MU then
LotSizeS=MU-MMUU
endif
myLotS=LotSizeS
//////////////////////////////////// ENDtimeframe (daily,default)
//myLot= 1
Volatilityperiod=V15// different fixed value for each currency pair: range=3-20, with step=1
Regression1period=R18// Linear Regression Imput Signal: range=6-100 with step=1
Regression2period=RR //1.3//Linear Regression Output Signal: range=1-2, with step=0.01hv=HistoricVolatility[VolatilityPeriod](close)
hv2=HistoricVolatility[VolatilityPeriod](typicalprice)
st=linearregression[ROUND(Regression1Period*hv)](close)
lr=linearregression[ROUND(Regression1Period*Regression2Period*hv2)](typicalprice)timeframe (60 minutes,default)
// Condizioni per uscire da posizioni short e entrare su posizioni long
IF NOT LONGONMARKET and st<lr and hour=>H1 and hour<=H2 THEN
exitshort myLotS contract at market
BUY myLotL contract AT lr stop
set stop ploss SL/myLotL*CX
set target pprofit TP
ENDIF
// Condizioni per uscire da posizioni long e entrare su posizioni short
IF NOT SHORTONMARKET and st>lr and hour=>H1 and hour<=H2 THEN
sell myLotL contract at market
SELLshort myLotS contract At st stop
set stop ploss SLS/myLotS*CX
set target pprofit TPS
ENDIFtimeframe (default)
1 user thanked author for this post.
08/25/2022 at 10:38 AM #19957908/25/2022 at 11:00 AM #19958708/25/2022 at 11:32 AM #19958908/25/2022 at 3:13 PM #199618Hi guys! I’m really glad you like it. I gave you a basis, now it would be really wonderful to implement it, that is, make it easy to optimize and create a database for each index with the fixed variable that is volatility. How can I help you?
-
AuthorPosts
Find exclusive trading pro-tools on