Hi guys, first post on this great forum. Just a quick question, I have made a strategy from a few bits of code found on this site. The strategy is ment to buy two contracts at entry conditions and set stoploss at 1.5xATR, then it is ment to sell 1 contract at the first profit target which is 1X ATR and also move the stoploss to breakeven/entry price, then sell the remining contract at exit condition. When running backtest I am having issues with the first contract not selling sometime. Anybody have any ideas. As you can tell by my code I am vey new to this.
Thanks guys.
Cheers.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.
//DEFPARAM FLATBEFORE = 180000
// Cancel all pending orders and close all positions at the "FLATAFTER" time
//DEFPARAM FLATAFTER = 080000
ONCE i1a = 60
ONCE i1d = 2.5
ONCE i1b = 23
ONCE i2a = 18
ONCE i2b = 40
ONCE i4a = 12
ONCE i4d = 1.5
ONCE i4b = 16
ONCE i5a = 7
ONCE i5b = 14
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 6
// Conditions to enter long positions
indicator1 = Average[i1a](close)+i1d*std[i1b](close)
c1 = (close CROSSES OVER indicator1)
indicator2 = MoneyFlowIndex[i2a]
c2 = (indicator2 > i2b)
IF (c1 AND c2) and OML = 0 AND not daysForbiddenEntry THEN
BUY 2 CONTRACT AT MARKET
PP=close
OML=1
ENDIF
if positionprice <> positionprice[1] then
PP = positionprice
endif
// Conditions to enter short positions
indicator4 = Average[i4a](close)-i4d*std[i4b](close)
c4 = (close CROSSES OVER indicator4)
indicator5 = MoneyFlowIndex[i5a]
c5 = (indicator5 < i5b)
IF (c4 AND c5) and OMS = 0 AND not daysForbiddenEntry THEN
SELLSHORT 2 CONTRACT AT MARKET
PPS = CLOSE
OMS = 1
ENDIF
if PositionPrice <> PositionPrice[1] then
PPS = PositionPrice
endif
//////////////////////////////////////////////////////////////////////////
////Scale out
//////////////////////////////////////////////////////////////////////////
//resetting variables when no trades are on market
ATRP = AverageTrueRange[14](close) //ATR for profit
ATRS = AverageTrueRange[14](close) * 1.5 //ATR + multiplier for stoploss
if not onmarket then
priceexitShort = 0
PriceExitLong = 0
ProfitTargetShort = 0
ProfitTargetLong = 0
EntryPrice = 0
CurrentPrice = 0
SL = 0
endif
//SHort scale out
if shortonmarket then
SL = ATRS //set stoploss at 1.5x ATR
EntryPrice = TradePrice(1)
CurrentPrice = Close[1]
ProfitTargetShort = EntryPrice-ATRP
if CurrentPrice <= ProfitTargetShort then
PriceExitShort = CLose - ProfitTargetShort
endif
endif
//Close 1 contract at first profit level
if OMS = 1 and PriceExitShort>0 then
SL = EntryPrice //set stoploss to breakeven
EXITSHORT 1 CONTRACT AT MARKET
OMS = OMS-1
endif
//Close 2nd contract when PTL under over price
indicator12, ignored, ignored, ignored = CALL "PRC_PerfectTrendLine 2"[7, 3]
c10 = (indicator12 CROSSES UNDER close)
if C10 then
EXITSHORT AT MARKET
OMS = 0
endif
//Long scale out
if longonmarket then
SL = ATRS //set stoploss at 1.5x ATR
EntryPrice = TradePrice(1)
CurrentPrice = Close[1]
ProfitTargetLong = EntryPrice+ATRP
if CurrentPrice >= ProfitTargetLong then
PriceExitLong = CLose + ProfitTargetLong
endif
endif
//Close 1 contract at first profit level at 1xATR and set stoploss to brek vevn
if OML = 1 and priceexitLong>0 then
SL = EntryPrice //set stoploss to break even
SELL 1 CONTRACT AT MARKET
OML = OML -1
endif
//Close 2nd contract when PTL crosses over price
indicator5, ignored, ignored, ignored = CALL "PRC_PerfectTrendLine 2"[7, 3] //7,3
c4 = (indicator5 CROSSES OVER close )
IF C4 THEN
sell at market
OML = 0
ENDIF
SET STOP LOSS SL
//SET TARGET PROFIT AverageTrueRange
Anybody have any ideas.
It could be to do with use of EntryPrice = TradePrice(1)?
When 1 lot of a 2 lot position has exited then TradePrice(1) would equal to the exit price of the 1 lot … I think I am correct in saying ?
After the 1 lot has exited, you would need to use EntryPrice = TradePrice(2)?
Best wait until somebody confirms I am correct in my assertion above … before you spend too much time changing your code?
You can use GRAPH TradePrice(1) to check if what I say above is correct? Be sure to let us know please.
Thanks for yor time. I will try this and see how it goes.
Thnaks for the GRAPH tip, makes problem solving alot easier, had a few mistakes in the code. One issue I have is the ATR stoploss and profit updating on every candle. Is there a way of taking the ATR value from the trade entry candle only?
ATRP = AverageTrueRange[14](close) //ATR for inital profit short
ATRS = AverageTrueRange[14](close) * 1.5 //ATR + multiplier for stoploss short
SL = EntryPrice + ATRS //set stoploss at 1.5x ATR short
The above code which I am using now changes the stop loss & profit every candle.
Thanks.
Is there a way of taking the ATR value from the trade entry candle only?
Yes … include the ATR values within the … If Not Longonmarket (and Not Shortonmarket) Endif … statement for entry conditions.