Hi,
I’d like to have a buy stop order in the market for a limited time, which is possible with the following simple code:
IF TIME >= Start AND TIME < End AND LongCounter = 0 THEN
BUY NbrContracts CONTRACTS AT XX STOP
SET STOP pLOSS SL
SET TARGET pPROFIT TP
ENDIF
I’d like the order only to be executed once in that time, which I solved with:
IF LongOnMarket THEN
LongCounter = 1
ENDIF
Still easy, right?
The tricky part is when I’m being stopped out on the same candle as the entry, so the code haven’t run again and realized that there’s already been an attempt at this trade which failed.
I looked in the handbook as well, which had the following example:
ONCE NbBarLimit = 10
MM20 = Average[20](close)
MM50 = Average[50](close)
// If MM20 crosses over MM50, we define 2 variables "MyLimitBuy" and "MyIndex" containing
the close price at that time and the index of the bar of the cross.
IF MM20 CROSSES OVER MM50 THEN
MyLimitBuy = close
MyIndex = Barindex
ENDIF
IF BarIndex >= MyIndex + NbBarLimit THEN
MyLimitBuy = 0
ENDIF
// Place an order at the price MyLimitBuy valid as long as this variable is greater than
0 and we are not in a long position.
// Remember: MyLimitBuy is greater than 0 for the 10 bars after the bar of the crossing.
IF MyLimitBuy > 0 AND NOT LongOnMarket THEN
BUY 1 SHARES AT MyLimitBuy LIMIT
ENDIF
I haven’t really tried that one, since I suspect that the result will be the same as with my code.
So, any ideas how to place a single stop order for a limited time?
Cheers!
Your first code is good. While the time condition is still true, the pending order will be set at market and because this kind of order only last one candlestick, no new stop order will be set after the “End” time variable value.
If the trade triggered by the pending order opened and closed on the same bar, you can try to know an order has existed with POSITIONPERF, POSITIONPRICE, TRADEINDEX, TRADEPRICE ..
Worked like a charm! Thanks, Cheif!
If you managed your problem, would be cool to share your code for future reference for other people in trouble 😉 Thanks.
Yes, that was the plan, I just had to try it out last night to make sure it worked. I solved it like this, fairly simple and probably not the most sexy solution but it gets the job done:
IF TIME = StartTrade THEN
TRD = TradeIndex
ENDIF
// Conditions to enter long position
IF TIME >= StartTrade AND TIME < EndTrade AND LongCounter = 0 AND TradeIndex = TRD THEN
BUY NbrContracts CONTRACTS AT XX STOP
SET STOP pLOSS SL
SET TARGET pPROFIT TP
ENDIF
So basically:
-At one time only, when it’s start to trade, set TRD = TradeIndex
-Before adding another buy stop in the market, make sure that TradeIndex still equals TRD (since it has changed if a trade was taken)
I guess the LongCounter variable is now pretty useless, but I’ll just leave it there for now.
Thanks again!