Add condition referencing last trade direction

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #53328 quote
    tom123
    Participant
    Junior

     I would like one of the conditions in the algorithm to depend on the direction of the last trade. 

    An example for simplicity: IF some set of conditions are met AND the last trade was a short position THEN take a long position… and vise versa.

    I haven’t been able to find an easy solution to call the last trade direction, so any help would be greatly appreciated! 

    #53329 quote
    robertogozzi
    Moderator
    Master
    .
    .
    ONCE LastTradeDirection = 0
    .
    .
    IF LongOnMarket THEN
       LastTradeDirection = 1      //1 = last trade went LONG
    ELSIF ShortOnMarket THEN
       LastTradeDirection = 2      //2 = last trade went SHORT
    ENDIF
    .
    .
    .
    .
    

    You need to set a variable when you either go LONG or SHORT, so that it will later let you know if the last trade was going northwards or southwards. If trades are open not so frequently you might decide to reset the variable to ZERO after “n” candlesticks have elapsed. It’s up to you.

    Roberto

    tom123 thanked this post
    #220361 quote
    MDBrown
    Participant
    New
    Hi, I am using a solution like the above which works fine for last trade direction but not if the trade opens and closes within the same bar. In that circumstance the variable does not change because we are Not OnMarket at the close of the bar. I have tried various solutions but just can’t seem to nail it. Any suggestions would be much appreciated. Thanks, Matt
    #220364 quote
    robertogozzi
    Moderator
    Master
    This snippet will do:
    // current version
    //
    TradeFlag = 0            //1=Long,  2=Short
    IF StrategyProfit > StrategyProfit[1] THEN
       IF TradePrice(1) > TradePrice(2) THEN
          TradeFlag = 1
       ELSE
          TradeFlag = 2
       ENDIF
    ELSIF StrategyProfit < StrategyProfit[1] THEN
       IF TradePrice(1) < TradePrice(2) THEN
          TradeFlag = 1
       ELSE
          TradeFlag = 2
       ENDIF
    ENDIF
    it will only work when you do not accumulate trades. But you can also use these new instructions to tell Long from Short trades (https://www.prorealcode.com/documentation/shorttriggered/):
    ShortTriggered[N]
    LongTriggered[N]
    N is the bar reference [1], [2], ec…  [0] is assumed by default and can be omitted.
    MDBrown thanked this post
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Add condition referencing last trade direction


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
tom123 @tom123 Participant
Summary

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

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