Problems with trading time and stoploss

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #167086 quote
    SweZerk
    Participant
    Junior

    Hi, iv been having some problems with trade time, some of my algos trade even when they shouldent.

    iv tried these 2 diffrent ways but in some algos they work and some dont

    // Prevents the system from creating new orders to enter the market or increase position size before the specified time
    noEntryBeforeTime = 090000
    timeEnterBefore = time >= noEntryBeforeTime
    
    // Prevents the system from placing new orders to enter the market or increase position size after the specified time
    noEntryAfterTime = 1730000
    timeEnterAfter = time < noEntryAfterTime
    
    // Prevents the system from placing new orders on specified days of the week
    daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
    // Conditions to enter long positions
    Ctime = time >=090000 and time <1730000
    

    with the condition on selling and buying aswell.

    been stairing at my code for a week now but cant find the problem.

    i also have a problem with a stoploss i found on this site, the problem is that it moves upp and down, not a fan of the down part

    //------------------------------------------------------------------------------------------------------------------------------------------------
    //                     Trailing Start
    //------------------------------------------------------------------------------------------------------------------------------------------------
    ONCE UseCLOSE = 1                                 //1=use CLOSE,  0=use High/Low
    srcH = close                                      //defaults to CLOSE
    srcL = close                                      //defaults to CLOSE
    IF UseCLOSE = 0 THEN
    srcH = high
    srcL = low
    ENDIF
    ONCE UsePerCentage = 1                           //0=use Pips  (default), 1=use Percentages
    
    //
    DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket)  //True when there's been a change in the direction (likely to be due to a Stop & Reverse)
    //
    IF Not OnMarket OR DirectionSwitch THEN
    //
    // when NOT OnMarket or thare's been a change in direction, reset values to their default settings
    //
    StartPerCent  = 0.25        //0.25%  to start triggering Trailing Stop       (when UsePerCentage=1)
    StepPerCent   = 50          //50%    (of the 0.25% above) as a Trqiling Step (when UsePerCentage=1) (set to 100 to make StepSize=TrailStart, set to 200 to make it twice TrailStart)
    //
    TrailStart    = 30          //30     Start trailing profits from this point  (when UsePerCentage=0)
    
    IF UsePerCentage THEN
    TrailStart = (close / PipSize) * StartPerCent / 100           //use current price (CLOSE) for calculations
    
    ENDIF
    ENDIF
    //
    BasePerCent   = 0.200       //20.0%  Profit percentage to keep when setting BerakEven
    //
    StepSize      = 10          //10     Pip chunks to increase Percentage
    IF UsePerCentage THEN
    StepSize   = TrailStart * StepPerCent / 100
    ENDIF
    //
    PerCentInc    = 0.100       //10.0%  PerCent increment after each StepSize chunk
    RoundTO       = -0.5        //-0.5   rounds always to Lower integer,   +0.4 rounds always to Higher integer,     0 defaults PRT behaviour
    PriceDistance = 7 * pipsize //7      minimun distance from current price
    y1            = 0           //reset to 0
    y2            = 0           //reset to 0
    ProfitPerCent = BasePerCent //reset to desired default value
    //PositionCount = 0
    SellPrice     = 0
    SellPriceX    = 0
    ExitPrice     = 9999999
    ExitPriceX    = 9999999
    
    //------------------------------------------------------
    // --- Update Stop Loss after accumulating new positions
    //------------------------------------------------------
    //PositionCount = max(PositionCount,abs(CountOfPosition))
    //
    // update Stop Loss only when PositionPrice has changed (actually when increased, we don't move it if there's been some positions exited)
    //
    //IF PositionCount <> PositionCount[1] AND (ExitPrice + SellPrice)<>9999999 THEN //go on only if Trailing Stop had already started trailing
    IF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailing
    IF LongOnMarket THEN
    q1         = PositionPrice + ((srcH - PositionPrice) * ProfitPerCent)      //calculate new SL
    SellPriceX = max(max(SellPriceX,SellPrice),q1)
    SellPrice  = max(max(SellPriceX,SellPrice),PositionPrice + (y1 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
    ELSIF ShortOnMarket THEN
    r1         = PositionPrice - ((PositionPrice - srcL) * ProfitPerCent)      //calculate new SL
    ExitPriceX = min(min(ExitPriceX,ExitPrice),r1)
    ExitPrice  = min(min(ExitPriceX,ExitPrice),PositionPrice - (y2 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
    ENDIF
    ENDIF
    // --- Update END
    
    //
    IF LongOnMarket AND srcH > (PositionPrice + (y1 * pipsize)) THEN                              //LONG positions
    //
    // compute the value of the Percentage of profits, if any, to lock in for LONG trades
    //
    x1 = (srcH - PositionPrice) / pipsize                                     //convert price to pips
    IF x1 >= TrailStart THEN                                                //    go ahead only if N+ pips
    Diff1         = abs(TrailStart - x1)                                 //difference from current profit and TrailStart
    Chunks1       = max(0,round((Diff1 / StepSize) + RoundTO))           //number of STEPSIZE chunks
    ProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
    ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))         //make sure ProfitPerCent doess not exceed 100%
    y1 = max(x1 * ProfitPerCent, y1)                                     //y1 = % of max profit
    ENDIF
    ELSIF ShortOnMarket AND srcL < (PositionPrice - (y2 * pipsize)) THEN                             //SHORT positions
    //
    // compute the value of the Percentage of profits, if any, to lock in for SHORT trades
    //
    x2 = (PositionPrice - srcL) / pipsize                                     //convert price to pips
    IF x2 >= TrailStart THEN                                                //      go ahead only if N+ pips
    Diff2         = abs(TrailStart - x2)                                 //difference from current profit and TrailStart
    Chunks2       = max(0,round((Diff2 / StepSize) + RoundTO))           //number of STEPSIZE chunks
    ProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCent
    ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))         //make sure ProfitPerCent doess not exceed 100%
    y2 = max(x2 * ProfitPerCent, y2)                                     //y2 = % of max profit
    ENDIF
    ENDIF
    //------------------------------------------------------------------------------
    //                    manage actual Exit, if needed
    //------------------------------------------------------------------------------
    IF y1 THEN                                                                 //Place pending STOP order when y1 > 0   (LONG positions)
    SellPrice = max(SellPrice,PositionPrice + (y1 * pipsize))                  //convert pips to price
    //
    // check the minimun distance between ExitPrice and current price
    //
    IF abs(close - SellPrice) > PriceDistance THEN
    //
    // place either a LIMIT or STOP pending order according to current price positioning
    //
    IF close >= SellPrice THEN
    SELL AT SellPrice STOP
    ELSE
    SELL AT SellPrice LIMIT
    ENDIF
    ELSE
    //
    //sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
    //
    SELL AT Market
    ENDIF
    ENDIF
    IF y2 THEN                                                                 //Place pending STOP order when y2 > 0   (SHORT positions)
    ExitPrice = min(ExitPrice,PositionPrice - (y2 * pipsize))                  //convert pips to price
    //
    // check the minimun distance between ExitPrice and current price
    //
    IF abs(close - ExitPrice) > PriceDistance THEN
    //
    // place either a LIMIT or STOP pending order according to current price positioning
    //
    IF close <= ExitPrice THEN
    EXITSHORT AT ExitPrice STOP
    ELSE
    EXITSHORT AT ExitPrice LIMIT
    ENDIF
    ELSE
    //
    //ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
    //
    EXITSHORT AT Market
    ENDIF
    ENDIF
    

     

    hope anyone can point me in the right direction cus im lost.

    #167087 quote
    GraHal
    Participant
    Master

    some of my algos trade even when they shouldent.

    By trade … do you mean open a trade or close a trade?

    #167088 quote
    SweZerk
    Participant
    Junior

    it opens trades

    #167092 quote
    robertogozzi
    Moderator
    Master

    Try using 173000 instead of 1730000 (1 exceeding 0).

    #167093 quote
    GraHal
    Participant
    Master

    with the condition on selling and buying aswell.

    So you can sell only when CTime = 1 / True?

    Or do you mean SellShort if CTime = 1 / True?

    Use GRAPH for all your Buy conditions.

    Or post the full code on here and somebody will spot the problem for you.

    #167094 quote
    SweZerk
    Participant
    Junior

    Try using 173000 instead of 1730000 (1 exceeding 0).

    nice spoting 🙂 i tryed removing one 0 but the problem still remains in the backtest atleast.

    #167098 quote
    SweZerk
    Participant
    Junior

    with the condition on selling and buying aswell.

    So you can sell only when CTime = 1 / True?

    Or do you mean SellShort if CTime = 1 / True?

    Use GRAPH for all your Buy conditions.

    Or post the full code on here and somebody will spot the problem for you.

    it looks like this

    // Conditions to enter long positions
    IF not longonmarket and Ctime and c1 and c2 THEN
    BUY 0.25 CONTRACT AT MARKET
    elsif longonmarket and Ctime and c1 and c1a
    and COUNTOFLONGSHARES then
    BUY 0.25 CONTRACT AT MARKET
    SET STOP %LOSS 1
    
    ENDIF
    
    // Conditions to enter short positions
    IF not shortonmarket and Ctime and c3 and c4 THEN
    sellshort 0.25 CONTRACT AT MARKET
    elsif shortonmarket and Ctime and c3 and c4 and COUNTOFSHORTSHARES  then
    sellshort 0.25 CONTRACT AT MARKET
    SET STOP %LOSS 1
    
    ENDIF

    dont know what graph is but looking into it now haha 🙂 thx m8

    #167099 quote
    JC_Bywan
    Moderator
    Master

    If 9h and 17h30 are the time limits you want, having in mind “opentime” is the time at candle open, and “time” is the time at candle close, you used:

    Ctime = time >=090000 and time <173000

    but maybe you meant to use:

    Ctime = opentime >=090000 and opentime <173000

    which if keeping “time” and could also be written as:

    Ctime = time >090000 and time <=173000

    so that with the swapping of >=/< into >/<= , the candle ending at 9h is not included, and the candle ending at 17h30 is included?

    #167100 quote
    robertogozzi
    Moderator
    Master

    Post the lines where you used your conditions.

    Apart from the number of digits, the above code doesn’t tell us more, the extra 0 will let trades open from 9am up to 235900.

    As to Ctime (and the other time conditions), it depends on how you use them.

    IF Ctime AND YourLongConditions THEN
       BUY....
    ENDIF

    will work.

    #167104 quote
    SweZerk
    Participant
    Junior

    i see now that i put and extra 0 at ctime aswell, it seems to fix it! thx allot for all your help guys. been stairing at it for a few days now and couldent figure out.

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

Problems with trading time and stoploss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
SweZerk @swezerk Participant
Summary

This topic contains 9 replies,
has 4 voices, and was last updated by SweZerk
4 years, 10 months ago.

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