Help about stop trading strategy

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #211754 quote
    Amer
    Participant
    Junior

    Hello!

    Is it possible to program the strategy to turn off after two losses. So if there are two losses regardless of time or if these are consecutive, the strategy is closed for that particular day.

    Or if the loss is a certain amount, let’s say 50 dollars, the strategy is turned off for that particular day.
    Thanks in advance and best regards!

    #211755 quote
    robertogozzi
    Moderator
    Master

    My code will use the variable TradeON to flag periods when the strategy is freezed. Each new day trading is restored to full functionality:

    MaxLosses = 2  //after 2 losses stop trading
    MaxLoss   = 50  //after 50 € loss stop trading
    Once TradeON = 1
    Once MyProfit = 0
    If IntraDayBarIndex = 0 then
       TradeON = 1
       Tally = 0
       MyProfit = StrategyProfit
    Endif
    If StrategyProfit < StrategyProfit[1] then
       Tally = Tally + 1
       If Tally = MaxLosses then
          TradeON = 0
       Endif
    Endif
    If (MyProfit - StrategyProfit) >= MaxLoss then
       TradeON = 0
    Endif
    If MyLongConditions and TradeuON then
       BUY 1 Contract at Market
    Endif
    If MyShortConditions and TradeON then
       SELLSHORT 1 Contract at Market
    Endif
    Midlanddave thanked this post
    #211793 quote
    GraHal
    Participant
    Master

    Link to above code saved as Log 359 here …

    Snippet Link Library

    robertogozzi thanked this post
    #211842 quote
    Amer
    Participant
    Junior

    Thank you so much!

    #211843 quote
    Amer
    Participant
    Junior

    Thanks guys!

    #237039 quote
    Snålänningen
    Participant
    Junior

    Hi!

    I searched and found this thread and need some coding help. What I want do is: if the last 2 trades were losses, dont take any trade for the next 500 bars, instead of the rest of the day. How could I code that?

    #237040 quote
    robertogozzi
    Moderator
    Master

    There you go:

    ONCE Pause   = 500   //wait 500 bars before trading again
    ONCE Losses  = 0
    ONCE TradeON = 1
    IF StrategyProfit > StrategyProfit[1] THEN
       Losses  = 0
    ELSIF StrategyProfit < StrategyProfit[1] THEN
       Losses  = Losses + 1
       LossBAR = BarIndex
       IF Losses = 2 THEN
          Losses  = 0
          TradeON = 0
       ENDIF
    ENDIF
    IF (BarIndex -LossBAR) = Pause THEN
       TradeON = 1
    ENDIF
    //
    IF MyLongConditions AND TradeON THEN
       BUY 1 CONTRACT AT MARKET
    ELSIF MyShortConditions AND TradeON THEN
       SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    Snålänningen thanked this post
    #237258 quote
    ali313313
    Participant
    New

    Hi robertogozzi

    I need some coding help. What I want do is: if the last 3 trades were winning , dont take any trade for the next 10 bars How could I code that?

    #237272 quote
    robertogozzi
    Moderator
    Master

    There you go:

    ONCE Pause   = 10      //wait 10 bars before trading again
    ONCE MaxWins = 3       //pause after 3 consecutive wins
    ONCE Wins    = 0
    ONCE TradeON = 1
    IF StrategyProfit > StrategyProfit[1] THEN
       Wins = Wins + 1
       IF Wins = MaxWins THEN
          TradeON = 0
          winBAR  = BarIndex
          Wins    = 0
       ENDIF
    ELSIF StrategyProfit < StrategyProfit[1] THEN
       Wins = 0
    ENDIF
    IF (BarIndex - winBAR) = Pause THEN
       TradeON = 1
    ENDIF
    //
    MyLongConditions  = Not OnMarket AND Close CROSSES OVER  Average[20,0](close)
    MyShortConditions = Not OnMarket AND Close CROSSES UNDER Average[20,0](close)
    //
    IF MyLongConditions AND TradeON THEN
       BUY 1 CONTRACT AT MARKET
    ELSIF MyShortConditions AND TradeON THEN
       SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    SET STOP   %LOSS   0.5
    SET TARGET %PROFIT 1
    //graph Wins    coloured("Green")
    //graph TradeON coloured("Red")
    #237277 quote
    ali313313
    Participant
    New

    thank you thats help my code alooot

    I have just one more idea..

    What I want do is: its like a trailing stop but not on the position it will be on the portfolio i will give you an example:

     

    lets say i gain 1000$ and the trade is closed if he open a trade again i want him to secure 500$ from the last trade if he touch it he will shut down the system.

    also Every time I win multiples of 500, I want the device to stop at a rate of 500.

    #237281 quote
    robertogozzi
    Moderator
    Master

    This seems to work as expected:

    ONCE Pause            = 10   //wait 10 bars before trading again
    ONCE MaxWins          = 3    //pause after 3 consecutive wins
    ONCE Wins             = 0
    ONCE TradeON          = 1
    ONCE GainTreshold     = 1000 //Gain treshold above which a 50% profit is locked in
    ONCE myStrategyProfit = 0
    ONCE myGainStopLevel  = 0
    // exit if the locked in profit is reached
    IF OnMarket AND (myGainStopLevel > 0) THEN
       tempProfit = positionperf * positionprice * PipValue
       IF (StrategyProfit + tempProfit) <= myGainStopLevel THEN
          SELL      AT MARKET
          EXITSHORT AT MARKET
          Quit
       ENDIF
    ENDIF
    //check the wins and update the locked in profit, if large enough
    IF StrategyProfit > StrategyProfit[1] THEN
       Wins = Wins + 1
       IF Wins = MaxWins THEN
          TradeON = 0
          winBAR  = BarIndex
          Wins    = 0
       ENDIF
       //check if locked in profit has to be updated
       IF StrategyProfit > 0 THEN
          //set tha base for the calculations
          BaseProfit = StrategyProfit[1]
          //if the previous StrategyProfit was negative set the BaseProfit to 0,
          //to avoid an incorrect addition, due to subtracting a negative value
          IF StrategyProfit[1] < 0 THEN
             BaseProfit = 0
          ENDIF
          Gain = StrategyProfit - BaseProfit
          IF Gain >= GainTreshold THEN
             //lock in additional 50% profits
             myGainStopLevel = BaseProfit + round(Gain / 2)
          ENDIF
       ENDIF
    ELSIF StrategyProfit < StrategyProfit[1] THEN
       Wins = 0
    ENDIF
    //set trading again after pausing
    IF (BarIndex - winBAR) = Pause THEN
       TradeON = 1
    ENDIF
    //
    MyLongConditions  = Not OnMarket AND Close CROSSES OVER  Average[20,0](close)
    MyShortConditions = Not OnMarket AND Close CROSSES UNDER Average[20,0](close)
    //
    IF MyLongConditions AND TradeON THEN
       BUY 1 CONTRACT AT MARKET
    ELSIF MyShortConditions AND TradeON THEN
       SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    SET STOP   %LOSS   0.5
    SET TARGET %PROFIT 1
    //graph Wins    coloured("Green")
    //graph TradeON coloured("Red")
    graph StrategyProfit  coloured("Fuchsia")
    graph myGainStopLevel coloured("Blue")
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Help about stop trading strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Amer @amer Participant
Summary

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

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