Close all open Orders

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #208890 quote
    Suffi
    Participant
    Junior

    Hello all,
    I need your help.
    in a strategy several orders are opened, sometimes more sometimes less.
    Now I want to limit the total loss and close all open positions without the “Quit” command.

    Greets

    Suffi

    #208892 quote
    GraHal
    Participant
    Master

    There are so many possible answers, I’ll start with one …

    Are you using below as the 1st line of your code?

    Defparam cumulateorders = false

    Above will mean – unless code states (?) – you won’t open more than 1.

    If you want to post your code then we may be able to help you a lot easier for us and quicker for you.

    #208901 quote
    Nicolas
    Keymaster
    Master

    If you are accumulating orders and you want to limit the loss of the orders basket, you can set an exit stop level at a certain price, by using POSITIONPRICE (which is the mean of all orders opening price):

    set the stoploss at mean opened price – 20 points, limiting the loss to “only” 20 points.

     

    //long positions
    if longonmarket then 
     //set the stoploss at mean opened price - 20 points, limiting the loss to "only" 20 points
     set stop price positionprice - 20*pointsize 
    endif
    GraHal thanked this post
    #208904 quote
    PeterSt
    Participant
    Master
    Sell at Market        // For Long.
    ExitShort at Market   // For Short.

    That’s all. Unless I misinterpret the situation.

    #208922 quote
    Suffi
    Participant
    Junior

    Hello all,
    Thanks for the answers.
    I can understand Nicolas approach.
    I had thought of determining the maxloss in euros.
    E.g. if loss< -500€ then close all

    I am on holiday at the moment and only have the iPad with me, so I can’t post a code.

    Greetings from New Zealand

    #209294 quote
    Suffi
    Participant
    Junior

    Hello,
    I am back from holiday and can now also post the code.
    I have tried the following code but something does not fit.

    DEFPARAM CumulateOrders = true
    ONCE maxLOSS      = 400 //Euro
    IF LONGONMARKET THEN
    IF ((tradeprice(0)-close)*POINTVALUE)>= maxLoss THEN
    SELL AT Market
    ENDIF
    ENDIF
    
    IF SHORTONMARKET THEN
    IF ((close-tradeprice(0))*POINTVALUE)>= maxLoss THEN
    EXITSHORT AT MARKET
    ENDIF
    ENDIF
    #209295 quote
    GraHal
    Participant
    Master

    Your version will check for exit conditions at the end of each bar.

    Nicolas version sets a pending order which can trigger during a bar.

    #209306 quote
    Suffi
    Participant
    Junior

    Hello,
    I can’t get my head around this and I’m trying to explain it again.

    Ex.
    Position value 1€/point
    maxloss: 500 €
    1st pos. buy at 10.000 € results in a distance of 500 points for the SL.
    2nd pos. buy at 10.100 € results in a buy price of 10.050.
    Now I have 2 positions. The SL changes to 250 points, but to the new purchase price of 10.050€.
    As more positions I have, the tighter the stoploss becomes from the changing purchase price.
    I can’t get the code to work.

    Greetings
    Suffi

    #209308 quote
    GraHal
    Participant
    Master

    Try below and let us know?

    ONCE maxLOSS      = 400*countofposition //Euro

    COUNTOFPOSITION

    #209312 quote
    Suffi
    Participant
    Junior

    Hi,

    I hope thats right.

    If LongOnMarket THEN
    set stop price POSITIONPRICE - (maxloss / (COUNTOFLONGSHARES * pointsize))
    ENDIF
    
    If ShortOnMarket THEN
    set stop price POSITIONPRICE + (maxloss / (COUNTOFSHORTSHARES * pointsize))
    ENDIF
    #209321 quote
    GraHal
    Participant
    Master

    Isn’t below what it should be?
    Try yours and mine on Live data on Demo Account.

    Let us know how it goes.

    If LongOnMarket THEN
    set stop price POSITIONPRICE - (maxloss * pointsize))
    ENDIF
     
    If ShortOnMarket THEN
    set stop price POSITIONPRICE + (maxloss* pointsize))
    ENDIF
    #209338 quote
    Suffi
    Participant
    Junior

    hello,
    this is the result of the code below, it should be correct.

    maxloss=300 //€

    If LongOnMarket THEN
    set stop price POSITIONPRICE - (maxloss / (COUNTOFLONGSHARES * pointsize))
    ENDIF
    
    If ShortOnMarket THEN
    set stop price POSITIONPRICE + (maxloss / (COUNTOFSHORTSHARES * pointsize))
    ENDIF
    Result-of-Code-Suffi.jpg Result-of-Code-Suffi.jpg
    #209343 quote
    GraHal
    Participant
    Master

    As more positions I have, the tighter the stoploss becomes from the changing purchase price.

    If you not want stop price to reduce with more positions then why are you using maxloss / COUNTOFSHARES  in your code above?

    If you are happy with the result you show in your screenshot then all is good? 

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

Close all open Orders


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Suffi @suffi Participant
Summary

This topic contains 12 replies,
has 4 voices, and was last updated by GraHal
3 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/01/2023
Status: Active
Attachments: 1 files
Logo Logo
Loading...