Trade not being entered

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #250955 quote
    Oklewis2
    Participant
    New

    Hi guys, probably really stupid but – Why is this code not entering a trade? i’m using a daily chart of US Tech 100:

    DEFPARAM CumulateOrders = true

    once initialTrade = 0

    IF initialTrade = 0 THEN
    BUY 1 CONTRACT AT MARKET
    initialTrade = 1
    ENDIF

    #250959 quote
    robertogozzi
    Moderator
    Master

    It’s weird, you can use this version, though:

    DEFPARAM CumulateOrders = false
    once initialTrade = 0
    if onmarket then
       initialTrade = 1
    endif
    IF initialTrade = 0 THEN
       BUY 1 CONTRACT AT MARKET
    ENDIF
    Iván González thanked this post
    #250963 quote
    Oklewis2
    Participant
    New

    Perfect, thanks for that – I’ll try it when I’m back at my computer 👍

    #250974 quote
    Iván González
    Moderator
    Master

    The problem is that the backtester loads and performs calculations even before the first candle is seen. This causes the initialTrade variable to already be equal to 1 when the backtesting starts.

    DEFPARAM CumulateOrders = true
    
    once initialTrade = 0
    
    IF initialTrade = 0 THEN
       BUY 1 CONTRACT AT MARKET
       initialTrade = 1
    ENDIF
    
    graph initialTrade

    Why does Roberto’s code work? Basically because initialTrade changes state only when the first buy is executed, and that actually happens on the first visible candle.

    robertogozzi thanked this post
    #251045 quote
    XORANDNOT
    Participant
    Senior

    You could also set “preloadbars” to 1000, for example, and not do any calculation of buy/sell trigger variables before bar No. 1000.

    This solves the problem that normally, all variables will be calculated beginning from bar 1.

     

    DEFPARAM preloadbars = 1000

    DEFPARAM CumulateOrders = true

    once initialTrade = 0

    IF initialTrade = 0 and barindex > 1000 THEN
    BUY 1 CONTRACT AT MARKET
    initialTrade = 1
    ENDIF

    Iván González and robertogozzi thanked this post
    #251046 quote
    XORANDNOT
    Participant
    Senior

    I just checked in demo mode : we need to say “if barindex >= 1000”, not “if barindex > 1000”

     

    DEFPARAM preloadbars = 1000

    DEFPARAM CumulateOrders = true

    once initialTrade = 0

    IF initialTrade = 0 and barindex >= 1000 THEN
    BUY 1 CONTRACT AT MARKET
    initialTrade = 1
    ENDIF

    #251047 quote
    PeterSt
    Participant
    Master

    @XORANDNOT has the best solution sofar. But the real solution encapsulates the whole code for its logic in that If. Thus, see 1st screenshot and assume that PreLoadBars was made equal to DefParamPreloadbars (which in itself is also a must to be obtained), then in the 2nd screenshot you see the very end of the program.

    Do it like this and you simply cannot make mistakes, but notice that above this main If only ONCE commands are allowed.

    Iván González thanked this post
    image_2025-09-17_160214959.png image_2025-09-17_160214959.png image_2025-09-17_160348944.png image_2025-09-17_160348944.png image_2025-09-17_160553559.png image_2025-09-17_160553559.png
    #251051 quote
    XORANDNOT
    Participant
    Senior

    As I said, you need to say :

     

    If barindex >= preloadedbars then

     

     

    If you say

    If barindex > preloadedbars then

     

     

    no position will be opened in the first bar of the really started system.

    Iván González thanked this post
    #251056 quote
    PeterSt
    Participant
    Master

    Yes, I understand that. But I don’t see where that matters ? To my understanding :

    In my case  I set PreLoadBars to 100, just because it requires that command or else nothing works as intended. I could also have made that 99 or 101 or 50 or 200. Even if you have bars of 1 day, it still does not matter that I can see. True, it would require one more day to preload with 100 and your system starting at day 101 for real. But this is only virtual and nothing starts later. Really all what happens is that one more bar/day is loaded ahead of the sequence of bars.

    What one should use the PreLoadBars for is the knowledge of averages and all which require x bars to calculate. Thus, using an average of 100 on 2 bars really does not work. Do you need 100 bars for that ? then the system should start at bar 101 or else it is not complete. Maybe on bar 100 … but this is all not important.

    Superfluously : if the time is now 17:22:01 and PreloadBars is 100 or 50 or 200 etc. and I start the system *now* and it goes Long immediately, then with a TF of 1 minute at 17:23 it will have taken a position.
    Would this be a backtest, then I can imagine that with 1M bars you will lose 100 (or 101 etc.) bars of precious backtest data. This is not important, right ?

    So now I am curious what you mean ? 🙂

    #251064 quote
    XORANDNOT
    Participant
    Senior

    It matters when you start a system on any timescale. For example, in a 1 hour chart, no position will be opened at the beginning of the bar after you started the system, but only one hour later, at the second bar. The same for any other timescale.

     

    “if the time is now 17:22:01 and PreloadBars is 100 or 50 or 200 etc. and I start the system *now* ” —– it will NOT go Long immediately, but at 17:24 at the earliest, in case you write  ” barindex > preloadbars “.

    It will go long at 17:23 when you write ” barindex >= preloadbars ”

    Does not sound logical, but it is so. Just try it out in demo, in a 10 second chart, for example.

    Iván González thanked this post
    #251066 quote
    PeterSt
    Participant
    Master

    I did not know that. Thank you !

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Trade not being entered


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Oklewis2 @oklewis2 Participant
Summary

This topic contains 10 replies,
has 5 voices, and was last updated by PeterSt
5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/16/2025
Status: Active
Attachments: 3 files
Logo Logo
Loading...