I need code. I have a position, after 12 hours in the market it is in losses. I want that if the price returns to the purchase price the operation is closed.
There you go:
If Not OnMarket then
Count = 0
Else
If Hour <> Hour[1] Then
x = 1
If Hour < Hour[1]
x = ((Hour + 24) - Hour[1])
Endif
Count = Count + x
If Count >= 12 And PositionPerf < 0 Then
Sell at Market
Exitshort at Market
Endif
Endif
Endif
Like doing the same thing but with 5-minute bars. Instead of 12 hours it would be 144 bars of 5 minutes.
The trade begins, if after 144 bars of 5 minutes the trade is lost, if the quote returns to the purchase price the trade is closed.
Thanks, and sorry.
I think your code does not work Roberto, also
I would need the code but with 5 minute bars.
Operation opens.
If after 144 bars the operation is in losses, if the price returns to the purchase point, the operation is closed.
Thanks!
Yes, my fault, I exited at market.
Replace lines 11-12 with:
Sell at PositionPrice LIMIT
Exitshort at PositionPrice LIMIT
I’ll make the other code later.
Please do NOT use the “insert PRT code” button to enter common text. Use it only for code, instead.
Fran55 – Please be more careful when posting. Your last two posts have had attached HTML (from I guess cutting and pasting translations) which moderators have had to tidy up to make your posts readable.
Ok Roberto.
I always have problems with translators, sorry.
One more thing:
I want to do the same with%.
That is … I have a position, I want that if the position loses 1% and returns to the purchase price, the operation is closed.
Thank you very much!
This is the code with the 3 different ways to exit when in loss:
//////////////////////////////////////////////////////////////////////////////
// --- Exit at Entry Price if losing AFTER N hours
If Not OnMarket then
Count = 0
Else
If Hour <> Hour[1] Then
x = 1
If Hour < Hour[1] Then
x = ((Hour + 24) - Hour[1])
Endif
Count = Count + x
If Count >= 12 And PositionPerf < 0 Then //12 hours
Sell at PositionPrice LIMIT
Exitshort at PositionPrice LIMIT
Endif
Endif
Endif
//////////////////////////////////////////////////////////////////////////////
// --- Exit at Entry Price if losing AFTER N bars
If OnMarket then
If (BarIndex - TradeIndex) >= 144 And PositionPerf < 0 Then //144 bars
Sell at PositionPrice LIMIT
Exitshort at PositionPrice LIMIT
Endif
Endif
//////////////////////////////////////////////////////////////////////////////
// --- Exit at Entry Price if losing 1%+
If Not OnMarket then
ExitFlag = 0
Else
If ((PosiTionPerf * 100) <= -1) OR ExitFlag THEN //1% loss
ExitFlag = 1
Sell at PositionPrice LIMIT
Exitshort at PositionPrice LIMIT
Endif
Endif
//////////////////////////////////////////////////////////////////////////////
Much, much, thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Okay. And if I want it to close 0.5% above the purchase price ???
We cannot always tidy up your code!
Please use plain text without embedded formatting codes. Thank you 🙂
Use
Sell at PositionPrice*1.005 LIMIT
Exitshort at PositionPrice*0.995 LIMIT