I am trying to setup a more complex stop through looping code. Somehow I am not able to use a local variable to store information inside the loop. In the simplified version below I illustrate this by using the local variable 'pos' to reflect the trading position. When I run it enters a long trade but it never exits (where I expect it to exit as the short signal will trigger an exit).
Been staring at this for days but do not see the flaw. Any tips?
ps. I am using latest 6.35.0
SetOption("FuturesMode", True); // Use MarginDeposit and PointValue in calculations
SetOption("InitialEquity", 100000 ); // Initial equity $
SetOption("AccountMargin", 100); // Account margin, 100 = no margin
SetOption("MinShares", 1); // Min number shares one
SetOption("MinPosValue", 0); // Min position value to make trade worthwhile, 0 = no limit
SetTradeDelays(0,0,0,0); // Trade delays applied by the backtester
contracts = 1; // contracts to acquire
SetPositionSize( contracts, spsShares ); // spsShares = size expressed in shares/contracts
//--- SIGNAL CODE ---//
EntryCondTime = TimeNum()==083500;
EntryLong = EntryCondTime AND L>OpenD ;
EntryShort = EntryCondTime AND H<OpenD;
//--- BUY|SELL|SHORT|COVER CODE ---///
//--- LOOPING CODE ---///
pos = 0;
shortexit = 0;
for( i=1; i<BarCount; i++ )
if ( pos >0)
if(EntryShort[i]>0 AND pos == 0)
tpprice = ShortPrice[i];
Very much appreciated. I missed this reset and that solved the first piece. I am now adding more conditions and the code gets stuck again after adding the condition that "C[i] < tpprice". After adding this, the code again does not executes the cover statement. See below the new code that does not run....again looks I am blind for my own mistakes
You do not say where you excute the code. I suppose you execute it in analysis.
So you have to check interval (Periodicity) there. You are entering at exact timenum value since you use equality check operator ( == ). So if timenum value is not to be found in timenum array then there won't be an entry.
Secondly you should enable Short in analysis.
FYI, I do get Cover exit on my end.
Rather use ApplyStop function. You do not need looping for profit target stop.
I have those settings ok I and run it at 5m periodicity.
If I remove the C[i]<tpprice condition it runs with covers with the condition it doesn't work at my end...
Why am I not using Applystop?
The bigger picture is that I want to simulate following condition TP:
a) Short or Long entry is under different entry condition at @8:30 in the morning
b) As of 11am I am enabling a profit target for SHORTS ONLY
c) Next day, in case the SHORT position is not closed or we are already in LONG position, we will not enter.
Do you think we can do this with Applystop? or alternative IIF conditions?
I don't see why you couldn't do your a), b), c) via Applystop().
As for b)... see valid from/to setting or you may use volatile = 1 and amount being an IIf() array and IIf() expression being TimeNum() >= 110000.