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.
//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 info
Volatilityperiod=15// different fixed value for each currency pair: range=3-20, with step=1
Regression1period=18// Linear Regression Imput Signal: range=6-100 with step=1
Regression2period=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 long
IF st<lr THEN
BUY n contract AT lr stop
ENDIF
// Condizioni per uscire da posizioni long e entrare su posizioni short
IF st>lr THEN
SELLshort n contract At st stop
ENDIF
Looks 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?
How would you like them to be optimized at runtime?
JSParticipant
Senior
I 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?
JSParticipant
Senior
Hi @GraHal
I also started the system today in Demo and it does start without error message…
Remains special that different behaviour in error messages…
What 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?
JSParticipant
Senior
I 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…
JSParticipant
Senior
Do you have no problems with the optimization?
When I optimize the system, all optimization parameters give the same (positive) result…
Try 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.
JSParticipant
Senior
I 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…
Hi 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 davidelaferla
DEFPARAM CUMULATEORDERS =FALSE
DEFPARAM FLATBEFORE=080000
DEFPARAM FLATAFTER=220000
timeframe (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
//////////////////////////////////// END
timeframe (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.01
hv=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
ENDIF
timeframe (default)
Sorry I forgot to say that now it’s running on Eu/$ ’cause Davide wrote that has to be used for Forex and not indexes
MaoRai54 please might you post the .itf and then we will have all the same variables as you so we can compare results etc?
Sharing feels good, like old times … before MarketPlace? 🙂
Grahal, see attachment !!
Hi 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?