ProRealCode - Trading & Coding with ProRealTime™
Have you tried …
IF LongTriggered OR ShortTriggered AND newSL > 0 Then
TrailingSL = newSL
ELSE
TrailingSL = Undefined
ENDIF
GraphOnPrice TrailingSL coloured(255,0,0) AS “My Tralling Stop”
When you say that you can put both functions in the same algorithm, I will answer yes because it is just lines of code, however if the long market function is executed first then the short market function will not be executed as a short but as a sale of the first long position,
Try to understand what is happening;
Nothing prevents you from putting a PENDING Limit and Stop order at the same time. If this is not understood, then go to the manual once more and look up how an order like
Buy x Contracts at PriceY Limit
and
SellShort x Contracts at PriceY Stop
works, and while you’re at it, how that would work with manual trading.
If I’d like, I could place 100 (and more) Limit and Stop orders all at the same time. And they would really all be there in Live (or Demo), including their order labels. Whether this is useful, is something else.
It is totally unrelated to having two opposite positions (!) in the system at the same time.
🙂
I completely agree with you that two orders can be put on hold at the same time in the same algorithm, this is not a problem,
On the other hand, here is what could happen in a very simple case with some round numbers to explain the example :
IF Conditons Then
Buy 1 Shares AT 10 Limit
SellShort 1 Shares AT 8 Limit
ENDIF
// Stop Loss & Take Profit
Set Stop %Loss 50 // in case of Long the SL = 5 in case of Short the SL = 12
Set Target %Profit 50
In this example we assume that the first order that could be executed was the Long order at a price of 10, In this case our SL will be calculated automatically and will be 50% of the entry price so it will be worth 5, So in this case we will be taken out of the market if the price reaches 5, Now if our market entry conditions are still valid, it means that the Short order at a price of 8 is still waiting to be executed,
In this case (and correct me if I’m wrong), if I understood the documentation correctly: the short sell order will turn into a normal sell order and it will take us out of our position at the price of 8 even if our SL is still pending at 5
Or will the program understand and make the difference between the SellShort order and the SL order and because it has a SL at 5 then it will ignore the SellShort 8 (the SellShort that could turn into a normal Sell in our case)?
I hope to be clear in what I am saying
Have you tried …
12345678910111213 IF LongTriggered OR ShortTriggered AND newSL > 0 ThenTrailingSL = newSLELSETrailingSL = UndefinedENDIFGraphOnPrice TrailingSL coloured(255,0,0) AS “My Tralling Stop”
tks for your answers, Not working too, but think it’s a limitation of the GraphOnPrice fonction, So now I will not spend more time on this think, I will ask other time if we need it, Now let’s focus on good stop and trailing stop management 🙂
Set Stop %Loss 50
Above means 50% of your Trade(s) Contract Value .. could result in a very big loss!
yes yes, Tks you GraHal , but the answers is for @PeterSt, and the 50% it’s just an exemple 😅
Here I am correcting my code at the level of the trailing stops,
they have 2 variable :
– the first one is Points2BE, with this vaiable we change the entry price to BE so to trade price entry and we add 2 time the spread of 2 in this code,
– the second one is trailinstart, with this variable we calculate every candle when we are OnMarket the difference between the hiest candle with the trailling stop and if we get more the trailinstart point we change the trailing stop to this differnce
// Strategy Name : END OF DAY - YEN
// Version : 4.0
// Information : I don't know what is this Strategie means ;-)
// Stroks : USD/JPY Mini
// Initial Capital : 100 000 JPY
// Test on : 50K Candles
// Spread : 2
// ============================================================
Defparam cumulateorders = false
Once Capital = 100000
// SIZE OF THE POSITIONS
N = 1
Ratio = 0.5
// TIMETABLE
StartTime = 210000
EndTime = 231500
// STOP LOSS & TAKE PROFIT (%)
SL = 12
Period = 12
Spread = 2
// CANDLE REFERENCE to StartTime
IF Time = StartTime THEN
Amplitude = Highest[Period](High) - Lowest[Period](Low)
Opening = Close
ENDIF
// LONGS & SHORTS
IF Not LongOnMarket AND Time >= StartTime AND Time <= EndTime AND DayOfWeek < 5 THEN
Buy N Shares AT (Opening - Amplitude * Ratio) Limit
ENDIF
IF LongOnMarket Then
Set Stop pLoss SL
ENDIF
Points2BE = 30 // Posiotion will go to BE + spread fees after Points2BE
trailinstart = 80 //trailing will start @trailinstart points profit
IF NOT ONMARKET THEN //reset
newSL=0
//breakevenLevel=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move to BreakEven
IF newSL=0 AND Close - TradePrice >= Points2BE * pipsize THEN
newSL = TradePrice + Spread * 2 * pipsize // BE + 2 x Spread fees
ENDIF
//next moves
IF newSL>0 AND High - newSL >= trailinstart * pipsize THEN
newSL = High - trailinstart * pipsize
ENDIF
ENDIF
IF newSL>0 THEN
SELL AT newSL STOP
ENDIF
// STOP STRATEGIE DrawDown
MaxDrawDownPercentage = 40 // Max DrawDown of x%
Equity = Capital + StrategyProfit
HighestEquity = Max (HighestEquity,Equity) // Save the Maximum Equity we got
MaxDrawdown = HighestEquity * (MaxDrawDownPercentage/100)
ExitFromMarket = Equity <= HighestEquity - MaxDrawdown
IF ExitFromMarket Then
Quit
ENDIF
// Drawing Trailing Stop
IF OnMarket AND newSL > 0 Then
TrailingSL = newSL
FirstSL = TradePrice - SL * pipsize
ELSE
TrailingSL = Undefined
FirstSL = Undefined
ENDIF
//GraphOnPrice newSL coloured(255,0,0) AS "My Tralling Stop"
GraphOnPrice TrailingSL coloured(230,0,0) AS "My Tralling Stop"
GraphOnPrice TradePrice coloured(0,220,0) AS "Long Trade Price"
GraphOnPrice FirstSL coloured(0,0,0) AS "First Stop Loss"
and tks to @JC_Bywan for GraphOnPrice and to @PeterSt for the tips
In this example we assume that the first order that could be executed was the Long order at a price of 10, In this case our SL will be calculated automatically and will be 50% of the entry price so it will be worth 5, So in this case we will be taken out of the market if the price reaches 5, Now if our market entry conditions are still valid, it means that the Short order at a price of 8 is still waiting to be executed,
In this case (and correct me if I’m wrong), if I understood the documentation correctly: the short sell order will turn into a normal sell order and it will take us out of our position at the price of 8 even if our SL is still pending at 5
Or will the program understand and make the difference between the SellShort order and the SL order and because it has a SL at 5 then it will ignore the SellShort 8 (the SellShort that could turn into a normal Sell in our case)?
I hope to be clear in what I am saying
just to answer my self, there is a good exemple where Proorder open a long position at limite price but they close it for execute the SellShort, in this case it executes the SellShort as et Normal Sell for the last Long posiion, and also they use the Buy order for close the SellShort position Instead the ExitShort :
Defparam cumulateorders = false
// LONGS & SHORTS USD/JPY - M15
if date > 20230330 then
IF IntraDayBarIndex >= 5 THEN
Buy 1 Shares AT 132.800 Limit
SellShort 1 Shares AT 133.250 Limit
ENDIF
IF LongOnMarket Then
Set Stop Loss 132
ENDIF
endif
TIP / friendly nudge 😉
When we Quote we highlight a phrase (a few words) in the text of a post and then click on Quote. This makes it much more user friendly (than Quoting 4 paragraphs of text). If folks want to read the 4 paragraphs / source of the Quote they only have to click on the Quote. 🙂
What you say in your post above (in answer to yourself) is normal behaviour for PRT. It happens the same for all of us.
tks for tip, I try it with out succes, I will try next time,
@GraHal @All if you know a good link explaining this methde of catching breakout or catching liquidity I will be happy to read or watch it and understand it for more comprenssion for this algo 😊
Have a look at this
Have a look at this
I also found this one :
if do find other where they explain why and the mening of the strategry it will be great, I will try to undrtsnad every one to get the max of informaion and see wht I can do with tht for this algo 😊
I also found this one :
https://youtu.be/bov9W02ZR_0
@GraHal I will try to do this video in the code , I think it’s realy simple, even I don’t understand what he use for chose the value of Stop loss, if sme one have some idea pls ?
also that is the Version 8 of this code, Not much exceptional even if the result in M 15 is 10070% (50K history), with 200K we have only 18% before the stratery qquit because of the huge drowdawn from 190K to 120k after only 3 trade with sure the very bad entry of this algorithme and also the bad manage of the number of share, but it’s intreseting,
Also I add the Tralling MFE with some different setting, it’s not the best but maybe can helpful to find somethink new
// Strategy Name : END OF DAY - YEN // Version : 8.0 // Information : Adding MFE // Stroks : USD/JPY Mini // Initial Capital : 100 000 JPY // Test on : 50K Candles // Spread : 2 // ######################################################################## Defparam cumulateorders = false Once Capital = 100000 Once Equity = Capital ONCE MFE=0 SL = 12 Period = 12 Spread = 2 // ######################################################################## // ______ _ __ //| ____| | | / \ //| |__ ___ _ __ ___| || () | //| __/ _ \| '_ \ / __| __\__/ //| | | (_) | | | | (__| |_ //|_| \___/|_| |_|\___|\__| // CANDLE REFERENCE to 210000 IF Time = 210000 THEN Amplitude = Highest[Period](High) - Lowest[Period](Low) Opening = Close ENDIF // LONGS & SHORTS IF Not LongOnMarket AND Time >= 210000 AND Time <= 231500 AND DayOfWeek < 5 THEN Buy N Shares AT (Opening - Amplitude * 0.5) Limit ENDIF IF LongOnMarket Then Set Stop pLoss SL ENDIF // ######################################################################## // _______ _ _ _ _ //|__ __| (_) | (_) // | |_ __ __ _ _| | |_ _ __ __ _ // | | '__/ _| | | | | '_ \ / _| // | | | | (_| | | | | | | | | (_| | // |_|_| \__,_|_|_|_|_|_| |_|\__, | // __/ | // |___/ once trailinstop= 0 //1 on - 0 off trailingstart = 140 //trailing will start @trailinstart points profit trailingstep = 10 //trailing step to move the "stoploss" ///2 BREAKEAVEN/////////// once breakeaven = 0 //1 on - 0 off startBreakeven = 30 //how much pips/points in gain to activate the breakeven function? PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread) //reset the stoploss value IF NOT ONMARKET THEN newSL=0 breakevenLevel=0 // MFE MAXPRICEMFE = 0 priceexitMFE = 0 Maxfloatingprofit = 0 ENDIF //manage long positions IF LONGONMARKET THEN //first move (breakeven) IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN newSL = tradeprice(1)+trailingstep*pipsize ENDIF //next moves IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN newSL = newSL+trailingstep*pipsize ENDIF ENDIF //stop order to exit the positions IF newSL>0 THEN SELL AT newSL STOP ENDIF //test if the price have moved favourably of "startBreakeven" points already IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN //calculate the breakevenLevel breakevenLevel = tradeprice(1)+PointsToKeep*pipsize ENDIF //place the new stop orders on market at breakevenLevel IF breakevenLevel>0 THEN SELL AT breakevenLevel STOP ENDIF IF Equity < 100000 Then TRAILINGMFE = 10 MFE = 1 N = 3 ELSIF Equity >= 100000 AND Equity < 120000 Then TRAILINGMFE = 20 MFE = 1 N = 5 ELSIF Equity >= 120000 AND Equity < 150000 Then TRAILINGMFE = 30 MFE = 1 N = 8 ELSIF Equity >= 150000 AND Equity < 180000 Then TRAILINGMFE = 40 MFE = 1 N = 10 ELSIF Equity >= 180000 Then MFE = 0 N = 15 ENDIF if MFE>0 then if longonmarket then MAXPRICEMFE = MAX(MAXPRICEMFE,close) //saving the MFE of the current trade if MAXPRICEMFE-tradeprice(1)>=TRAILINGMFE*pointsize then //if the MFE is higher than the trailingstop then priceexitMFE = MAXPRICEMFE-TRAILINGMFE*pointsize //set the exit price at the MFE - trailing stop price level endif endif if onmarket and priceexitMFE>0 then SELL AT priceexitMFE STOP endif endif floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains Maxfloatingprofit = Max(Maxfloatingprofit,floatingprofit) IF LongOnMarket Then IF COUNTOFLONGSHARES > 3 AND floatingprofit < floatingprofit[1] Then Sell 1 Share AT Market ENDIF ENDIF //IF LongOnMarket Then //IF COUNTOFLONGSHARES > 1 AND Maxfloatingprofit > Maxfloatingprofit[1] Then //Sell 1 Share AT Market //ENDIF //ENDIF // ######################################################################## // _____ _ // / ____| | | // | | __ _ __ __ _ _ __ | |__ // | | |_ | '__/ _` | '_ \| '_ \ // | |__| | | | (_| | |_) | | | | // \_____|_| \__,_| .__/|_| |_| // | | // |_| // Drawing Trailing Stop IF OnMarket AND newSL > 0 Then TrailingSL = newSL FirstSL = TradePrice - SL * pipsize //EntryPrice = TradePrice ELSE TrailingSL = Undefined //FirstSL = Undefined //EntryPrice = Undefined ENDIF Graph PositionPerf * 100 AS "PositionPerf" //Graph PositionPerf(1) * 100 AS "PositionPerf[1]" //Graph floatingprofit AS "floatingprofit" //GraphOnPrice TradePricecoloured(0,220,0) AS "Long Trade Price" //// Draw Equity //Graph Equity AS "Equity" //Graph Capital AS "Capital" // ######################################################################## // _____ _ _____ _ // / ____| | / ____| | | // | (___ | |_ ___ _ __ | (___ _ __ __ _| |_ ___ __ _ _ _ // \___ \| __/ _ \| '_ \ \___ \| '__/ _| __/ _ \/ _| | | | // ____) | || (_) | |_) | ____) | | | (_| | || __/ (_| | |_| | // |_____/ \__\___/| .__/ |_____/|_| \__,_|\__\___|\__, |\__, | // | | __/ | __/ | // |_| |___/ |___/ MaxDrawDownPercentage = 40 // Max DrawDown of x% Equity = Capital + StrategyProfit HighestEquity = Max (HighestEquity,Equity) // Save the Maximum Equity we got MaxDrawdown = HighestEquity * (MaxDrawDownPercentage/100) ExitFromMarketCond = Equity <= HighestEquity - MaxDrawdown IF ExitFromMarketCond Then Quit ENDIF
if some one have some good idea or strategy to control the drawdown and the exposition it will be welcom ?
In the continuity of the improvement of this algorithm of which I see many subjects now on the forum, I made a copy of this code from this topic with a very small modification on the code so that it is a little more clear,
there is the original code :
if time = 061000 then //
x1= barindex[0] //
x2 = barindex[86] //
yh = highest[86](high)
yL=lowest[86](low)
drawsegment(x1,yh,x2,yh) coloured (0,0,255)
drawsegment(x1,yL,x2,yL) coloured (255,0,0)
// set text offset from line
os = 4*pipsize
drawtext("#yh#",barindex,yh+os,SansSerif,bold,20) coloured(0,0,0)
drawtext("#yL#",barindex,yL-os,SansSerif,bold,20) coloured(0,0,0)
endif
return yh as "hi" ,yL as "Lo"
to this code :
if time = 061000 then //
x1= barindex[0] //
x2 = barindex[86] //
yh = highest[86](high)
yL=lowest[86](low)
DRAWRECTANGLE(x1, yh, x2,yL ) COLOURED(0,0,0)
os = 4*pipsize
drawtext("#yh#",x2,yh+os,SansSerif,bold,10) coloured(0,0,0)
drawtext("#yL#",x2,yL-os,SansSerif,bold,10) coloured(0,0,0)
endif
if time >= 061000 AND time <= 230000 then
drawsegment(x1,yh,barindex,yh) coloured (0,0,255) STYLE(DOTTEDLINE3,1)
drawsegment(x1,yL,barindex,yL) coloured (0,0,255) STYLE(DOTTEDLINE3,1)
endif
return
Next messag eI will post some picture and GTM problem with France for be sure to adjust the time to Tokyo session there is only from 9AM to 3 PM in tokyo if I’m not wrong
Let’s start by correcting the timetable, the Japanese market is open from 9am until 11:30am and then from 12:30pm to 3pm Tokyo time, we can summarise that the market is open from 6 a.m. to 3 p.m., So 6 hours of opening time
so in GTM (UTC) time the Japanese market is open from 0 to 6 a.m GTM, so for france trader like me they have 2 situation :
– in the summer France is +2 GTM, so this tokyo box is from 2am to 8am
– in the winter France is +1 GTM, so this tokyo box is from 1am to 7am
so now we e in the summer, the code about it’s like this :
if time = 081000 then
x1= barindex[0]
x2 = barindex[74] // 74 - 2 = 72 ==> 72 x 5 = 360min ==> 360min / 60 = 6h
yh = highest[74](high)
yL=lowest[74](low)
DRAWRECTANGLE(x1, yh, x2,yL ) COLOURED(0,0,0)
os = 4*pipsize
drawtext("#yh#",x2,yh+os,SansSerif,bold,10) coloured(0,0,0)
drawtext("#yL#",x2,yL-os,SansSerif,bold,10) coloured(0,0,0)
endif
if (time >= 081000 AND time <= 240000) then
drawsegment(x1,yh,barindex,yh) coloured (0,0,255) STYLE(DOTTEDLINE3,1)
drawsegment(x1,yL,barindex,yL) coloured (0,0,255) STYLE(DOTTEDLINE3,1)
endif
return
End Of Day – YEN M15 Strategy
This topic contains 95 replies,
has 8 voices, and was last updated by ZeroCafeine
2 years, 9 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 03/24/2023 |
| Status: | Active |
| Attachments: | 39 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.