How do I open and close trades at levels relative to the signal bar?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #48201 quote
    paulon
    Participant
    Average

    As an example, when  I get a Buy signal (say caused by 2 moving averages crossing), I would like to enter the trade if the next bar’s price goes above the high of the signal bar (and enter the trade at that price not the open of the following bar)

    The profit target would be lets say 2 * ATR(14) above the high of the signal bar.

    The exit would be if the price crosses below the high of the signal bar – the ATR(14) at the time of the signal bar.

    Lastly,  I have borrowed part of this code from MattyJ (thank you). Using the money management code here, how do I code this so the trade size (I am spread betting) is 5% of the account size at the time of the trade?

    defparam cumulateorders=false
     
    barquantity=5 //how many bars to wait after a signal to open a new order
     
    REM Money Management
    Capital = 1000 // initial capital at launch of the strategy
    Risk = 0.05 // risk in percent
     
    REM Calculate contracts
    equity = Capital + StrategyProfit
    maxrisk = round(equity*(Risk/100))
     
    indicator1 = Average[25](close)
     
    indicator2 = Average[250](close)
     
    indicator3 = Average[15](close)
     
    c1 = (indicator1 CROSSES OVER indicator2)
     
    c2 = (indicator1 CROSSES UNDER indicator3)
     
    if c1 then
    buybar=barindex
    buyprice=close
    endif
    if c2 then
    sellbar=barindex
    sellprice=close
    endif
     
    stillbullish = buybar>sellbar
    stillbearish = sellbar>buybar
     
    if stillbullish and barindex-buybar>=barquantity and gobuy<>buybar and close>buyprice then
    buy maxrisk -0.5 perpoint at market
    gobuy=buybar
    endif
     
    if stillbearish and barindex-sellbar>=barquantity and gosell<>sellbar and close<sellprice then
    sellshort maxrisk-0.5 perpoint at market
    gosell=sellbar
    endif
    #48209 quote
    Nicolas
    Keymaster
    Master

    I recognize the code we made recently in the Mattyj’s topic.

    I understand your request about the changes to made to this code, but you did not mention if the elapsed bars after the MA cross conditions should be left or not?

    5% of the account size at the time of the trade

    It is already the case in the strategy, despite the fact that Mattyj reduced the calculated lots by 0.5 at lines 36 and 41 (don’t know why).

    #48214 quote
    paulon
    Participant
    Average

    Thank you for the very quick reply Nicolas.  I’m not sure what you mean about the elapsed bars being left or not.  Sorry if I wasn’t very clear.  What I meant was the bar at which time the MAs cross is what I call the signal bar.  This should be the reference point for the entry . So in an up trade the high of the signal bar would be the entry level and the take profit would be 2 ATRs above the high and the stop loss would be 1 ATR below the high.

    After each trade the account size would be adjusted and the amount per point on the next trade would change based on the risk percentage stated.

    #48259 quote
    Nicolas
    Keymaster
    Master

    ok I understand your strategy, but the code provided embed a lot more than a simple MA cross: it has condition about bars to wait before opening a trade after a cross and it has also 3 MA, not only 2. I’d rather start from scratch 🙂

    So, to be sure: only 2 moving average and no need to wait X bars before launching orders? (apart from the fact that it is necessary to wait for a break of the High of the signal bar).

    #48299 quote
    paulon
    Participant
    Average

    Yes that’s right Nicolas.

    #48314 quote
    Nicolas
    Keymaster
    Master

    Fine, let’s try with this code, do your own tests cause I didn’t.

    defparam cumulateorders=false
    
    REM Money Management
    Capital = 1000 // initial capital at launch of the strategy
    Risk = 5 // risk in percent
     
    REM Calculate contracts
    equity = Capital + StrategyProfit
    maxrisk = round(equity*(Risk/100))
     
    MAfast = Average[7](close)
    MAslow = Average[21](close)
    atr = averagetruerange[14]
     
    c1 = (MAfast CROSSES OVER MAslow)
    c2 = (MAfast CROSSES UNDER MAslow)
    
    //The profit target would be lets say 2 * ATR(14) above the high of the signal bar.
    //The exit would be if the price crosses below the high of the signal bar – the ATR(14) at the time of the signal bar.
     
    if c1 then 
     trigger = high
     tp = atr*2
     sl = atr 
    elsif c2 then 
     trigger = low
     tp = atr*2
     sl = atr
    endif
     
    if MAfast>MAslow and trigger>0 and close<trigger then 
     BUY maxrisk CONTRACTS AT trigger stop
    endif
     
    if MAfast<MAslow and trigger>0 and close>trigger then 
     SELLSHORT maxrisk CONTRACTS AT trigger stop
    endif
    
    set target profit tp 
    set stop loss sl
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

How do I open and close trades at levels relative to the signal bar?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
paulon @paulon Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by Nicolas
8 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/04/2017
Status: Active
Attachments: No files
Logo Logo
Loading...