I want to express my appreciation for the mentoring qualities of this forum. Thanks a bunch!
I'm trying to get my exits to perform. Currently I'm just trying to get a stop loss to function. My general question is: why doesn't the back test honor my stop losses. My specific question is: Does the nStopLevel at lines 98, 103, 110 and 115 fix a specific number that will be used to exit the trade at lines 44 and 55 or does that number keep changing with each succeeding bar?
A second area of questions has to do with coloring the bars of the trades. Do I need the return to grey command at lines 46 and 58? Or if I get the exits to perform will the program automatically return to the default grey from the premain settings?
Here's the formula I'm currently working with:
/*-------------------------------------------------------------------------
PB4 is a Point Break Strategy based on PB3 with Strategy.isInTrade == False added and return to grey added
--------------------------------------------------------------------------*/
var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
var nsignal; // returns the direction of the trading signal
var nTradeEntryPrice;
var nStopLevel;
function preMain() {
setPriceStudy(true);
setStudyTitle("PB4");
setCursorLabelName("PB4");
setColorPriceBars(true); //ADDED
setDefaultPriceBarColor(Color.grey); //ADDED
}
function main() {
/*----------------------------------------------------------------
// If new trade, get entry price - used for our profit target
----------------------------------------------------------
This portion of the code identifies if a new trade has been issued
and records the entry price of our trade. If no new trade has been
triggered (nNewTrade == 1), then this portion of the code is ignored.
----------------------------------------------------------*/
if (Strategy.isInTrade() == true && (nNewTrade == 1)) {
// This sets the expected entry price of the current short trade
nTradeEntryPrice = open();
// This switches off the nNewTrade variable
nNewTrade = 0; // Turn off NEW TRADE switch
}
/*-----------------------------------------------
This portion of the code tests for a stop level breach and
executes trades accordingly.
----------------------------------------------------------*/
if (Strategy.isInTrade() == true && Strategy.isShort() == true) {
// Check if the profit target has been reached/breached
if (high() >= nStopLevel) {
// Stop Breached, Execute Cover order.
Strategy.doCover("Short Stop Exit", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, nStopLevel);
if (Strategy.isInTrade() == false) { // Reset grey color bars
setPriceBarColor(Color.grey);
}
}
}
if (Strategy.isInTrade() == true && Strategy.isLong() == true) {
// Check if the profit target has been reached/breached
if (low() <= nStopLevel) {
// Stop Breached, Execute Sell order.
Strategy.doSell("Long Stop Exit", Strategy.STOP, Strategy.THISBAR, Strategy.ALL,nStopLevel);
if (Strategy.isInTrade() == false) { //Reset grey color bars
setPriceBarColor(Color.grey);
}
}
}
/*----------------------------------------------------------------
// Identify new trade signals
----------------------------------------------------------------- */
if (Strategy.isInTrade() == false) {
if (close(-1) < low(-2) && close(-2) < low(-3) && close(-3) < low(-4) && close() > high (-3)) {
nNewTrade = 1; // New Trade Trigger
nsignal = 1; // Buy Signal - Trade Type
}
if ( close(-1) > high(-2) && close(-2) > high(-3) && close(-3) > high(-4) && close() < low(-3)) {
nNewTrade = 1; // New Trade Trigger
nsignal = -1; // Sell Signal - Trade Type
}
}
if (Strategy.isInTrade() == true) { //ADDED
if (Strategy.isLong() == true) {
setPriceBarColor(Color.lime); //ADDED
}
if (Strategy.isShort() == true) {
setPriceBarColor(Color.red); //ADDED
}
}
/*----------------------------------------------------------------
// Execute Trades ONLY if nNewTrade is triggered ....
----------------------------------------------------------------- */
if (nNewTrade == 1) { //Execute New Trade
// new or reversed trade position
if (Strategy.isInTrade() == true) {
if ((nsignal > 0) && Strategy.isShort() == true) {
Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.ALL);
Strategy.doLong("Rev Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
nStopLevel = low(-1) - 5.0;
//lowest = low(-1); has been removed
}
if (nsignal < 0 && Strategy.isLong() == true) {
Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.ALL);
Strategy.doShort("Rev Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
nStopLevel = high(-1) + 5.0;
//highest = high(-1); has been removed
}
} else {
if (Strategy.isInTrade() == false) {
if (nsignal > 0) {
Strategy.doLong("Go Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
nStopLevel = low(-1) - 5.0;
//lowest = low(-1); has been removed
}
if (nsignal < 0)
Strategy.doShort("Go Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() *3);
nStopLevel = high(-1) + 5.0;
//highest = high(-1); has been removed
}
} // end if IN TRADE
} // END EXECUTE NEW TRADE
}
I'm trying to get my exits to perform. Currently I'm just trying to get a stop loss to function. My general question is: why doesn't the back test honor my stop losses. My specific question is: Does the nStopLevel at lines 98, 103, 110 and 115 fix a specific number that will be used to exit the trade at lines 44 and 55 or does that number keep changing with each succeeding bar?
A second area of questions has to do with coloring the bars of the trades. Do I need the return to grey command at lines 46 and 58? Or if I get the exits to perform will the program automatically return to the default grey from the premain settings?
Here's the formula I'm currently working with:
/*-------------------------------------------------------------------------
PB4 is a Point Break Strategy based on PB3 with Strategy.isInTrade == False added and return to grey added
--------------------------------------------------------------------------*/
var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
var nsignal; // returns the direction of the trading signal
var nTradeEntryPrice;
var nStopLevel;
function preMain() {
setPriceStudy(true);
setStudyTitle("PB4");
setCursorLabelName("PB4");
setColorPriceBars(true); //ADDED
setDefaultPriceBarColor(Color.grey); //ADDED
}
function main() {
/*----------------------------------------------------------------
// If new trade, get entry price - used for our profit target
----------------------------------------------------------
This portion of the code identifies if a new trade has been issued
and records the entry price of our trade. If no new trade has been
triggered (nNewTrade == 1), then this portion of the code is ignored.
----------------------------------------------------------*/
if (Strategy.isInTrade() == true && (nNewTrade == 1)) {
// This sets the expected entry price of the current short trade
nTradeEntryPrice = open();
// This switches off the nNewTrade variable
nNewTrade = 0; // Turn off NEW TRADE switch
}
/*-----------------------------------------------
This portion of the code tests for a stop level breach and
executes trades accordingly.
----------------------------------------------------------*/
if (Strategy.isInTrade() == true && Strategy.isShort() == true) {
// Check if the profit target has been reached/breached
if (high() >= nStopLevel) {
// Stop Breached, Execute Cover order.
Strategy.doCover("Short Stop Exit", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, nStopLevel);
if (Strategy.isInTrade() == false) { // Reset grey color bars
setPriceBarColor(Color.grey);
}
}
}
if (Strategy.isInTrade() == true && Strategy.isLong() == true) {
// Check if the profit target has been reached/breached
if (low() <= nStopLevel) {
// Stop Breached, Execute Sell order.
Strategy.doSell("Long Stop Exit", Strategy.STOP, Strategy.THISBAR, Strategy.ALL,nStopLevel);
if (Strategy.isInTrade() == false) { //Reset grey color bars
setPriceBarColor(Color.grey);
}
}
}
/*----------------------------------------------------------------
// Identify new trade signals
----------------------------------------------------------------- */
if (Strategy.isInTrade() == false) {
if (close(-1) < low(-2) && close(-2) < low(-3) && close(-3) < low(-4) && close() > high (-3)) {
nNewTrade = 1; // New Trade Trigger
nsignal = 1; // Buy Signal - Trade Type
}
if ( close(-1) > high(-2) && close(-2) > high(-3) && close(-3) > high(-4) && close() < low(-3)) {
nNewTrade = 1; // New Trade Trigger
nsignal = -1; // Sell Signal - Trade Type
}
}
if (Strategy.isInTrade() == true) { //ADDED
if (Strategy.isLong() == true) {
setPriceBarColor(Color.lime); //ADDED
}
if (Strategy.isShort() == true) {
setPriceBarColor(Color.red); //ADDED
}
}
/*----------------------------------------------------------------
// Execute Trades ONLY if nNewTrade is triggered ....
----------------------------------------------------------------- */
if (nNewTrade == 1) { //Execute New Trade
// new or reversed trade position
if (Strategy.isInTrade() == true) {
if ((nsignal > 0) && Strategy.isShort() == true) {
Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.ALL);
Strategy.doLong("Rev Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
nStopLevel = low(-1) - 5.0;
//lowest = low(-1); has been removed
}
if (nsignal < 0 && Strategy.isLong() == true) {
Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.ALL);
Strategy.doShort("Rev Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
nStopLevel = high(-1) + 5.0;
//highest = high(-1); has been removed
}
} else {
if (Strategy.isInTrade() == false) {
if (nsignal > 0) {
Strategy.doLong("Go Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
nStopLevel = low(-1) - 5.0;
//lowest = low(-1); has been removed
}
if (nsignal < 0)
Strategy.doShort("Go Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() *3);
nStopLevel = high(-1) + 5.0;
//highest = high(-1); has been removed
}
} // end if IN TRADE
} // END EXECUTE NEW TRADE
}
Comment