Determine next trade long or short if previous trade exited due to stop loss

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #111297 quote
    sulimaster
    Participant
    Average

    Hi

    My trading strategy alternates between long and short trades i.e. long closes and short opens at the same time and vice versa. If a long trade is closed early due to a stop loss being triggered, I want the next trade to be a short trade (and vice versa if a short trade is closed early due to a stop loss being triggered).

    Please, how do I code to check whether the previous trade was long or short and whether it was exited due to a stop loss being triggered?

    Something like:

    IF [previous trade was long] AND [previous trade exited due to stop loss being triggered] THEN
    a = 1
    ENDIF
    
    IF [previous trade was short] AND [previous trade exited due to stop loss being triggered] THEN
    b = 1
    ENDIF

    I can then use the above a and b as conditions to enter the next trade but I do not know how to code the bits in the square brackets. Please help

    Thanks

    Sachin

    #111314 quote
    Nicolas
    Keymaster
    Master

    I already used this snippet to identify what was the last order type and if it was in gain, so it could be adapted to make the opposite:

    //------------------
    //check profit 
    if strategyprofit<>strategyprofit[1] then 
     if positionperf(1)>0 then 
      if close<tradeprice(2) then 
       gain=-1 //was sell
      elsif close>tradeprice(2) then 
       gain=1 //was buy
      endif
     else
      gain=0
     endif
    endif
    sulimaster thanked this post
    #111315 quote
    GraHal
    Participant
    Master

    Added as Log 181 here …

    Snippet Link Library

    #111565 quote
    sulimaster
    Participant
    Average

    Thanks Nicolas

    Is the below code valid and would it give me result of a = 1 if the previous trade was long and b =1 if the previous trade was short?

    IF LONGONMARKET(TRADEINDEX(2)) = 1 THEN
    a = 1
    ENDIF
    
    IF SHORTONMARKET(TRADEINDEX(2)) = 1 THEN
    b = 1
    ENDIF
    #111595 quote
    sulimaster
    Participant
    Average

    Hi Nicolas

    Thinking about it further, BARINDEX starts ascending from left to right and ONMARKET starts ascending from right to left. So is the below code correct instead of my reply earlier to give a result of a = 1 if the previous trade was long and b = 1 if the previous trade was short?

    IF LONGONMARKET(BARINDEX - TRADEINDEX(2)) = 1 THEN
    a = 1
    ENDIF
     
    IF SHORTONMARKET(BARINDEX - TRADEINDEX(2)) = 1 THEN
    b = 1
    ENDIF

    Thanks

    Sachin

    #111662 quote
    GraHal
    Participant
    Master

    Why don’t you GRAPH Longonmarket and GRAPH a then you will see what the code gives you on the BT results?

    If you don’t know what GRAPH involves just say?

    sulimaster thanked this post
    #111670 quote
    Nicolas
    Keymaster
    Master

    That could work but offset must be in brackets and not parenthesis !

    sulimaster thanked this post
    #112353 quote
    sulimaster
    Participant
    Average

    Thanks Nicolas

    Yes, I should check when to use [] or ()

    #112354 quote
    sulimaster
    Participant
    Average

    Thanks GraHal

    I was aware of RETURN but completely missed GRAPH. I’ll have a look and come back if I need assistance. Thanks again

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

Determine next trade long or short if previous trade exited due to stop loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
sulimaster @sulimaster Participant
Summary

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

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/26/2019
Status: Active
Attachments: No files
Logo Logo
Loading...