Sorry for my delay..
I've been busier than a --- well - don't know what..
Anyway...
Stops are relatively easy... Here is some sample code..
This routine accomplishes the following...
1. Test for Entries
2. Sets initial stop levels
3. Adjusts stop levels
4. Tests for Stop Exits (and handles unexpected conditions for stops - such as gaps thru stop levels).
Notice the structure of the code..
1. Test for stop exits (if we are in a trade).
2. Test for new entry conditions (if we are NOT in a trade).
3. Adjust stop levels (if we are in a trade).
Now, you might set some other conditions - such as..
a. When to adjust the stop - maybe after a profit target is hit or a certain number of bars have passed since entry.
b. filter in some additional technical indicators for the ENTRY SIGNALS.
c. vary the stop system to include market volatility.
Hope this helps.
PS, the example code in my FILESHARE group works, but as I have become more experienced with EFS, I've changed the way I handle certain things. Practice makes perfect - eh??
Good luck.
B
I've been busier than a --- well - don't know what..
Anyway...
Stops are relatively easy... Here is some sample code..
PHP Code:
var nStopPrice = 0;
var StopOffset = 2.0;
function main()
{
//--------------------------------------------------------------------
// Test for Stop Exit Conditions
//--------------------------------------------------------------------
if (Strategy.isShort()) {
if (high() >= nStopPrice) // stop hit
if (low() > nStopPrice) {
Strategy.doCover("Short Stop Hit", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, open());
} else {
Strategy.doCover("Short Stop Hit", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nStopPrice);
}
}
}
if (Strategy.isLong()) {
if (low() <= nStopPrice) // stop hit
if (high() < nStopPrice) {
Strategy.doSell("Long Stop Hit", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, open());
} else {
Strategy.doSell("Long Stop Hit", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nStopPrice);
}
}
}
//--------------------------------------------------------------------
// END - Test for Stop Exit Conditions
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Test for ENTRY Conditions here - sample entry conditions included
//--------------------------------------------------------------------
if (!Strategy.isInTrade()) {
if (close() > high(-1)) {
Strategy.doLong("Long Entry", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
nStopPrice = high(-1) - StopOffset;
}
if (close() < low(-1)) {
Strategy.doShort("Short Entry", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
nStopPrice = low(-1) + StopOffset;
}
}
//--------------------------------------------------------------------
// Adjust Stop Levels Here...
//--------------------------------------------------------------------
if (Strategy.isLong()) {
nStopPrice = Math.max(high(-1),high(-2),high(-3),high(-4))-StopOffset;
}
if (Strategy.isShort()) {
nStopPrice = Math.min(low(-1),low(-2),low(-3),low(-4))+StopOffset;
}
}// end of main()
1. Test for Entries
2. Sets initial stop levels
3. Adjusts stop levels
4. Tests for Stop Exits (and handles unexpected conditions for stops - such as gaps thru stop levels).
Notice the structure of the code..
1. Test for stop exits (if we are in a trade).
2. Test for new entry conditions (if we are NOT in a trade).
3. Adjust stop levels (if we are in a trade).
Now, you might set some other conditions - such as..
a. When to adjust the stop - maybe after a profit target is hit or a certain number of bars have passed since entry.
b. filter in some additional technical indicators for the ENTRY SIGNALS.
c. vary the stop system to include market volatility.
Hope this helps.
PS, the example code in my FILESHARE group works, but as I have become more experienced with EFS, I've changed the way I handle certain things. Practice makes perfect - eh??
Good luck.
B
Comment