Hi, Can someone advise me on how to use the limit and stop order in prt. I want to automate a simple order eg, go short when the price reaches the value 22$ and then 23$. Should I use Limit or Stop order?
SELLSHORT 10 CONTRACTS AT 22 LIMIT
SELLSHORT 10 CONTRACTS AT 23 LIMIT
or
SELLSHORT 10 CONTRACTS AT 22 STOP
SELLSHORT 10 CONTRACTS AT 23 STOP
When the VIX price (IG quotes) reached 22$ and 23$ today it did not trigger the LIMIT order. Simple test strategy source code below.
DEFPARAM CumulateOrders = True
x1 = 50
// Enter short positions
IF CLOSE > 18 AND CLOSE < 26 THEN
SELLSHORT x1 CONTRACTS AT 22.00 LIMIT
SELLSHORT x1 CONTRACTS AT 23.00 LIMIT
SET TARGET PROFIT 1 // 1 = $1
SET STOP pTRAILING 10 // Use this one
ENDIF
I guess you read up on Stop and Limit Orders?
https://www.investopedia.com/ask/answers/04/022704.asp
Our code needs to check where price level is relative to a Stop or Limit Order (i.e. above or below) else we may end buying or selling straight off (at a bad price) if we dont code the correct Order.
I take it you are testing your code on Demo / Virtual Account, best to be safe until we are certain of our code?
Try …
If Close > 22 Then
SellShort 1 Contract at 22 Stop
Endif
OR
If Close < 22 Then
SellShort 1 Contract at 22 Limit
Endif
Yes, GraHal is right, as you can see from attached pic.
Ye I did read up on LIMIT and STOP order. However, the purpose of the automation is to replicate my manual process where I set up a limit order in IG for $22 so this order get executed when the VIX price touches $22. The PRT document shows that the LIMIT order could be used but as it failed during the test hence I asked for your input. I will try your suggestion above and update you.
Grahal and Robert,
So I had simply did an in depth step through your suggestion / code. There is very little difference between you code suggestion and my code, please advise?
If Close < 22 Then
SellShort 1 Contract at 22 Limit
Endif
x1 = 50
IF CLOSE > 21.5 AND CLOSE < 23.5 THEN // the loop condition is tighter
SELLSHORT x1 CONTRACTS AT 22.00 LIMIT // set up a short order
SELLSHORT x1 CONTRACTS AT 23.00 LIMIT // set up the next short order
ENDIF
Try this …
If Close < 22 Then
SellShort 1 Contract at 22 Limit
Endif
x1 = 50
IF CLOSE < 22 OR CLOSE < 23.5 THEN // the loop condition is tighter
SELLSHORT 1 CONTRACTS AT 22.00 LIMIT // set up a short order
SELLSHORT 1 CONTRACTS AT 23.00 LIMIT // set up the next short order
ENDIF
The difference appears mainly live, not just backtesting, as the broker will reject your orders, or they may be entered at a wrong price.
STOP has a meaning, LIMIT a different one, they can’t be exchanged without knowing the position of the entry compared to the current price.
Then you will have to account for the distance required by the broker.
Pending orders require quite some coding effort to be dealt with.
Robert, I know it sounds simple but the coding can require more effort. Can you provide any example from previous code that you have came across to illustrate what you said “Pending orders require quite some coding effort to be dealt with”. Thanks.
For each pending order you place you need to check both if the entry price is above/below the current price (CLOSE) and if there’s enough distance to meet the broker’s requirements:
Distance = 10 //10 pips required by the broker (it's just an example)
If Close < 22 Then
IF (close + Distance) < 22 THEN
SellShort 1 Contract at 22 Limit//Pending order if within the broker's requirements
ELSE
SellShort 1 Contract at Market //comment out this line to skip at market, bu you
ENDIF //may lose a trade to wait for the next bar
Endif
x1 = 50
IF CLOSE < 22 OR CLOSE < 23.5 THEN // the loop condition is tighter
IF (close + Distance) < 22 THEN
SELLSHORT 1 CONTRACTS AT 22.00 LIMIT // set up a short order
ELSIF (close - Distance) > 22 THEN
SELLSHORT 1 CONTRACTS AT 22.00 STOP // set up a short order
ELSE
SellShort 1 Contract at Market //comment out this line to skip at market, bu you
ENDIF //may lose a trade to wait for the next bar
//
IF (close + Distance) < 23 THEN
SELLSHORT 1 CONTRACTS AT 23.00 LIMIT // set up a short order
ELSIF (close - Distance) > 23 THEN
SELLSHORT 1 CONTRACTS AT 23.00 STOP // set up a short order
ELSE
SellShort 1 Contract at Market //comment out this line to skip at market, bu you
ENDIF
ENDIF
distance is usually 6 pips on DAX, 1 on S&P500, etc… you can find it out on the broker’s website.
Robert, thanks in advance and I will see what I can learn and adopt. I have attached my simple grid trading script and failed snapshot from yesterday/today but it has been going on for the last 5 to 10 days since I released in to Live.
// VIX GRID TRADING SHORT STRATEGY WITH 2 HOURS TIMEFRAME
// This is a simple grid trading system to enter long and short positions when VIX is moving sideways between $18 to $20 (or $21 to $23 in IG.com).
DEFPARAM CumulateOrders = True
x1 = 50
// Enter short positions
IF CLOSE >= 21.50 AND CLOSE <= 23.00 THEN
//IF NOT SHORTONMARKET THEN
SELLSHORT x1 CONTRACTS AT 21.50 LIMIT
SELLSHORT x1 CONTRACTS AT 21.75 LIMIT
SELLSHORT x1 CONTRACTS AT 22.00 LIMIT
SELLSHORT x1 CONTRACTS AT 22.25 LIMIT
SELLSHORT x1 CONTRACTS AT 22.50 LIMIT
SELLSHORT x1 CONTRACTS AT 22.75 LIMIT
SELLSHORT x1 CONTRACTS AT 23.00 LIMIT
//SET TARGET %PROFIT 2 // 5% = $1 based on $20 price
//SET TARGET $PROFIT 1000 // 1000 = $1
SET TARGET PROFIT 1 // 1 = $1
//SET TARGET pPROFIT 1 // 1 = $1. Use this one
//SET STOP %LOSS 5 // This 25% will ensure that the stop loss will not reach but it’s a risk mgt parameter
SET STOP pTRAILING 10 // Use this one
//ENDIF
ENDIF
vix grid trading failed snapshot.
Robert, Let me know if this looks right (also AND is used instead of OR) to you but the output seems good???
DEFPARAM CumulateOrders = True
distance = 10 // 10 pips required by the broker
distancePlus = Close + distance
distanceMinus = Close – distance
x1 = 100
If Close >= 21 AND Close <= 23 Then // The loop condition is tighter
If distancePlus <= 21 Then
SellShort x1 Contracts At 21 Limit // Set up a short order
Elsif distanceMinus >= 21 Then
SellShort x1 Contracts At 21 Stop // Set up a short order
Else
SellShort x1 Contracts At Market // Comment out this line to skip At Market but you
Endif // may lose a trade to wait for the next bar
//Set Target %Profit 5 // 5% = $1 based on $20 price
Set Target Profit 1 // 1 = $1
//Set Stop %Loss 5
//Set Stop %Trailing 5
//Set Stop pTrailing 2
Endif
To use your currency for the target, you must use the $ sign:
Set Target $Profit 1 // 1 = $1
As you wrote it, it means 1 unit as a price difference, which would work with most indices (if their value per pip, PipValue, is 1), but would fail with currency pairs such as GBPUSD (what if 1 was added or subtracted to/from 1.3500!?).
A similar issue could be in
distance = 10
it should read either:
distance = 10*PipSize
or
distancePlus = Close + distance*PipSize
distanceMinus = Close – distance*PipSize
The code to place pending orders is correct.
Further tests have shown that only the ‘SellShort 1 Contract at Market’ in the If – Else loop was called so the Limit and Stop order are redundant. Therefore, your code and my modification of your code (uses MARKET ORDER) doesn’t address my original question and issue because I want to automate my manual process where I go to IG and set a LIMIT ORDER to short at $23 and take profit at $22. Any other idea ie use a while-loop or thought on this?
Original script:
DEFPARAM CumulateOrders = True
x1 = 50
// Enter short positions
IF CLOSE >= 21.50 AND CLOSE <= 23.00 THEN
SELLSHORT x1 CONTRACTS AT 21.50 LIMIT
SELLSHORT x1 CONTRACTS AT 21.75 LIMIT
SELLSHORT x1 CONTRACTS AT 22.00 LIMIT
SELLSHORT x1 CONTRACTS AT 22.25 LIMIT
SELLSHORT x1 CONTRACTS AT 22.50 LIMIT
SELLSHORT x1 CONTRACTS AT 22.75 LIMIT
SELLSHORT x1 CONTRACTS AT 23.00 LIMIT
SET TARGET PROFIT 1 // 1 = $1
SET STOP pTRAILING 10 // Use this one
ENDIF
New Script:
DEFPARAM CumulateOrders = True
distance = 10*PipSize
distancePlus = Close + distance
distanceMinus = Close – distance
x1 = 100
If Close => 21 AND Close <= 23 Then //
If distancePlus <= 21 Then
SellShort x1 Contracts At 21 Limit // Don’t need this
Elsif distanceMinus >= 21 Then
SellShort x1 Contracts At 21 Stop // Don’t need this
Else
SellShort x1 Contracts At Market // This is called 100% of the time
Endif
// Set Target %Profit 5 // 5% = $1 based on $20 price
Set Target Profit 1 // 1 = $1
//Set Stop %Loss 5
//Set Stop %Trailing 5
//Set Stop pTrailing 2
Endif