crossing higher than previous crossing

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #198716 quote
    Vogeltje
    Participant
    New

    I have an indicator that has two MA-lines (line p and q), just like the MACD-line and its signal-line. I want to build a system that goes long when p crosses over q AND when this crossing is higher than the previous crossing (because then there is the change of nice rising pattern). Similar: go short when p crosses under q AND this crossing is lower than the previous crossing.
    My knowlegde of programming is not enough. I tried this code but it is not working:

    IF p CROSSES OVER q THEN
    a = p
    ENDIF
    IF p CROSSES UNDER q THEN
    b = p
    ENDIF

    //Conditions for long
    c1 = (p CROSSES OVER q)
    IF c1 AND a > a[1] THEN
    BUY 1 SHARES AT MARKET
    ENDIF

    Who can help? Thanks!

    #198726 quote
    JS
    Participant
    Senior

    Hi @Vogeltje

    I have a comment about the “previous crossing”

    For example, if the “FastMA crosses over SlowMA” then the “previous crossing” must always be “FastMA crosses under SlowMA”.

    The “previous crossing” can therefore never be a confirmation of the current crossing but is always the opposite.

    (A “Crosses Under” is always followed by a “Crosses Over” and this is always followed by a “Crosses Under”, and so on)

    Vogeltje thanked this post
    #198731 quote
    robertogozzi
    Moderator
    Master
    Not tested:
    ONCE PrevOVER  = 0
    ONCE PrevUNDER = 99999999
    //Conditions for long
    c1 = (p CROSSES OVER q)
    IF c1 AND high > PrevOVER THEN
       BUY 1 SHARES AT MARKET
       PrevOVER = high
    ENDIF
    //Conditions for short
    c2 = (p CROSSES UNDER q)
    IF c2 AND low < PrevUNDER THEN
       SELLSHORT 1 SHARES AT MARKET
       PrevUNDER = low
    ENDIF
    Vogeltje thanked this post
    #198734 quote
    JS
    Participant
    Senior
    DefParam CumulateOrders = False
    
    LongCrossingPrice = 0
    LongPrevCrossingPrice = 0
    
    ShortCrossingPrice = 0
    ShortPrevCrossingPrice = 0
    
    FastMA = Average[10](Close)
    SlowMA = Average[20](Close)
    
    If FastMA Crosses Over SlowMA then
    LongCrossingPrice = Close
    For i = 1 to BarIndex[1]
    If FastMA[i] Crosses Over SlowMA[i] then
    LongPrevCrossingPrice = Close[i]
    Break
    EndIf
    Next
    EndIf
    
    If LongCrossingPrice > LongPrevCrossingPrice then
    Buy 1 contract at Market
    EndIf
    
    If FastMA Crosses Under SlowMA then
    ShortCrossingPrice = Close
    For i = 1 to BarIndex[1]
    If FastMA[i] Crosses Under SlowMA[i] then
    ShortPrevCrossingPrice = Close[i]
    Break
    EndIf
    Next
    EndIf
    
    If ShortCrossingPrice < ShortPrevCrossingPrice then
    SellShort 1 contract at Market
    EndIf
    

    This system is based on two of the same crossings so for example the last crossing was a “LONG crossing” (crosses over) and when this LONG crossing takes place higher than the previous “LONG crossing” then the system goes LONG and vice versa for SHORT…

    Vogeltje thanked this post
    #198753 quote
    Vogeltje
    Participant
    New
    Yes JS that is wright. I should have said: the positive crossing must be on a higher level than the previous positive crossing. Thank you.
    JS thanked this post
    #199455 quote
    Vogeltje
    Participant
    New
    Hello Roberto, Thanks for your work but your code is not working. The one JS built works.
    #199456 quote
    Vogeltje
    Participant
    New
    Thanks a lot JS this code is working. But, there is a “but”. And that is that on every new day this systems acts on the very first crossing. By the start of a new day the system does not look at the previous crossing, which should be higher or lower. I have studied your code and I can understand a lot of it (I could not have figured it ou my self). Is it possible that this mistake has to do with: For i = 1 to BarIndex[1]  ? Does that start every day new? And can this be fixed? Thanks!
    #199458 quote
    JS
    Participant
    Senior

    Hi @Vogeltje

    Nice that the code works…

    How do you use this system?

    Do you stop it every night or does it continue automatically?

    Do you (manually) close positions?

    Is the market you trade on a 24-hour market, or does it have a limited opening?

    #199460 quote
    JS
    Participant
    Senior
    DefParam PreLoadBars = 1000
    DefParam CumulateOrders = False
    
    FastMA = Average[10](Close)
    SlowMA = Average[20](Close)
    
    If FastMA Crosses Over SlowMA then
    LongCrossingPrice = Close
    For i = 1 to BarIndex[1]
    If FastMA[i] Crosses Over SlowMA[i] then
    LongPrevCrossingPrice = Close[i]
    Break
    EndIf
    Next
    If LongCrossingPrice > LongPrevCrossingPrice then
    Buy 1 contract at Market
    EndIf
    EndIf
    
    If FastMA Crosses Under SlowMA then
    ShortCrossingPrice = Close
    For i = 1 to BarIndex[1]
    If FastMA[i] Crosses Under SlowMA[i] then
    ShortPrevCrossingPrice = Close[i]
    Break
    EndIf
    Next
    If ShortCrossingPrice < ShortPrevCrossingPrice then
    SellShort 1 contract at Market
    EndIf
    EndIf
    I think this will solve the problem…
    #199466 quote
    Vogeltje
    Participant
    New
    Hi JS, I use this only in backtesting at the moment. I try different intraday-timeframes like 15 min, 30 min, 1 hour. I backtest over longer periods like 3 months. I test on futures like miniDAX (closes 22.00 – 08.00) or miniNasdaq (closes 23.00 -00.00). Thank you. Does the new system replace the one before? I guess so and will try.
    #199469 quote
    JS
    Participant
    Senior
    Hi @Vogeltje Yes, the “new” system (V2) replaces the previous system… I have removed the lines that set the “CrossingPrice” to zero. I have made sure that there are always enough bars present for the calculation (PreLoadBars = 1000) I think this is going to solve the problem…
    #199485 quote
    Vogeltje
    Participant
    New
    Yes JS I think tis is working…..  but the results are much worse  🙂 🙂  But that is not your mistake….. Few simple questions:
    • why is good to remove the lines that set previous cross to zero?
    • you write: FOR….. Break     in the tutorial I see   For… Else…  Break    I added Else but it makes no difference    ( 🙂 )
    • You end   “”Break ..  Endif… Next… Endif”        is there  a reason for this extra   Next.. Endif?
    #199494 quote
    JS
    Participant
    Senior
    Hi @Vogeltje

    You just must assume that the results are always bad … 😉

    • The code is executed at the close of each bar, so after each bar the “CrossingPrice” and “PrevCrossingPrice” are set to zero.This is not desirable because when you compare them, one can still be at zero and then the comparison is always true.

    • You can use “else” when you want to set a variable to zero or one, for example:

    If ….then

    Variable = 1

    Else

    Variable = 0

    EndIf

    • You are looking for the previous crossing with “For i=1 to BarIndex[1]”, when the “If … then” is not true then you end up after the “EndIf” and that is “Next”

    When the “If… then” is true then you end up at “Break” and the “For … to” ended.

    Hopefully a little clear.

    #200043 quote
    Vogeltje
    Participant
    New
    Thanks JS, I must think hard about this. For me this is not easy stuff….
    JS thanked this post
Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.

crossing higher than previous crossing


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Vogeltje @vogeltje Participant
Summary

This topic contains 13 replies,
has 3 voices, and was last updated by Vogeltje
3 years, 5 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 08/07/2022
Status: Active
Attachments: 1 files
Logo Logo
Loading...