Calculate Position Size based on fixed risk (ie. $100)

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #144839 quote
    SaschaSascha
    Participant
    Average

    Hi,

    In the attached code I tried to calculate the position size based on a fixed risk of $100 with a profit target of $200. But for some reason it sometimes does it correctly and sometimes it doubles the position size which then for a losing trade causes twice as the defined loss (-$200) and also gets twice the profit (+$400) in case of a winning trade. This happens for long and short trades. I tested it on AUDUSD.

    I thought the code is correct (see line 105-132 and line 216-254) but there must be a mistake that I can’t see it.

     

    The second problem is, that when there is a long position and then a new short position is triggered, the long position will be closed. How can I avoid that?

    Thank you.

    Sascha

    Decision.itf
    #144884 quote
    SaschaSascha
    Participant
    Average
    //--------------LONG:
    
    if (LongSetups AND LFiveDayCond55 AND NOT LongNoEntry AND NOT Events AND NOT Sun AND NOT Fri AND NOT LongOnMarket) then
    ISL = 10000*(close-lowest[2](low) + LBuffer)
    TradeRisk = pipvalue*ISL
    ContractSize = 100/TradeRisk
    BUY ContractSize SHARES AT market
    
    // Stops and targets
    SET STOP LOSS (close-lowest[2](low) + LBuffer)
    SET TARGET PROFIT (close-lowest[2](low) + LBuffer)*2
    
    ENDIF
    
    //--------------SHORT:
    if (ShortSetups AND SFiveDayCond55 AND NOT ShortNoEntry AND NOT Events AND NOT Sun AND NOT ShortOnMarket) then
    ISL = 10000*(highest[2](high)-close + SBuffer)
    TradeRisk = pipvalue*ISL
    ContractSize = 100/TradeRisk
    SELLSHORT ContractSize SHARES AT market
    
    // Stops and targets
    SET STOP LOSS (highest[2](high)-close + SBuffer)
    SET TARGET PROFIT (highest[2](high)-close + SBuffer)*2
    
    ENDIF

    Hi,

    This is the part of the code that should calculate the position size.

    If anyone can find the mistake, I would be very grateful.

    What it should do, is simply use the current close and the lowest low of the last two days as the initial stop loss (for a long trade) and use twice that risk as a profit target.

    Thanks.

    #144886 quote
    NicolasNicolas
    Keymaster
    Legend

    I don’t see any reason why it doubles the contract size. Are you sure you have set the cumulating orders to false?

    In order to not order a contrarian order while you are already at market, just add “AND NOT ONMARKET” in your conditions lines 3 and 16.

    Sascha thanked this post
    #144888 quote
    SaschaSascha
    Participant
    Average

    Thank you Nicolas for your quick reply. You’re a genius. I indeed still had set the cumulating orders to True. It’s now not doubling anymore the position size.

    Regarding a contrarian order: How can I allow a contrarian order to be placed without closing the existing order?

    #144893 quote
    NicolasNicolas
    Keymaster
    Legend

    It is not possible to hedge position with ProOrder sorry. The only possibility is to divide your strategy into 2 separated ones, 1 for buy orders and 1 for sellshort orders.

    Sascha thanked this post
    #162650 quote
    s00071609s00071609
    Participant
    Senior

    I have max risk set to $300, but this is giving me position size of 61 contract on NASDAQ. The order list shows 1 position with 2100$ loss. It has correct size in other orders. Why would it do this?

    I have attached the image. The risk graph is flat meaning the risk is always $300, position size has spiked around the area. The largest spike in the image on 4th bar from where my cursor is 189 contracts size

    #162651 quote
    s00071609s00071609
    Participant
    Senior

    This is the code being used with accumulating order set to false

    Timeframe (daily)
    REM Money Management
    Capital = 30000
    Risk = 0.01
    
    REM Calculate contracts
    equity = Capital //+ StrategyProfit
    maxrisk = round(equity*Risk)
    PositionSize1 = (abs(((maxrisk/SL)/PointValue)*pipsize))
    IF PositionSize1>10 then
    PositionSize=Round(abs(((maxrisk/SL)/PointValue)*pipsize))
    Else
    Positionsize=Positionsize1
    Endif
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Calculate Position Size based on fixed risk (ie. $100)


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Sascha @goedelsa Participant
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by s00071609s00071609
5 years, 7 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/20/2020
Status: Active
Attachments: 1 files
ProRealCode ProRealCode
Loading...