Sell at market instead of hitting guaranteed stop

Viewing 15 posts - 46 through 60 (of 62 total)
  • Author
    Posts
  • #188052 quote
    Mike Boorman
    Participant
    Average

    Thanks for your efforts Peter. I’ve done some more work on this. I’ve modified the code from @robertogozzi so the exits are now 99% accurate to what they were originally (I moved the “slprice” instruction to the bottom of the code), but the problem remains that trades are hitting a real stop, which means I will be charged the fee for hitting a guaranteed stop 55% of the time.  How can the below code be modified so the “if longonmarket” and “if shortonmarket” instructions can sell or exitshort without it being a stop? I’m sure “sell at market” is the key here, but I can’t find a way to use “sell at market” without creating a condition that is dependent on the close of a bar (which means I might have up to 75 minutes of slippage on an exit price).

    see the annotated lines of the code at the bottom for what I’m attempting with “sell at market”

    if longonmarket then
    slprice = tradeprice - sl
    sell at slprice stop
    endif
    
    if shortonmarket then
    slprice = tradeprice + sl
    exitshort at slprice stop
    endif
    
    set stop loss 100
    
    //if longonmarket then
    //slprice = tradeprice - sl
    //if longonmarket and close < slprice then 
    //sell at market
    //endif
    //endif
    
    //if shortonmarket then
    //slprice = tradeprice + sl
    //if close > slprice then
    //sell at market
    //endif
    //endif
    
    #188059 quote
    PeterSt
    Participant
    Master

    Hey Mike – A few days ago I was originally ready to hand you the code (which I still could do) but a lack of time did not permit that. So yes, I definitely have this working. But anecdotal is :

    What I learned from there I applied to my own code in order to check what it would bring. And surprise !! … I worked 3-4 hours on that because something with the Limit order itself seems not to work at all. This is the same Limit for entry you use, Mike. And from there I let it rest for a while. For you the same “issue” would occur, though this is hard to check or test.

    #188061 quote
    PeterSt
    Participant
    Master

    Mike, here you are.

    DEFPARAM CumulateOrders = False
    
    Starttime = 000000
    Limitentrytime = 220000
    Exclude=time >=020000 and time <=050000
     
     
    timeframe(225 minutes,updateonclose)
     
    if MACD[12,26,9](close) >0 then
      mac4flag=1
    endif
     
    if MACD[12,26,9](close) <0 then
      mac4flag=-1
    endif
     
    if close >ExponentialAverage[5](high) then
      upperflag=1
    endif
     
    if close <ExponentialAverage[5](high) and close >ExponentialAverage[5](low) then
      upperflag=0
    endif
     
    if close < ExponentialAverage[5](low) then
      upperflag=-1
    endif
     
    timeframe(75 minutes,default)
    
    Once slpricelong = 0   // 08-02-2022,PS.
    Once slpriceshort = 0  // 08-02-2022,PS.
     
    Averagecrosslong= Average[8](close) CROSSES OVER Average[20](close)
    Averagecrossshort= Average[8](close) CROSSES under Average[20](close)
     
     
    ONCE crossflag=0
    ONCE avgflag=0
     
     
    if close > ExponentialAverage[5](high) then
      avgflag=1
    endif
     
    if close < ExponentialAverage[5](low)then
      avgflag=-1
    endif
     
     
    longentry=0
     
    if avgflag=1 and averagecrosslong then
      crossflag=1
    endif
     
     
    c2 = close > Average[50](close)
     
    longentry= c2
     
    limitbuy=ExponentialAverage[5](low)
     
    if not onmarket and crossflag=1 and longentry=1 and mac4flag=1 and upperflag=1 then //and time >=StartTime and time <LimitEntrytime and not exclude then
      sl=AverageTrueRange[14]*2   // 0.00235
      tp=sl * 1.5                 // 0.003525
      slpricelong= close - sl     // 0.72685
      //Set stop loss sl
      risk=100/sl/10000                 // 42553
      BUY risk PERPOINT AT limitbuy limit   // Mike, Watch out for the /10000 (re my contract size).
    endif
     
    shortentry=0
     
    if avgflag=-1 and averagecrossshort then
      crossflag=-1
    endif
     
    d2 = close < Average[50](close)
    shortentry= d2
     
    limitsell=ExponentialAverage[5](high)
     
    if not onmarket and crossflag=-1 and shortentry and mac4flag=-1 and upperflag=-1 then //and time >=StartTime and time <LimitEntrytime and not exclude then
      sl=AverageTrueRange[14]*2
      tp=sl * 1.5
      Set target profit tp
      //slpriceshort = close + sl
      slpriceshort = sl - close   // 07-02-2022,PS, Must be like this.
      //Set stop loss sl
      risk=100/sl/10000    // Mike, Watch out for the /10000 (re my contract size).
      Sellshort risk PERPOINT AT limitsell limit
    endif
    if onmarket then
      crossflag=0
    endif
     
    Once CountL = 0
    Once CountS = 0
     
    if longonmarket and close < slpricelong then  // >
      //sell at Close - sl stop   // This does not work out in the middle of a bar.
      SellShort risk PERPOINT AT Close + sl limit   // This works out in the middle of a bar.
      LongDone = 1
      CountL = CountL + 1
    endif
     
    if shortonmarket and close > slpriceshort then  // <
      //exitshort at Close + sl stop   // This does not work out in the middle of a bar.
      BUY risk PERPOINT AT Close - sl limit   // This works out in the middle of a bar.
      ShortDone = 1
      CountS = CountS + 1
    endif
    
    Graph sl coloured (255,255,0)
    Graph tp coloured (0,255,255)
    
    Graph CountL
    Graph CountS coloured (255,0,0)
    
    Mike Boorman thanked this post
    #188062 quote
    robertogozzi
    Moderator
    Master

    ….How can the below code be modified so the “if longonmarket” and “if shortonmarket” instructions can sell or exitshort without it being a stop?

    you can’t, as when IG receives your exit from a losing position, it can’t be but a stoploss and charges you accordingly.

    #188068 quote
    Mike Boorman
    Participant
    Average

    ….How can the below code be modified so the “if longonmarket” and “if shortonmarket” instructions can sell or exitshort without it being a stop?

    you can’t, as when IG receives your exit from a losing position, it can’t be but a stoploss and charges you accordingly.

     

    But that isn’t true. I’ve used code for two years that exits a losing position without hitting a guaranteed stop. It is this:

    if longonmarket then
    slprice = tradeprice - sl
    if longonmarket and close < slprice then 
    sell at market
    endif
    endif
    
    set stop loss 350

     

    You can definitely “sell at market” on a losing position without hitting a stop… I’m already doing it. The “set stop loss” above at 350 is never hit during normal trading – it is only there as an emergency. But I want to find a way to sell/exit during a bar. Maybe there is a way of doing a sell/exit on close on a lower timeframe? Like a 1 minute chart? But I’m not quite sure how I’d link an exit rule between two timeframes.

    #188072 quote
    PeterSt
    Participant
    Master

    you can’t, as when IG receives your exit from a losing position, it can’t be but a stoploss and charges you accordingly.

    I can’t judge that, because my StopLoss never gets hit. 😉

    And you both didn’t miss my last post ?

    #188079 quote
    Mike Boorman
    Participant
    Average

    But Peter, the code you’ve submitted has same problem as before. There are no losing trades!!

    This is not what I want. In a 5-year backtest, there is a long trade which earns 35 pips which lasts nearly two years. Not only is that a massive risk if the market keeps going down, but even if in the end it comes good and you close at a 35 pip profit, you’ve spent two years burning your margin for a tiny gain.

    The point of my system is that it’s day trading with small stops and small gains. It’s little nibbles in the market with a win rate of less than 50%. It works perfectly fine as a principle. I don’t want to avoid losing exits – I just simply want to avoid paying an extra commission for losing exits.

    #188083 quote
    robertogozzi
    Moderator
    Master

    Yes, they are not PENDING stop orders.

    I probably misunderstood your question as I thought you wanted to use pending orders, instead of orders at market.

    #188089 quote
    Mike Boorman
    Participant
    Average

    Yes, they are not PENDING stop orders.

    I probably misunderstood your question as I thought you wanted to use pending orders, instead of orders at market.

    Roberto,

    Is there a way to code an exit rule on a 1-minute timeframe to apply to an entry on a 30 or 75-minute timeframe?

    #188093 quote
    PeterSt
    Participant
    Master

     

     

     

    But Peter, the code you’ve submitted has same problem as before. There are no losing trades!!

    Mike, then you have a setting different than I have. 100s of losing orders. Please look back in post 187856.

    Sell at market instead of hitting guaranteed stop


    (this link probably won’t work, but you will find it on the previous page)

    The screenshots in there are from this very code (though in that post I compared the two lines of exit, Pending Stop vs Buy Limit).

    I can’t do more than tell you to work hard to get this working on your side. It really works over here (PRT-IG).
    Ask ahead if you want to know something.
    I assume you took care of that “/10000” I mention in the comment in the code. If that is wrong (not in balance of some kind), nothing may work.

    Please remember what I said earlier today : there seems to be something wrong with the Buy Limit lines from your code. Not that the commands are wrong, but the Limit orders don’t work out as should; not in my own code where I now applied the same.

     

    PS: Below is a repeat of a screenshot from that post on the previous page. That would be something like 200 losses.

    Mike Boorman thanked this post
    image_2022-02-11_154513.png image_2022-02-11_154513.png
    #188112 quote
    robertogozzi
    Moderator
    Master

    Yes Mike Boorman, you can use MTF (Multiple TimeFrame) support to use more than one TF:

    Timeframe(30 minute,UpdateOnClose)   //or 75 minute (plural is optional)
    IF MyLongEntryConditions THEN
       BUY 1 CONTRACT AT MARKET
    ENDIF
    //
    Timeframe(default)
    IF LongOnMarket AND MyLongExitConditions THEN
       SELL AT MARKET
    ENDIF

    You will have to use a 1-minute chart to run this strategy. It can be also be less than 1 minute or even greater, BUT the higher timeframe (or timeframes) in your code MUST (ALL, if more than one) be a multiple of the TF on the chart (named default TF). In such case you cannot run this code on a 7-minute TF as both 30 and 75 are not multiples of 7.

    #188118 quote
    Mike Boorman
    Participant
    Average

    But Peter, the code you’ve submitted has same problem as before. There are no losing trades!!

    Mike, then you have a setting different than I have. 100s of losing orders. Please look back in post 187856.

    https://www.prorealcode.com/topic/sell-at-market-instead-of-hitting-guaranteed-stop/page/3/#post-187856

    (this link probably won’t work, but you will find it on the previous page)

    The screenshots in there are from this very code (though in that post I compared the two lines of exit, Pending Stop vs Buy Limit).

    I can’t do more than tell you to work hard to get this working on your side. It really works over here (PRT-IG).

    Ask ahead if you want to know something.

    I assume you took care of that “/10000” I mention in the comment in the code. If that is wrong (not in balance of some kind), nothing may work.

    Please remember what I said earlier today : there seems to be something wrong with the Buy Limit lines from your code. Not that the commands are wrong, but the Limit orders don’t work out as should; not in my own code where I now applied the same.

    PS: Below is a repeat of a screenshot from that post on the previous page. That would be something like 200 losses.

    That’s weird!! On my side there are no losing trades.

    I eliminated the risk issue by just buying/selling at £1 per point. Dividing by 100 works fine for me but to make your code work I took it out.

    The only fault I noticed with the take profit was when we added in new rules for the exits on losing positions. So I moved the exits on losing positions down to the bottom of the code and put them inside “if longonmarket/shortonmarket” (which separated them from the take profit positions, which were calculated at entry) and then that worked fine. I notice you have done the same, so I’d be surprised if it’s that issue which is making my results go strange.

    Looking at your code, I just can’t see how there could ever be a losing trade, and I’d have said that even if I hadn’t generated my results. On the “longonmarket” conditions for example, the action that is taken when they are met is to “sellshort” (which is to buy a short position). And on “shortonmarket” the action is to “buy” (which I thought could only mean to open a long position?). Have I misunderstood what “sellshort” and “buy” mean? Can they be used to exit as well?

    #188120 quote
    Mike Boorman
    Participant
    Average

    Yes Mike Boorman, you can use MTF (Multiple TimeFrame) support to use more than one TF:

    You will have to use a 1-minute chart to run this strategy. It can be also be less than 1 minute or even greater, BUT the higher timeframe (or timeframes) in your code MUST (ALL, if more than one) be a multiple of the TF on the chart (named default TF). In such case you cannot run this code on a 7-minute TF as both 30 and 75 are not multiples of 7.

    Is it possible to backtest more than 1 million bars? I had an idea that maybe it was, but my version of PRT won’t let me do more than 1 million.

    #188124 quote
    robertogozzi
    Moderator
    Master

    1M units are only available on IG’s real accounts, not demo ones.

    #188128 quote
    PeterSt
    Participant
    Master

    So I moved the exits on losing positions down to the bottom of the code and put them inside “if longonmarket/shortonmarket” (which separated them from the take profit positions, which were calculated at entry) and then that worked fine. I notice you have done the same

    Hi Mike,

    So it must be my conclusion that you did not try my code ?
    I copied back the whole code for a reason. I applied several changes in there. And if this is only a snippet of your larger code, then later you can see how to integrate it. OK ?

    On the “longonmarket” conditions for example, the action that is taken when they are met is to “sellshort” (which is to buy a short position). And on “shortonmarket” the action is to “buy” (which I thought could only mean to open a long position?). Have I misunderstood what “sellshort” and “buy” mean? Can they be used to exit as well?

    My earlier answers in this topic were exactly about that. Sadly I made mistakes myself (not reading accurately on the Pending Stop). And of you don’t understand, no worries, I see no others who understand (which does not mean they don’t exist). As I tried to explain – I use the IB version with its way larger possibilities for orders, so one learns the possibilities, which learning is not possible with the IG.
    Let me add that you can see during the evolvement of your topic, that I myself only had the idea that what now works should be possible. Thus, pure theory. However, springing from manual trading in PRT-IB. And now the theory is practice.

    On a not unimportant side note to myself, but possibly interesting for others :
    I use 1 the second timeframe. This allows for one month of history only (1M bars). If I, for example,  make 2 seconds of that, nothing works any more (all is consistent for the 1 second TF). But also, if I would make it 2 second (or a bit more) I will always be too slow with whatever action. Now look at your own 75 minute vs 225 minutes. You have the 75 for the exact same reason, BUT you are unsatisfied with the SL response. With my code, this response is “instantly” which a Set StopLoss also would do. In the end exactly what you asked for. But also in the end exactly what my solution will be to use more history (like 2 months).

    Now try my code and let me know what comes from it. Don’t forget about the  /10000 which you probably just have to remove. It is a bit tough for me to give you a consistent version because of the contract size on one hand, but the mistakes you provided in the code on the other (you used pips of profit/sl instead of their respective targets (see the Close + SL and Close – SL parts). Then nothing works but it coincidentally could with a smaller contract size. Might it help, the contract size I used was IIRC 10000 (AUD/USD Mini). And, the vagueness (for me !) is about the number of units being unknown (you calculate that). In aftermath, I think I could see that your TP’s were 10 times smaller than mine, implying that my contract size was 10x higher.

    Let me know ! 🙂

Viewing 15 posts - 46 through 60 (of 62 total)
  • You must be logged in to reply to this topic.

Sell at market instead of hitting guaranteed stop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 61 replies,
has 6 voices, and was last updated by PeterSt
4 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/04/2022
Status: Active
Attachments: 20 files
Logo Logo
Loading...