Trading System Auto Closing Position

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #175280 quote
    LuckyClover18LuckyClover18
    Participant
    New

    Good day!

    I hope I am posting on the correct forum.

    I recently created a program for my strategy. I ran it in proorder auto trading on a few stocks in my IG demo and CFD accounts.

    It is automatically opening a position according to my conditions. But, the next day at the opening, it automatically closes. I am not sure where the bug is. I hope somebody can have a look and coach me in the proper way of executing a trading system. I am using daily timeframe for the strategy.

    //-------------------------------------------------------------------------
    // Main code : SFBTradeMND
    //-------------------------------------------------------------------------
    //indicators
    indBullishWatcher, indEntrySignalBI = CALL DIVERoutput
    indALMA = CALL ALMA[9, 6, 0.85]
    indMACDLn = ExponentialAverage[12](close)-ExponentialAverage[26](close)
    indMACDSign = ExponentialAverage[9](indMACDLn)
    indMACDHis = indMACDLn - indMACDSign
    indRSI = RSI[30](close)
    //indBollHi, indBollLo, indBollMid = CALL BOLLINGERBAND[0.2, 50]
    ignored, indBollLo, indBollMid = CALL BOLLINGERBAND[0.2, 50]
    indEMA20 =ExponentialAverage[20](close)
    CutLossPercentage = 0.05
    
    if barindex = 26 then
    BBLoSupportScan=1
    SCANRSI = 1
    SCANBoll = 0
    SCANMACDx = 0
    condParabolic =0
    condlong =0
    endif
    
    //initial conditions
    EntrySignalBar = indEntrySignalBI
    
    //checking for bullish divergence, DIVERoutput = 1
    if indBullishWatcher crosses over 0.5 AND (EntrySignalBar <> EntrySignalBar[1]) then
    //ScanEntryDiver = 0
    condDIVERoutput = 1
    BBLoSupportScan=1
    endif
    
    if indBullishWatcher = 1  then//AND condDIVERoutput=1
    //scan for RSI30 crossing over 50
    if SCANRSI = 1 AND BBLoSupportScan=1 AND (indRSI crosses over 50 OR indRSI crosses over 55)  AND condlong = 0 then //OR (indRSI >50 AND indRSI < 56))
    SCANRSI = 0
    SCANBoll = 1
    RSI50xBI = barindex
    BBLoSupportScan=0
    else
    if SCANRSI = 1 AND BBLoSupportScan=1 AND (indRSI >50)  AND condlong = 0 then
    cutlosslevel = indBollLo
    calcrisk = (Close - cutlosslevel)/Close
    if calcrisk < (CutLossPercentage) then
    SCANRSI = 0
    SCANBoll = 1
    RSI50xBI = barindex
    BBLoSupportScan=0
    endif
    endif
    endif
    
    if SCANBoll = 1 AND BBLoSupportScan=0 AND SCANRSI = 0 AND barindex => RSI50xBI AND (Close crosses over indBollMid or Close > indBollMid) AND condlong =0 then
    SCANBoll = 0
    SCANMACDx = 1
    BOLLxBI = barindex
    endif
    
    if barindex > BOLLxBI AND (High < indBollLo OR (OPEN < indBollLo AND CLOSE < indBollLo)) AND condlong =0 then
    BBLoSupportScan=1
    SCANRSI = 1
    SCANBoll = 1
    endif
    
    if (SCANMACDx = 1 and SCANRSI = 0 AND SCANBoll = 0 ) AND barindex => BOLLxBI AND ((indMACDLn >0 AND indMACDHis >0) OR (indMACDHis crosses over 0)) and BBLoSupportScan=0 AND condlong =0 then //OR (indMACDLn crosses over 0 AND indMACDHis > 0)
    
    cutlosslevel = indBollLo
    calcrisk = (Close - cutlosslevel)/Close
    if calcrisk < (CutLossPercentage) then //Cut Loss Percentage
    //SCANMACDx = 0
    BBLoSupportScan = 1
    BuyingSignal = 1
    condDIVERoutput = 0
    //calc limit level for buying, LBL
    LBL = cutlosslevel/(1-CutLossPercentage)
    else
    //ALMASupportScan = 1
    BBLoSupportScan = 1
    SCANRSI = 1
    condDIVERoutput = 0
    BuyingSignal = 0
    endif
    endif
    
    IF NOT LongOnMarket and BuyingSignal = 1 THEN
    //BUY 1000 CASH ROUNDEDDOWN AT LBL LIMIT
    //BUY 500 CASH ROUNDEDDOWN AT MARKET
    BUY 100 SHARES AT LBL LIMIT
    //BUY 10 CONTRACTS AT MARKET
    condLong = 1
    BuyingBI = barindex
    condParabolic = 0
    BBLoSupportScan = 1
    condDIVERoutput = 0
    ENDIF
    
    ENDif
    
    //sell settings
    If barindex => BuyingBI and condLong= 1 AND indRSI CROSSES OVER 70 then
    condParabolic = 1
    parabolicBI = barindex
    endif
    
    //condBBlowExit= Low crosses under indBollLo
    //condEMA20Exit = Low CROSSES under indEMA20
    //condCutLoss = Close crosses under indBollLo
    
    
    If  condLong = 1 AND barindex > BuyingBI then
    condBBlowExit= Low crosses under indBollLo
    condEMA20Exit = Low CROSSES under indEMA20
    condCutLoss = Close crosses under indBollLo
    
    //if condParabolic = 0 AND condALMAExit then
    if condParabolic = 0 AND condBBlowExit then
    BBSellSignal = 1
    //ALMASellSignal = 1
    //ALMASellSignalBI = barindex
    endif
    
    if barindex > parabolicBI AND condParabolic = 1 AND condEMA20Exit then
    EMA20SellSignal = 1
    //EMA20SellSignalBI = barindex
    endif
    
    //if ALMASellSignal = 1 or EMA20SellSignal = 1 or condCutLoss then
    if BBSellSignal = 1 or EMA20SellSignal = 1 or condCutLoss then
    condEXIT =1
    endif
    
    If LongOnMarket AND condEXIT =1 then
    SELL AT MARKET
    //ALMASupportScan = 1
    BBLoSupportScan = 1
    SCANRSI = 1
    SCANBoll = 0
    SCANMACDx = 0
    condParabolic = 0
    condLong = 0
    BBSellSignal = 0 //ALMASellSignal =0
    EMA20SellSignal =0
    condEXIT =0
    BuyingSignal = 0
    //condDIVERoutput =0
    ActivateTrailStop = 0
    //ScanDiver = 1
    endif
    
    ENDIF
    
    SET STOP pLOSS 173.0
    
    
    //-------------------------------------------------------------------------
    // Function : ALMA
    //-------------------------------------------------------------------------
    //ALMA Arnaud Legoux Moving Average
    // parameters
    // Window = 9
    // Sigma = 6
    // Offset = 0.85
     
    Price = Close
     
    m = (Offset * (Window - 1))
    s = Window/Sigma
     
    WtdSum = 0
    CumWt  = 0
     
    for k = 0 to Window - 1 do
    Wtd = Exp(-((k-m)*(k-m))/(2*s*s))
    WtdSum = WtdSum + Wtd * Price[Window - 1 - k]
    CumWt = CumWt + Wtd
    next
     
    ALAverage = WtdSum / CumWt
     
    RETURN ALAverage
    
    
    //-------------------------------------------------------------------------
    // Function : BollingerBand
    //-------------------------------------------------------------------------
    REM Computes the Bollinger's moving average and the Standard Deviation
    
    BollingerMA = ExponentialAverage[p](close)
    
    REM Computes the Standard Deviation
    REM Please note that it is much faster to use the built-in function STD
    
    IF barindex >= p-1 THEN
    sumy2 = 0
    sumy = 0
    FOR i = 0 TO p-1 do
    sumy2 = sumy2 + SQUARE(close[i])
    sumy = sumy + close[i]
    NEXT
    STDDEV =SQRT(sumy2 / p - SQUARE(sumy / p))
    ELSE
    STDDEV = undefined
    ENDIF
    
    
    REM We can now compute the Bollinger Bands
    
    bollUP = BollingerMA + s * STDDEV
    bollDOWN = BollingerMA - s * STDDEV
    bollMID = BollingerMA
    
    RETURN bollUP AS "Boll+", bollDOWN AS "Boll-", bollMID AS "BollMid"
    
    
    //-------------------------------------------------------------------------
    // Function : DIVERoutput
    //-------------------------------------------------------------------------
    //EMRSDIVERINTER // ######################################################################################################
    //BEARISH RSI DIVERGENCE
    
    //Variable Declarations
    
    obLevel = 65 //20210308
    oslevel = 40 //20210308
    
    
    //Indicator Declarations
    
    indMACDLn = MACDline[12,26,9](close)
    //indMACDLn = ExponentialAverage[12](close) -ExponentialAverage[26](close)
    
    //indMACDSign = MACDSignal[12,26,9](close)
    indMACDSign = ExponentialAverage[9](indMACDLn)
    
    indMACDHis = MACD[12,26,9](close)
    //indMACDHis = indMACDLn - indMACDSign
    indRSI = RSI[14](close)
    
    
    //histogram colour
    
    if indMACDHis>0 then
    r=0
    g=100
    b=0
    else
    r=255
    g=0
    b=0
    endif
    
    //Conditions to Reset
    
    ob = indRSI>obLevel //20210308
    oS = indRSI<osLevel //20210308
    condMACDLnOver0 = indMACDLn crosses over 0
    condMACDLnUnder0 = indMACDLn crosses under 0
    
    //Conditions to Start
      
    if barindex = 0 then
    InterPriceMax=0
    InterMACDLnMax =0
    oldPriceMax = 0
    oldMACDLnMax=0
    InterBIMax = 0
    endif
    
    If condMACDLnOver0 then
    
    PriceMax=0
    MACDLnMax =0
    BIStartBear = barindex
    EntrySignalBear =0
    SCANBear = 1
    obpassed =0
    
    endif
    
    if SCANBear = 1  and barindex >BIStartBear and ob then
    //Get max of Price
    PriceMax = max(PriceMax, High)
    if PriceMax <> PriceMax[1] then
    PriceMaxBI = barindex
    PriceMaxMACDLn = indMACDLn
    endif
    //Get max MACDLn
    MACDLnMax =max(MACDLnMax,indMACDLn)
    if MACDLnMax <> MACDLnMax[1] then
    BIMax = barindex
    endif
    
    if MACDLnMax > PriceMaxMACDLn then
    BIMax = PriceMaxBI
    endif
    
    obpassed = 1
    
    endif
    
    // Check for bearish crossover and divergences
    If SCANBear=1 and condMACDLnUnder0 and obpassed = 1 then
    
    oldPriceMax = InterPriceMax
    InterPriceMax =PriceMax
    oldMACDLnMax = InterMACDLnMax
    InterMACDLnMax =MACDLnMax
    oldBIMax = InterBImax
    InterBImax = BIMax
    
    //drawtext ("InterMACDLnMax = #InterMACDLnMax#", BIMax,MACDLnMax) coloured(200,0,0)
    //drawtext ("oldMACDLnMax = #oldMACDLnMax#", BIMax,MACDLnMax*1.1) coloured(200,0,0)
    //drawtext ("MACDLnMax = #MACDLnMax#", BIMax,MACDLnMax*1.2) coloured(200,0,0)
    
    BearishDivMACD = (InterPriceMax >= oldPriceMax) AND (InterMACDLnMax < oldMACDLnMax) AND (BIMax > BIStartBear)
    
    if BearishDivMACD then
    BIDivBear = BImax
    //drawtext ("BearishDivMACD = #BearishDivMACD#", BIMax,MACDLnMax) coloured(200,0,0)
    //drawsegment(oldBIMax,oldMACDLnMax,BIMax,MACDLnMax) coloured(200,0,0)
    //drawarrowdown(BIMax,MACDLnMax) coloured(200,0,0)
    SCANBear=0
    InterPriceMax=0
    InterMACDLnMax =0
    oldPriceMax = 0
    oldMACDLnMax=0
    endif
    
    obpassed = 0
    
    endif
    
    If (barindex >= BIDivBear) and BearishDivMACD AND condMACDLnUnder0  Then
    EntrySignalBear = -1*(indMACDHis)
    EntrySignalBIBear = barindex
    BearishDivMACD =0
    ClearSignalBear = 1
    ENDIF
    
    //20210308
    if barindex > EntrySignalBIBear and  ClearSignalBear= 1 then
    EntrySignalBear = 0
    ClearSignalBear= 0
    endif
    // //20210308
    
    
    //BULLISH MACD RSI DIVERGENCE //###################################################
    
    //Conditions to Start
    
    if barindex = 0 then
    InterPriceMin=100000
    InterMACDLnMin =100000
    oldPriceMin = 100000
    oldMACDLnMin=100000
    InterBIMin = 0
    endif
    
    //IF os <> os[1] then//20210308
    If condMACDLnUnder0 then
    
    PriceMin=100000
    MACDLnMin =100000
    BIStartBull = barindex
    EntrySignalBull =0
    SCANBull = 1
    ospassed =0
    
    endif
    //endif//20210308
    
    //if SCANBull = 1  and barindex >BIStartBull and indMACDHis < 0 then
    if SCANBull = 1  and barindex >= BIStartBull  and os then
    
    //Get min PriceMin, MACDLn
    PriceMin = min(PriceMin, Low)
    if PriceMin <> PriceMin[1] then
    PriceMinBI = barindex
    PriceMinMACDLn = indMACDLn
    endif
    
    MACDLnMin =min(MACDLnMin,indMACDLn)
    if MACDLnMin <> MACDLnMin[1] then
    BIMin = barindex
    endif
    
    if MACDLnMin < PriceMinMACDLn then
    BIMin = PriceMinBI
    endif
    
    ospassed = 1
    WatcherOnBull = 0
    endif
    
    // Check for bearish crossover and divergences
    If SCANBull=1 and condMACDLnOver0  and ospassed = 1 then
    
    oldPriceMin = InterPriceMin
    InterPriceMin =PriceMin
    oldMACDLnMin = InterMACDLnMin
    InterMACDLnMin =MACDLnMin
    oldBIMin = InterBImin
    InterBImin = BIMin
    
    
    //drawtext ("InterMACDLnMin = #InterMACDLnMin#", BIMin,MACDLnMin) coloured(200,0,0)
    //drawtext ("oldMACDLnMin = #oldMACDLnMin#", BIMin,MACDLnMin+20) coloured(200,0,0)
    //drawtext ("MACDLnMin = #MACDLnMin#", BIMin,MACDLnMin+40) coloured(200,0,0)
    
    //declare divergence condition
    BullishDivMACD = (InterPriceMin < oldPriceMin ) AND (InterMACDLnMin > oldMACDLnMin)AND (BIMin > BIStartBull)
    if BullishDivMACD then
    BIDivBull = BImin
    //drawsegment(oldBIMin,oldMACDLnMin,BIMin,MACDLnMin) coloured(200,0,0) //this is it
    //drawsegment(oldBIMin,oldMACDLnMin,InterBImin,InterMACDLnMin) coloured(200,0,0)
    //drawarrowup(BIMin,MACDLnMin) coloured(200,0,0) //this is it
    //drawarrowup(InterBImin,InterMACDLnMin) coloured(200,0,0)
    //drawtext ("MACDLnMin = #MACDLnMin#", BIMin, MACDLnMin) coloured(200,0,0)
    //RESET = 1
    SCANBull=0
    //drawarrowup(BIMin,indMACDLn) coloured(255,255,0)
    //EntrySignalBull= 1//indMACDLn
    //BullishDivMACD =0
    InterPriceMin=100000
    InterMACDLnMin =100000
    oldPriceMin = 100000
    oldMACDLnMin=100000
    //InterBIMin = 0
    endif
    
    ospassed = 0
    
    endif
    
    If (barindex >= BIDivBull) and BullishDivMACD AND condMACDLnOver0  Then
    //If indRSI < 51 then
    //drawarrowup(barindex,indMACDLn) coloured(255,255,0)
    EntrySignalBull= (indMACDHis)
    EntrySignalBIBull  = barindex
    BullishDivMACD =0
    ClearSignalBull = 1
    WatcherOnBull = 1
    //endif
    ENDIF
    
    if barindex > EntrySignalBIBull and  ClearSignalBull= 1 then
    EntrySignalBull = 0
    ClearSignalBull= 0
    endif
    
    //return indMACDLn coloured (255,255,0) style(line,1) as "MACDLine",indMACDSign coloured (100,100,100) style (line,1) as "MACDSign",indMACDHis coloured(r,g,b) style(histogram) as "MACDHistogram", 0 coloured(168,168,168) style(line,2) as "0 level",EntrySignalBull coloured(160,75,180) style (histogram) as "Enter here bullish",EntrySignalBear coloured(160,75,180) style (histogram) as "Enter here bearish", WatcherOnBull
    
    return  WatcherOnBull as "WatcherOnBull", EntrySignalBIBull as "EntrySignalBIBull"
    
    #175303 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    No, the correct support forum is ProOrder, as it is a stratregy. I moved it.

    Below you can read the basic rules highlighted in yellow.

    I couldn’t spot any issue. You should post:

    • instrument/asset traded
    • TF
    • date and time of entry of the trade closed incorrectly.
    LuckyClover18 thanked this post
    #175309 quote
    LuckyClover18LuckyClover18
    Participant
    New

    Thanks for correcting, robertogozzi!

Viewing 3 posts - 1 through 3 (of 3 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

Trading System Auto Closing Position


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by LuckyClover18LuckyClover18
5 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/14/2021
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...