Simple limit or stop order
Forums › ProRealTime English forum › ProOrder support › Simple limit or stop order
- This topic has 18 replies, 3 voices, and was last updated 3 years ago by
micquan.
-
-
01/18/2022 at 7:02 PM #185869
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
01/18/2022 at 7:56 PM #185875When 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 THENSELLSHORT x1 CONTRACTS AT 22.00 LIMIT
SELLSHORT x1 CONTRACTS AT 23.00 LIMITSET TARGET PROFIT 1 // 1 = $1
SET STOP pTRAILING 10 // Use this oneENDIF
01/18/2022 at 10:10 PM #185884I 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 …
123If Close > 22 ThenSellShort 1 Contract at 22 StopEndifOR
123If Close < 22 ThenSellShort 1 Contract at 22 LimitEndif01/18/2022 at 10:46 PM #18588601/20/2022 at 2:15 PM #186066Ye 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.
01/20/2022 at 3:03 PM #186078Grahal 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 ThenSellShort 1 Contract at 22 LimitEndifx1 = 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 orderENDIF
01/20/2022 at 3:35 PM #186084Try this …
12345678910111213If Close < 22 ThenSellShort 1 Contract at 22 LimitEndifx1 = 50IF CLOSE < 22 OR CLOSE < 23.5 THEN // the loop condition is tighterSELLSHORT 1 CONTRACTS AT 22.00 LIMIT // set up a short orderSELLSHORT 1 CONTRACTS AT 23.00 LIMIT // set up the next short orderENDIF01/20/2022 at 3:45 PM #186086The 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.
01/20/2022 at 4:15 PM #186094Robert, 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.
01/20/2022 at 5:16 PM #186100For 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:
1234567891011121314151617181920212223242526Distance = 10 //10 pips required by the broker (it's just an example)If Close < 22 ThenIF (close + Distance) < 22 THENSellShort 1 Contract at 22 Limit//Pending order if within the broker's requirementsELSESellShort 1 Contract at Market //comment out this line to skip at market, bu youENDIF //may lose a trade to wait for the next barEndifx1 = 50IF CLOSE < 22 OR CLOSE < 23.5 THEN // the loop condition is tighterIF (close + Distance) < 22 THENSELLSHORT 1 CONTRACTS AT 22.00 LIMIT // set up a short orderELSIF (close - Distance) > 22 THENSELLSHORT 1 CONTRACTS AT 22.00 STOP // set up a short orderELSESellShort 1 Contract at Market //comment out this line to skip at market, bu youENDIF //may lose a trade to wait for the next bar//IF (close + Distance) < 23 THENSELLSHORT 1 CONTRACTS AT 23.00 LIMIT // set up a short orderELSIF (close - Distance) > 23 THENSELLSHORT 1 CONTRACTS AT 23.00 STOP // set up a short orderELSESellShort 1 Contract at Market //comment out this line to skip at market, bu youENDIFENDIFdistance is usually 6 pips on DAX, 1 on S&P500, etc… you can find it out on the broker’s website.
01/21/2022 at 12:41 PM #186165Robert, 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
ENDIF01/21/2022 at 12:43 PM #18616701/21/2022 at 6:37 PM #186230Robert, 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 – distancex1 = 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
Endif01/21/2022 at 6:55 PM #186237To use your currency for the target, you must use the $ sign:
1Set Target $Profit 1 // 1 = $1As 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 in1distance = 10it should read either:
1distance = 10*PipSizeor
12distancePlus = Close + distance*PipSizedistanceMinus = Close – distance*PipSizeThe code to place pending orders is correct.
01/22/2022 at 4:22 PM #186310Further 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 THENSELLSHORT 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 LIMITSET TARGET PROFIT 1 // 1 = $1
SET STOP pTRAILING 10 // Use this oneENDIF
New Script:
DEFPARAM CumulateOrders = Truedistance = 10*PipSizedistancePlus = Close + distancedistanceMinus = Close – distancex1 = 100If Close => 21 AND Close <= 23 Then //If distancePlus <= 21 ThenSellShort x1 Contracts At 21 Limit // Don’t need thisElsif distanceMinus >= 21 ThenSellShort x1 Contracts At 21 Stop // Don’t need thisElseSellShort x1 Contracts At Market // This is called 100% of the timeEndif// Set Target %Profit 5 // 5% = $1 based on $20 priceSet Target Profit 1 // 1 = $1//Set Stop %Loss 5//Set Stop %Trailing 5//Set Stop pTrailing 2Endif -
AuthorPosts
Find exclusive trading pro-tools on