The following code gives me a 10 point profit in backtesting every trade. Apparently from the ProfitTarget section. However, the chart seems to print blue bars until the nStopLevel is triggered. I had thought that the
nStopLevel = low(-1) would be a number that didn't change. It seems like the chart is treating the low(=1) like a trailing stop rather than a fixed stop. But the backtester is only interested in the ProfitTarget exit. Now when I deleted the ProfitTarget section and implemented the Trailing Stop section (between the /* */'s), all the bars are simply grey. Except for one blue bar at the beginning of the bars on the chart (far left). Wow! This gets complicated!
So here are my questions:
1) Why doesn't the Trailing Stop section print the blue bars the way the ProfitTarget section does?
2) Why does the Backtester not pick up on the way the nStopLevel is operating in the same manner that the blue bars do as the code now stands?
3) How do I get the Trailing Stop section to print blue bars?
4) How do I get the Backtester to pay attention to the Trailing Stop section? (I just realized I haven't back tested the code with the Trailing Stop section in place.)
5) Would you appreciate my code as an attachment?
Again, thanks for your help!
nStopLevel = low(-1) would be a number that didn't change. It seems like the chart is treating the low(=1) like a trailing stop rather than a fixed stop. But the backtester is only interested in the ProfitTarget exit. Now when I deleted the ProfitTarget section and implemented the Trailing Stop section (between the /* */'s), all the bars are simply grey. Except for one blue bar at the beginning of the bars on the chart (far left). Wow! This gets complicated!
So here are my questions:
1) Why doesn't the Trailing Stop section print the blue bars the way the ProfitTarget section does?
2) Why does the Backtester not pick up on the way the nStopLevel is operating in the same manner that the blue bars do as the code now stands?
3) How do I get the Trailing Stop section to print blue bars?
4) How do I get the Backtester to pay attention to the Trailing Stop section? (I just realized I haven't back tested the code with the Trailing Stop section in place.)
5) Would you appreciate my code as an attachment?
Again, thanks for your help!
PHP Code:
/*------------------
//PB7 is Longs Only,
//2 Bar Breakout and Sound Alert as Preparation for Entry.
//Trailing Stop at high(-1)
------------------*/
var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
var nsignal; // returns the direction of the trading signal
var nTradeEntryPrice;
var nStopLevel;
var ProfitTarget1 = 10.0;
var ProfitTargetTrigger1;
function preMain() {
setPriceStudy(true);
setStudyTitle("PB7");
setCursorLabelName("PB7");
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.isLong() == true) {
// Check if the profit target has been reached/breached
if (low() <= nStopLevel) {
// Stop Breached, Execute Sell order.
Strategy.doSell("Stop Loss Exit", Strategy.STOP, Strategy.THISBAR, Strategy.ALL,nStopLevel);
setPriceBarColor(Color.red);
}
}
/*----------------------------------------------------------------
//New Profit exit
// Test for Profit Target Breach (ProfitTarget1)
----------------------------------------------------------
This portion of the code identifies if our profit target has
been reached and exits our trades.
----------------------------------------------------------*/
if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
// Check if the profit target has been reached/breached
if ((high() >= low(-1)) && ProfitTargetTrigger1 != 1) {
// Profit Target Breached, Execute Sell order.
Strategy.doSell("PT1 Exit", Strategy.STOP, Strategy.THISBAR,
Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
}
}
/*------------------------------------------------------------------
This section of code establishes a Trailing Stop and exits at h(-1).
-------------------------------------------------------------------*/
/* if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
//Move nStopLevel to high(=1)
nStopLevel = high(-1);
//Establish Exit at breach of nStopLevel
if (nStopLevel > low() + 0.25) {
Strategy.doSell("Stop Loss Exit", Strategy.STOP, Strategy.THISBAR,
Strategy.getDefaultLotSize(), (nStopLevel));
setPriceBarColor(Color.yellow);
}
}
*/
/*----------------------------------------------------------------
// Identify new trade signals
----------------------------------------------------------------- */
if (Strategy.isInTrade() == false) {
if (close(-1) > high(-2) && close(-2) > high(-3)) {
//Removed && close(-3) > high(-4)
nNewTrade = 1; // New Trade Trigger
nsignal = 1; // Buy Signal - Trade Type
}
}
/*---------------------------------------------------------------
// Execute Trades ONLY if nNewTrade is triggered ....
----------------------------------------------------------------- */
if (Strategy.isInTrade() == false) {
if (nNewTrade == 1) { //Execute New Trade
//if (nsignal > 0) {
if (nsignal == 1) {
Strategy.doLong("Go Long", Strategy.MARKET, Strategy.THISBAR,Strategy.getDefaultLotSize() );
//Removed *3 at end of .getDefaultLotSize ()
nStopLevel = low(-1) - 5.0;
/*--------------------------------------------------------------
//Colors Entry Bar Blue
---------------------------------------------------------------*/
if (Strategy.isInTrade() == true) {
if (Strategy.isLong() == true) {
setPriceBarColor(Color.blue); //ADDED
}
}
} // end nSignal
} // end if IN TRADE
} // END EXECUTE NEW TRADE
}
Comment