Simple strategy Code conversion

Forums ProRealTime English forum ProOrder support Simple strategy Code conversion

Viewing 15 posts - 1 through 15 (of 19 total)
  • #220946

    Hi All

    I am quite new to this platform ProRealTime coding, I have a simple code from TradingView below and converted it to ProBacktest, but testing this strategy does not give the expected results in this ProRealTime platform (due to the lack of my knowledge) I see in Trading View, for example, the position open and close happening after (currentBar+1) bar and backtesting does only happening for 3 days in this platform as opposed to 3 months in TradingView in a 5min timeFrame, could you please help to convert it, so I could build on further from here as a base.

    Extra Strategy Rules, to be added to the below TradingView code in testing this strategy to ProBackTest:

    1) Do NOT BUY or enter into Trade, when stoch(10,3,5) is > 90

    2) Do NOT SELL or enter into Trade, when stoch(10,3,5) is < 10

    Trading View simple script:

    // @version=5
    strategy(title=”Higher Timeframe EMA (HTF EMA)”, shorttitle=”EMA+”,
         overlay=true,
         calc_on_every_tick = true,
         initial_capital=10000,
         default_qty_value=1, // the cash amount to trade with each order. Such as 1 currency per order
         pyramiding=1)
    // *********Get user input*********
    timeframe   = input.timeframe(title=”EMA Timeframe”, defval=”5″)
    length      = input.int(title=”EMA Length”, defval=50)
    colour      = input.bool(title=”Color EMA”, defval=true)
    smooth      = input.bool(title=”Smooth”, defval=true)
    // *********TimeFrame to Trade*********
    i_startTime     = input.time(title=”Start Filter”, defval=timestamp(“01 Jan 2000 00:00 +0000″), group=”Time Filter”, tooltip=”Start date & time to begin searching for setups”)
    i_endTime       = input.time(title=”End Filter”, defval=timestamp(“01 Jan 2024 19:30 +0000″), group=”Time Filter”, tooltip=”End date & time to stop searching for setups”)
    f_dateFilter    = (time >= i_startTime and time <= i_endTime) // Check datefilter assignment
    // *********Calculate EMA*********
    ema = ta.ema(close, length)
    emaStep = request.security(syminfo.tickerid, timeframe, ema[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1] //To avoid re-painting issues,
    emaSmooth = request.security(syminfo.tickerid, timeframe, ema[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_on)[barstate.isrealtime ? 0 : 1] //To avoid re-painting issues,
    // *********Long Conditions: Price crosses above EMA*********
    buy_Condition     =(close>=emaStep)and(strategy.position_size==0)
    // *********Long Exit Conditions: Price crosses below EMA, or price is below the previous bar’s low, or stop loss percent is reached*********
    exit_Buy_condition    = (close <= emaStep) or (close >= (strategy.position_avg_price+24)) or (low <= (strategy.position_avg_price-6))
    // *********Short Conditions*********
    short_Condition = (close <= emaStep)
    // *********Short Exit Conditions*********
    exit_Short_condition = (close >= emaStep) or (close <= (strategy.position_avg_price-24)) or (high >= (strategy.position_avg_price+6))
    // ***************************Long Positions***************************
    if (buy_Condition and f_dateFilter)
        strategy.entry(id=”Long”, direction=strategy.long)
    if (exit_Buy_condition)
        strategy.close(id=”Long”, comment=”LongExit”)
    // ***************************Short Positions***************************
    if (short_Condition and f_dateFilter)
        strategy.entry(id=”Short”, direction=strategy.short)
    if (exit_Short_condition)
        strategy.close(id=”Short”, comment=”ShortExit”)
    // Draw EMA
    plot(smooth ? emaSmooth : emaStep, color=colour ? (close > emaStep ? color.green : color.red) : color.black, linewidth=2, title=”HTF EMA”)
    Thanks
    Harry
    #220951

    With regards to the above code, I did some simple tests and as you can see from the screenshot, For example: during the backtest the trade entered AFTER the red arrow in both places, instead of at the red arrows as the price before the arrows closed or crossed above the EMA

    #220968

    Hi there,

    In the bars your drawn arrows point at, you(r code) detect the crossing. Then in that same bar you give the command to buy, but that is only executed when that bar finishes.
    And so the buy occurs in the next bar indeed. This is just how it works and ought to work.

    So yes, you are always one bar late (two actually). This time can be shortened by means of using a shorter time frame. You will still be that one bar behind the facts, but still faster.

    The base for the time frame is the time chosen in the area you see below. Next, in your code you can give TimeFrame commands with a shorter time than the TimeFrame of the chart. Now you can determine the crossings as you already did, but buy faster (the buy command should be under the scope of that faster TimeFrame command. The chart’s TF must always be a multiple of the TF commands you use. E.g. if the chart bears 15 minutes, the TF commands can be 5, 3 and 1 minutes, but also 10 seconds, etc. Not 10 minutes and also not 30 minutes.

    Have fun !
    Peter

    1 user thanked author for this post.
    #220981

    Hi Peter

     

    Thanks for getting back. I just changed the code to add TimeFrame as in the screenshot and made it to 5min/100ticks as suggested.

    But still, as I pointed out in the screenshot the Buy Condition met at C1, but bought at the 2 bar instead of the next bar open. And note EMA(50) shown in the chart is the standard indicator i added to the chart. Could this be the issue as it was not plotting from the code automatically?

    Would be nice if anyone could modify the above TradingView Code as a starter for me to get going in ProReal, so I can learn from there.

    Thanks again

    harry

    #220983

    sorry meant to say * 5min/100UNITS as suggested *

    #220991

    Haha, Harry, No …

    The TF of your chart is set to 5 minutes. I take it that you want that for real. Now the TimeFrame command can be set to e.g. 1 minute and if now the c1 becomes true after 1, 2, 3 or 4 minutes from the start of the 5 minute bar, you will see the buy arrow at the 5 minute bar. But also :

    For this to work, you must set the TimeFrame commands under the determination of the condition. Thus in your case this would be above the If c1 command. And :
    Right under the Endif of that If you must set back the TF to the TF of the chart. Then the (c2) condition again then again back to the TimeFrame of 1 minute.
    This will show you how it functionally works. You will soon see that this makes no sense in this consistent situation, because the entry will always be there after minute 1 (of your 5 minute bar). This is because of the construction I depicted for you; Leave the program as it is but change the TF command to 1 minute, and my “1,2,3,4” text from my first sentence will apply.  But now the 5 minute of the chart won’t make sense (it has no goal).

    But from here you can start exploring further …

     

    1 user thanked author for this post.
    #221030

    Hi Peter

    I am still not clear how this works now, I want the whole code including EMA indicator, conditions, buy/sell everything to be in 5 minutes and the chart setting is also in 5 minutes as you can see from screenshot in my earlier post.

    I have attached another screenshot with fe markings, were all the Sell conditions (marked in blue) are good as per the c2 conditions, whenever the current bar low is <= indicator it is selling on the next bar open.

    On item marked in YELLOW (1 and 3) : But why only the c1 condition (marked in Yellow) is NOT working, it is opening the positions on ‘N+2’ bar instead of the usual ‘N+1’ bar (next bar open, N=Current bar) as we expect, given everything is set to 5minutes including the chart.

    On item marked in YELLOW (2) : it did BUY immediately after the SELL of (1) and also C1 >=indicator at that time, which seems to have satisfied the condition. I didn’t get this either.

    Surely i am missing something here, or i am not understanding correctly the behaviour of this platform compared to the other one i am using.

    If you don’t mind, could any of you please update the below code if any corrections to be made, I followed what you said in above post, but not seeing any difference.

    // Definition of code parameters
    DEFPARAM CumulateOrders = false // Cumulating positions deactivated
    timeframe(5 minute)
    // Conditions to enter long positions
    indicator1 = ExponentialAverage[50](close)
    c1 = close>=indicator1

    //TIMEFRAME(5 minute)
    IF c1 THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    //TIMEFRAME(5 minute)

    // Conditions to exit long positions
    indicator2 = ExponentialAverage[50](close)
    c2 = low<=indicator2
    //c3a = (close >= (POSITIONPRICE+24))
    //c32 = (low <= (POSITIONPRICE-6))

    IF c2 THEN
    SELL AT MARKET
    ENDIF

    #221043

    Harry, let me try it this way :

    Say that we have the bars 1,2,3,4,5,6,7,8,9,10.
    Envision that the bar I pointed at in below picture is bar 3. Now :

    At the beginning of each bar your code is called. Thus, it is run from top to bottom, each bar passing by. In your case this is each 5 minutes.

    In bar 3 the crossing occurs (if you think otherwise, let me know).
    You can’t know this in the call of bar 3, because that bar is not finished and your code is called at the end of bar 3. This is equal to the beginning of bar 4.
    So in bar 4 you can detect what happened in bar 3, and you set yourself for a nice Buy because you detect a crossing (in bar 3).
    All great, but notice that your command to Buy only happens when bar 4 finishes. Thus, not immediate, but at the end of bar 4.
    The broker picks up your order at the beginning of bar 5 …
    … which is where the Buy occurs.

    Notice that I did not involve different TimeFrames because you did not do that either (you refused, haha). This is no problem, and all works as advertised.
    You may not like it – that is something else. You can solve that with smaller TFs as I tried to lay out. In that case the Buy will happen in Bar 4 (of the major TF which is the TF of the chart), but it will still happen not instantly. Like after 1 minute with TF commands of 1 minute in there. Or even after 1 second, with TF commands of …

    🙂

    #221047

    Re code below and using Peter’s 10 bar example above …
    If C1 is True in Bar 3 then a Long trade will be opened at the beginning of Bar 4.

    #221052

    … while I said 5.
    So we don’t agree ? (which is fine – I only wanna know)

    #221057

    Seems we don’t agree, perhaps a slip of the keyboard or now you are using the wrong pinhole glasses? 😉

    #221062

    OK, then you have the better answers for Harry. Please go ahead.
    Haha.

    #221071

    On item marked in YELLOW (1 and 3) : But why only the c1 condition (marked in Yellow) is NOT working, it is opening the positions on ‘N+2’ bar instead of the usual ‘N+1’ bar (next bar open, N=Current bar) as we expect, given everything is set to 5minutes including the chart.

    Post a screenshot of ‘cursor details’ for the scenario above, then there is no guesswork involved.

    If you are not sure what cursor details are then just say.

    #221077

    Re code below and using Peter’s 10 bar example above …

    If C1 is True in Bar 3 then a Long trade will be opened at the beginning of Bar 4.

    Hi GraHal

    Thanks for this, I am already using the above code but it does not work.

    Attached screenshot with cursor details for the C1 condition for the same time period similar to my last screenshot in the post for comparison.

    And also attaching the behavior from the other platform which is executing orders during the backtest as expected i.e. when the C1 condition is met immediately in the next bar it bought (there is some platform pricing difference which is fine). If any of you could let me know how I can achieve this similar behavior from the other platform in ProRealCode would be great with this simple code. I have already shared the other platform code at the top of this post.

    @PeterSt – I am all ears to learn and want to understand this behavior of ProRealCode to place orders in IG correctly as per my expectations relative to the other platform screenshot. It does not matter which timeframe() you would suggest in the code to achieve this goal, but the main chart needs to be in 5 minutes or i am happy to change this as well (but then it is not the same comparison with the other platform).

    #221083

    Check your Orders, I think you will find that a buy and a sell both occured on the doji bar, but as they cancel each other out no arrow heads show.

    Reason: C1 and C2 are both True in the bar for which you show Cursor Details … Close > Ind1 and also Low<Ind2.

    The Long opened within the red circle is due to the doji where C1 is True.

Viewing 15 posts - 1 through 15 (of 19 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login