Hello all,
I would like to seek some advice on coding time-based limit order. For example if the buy condition is met, I do not want to buy right after the next candle. Instead, if the condition is met, and when the new candle starts, I want to put a buy limit on the previous candle to buy a 50% of the high and low. Say the condition is met, and at the start of the new candle, I look at the previous candle. The previous candle high is 15, and low is 5, and thus I would like to out a buy limit at 10, as shown in the code below
//Find the mid point of the previous candle
UBand = highest[1](high[1])
LBand = lowest[1](low[1])
ToBuyLevel = (UBand+LBand)/2
If MyLongConditions And Not LongOnMarket Then
Buy 1 Contract At ToBuyLevel Limit
EndIf
But if i want to make the buy limit order at 10 to be good for only the current candle + the next 4 candles only, how do I go about modifying the code to meet this criteria? Appreciate your advice. Thank you very much.
You cannot use use LIMIT or STOP randomly.
You need to use LIMIT orders when the entry price is more favourable than the current one (you want to BUY at a lower price or SELLSHORT at a higher one), while STOP orders are to be placed when the entry price is less favourable (you want to BUY at a higher or SELLSHORT at a lower one).
When placing pending orders you should also consider the minimum distance required by the broker for the instrument you are trading.
Anyway, your code to accomodate for a 5-bar (4 + current one) expiration is:
//Find the mid point of the previous candle
UBand = highest[1](high[1])
LBand = lowest[1](low[1])
ToBuyLevel = (UBand+LBand)/2
ONCE Count = 0
Count = Count -1
If MyLongConditions And Not LongOnMarket AND Count >= 0 Then
Buy 1 Contract At ToBuyLevel Limit
Count = 4
EndIf
This version will compare the current price to the entry price (with a 10-pip distance):
ONCE Distance = 10
ONCE Count = 0
//Find the mid point of the previous candle
UBand = highest[1](high[1])
LBand = lowest[1](low[1])
ToBuyLevel = (UBand+LBand)/2
Count = Count -1
If MyLongConditions And Not LongOnMarket AND Count >= 0 Then
IF close > (ToBuyLevel + Distance) THEN
Buy 1 Contract At ToBuyLevel LIMIT
Count = 4
ELSIF close < (ToBuyLevel - Distance) THEN
Buy 1 Contract At ToBuyLevel STOP
Count = 4
ELSE
Buy 1 Contract At MARKET //you may want to comment out this line to skip entering At MARKET
ENDIF
EndIf
as to the SHORT size you will have to revert conditions for the STOP/LIMIT order decision.
Thank you Roberto,
Yes the buy limit is to buy at a better level compare to current market. Basically the strategy is if it is a long bodied green candle, I do not want to buy at the next candle which will open at the high, and thus would prefer to put a buy limit somewhere below, say 50 pct retracement of that candle. And the buy limit is good for the current candle + the next 4 candle.
But the code:
ONCE Count = 0
Count = Count -1
If MyLongConditions And Not LongOnMarket AND Count >= 0 Then
Buy 1 Contract At ToBuyLevel Limit
Count = 4
EndIf
at the current candle is correct. But the Buy limit order is still good for the next candle, even tho the MyLongConditions is no longer valid. So even if the buy limit is not filled in the current candle, the buy limit supposed to be still live. But in the current code, because in the next candle, ifthe MyLongCondition is no longer valid, it will not continue to place the buy limit. Any thoughts?
I also spotted a logical error. Replace line 3 with this one:
If ((MyLongConditions AND Count < 0) OR (summation[5](MyLongConditions) AND Count >= 0)) And Not LongOnMarket Then