Hi I have the code below to stop opening new positions when one of the positions in the day closed with loss more than 200 or profit more than 350.
It works perfectly in the proback test but when I execute it in IG in real market it still open position even after the loss happens. I am not sure what I am missing here?
Please note the code works when I am in positive and will not open new position after hitting 350. Could it be because of negative number in the strategy profit somehow mess up the formula?
This is my code:
If time=100000 then
MyProfit=STRATEGYPROFIT[0]
endif
if time>100000 and time <150000 and countoflongshares<90 and range[1]<14 and countofshortshares<90 and Strategyprofit[0]>MyProfit-200 and Strategyprofit[0]<MyProfit+350
then ……..
I am starting my code with : once myprofit=0 at the begining, could this be the reason?
You have to use a variable as a signal that you don’t want to trade any longer that day (I will use TradeOK, setting to 1 at the beginning of each day), as soon as your strategy detects a loss of X, it will set TradedOK to 0. Your strategy will have to use TradeOK to tell whether trading or not:
ONCE TradeOK = 1
ONCE MyLimit = 200
IF InstraDayBarIndex = 0 THEN
TradeOK = 1
ENDIF
IF (StrategyProfit[1] - StrategyProfit) >= MyLimit THEN
TradeOK = 0
ENDIF
IF MyLongConditions AND TradeOK THEN
BUY AT MARKET
ENDIF
thanks, so why my code worked in the probacktest but not in reallife?
I have no idea without testing the complete code used.
since I want to look at the whole day performance , should I change your code to the below to not only check with the last bar but for the whole day from morning:
ONCE TradeOK = 1
ONCE MyLimit = 200
IF time = 100000 THEN
TradeOK = 1
Myprofit=strategyprofit
ENDIF
IF (Myprofit- strategyprofit) >= MyLimit THEN
TradeOK = 0
ENDIF
Also will this still work if your strategy starts in negative. What I noticed is that if my total strategy profit is positive then next day the code works and when I have lost it stops but when the strategy was already in negative from last day the code somehow can’t calculate the negative minus negative and stop working
Try replacing line 6 with:
IF (abs(StrategyProfit - StrategyProfit[1]) >= MyLimit) and (StrategyProfit < StrategyProfit[1]) THEN
Thanks, but I need the strategy profit be calculated for the total of positions from 10:00 am. In your code if one of the positions closes in negative the strategy will stop because it will always compare the current bar with the last bar not the total strategyprofit from 10:00 am
You code above works fine for me. Why don’t you think it’s correct?
Try appending some GRAPH instructions to monitor data candle by candle:
ONCE TradeOK = 1
ONCE MyLimit = 200
IF time = 100000 THEN
TradeOK = 1
Myprofit=strategyprofit
ENDIF
IF (Myprofit- strategyprofit) >= MyLimit THEN
TradeOK = 0
ENDIF
if average[20] crosses over average[200] and not onmarket then
buy at market
endif
set target pprofit 200
set stop ploss 50
graph MyProfit coloured(255,0,0,255)
graph StrategyProfit coloured(0,255,0,255)
graph Myprofit- strategyprofit