Need help to code second entry with same stoploss level

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #178264 quote
    VinzentVega
    Participant
    Veteran

    Good morning

    I need help to code an second entry after the first position is “x”  points in minus. The sl of the second position must be the same as that of the first position.

    For example:

    1. position: SL 200

    2. position: entry at -150 / sl -50

     

    Could anybody help?

    #178266 quote
    JS
    Participant
    Senior
    DefParam CUMULATEORDERS = True
    
    StopDistance = 200
    
    If LongOnMarket and TradePrice(1) - Close > 150 Then
    Buy at Market
    Set Stop pLoss StopDistance - (TradePrice(2) - TradePrice(1))
    
    ElsIf ShortOnMarket and Close - TradePrice(1) > 150 Then
    Sell at Market
    Set Stop pLoss StopDistance - (TradePrice(1) - TradePrice(2))
    
    End
    #178267 quote
    JS
    Participant
    Senior

    Not “End” but “EndIf” (last line)

    #178276 quote
    robertogozzi
    Moderator
    Master

    Bear in mind that the stop loss is NOT separate for each position entered. It is calculated on PositionPrice (the average price). So there will only be ONE stop loss. If it’s hit, the first position entered will lose more than 200 pips and the second one will lose less than 200 pips. In the end you will suffer a 200-pip loss, anyway.

    #178281 quote
    VinzentVega
    Participant
    Veteran

    @JS

    Thank you very much.

    @robertogozzi

    Thats true. In case of SL, i will catch the first entry with SL 200 and the second entry with SL 50. In total 250 SL

    #178286 quote
    palettofix
    Participant
    Junior

    is this correct?

    If LongOnMarket and TradePrice(1) - Close > 150 Then
    Buy at Market
    Set Stop pLoss StopDistance - (TradePrice(2) - TradePrice(1))
    endif
    
    // short omitted

    As far as I know,
    the buy order is executed at the start of the next candle. So TradePrice is not updated yet.

    #178290 quote
    VinzentVega
    Participant
    Veteran

    Pls find attached two backtests. Left side is the origin one, right side ist the version with the second entry. But something went wrong. I dont see the second entrys in the orderlist and SL is to high.

    test.png test.png
    #178294 quote
    robertogozzi
    Moderator
    Master

    Thats true. In case of SL, i will catch the first entry with SL 200 and the second entry with SL 50. In total 250 SL

    Not exactly, it’s always 200 pips from POSITIONPRICE (the average price, see also my pic). Example:

    • first LONG      entry at 15600  – PositionPrice = 15600)                                    (initial SL    at 15400) – if hit, the position will lose 200 pips (200 pips per position)
    • second LONG entry at 15520  – PositionPrice = 15560,(15600+15520)/2)  (updated SL at 15360) – if hit, the 1st position will lose 240 pips, while the 2nd position will lose 160 pips (so it’s 200 pips per position)
    Calcolo-del-Prezzo-Medio-di-una-Posizione-Position-Average-Price-calculation.jpg Calcolo-del-Prezzo-Medio-di-una-Posizione-Position-Average-Price-calculation.jpg
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Need help to code second entry with same stoploss level


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 7 replies,
has 4 voices, and was last updated by robertogozzi
4 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/24/2021
Status: Active
Attachments: 2 files
Logo Logo
Loading...