Strategy Name: Multilevel Take Profit Strategy

Data Cycle: Multi-Cycle

Support: Commodity Futures, Digital Currency


  • Main chart:
    Upper line, formula:UPPERLINE^^TODAYOPEN+BAND;
    Lower line, formula:LOWERLINE^^TODAYOPEN-BAND;
    MA, formula:MALINE^^MA(CLOSE,LENGTH);
  • Secondary chart:
    none
(*backtest
start: 2018-01-01 00:00:00
end: 2018-04-09 00:00:00
period: 1d
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}]
args: [["LENGTH",80]]
*)

STARTPER1:=5;  // 1 level tracking take profit, if profit 5% then start

STOPPER1:=100; // 1 level tracking take profit, if profit retracement is 100% then trigger
               
STARTPER2:=10; // 2 level tracking take profit, if profit 10% then start

STOPPER2:=50;  // 2 level tracking take profit, if profit retracement is 50% then trigger

STARTPER3:=20; // 3 level tracking take profit, if profit 20% then start

STOPPER3:=20;  // 3 level tracking take profit, if profit retracement is 20% then trigger
    
// Upper and lower intervals

NN:=BARSLAST(DATE<>REF(DATE,1))+1;  // The number of cycles since the opening today

TODAYOPEN:=VALUEWHEN(NN=1,O);      // Opening price of the day

TODAYHIGH:=HHV(H,NN);                    // today’s highest price
TODAYLOW:=LLV(L,NN);                     // today’s lowest price
YESTDAYHIGH:=REF(TODAYHIGH,NN);          // yesterday's highest price
YESTDAYLOW:=REF(TODAYLOW,NN);            // yesterday's lowest price

BAND:=YESTDAYHIGH-YESTDAYLOW;
UPPERLINE^^TODAYOPEN+BAND;
LOWERLINE^^TODAYOPEN-BAND;
    
// MA
MALINE^^MA(CLOSE,LENGTH);
            
// When to open and close position
SKCOND: = C<LOWERLINE AND LOWERLINE<MALINE;
BKCOND: = C>UPPERLINE AND UPPERLINE>MALINE;
BPSHORT: = C>UPPERLINE OR C>MALINE;
SPLONG: = C<LOWERLINE OR C<MALINE;

//the mainbody of program
//open position

SKCOND,SK;
BKCOND,BK;

//close position
SPLONG,SP;
BPSHORT,BP;
        
//take profit     
SKLOW<=SKPRICE*(1-0.01*STARTPER3) AND HIGH>=SKLOW+(SKPRICE-SKLOW)*0.01*STOPPER3,BP;                                                          // If profit get “STARTPER3”% and profit retracement get “STOPPER3”%, then close short position
BKHIGH>=BKPRICE*(1+0.01*STARTPER3) AND LOW<=BKHIGH-(BKHIGH-BKPRICE)*0.01*STOPPER3,SP;                                                          // If profit get “STARTPER3”% and profit retracement get “STOPPER3”%, then close long position
SKLOW<=SKPRICE*(1-0.01*STARTPER2) AND HIGH>=SKLOW+(SKPRICE-SKLOW)*0.01*STOPPER2,BP;                                                      // If profit get “STARTPER2”% and profit retracement get “STOPPER2”%, then close short position
BKHIGH>=BKPRICE*(1+0.01*STARTPER2) AND LOW<=BKHIGH-(BKHIGH-BKPRICE)*0.01*STOPPER2,SP;                                                          // If profit get “STARTPER2”% and profit retracement get “STOPPER2”%, then close long position
SKLOW<=SKPRICE*(1-0.01*STARTPER1) AND HIGH>=SKLOW+(SKPRICE-SKLOW)*0.01*STOPPER1,BP;                                                         // If profit get “STARTPER1”% and profit retracement get “STOPPER1”%, then close short position
BKHIGH>=BKPRICE*(1+0.01*STARTPER1) AND LOW<=BKHIGH-(BKHIGH-BKPRICE)*0.01*STOPPER1,SP;                                                                                  // If profit get “STARTPER1”% and profit retracement get “STOPPER1”%, then close long position

//stop loss
C>=SKPRICE*(1+STOPRANGE*0.01),BP; 
C<=BKPRICE*(1-STOPRANGE*0.01),SP; 
AUTOFILTER;

Backtest on FMZ Quant to know more

Source Codehttps://www.fmz.com/strategy/128122

Leave a Reply

Your email address will not be published. Required fields are marked *