my first strategy – beginner level
Forums › ProRealTime English forum › ProOrder support › my first strategy – beginner level
- This topic has 9 replies, 3 voices, and was last updated 7 years ago by
Nicolas.
-
-
07/30/2017 at 11:23 PM #42041
Hello,
I am starting my first strategy, just to understand the logic of proorder and how things work.
At this stage I am facing 2 issues:
- how to take partiel profit (50%) at a certain level
- have a dynamic stop loss:
- close 100% if the bar after the entry does not confirm (red bar in case of a long trade)
- else: stop moves below each bar low
1234567891011121314151617181920212223DEFPARAM CumulateOrdersmm20 = Average[20](close)mm50 = Average[50](close)openLong= mm20 crosses over mm50if not longonmarket and openLong thenbuy 1 lot at market // buy 1 contracts at marketamplitude = close - lowendifset stop loss amplitudeset target profit amplitude// partial profit - to be completed// to do// stop move - to be completedif longonmarket thennewStop = low[1]endifsell at newStop STOP07/31/2017 at 6:38 AM #42050Partial closure of position is still not supported by ProOrder.
About your stoploss, the exit at each new bar low should operate nicely the way you have coded it. In the code below I implemented the exit on red candlestick:
1234567891011121314151617181920212223242526DEFPARAM CumulateOrdersmm20 = Average[20](close)mm50 = Average[50](close)openLong= mm20 crosses over mm50if not longonmarket and openLong thenbuy 1 lot at market // buy 1 contracts at marketamplitude = close - lowendifset stop loss amplitudeset target profit amplitude// partial profit - to be completed// to do// stop move - to be completedif longonmarket thenif tradeindex=barindex[1] and close<open thensell at marketelsenewStop = low[1]sell at newStop STOPendifendifNot tested, please confirm.
1 user thanked author for this post.
07/31/2017 at 10:48 PM #42142Hello,
thx a lot Nicolas,
I tested the proposed code and there still have an issue with the stop execution.
normally the stop is set to the previous low (low[1]) but as you can see in the screenshot, it executes one bar later than expected. please advice if you have an idea to fix this.
I tried both “sell at newStop STOP” between the if / endif and outside, but behaviour is the same.
Below my code:
1234567891011121314151617181920212223242526272829303132333435DEFPARAM CumulateOrders = False // Cumul des positions désactivémm50 = Average[50](close)openLong= close > mm50 and close[1] < mm50openShort= close < mm50 and close[1] > mm50graph openLonggraph openShortif not longonmarket and openLong thenbuy 1 lot at market // buy 1 contracts at marketendifif not shortonmarket and openShort thensellShort 1 lot at market // buy 1 contracts at marketendifif longonmarket thenif tradeindex=barindex and close<open thensell at marketelsenewStop = low[1]sell at newStop STOPendifendifif shortonmarket thenif tradeindex=barindex and close>open thenbuy at marketelsenewStop = high[1]buy at newStop STOPendifendifgraph newStop //debugging08/01/2017 at 5:41 AM #4214908/01/2017 at 11:09 AM #4218408/02/2017 at 4:29 AM #42276Thx gentlemen,
I have re-written my code, but still struggling with the stop.
the stop executes one bar late and at close price (not at the defined stop level)
Below my code and I also attached the ITF file
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152// ================================================================================ Paramsdefparam cumulateorders = false // Cumul des positions désactivépositionSize = 1// ================================================================================ calc openlong openShort closeLong closeShort moveStopLong moveStopShortmm50 = Average[50](close)openLong= close > mm50 and close[1] < mm50openShort= close < mm50 and close[1] > mm50graph openLonggraph openShortcloseLong = barindex = tradeindex and close < opencloseShort = barindex = tradeindex and close > open// ================================================================================ position openingif not onmarket and openLong thenbuy positionSize lot at marketendifif not onmarket and openShort thensellShort positionSize lot at marketendif// ================================================================================ set stop & targetif longonmarket thenif closeLong thensell at marketelsestopLevel = low[1]sell at stopLevel STOPendifendifif shortonmarket thenif closeShort thenexitshort at marketelsestopLevel = high[1]exitshort at stopLevel STOPendifendifgraph stopLevelif not onmarket thenstopLevel=0endif08/02/2017 at 9:02 PM #42369Are you sure your “stoplevel” is correctly calculated? If the answer is yes, then the problem should come from the way you are putting your pending stop orders, because they are included in a conditional block, they may be put on market too late.
08/04/2017 at 11:20 PM #42490Hello Nicolas,
thanks a lot for your help, I did several tests and it does not work correctly.
could you please tell me simply how to set a stop at the previous low (for a long)?
illustration in the screenshot.
My current code (not ok):
1234567891011121314defparam cumulateorders = falseif not onmarket and close > Average[50](close) and close[1] < Average[50](close) thenbuy 1 lot at marketendifif not onmarket thenlastStop=0endifif longonmarket thenlastStop = low[1]sell at lastStop STOPendif08/05/2017 at 12:21 AM #42492my solution have been found here:
https://www.prorealcode.com/topic/bullish-engulfing-breakout-bearish-engulfing/
below the corrected code:
12345678910111213defparam cumulateorders = falseif not onmarket and close > Average[50](close) and close[1] < Average[50](close) thenbuy 1 lot at market// initial stoplastStop = lowsell at lastStop STOPendifif longonmarket thenlastStop = lowsell at lastStop STOPendifPS.: some strategies that I have checked during my research for the stop are wrong, meaning the “lastStop = low[1]” that we can see in several strategies is not ok!!
08/05/2017 at 9:53 AM #42496Because the code is read at Close and position opened at the next candle open, you need to refer to Low[0]. When you find a variable not calculated correctly, use GRAPH! and the solution will come to you quickly 🙂
1 user thanked author for this post.
-
AuthorPosts