multiple events on a single bar – multitimeframe?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #81836 quote
    aland
    Participant
    Average

    Hi there

    I wrote a simple code that takes the high/low of the market at close time and sets a buy/sell orders.

    The buy/sell orders should only be called once per day.

    I used an hourly time frame but I see that sometimes the orders are called more than once a day. It happens when the order was triggered and successful on the same bar.

    So on the next bar there was no time to set a flag to state that the order was already ONMARKET to prevent it from going in again.

    If I move the time frame to 1 second or even 1 minute this problem will probably not occur or occur less often but then I would not have access to enough historical data.

    I am attaching this simple code snippet.

    Is this a multiframe issue? is there any simple solution for this?

    Any advice would be welcomed

    Thanks in advance

    DEFPARAM CumulateOrders = false
    DEFPARAM FLATBefore     = 220000
    DEFPARAM FLATAfter      = 090000
    
    ONCE EndOfMarketTime = 220000
    ONCE CutOffTime = 09000
    ONCE WasInMarketFlag=0
    ONCE StopLoss=50
    ONCE StopLimit=50
    ONCE StakeSize=10
    ONCE EntryBuffer=10
    ONCE LongTriggered = 0
    ONCE ShortTriggered = 0
    ONCE State=100
    
    //GRAPH MyBuyprice
    //GRAPH MySellprice
    GRAPH State
    
    //for every bar
    IF ONMARKET THEN
    WasInMarketFlag=1
    ENDIF
    //if time is EndOfMarketTime - reset all states for new day and set buy/sell
    IF time = EndOfMarketTime THEN
    MyBuyprice = DHIGH(0)+EntryBuffer
    MySellprice= DLOW(0)-EntryBuffer
    StopLoss = MyBuyprice - MySellprice
    State=1
    WasInMarketFlag=0
    ENDIF
    
    //if between EndOfMarketTime && CutOffTime -> if WasInMarketFlag is false - reset bar for buy/sell
    IF WasInMarketFlag = 0 THEN
    IF LongTriggered=0 THEN //if long
    BUY StakeSize CONTRACTS AT MyBuyprice stop
    State=3
    ENDIF
    IF ShortTriggered=0 THEN
    SELLSHORT StakeSize CONTRACTS AT MySellprice stop
    State=4
    ENDIF
    SET STOP LOSS StopLoss
    SET TARGET PROFIT StopLimit
    ENDIF
    
    //if time is CutOffTime - stop all markets (if ONMARKET)
    IF time >= CutOffTime AND WasInMarketFlag>0 THEN
    IF LONGONMARKET THEN //TERMINATE TRADE AT END OF DAY
    SELL AT MARKET
    ELSIF SHORTONMARKET THEN
    EXITSHORT AT MARKET
    ENDIF
    ENDIF
    #81838 quote
    robertogozzi
    Moderator
    Master

    This is a version supporting MTF (I only have tested it for syntax errors):

    DEFPARAM CumulateOrders = false
    DEFPARAM FLATBefore     = 220000
    DEFPARAM FLATAfter      = 090000
    
    TIMEFRAME (1 hour,updateonclose)
    ONCE EndOfMarketTime = 220000
    ONCE CutOffTime = 090000   // ---> I added one more ZERO  (it was 00:90:00 in your code)
    ONCE StopLoss=50
    ONCE StopLimit=50
    ONCE StakeSize=10
    ONCE EntryBuffer=10
    ONCE LongTriggered = 0
    ONCE ShortTriggered = 0
    ONCE State=100
     
    //GRAPH MyBuyprice
    //GRAPH MySellprice
    GRAPH State
    
    //if time is EndOfMarketTime - reset all states for new day and set buy/sell
    IF time = EndOfMarketTime THEN
       MyBuyprice = DHIGH(0)+EntryBuffer
       MySellprice= DLOW(0)-EntryBuffer
       StopLoss = MyBuyprice - MySellprice
       State=1
    ENDIF
    
    //if between EndOfMarketTime && CutOffTime -> if WasInMarketFlag is false - reset bar for buy/sell
    IF WasInMarketFlag = 0 THEN
       IF LongTriggered=0 THEN //if long
          BUY StakeSize CONTRACTS AT MyBuyprice stop
          State=3
       ENDIF
       IF ShortTriggered=0 THEN
          SELLSHORT StakeSize CONTRACTS AT MySellprice stop
          State=4
       ENDIF
       SET STOP LOSS StopLoss
       SET TARGET PROFIT StopLimit
    ENDIF
    //////////////////////////////////////////////////////////////////////////
    TIMEFRAME (default)
    ONCE WasInMarketFlag=0
    ONCE IamLong        =0
    ONCE IamShort       =0
    IF IntraDayBarIndex = 0 THEN
       WasInMarketFlag=0
       IamLong        =0
       IamShort       =0
    ENDIF
    IF time = EndOfMarketTime THEN
       WasInMarketFlag=0
    ENDIF
    //if time is CutOffTime - stop all markets (if ONMARKET)
    IF time >= CutOffTime AND WasInMarketFlag>0 THEN
       IF LONGONMARKET THEN //TERMINATE TRADE AT END OF DAY
          SELL AT MARKET
       ELSIF SHORTONMARKET THEN
          EXITSHORT AT MARKET
       ENDIF
    ENDIF
    IF WasInMarketFlag = 0 THEN
       IamLong         = LongOnMarket
       IamShort        = ShortOnMarket
       WasInMarketFlag = IamLong OR IamShort
    ENDIF

    This will set WasInMarketFlag once in a 1-Minute TF and will be cleared each new day, so your 1-hour TF will not enter any longer for the rest of the trading day.

    You will have less history.

    If you don’t want to use MTF, then you would be able to know you were ONMARKET by checking STRATEGYPROFIT against its initial value saved at the beginning of each new day, something like:

    ONCE MyProfit = 0
    IF IntraDayBarIndex = 0 THEN
       MyProfit = STRATEGYPROFIT
    ENDIF
    IF Not OnMarket AND Not OnMarket[1] THEN //no trades apparently since last bar
       IF MyProfit <> STRATEGYPROFIT THEN
          IwasLong = 1
       ENDIF
    ENDIF
    #81847 quote
    aland
    Participant
    Average

    You will have less history.

    Thats great! thank you so much for this.

    A question about the “less history” part.

    Does it meant that MTF will always give the amount of data based on the lower time frame (so 1 sec time frame will have a few days even if the “main” code is based on daily)

     

    Thanks again

    Alan

    #81848 quote
    robertogozzi
    Moderator
    Master

    Yes, the importance of the TF is just the opposite in MTF.

    MTF recognizes the lowest TF as the main TF, the one used to launch your strategies on which BARINDEX, TRADEINDEX, INTRADAYBARINDEX, ONMARKET and so on… are updated.

    All other TF’s must be multiple of the lower ones and variables can be created/changed only in one TF but read by any TF.

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

multiple events on a single bar – multitimeframe?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
aland @aland Participant
Summary

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

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