Grid orders with one combined stop loss and limit, can it be done?

Viewing 15 posts - 1 through 15 (of 308 total)
  • Author
    Posts
  • #5409 quote
    cfta
    Participant
    Senior

    Hi Traders,

    This is my first post in the forum so let me introduce myself. I discovered the user friendly and creative PRT platform a couple of months ago and made the switch from the more frustrating MT4 platform, I have been trading FX along with commodity and index CFDs for over 7 years and picked up a few good strategies along the way, one which I am going to share and ask for help coding.

    The strategy is based on scaling into positions while maintaining the same risk by placing stop orders in the direction of the trend and momentum of the selected instrument, for instance earlier this week I was anticipating that EUR/USD would drop and at the time the market price was 1.1385. I would start the system placing an order to sell 1 contract at market price along with several sell stop orders to sell additional contracts at predetermined intervals by 20 pips, 1.1365, 1.1345 and 1.1325 etc.

    The key for the strategy is to maintain the same risk as more contracts are added to the position, starting with a 1% risk on the initial market order taken at 1.1385 with 1% risk equaling 100 pips with a stop loss at 1.1485, once the second order is filled at 1.1365 the 1% risk is maintained by the stop loss being moved automatically to 1.1425 (50 pips from the average price at 1.1375), once the third order is filled at 1.1345 the 1% risk is maintaned by the stop loss being moved automatically to 1.1398 (33 pips from the new average price at 1.1365) and on it goes until reaching the desired number of contracts, in my case most often 5-10 contracts. In a perfect world I would like to include a function to set the stop loss to break even once reaching a certain percentage of profit but I have not figured that part out yet…

    If going for 5 contracts at 20 pip intervalls the first 100 pips yield a 3% return and evry additional 20 pips yield 1 additional percent, for instance given that pullbacks and whipsaws are moderate and do not cause an exit by the stop loss a 240 pip move yield a 10% profit with only 1% risk, I say that is pretty sweet R:R and MM 🙂

    I already have the code to do this on the frustrating and not so reliable MT4 platform but can it be coded for PRT?

    A few weeks ago I thought of course it can be done! Now I am a bit less convinced since I have made two requests to the inhouse ProRealTime programmers and been given two different sets of code but neither of them works as intended for my IG account so now I am asking all of you and in particular Nicolas if it es even possible to code this?

    This is the first code provided by the PRT programmers;

    stop1=round(close*1000)/1000
    
    if stop1 <= close then
    
    stop1=stop1+0.001
    
    endif
    
    tradesum=0
    
     
    stop2=stop1+0.002
    
    stop3=stop2+0.002
    
    stop4=stop3+0.002
    
    stop5=stop4+0.002
    
     
    buy 1 share at stop1 stop
    
    buy 1 share at stop2 stop
    
    buy 1 share at stop3 stop
    
    buy 1 share at stop4 stop
    
    buy 1 share at stop5 stop
    
     
    if longonmarket then
    
    for i=0 to countoflongshares-1 do
    
    tradesum=tradesum+tradeprice(i)
    
    next
    
    averagetrade=tradesum/countoflongshares
    
    sell at averagetrade-((100/countoflongshares)*pointsize) stop
    
    endif

    And here is the second code;

    DefParam CumulateOrders = True
    
    Once firstPrice = undefined
    
    // Change these settings accordingly
    Once lotSize = 10
    Once orderLimit = 5
    Once pipInterval = 10
    Once profitPercentage = 0.2
    Once lossPercentage = 0.5
    
    
    If CountofLongShares[1]=0 and CountofLongShares=lotSize then
       firstPrice = TradePrice
    Elsif CountOfLongShares>lotSize then
       Sell At firstPrice Stop
    Endif
    
    If Not LongOnMarket then
       Buy lotSize Lots At Market
    Elsif CountOfLongShares<orderLimit*lotSize then
       Buy lotSize Lots At TradePrice+pipInterval*PointValue Stop
    Endif
    
    Set Target %Profit profitPercentage
    Set Stop %Loss lossPercentage

    In the first code the SL function seemed to be working fine but not the order placement which tended to place all orders virtually instantly at the same price, in second code the orders were sometimes place at the proper intervalls and sometimes at the same price as a previous order.

    Please help me figure this out, I look forward to any replies and assistance!

     

    Regards, Filip

    Adolfo thanked this post
    #5414 quote
    Henrik
    Participant
    Veteran

    Hi !

    Im trying to code a simular strategy and and having trouble to. So i would be very happy if someone can solve this problem.

    Regards, Henrik

    #5440 quote
    Nicolas
    Keymaster
    Master

    Hello,

    Welcome here and thanks for sharing your strategy with everyone.

    About your tests and bugs finding, have you spotted them in real market conditions or only in backtest please?

    So the 1% stoploss would be dynamically compute at each bar for the whole basket orders, isn’t it?

    #5482 quote
    cfta
    Participant
    Senior

    Hi fellows,

    I appreciate your interest and fast replies.

    I have only been forward testing in real market conditions so far.

    Yes, I suppose so regarding the stoploss or alternatively move automatically to a new fixed level once a new order is triggerd.

    As mentioned above it seems that the first code is working better for the stoploss but the entry orders have been filled at the same or random levels…

    #5487 quote
    Nicolas
    Keymaster
    Master

    Hi,

    I rewrote the whole thing for the sell side only. I think it should make it, but test it with minimal lot size under real market conditions please.

    I add 2 GRAPH instructions for displaying the new stoploss and the average price position of the whole basket for debugging purpose (see picture attached). Please have a look if it’s ok with your strategy.

    // first trade whatever condition
    if NOT ONMARKET AND close<close[1] then
     SELLSHORT 1 LOT AT MARKET
    endif
    
    // case SELL - add orders on the same trend
    if tradeprice(1)-close>20*pipsize then
     SELLSHORT 1 LOT AT MARKET
    endif
    
    // stoploss at 1% from the average price of the whole positions on market
    if shortonmarket then
     newSL = positionprice + (positionprice*0.01)
      if close>newSL then
       EXITSHORT AT MARKET
      endif
     GRAPH newsl coloured(200,0,0) as "stop loss 1%"
     GRAPH positionprice coloured(100,100,100) as "position price"
    endif 
    grid-pyramidal-orders-prorealtime.png grid-pyramidal-orders-prorealtime.png
    #5489 quote
    cfta
    Participant
    Senior

    Wow, you are really fast Nicolas, I’m impressed!

    Sure thing, I will test it in real market conditions first thing on Monday morning and come back with an update.

    Thanks a lot for your help and have a nice weekend.

    #5499 quote
    Henrik
    Participant
    Veteran

    HI!

    This would work in small timeframe like 1 sec but not in 1h, 1d or 1w. The problem is that its hard to find god buy/sell conditions in small timeframes. How do you solve that? Im using IG version by the way. Is it possible to build indicators to simulate 15min, 1d, 1w price and then connect other inducaters to that  simulated price timeframe?

    Regards, Henrik

    #5597 quote
    cfta
    Participant
    Senior

    Hello Nicolas,

    Unfortunately I lost my remote connection from work today so I have only been able to test for a couple of hours tonight. I learned that graphs are not allowed when testing in real market conditions so I did a slight modification to the code removing the graphs;

    // first trade whatever condition
    if NOT ONMARKET AND close<close[1] then
    SELLSHORT 1 LOT AT MARKET
    endif
    
    // case SELL - add orders on the same trend
    if tradeprice(1)-close>20*pipsize then
    SELLSHORT 1 LOT AT MARKET
    endif
    
    // stoploss at 1% from the average price of the whole positions on market
    if shortonmarket then
    newSL = positionprice + (positionprice*0.01)
    if close>newSL then
    EXITSHORT AT MARKET
    endif
    endif

    However I also did some backtesting and it seems like the code cause the positions to close at -1 % each instead of -1 % in total, see the attached screenshot, for the first set of trades with 10 sell orders the average price once all orders had been filled was 1.4162 and the-1 % in total SL should have kicked in at about 10 pips higher around 1.4172.

    Nicolas do you have an idea how to fix this please?

    PRT-grid-error.png PRT-grid-error.png
    #5603 quote
    Nicolas
    Keymaster
    Master

    Sorry for graph instruction, i always forget to remove it when i send code to people … 🙂 It’s only for debugging purpose and not allowed by ProOrder.

    Well, I never used averaging down in real market conditions with IG. So I believe the problem is in the use of POSITIONPRICE which would return the average open price of the whole trades… Apparently not! I will ask PRT guys why.

    So I believe the solution is in the first code you post (from ProRealTime services), so remove the lines 11 to 19 from my code and add this instead:

    if shortonmarket then
    
     for i=0 to countofshortshares-1 do
      tradesum=tradesum+tradeprice(i)
     next
    
     averagetrade=tradesum/countofshortshares
    
     sell at averagetrade-((100/countofshortshares)*pointsize) stop
    
    endif
    #5607 quote
    cfta
    Participant
    Senior

    No worries I would probably forget it all the time too 🙂

    I updated the code with some minor modifications replacing “lots” with “shares” to match your code with the first one I got;

    // first trade whatever condition
    if NOT ONMARKET AND close<close[1] then
    SELLSHORT 1 SHARE AT MARKET
    endif
    
    // case SELL - add orders on the same trend
    if tradeprice(1)-close>20*pipsize then
    SELLSHORT 1 SHARE AT MARKET
    endif
    
    if shortonmarket then
    
    for i=0 to countofshortshares-1 do
    tradesum=tradesum+tradeprice(i)
    next
    
    averagetrade=tradesum/countofshortshares
    
    sell at averagetrade-((100/countofshortshares)*pointsize) stop
    
    endif

    In backtesting the problem remains, five orders triggered but not exited until way past the total -1 %. Could it be due to the first code being a long only and yours being short only? Or is necessary for line 1 – 9 in the first code to be included for the SL to work?

    PRT-grid-error-2.png PRT-grid-error-2.png
    #5609 quote
    Nicolas
    Keymaster
    Master

    Maybe averagetade variable is not calculated correctly? Do the whole orders closed at the same time anyway? I’ll have a look by tomorrow.

    #5623 quote
    Nicolas
    Keymaster
    Master

    I have just tested once more, this time with long only orders, and set the exit at 1% and things go well under tester:

    // first trade whatever condition
    if NOT ONMARKET AND close>close[1] then
     BUY 1 LOT AT MARKET
    endif
    
    // case BUY - add orders on the same trend
    if close-tradeprice(1)>20*pipsize then
     BUY 1 LOT AT MARKET
    endif
    
    // stoploss at 1% from the average price of the whole positions on market
    if longonmarket then
     newSL = positionprice - (positionprice*0.01)
     if close<=newSL then
      SELL AT MARKET
     endif
     GRAPH newSL
    endif

     

    POSITIONPRICE is really the average open price of the whole opened orders.

    positionprice.jpg positionprice.jpg
    #5678 quote
    cfta
    Participant
    Senior

    Thanks Nicolas, it looks like we are making a lot of progress.

    I only removed a zero from (positionprice*0.001), the entry orders seem to be working good except for a minor issue that sometimes when the system is started it buys two contracts instead of one, in addition occasionally the intervals are a bit larger around 30 instead of 20 pips I guess this is due to dependency to close of the current candle?

    If you have time it would be great if it is possible to a limit function to exit all positions for an instrument.

    I have begun testing your latest code in real time market conditions so we will see if it works and I will give an update tomorrow!

    #5680 quote
    Nicolas
    Keymaster
    Master

    About the 30 pips instead of 20, it is obviously about the one time test only on each bar.

    And for the 2 contracts instead of 1 only at system start up, I have no clue right now… sorry. Maybe the entry position has to be better defined than a close versus close[1] ?

    #5716 quote
    cfta
    Participant
    Senior

    Hey guys,

    I bought a new aparment today so I haven’t had as much time for testing as I wanted but I set up the system on NZD/JPY and Dax which have been running on real market conditions for about 24 hours now.

    The good news is that the entries seem to work very good with orders being filled at every 20 pips with small differencies. The less good news is that the SL seem to kick in too early with (positionprice*0.001) at only -0.1 % but kick in too late with (positionprice*0.01) with floating loss of CAD 200 on a USD 10 000 account. I may have missed something in the calculations but so far I can’t figure it out.

    NOTE! Due to my lack of experience of PRT and lack of coding knowledge I have not mentioned one of the most important features, since this strategy is meant to manage R:R and MM for manual trades, though taken automatically, it is crucial that no new entry orders are placed after a SL kicked in. The system must wait to be started again manually, how can this feature be added?

    PRT-grid-error-8.png PRT-grid-error-8.png PRT-grid-error-9.png PRT-grid-error-9.png
Viewing 15 posts - 1 through 15 (of 308 total)
  • You must be logged in to reply to this topic.

Grid orders with one combined stop loss and limit, can it be done?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
cfta @cfta Participant
Summary

This topic contains 307 replies,
has 1 voice, and was last updated by OtherAttorney
1 year, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/14/2016
Status: Active
Attachments: 106 files
Logo Logo
Loading...