Hello
I have copied and pastet the 'Sample Profit Target/Trailing Stop Code' for the efs library, however I'm unable to get it to work. I keep on getting a the following syntax error.
if (Strategy.isInTrade() == true) && (nNewTrade == 1)) {
I do believe this was originally written by Brad Matheny.
The full code is outlined below. I have already sent out a request asking if any of you kind people would help me get it to work but no one seems to either know or may be just too busy to help out so I thought I would repost.
Once again I would really appreciate any help I can.
Cheers
Carlton
/*----------------------------------------------------------
Sample Profit Target/Trailing Stop Code
----------------------------------------------------------
Declare new global variables for the code. Notice we are
Only adding one new variables to handle the stop loss
Function – nStopLevel.
nStopLevel = our stop/trailing stop level.
----------------------------------------------------------*/
var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
var nsignal; // returns the direction of the trading signal
var nTradeEntryPrice;
var ProfitTarget1 = 5.0;
var nStopLevel;
function premain() {
setPriceStudy(true);
setStudyTitle("Sample Profit Code");
setCursorLabelName("PT CODE");
}
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
}
/*----------------------------------------------------------------
// 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.isShort() == true)) {
// Check if the profit target has been reached/breached
if (low() <= (nTradeEntryPrice - ProfitTarget1)) {
// Profit Target Breached… Execute Cover order.
Strategy.doCover("Short PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice – ProfitTarget1));
}
}
if (Strategy.isInTrade() == true) && (Strategy.isLong() == true)) {
// Check if the profit target has been reached/breached
if (high() >= (nTradeEntryPrice + ProfitTarget1)) {
// Profit Target Breached… Execute Sell order.
Strategy.doSell("Long PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
}
}
/*----------------------------------------------------------
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.getDefaultLotSize(), nStopLevel);
}
}
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.getDefaultLotSize(),nStopLevel);
}
}
/*----------------------------------------------------------
This portion of the code calculates and sets the new stop levels.
----------------------------------------------------------*/
if (Strategy.isInTrade() == true) && (Strategy.isShort() == true)) {
nStopLevel = high(-1) + 0.25;
}
if (Strategy.isInTrade() == true) && (Strategy.isLong() == true)) {
nStopLevel = low(-1) - 0.25;
}
/*----------------------------------------------------------------
// Identify new trade signals…
----------------------------------------------------------------- */
if (Strategy.isInTrade() == false) {
if (/*Buy Condition*/)) {
nNewTrade = 1; // New Trade Trigger
nsignal = 1; // Buy Signal – Trade Type
}
if (/*Sell Condition*/) {
nNewTrade = 1; // New Trade Trigger
nsignal = -1; // Sell Signal – Trade Type
}
}
/*----------------------------------------------------------------
// 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.doLong("Rev Long", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = low(-1) - 0.25;
}
if ((nsignal < 0) && (Strategy.isLong() == true)) {
Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.NEXTBAR);
Strategy.doShort("Rev Short", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = high(-1) + 0.25;
}
} else {
if ((nsignal > 0)) {
Strategy.doLong("Go Long", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = low(-1) - 0.25;
}
if ((nsignal < 0)) {
Strategy.doShort("Go Short", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = high(-1) + 0.25;
}
} // end if IN TRADE
} // END EXECUTE NEW TRADE
}
Again thanks in advance.
I have copied and pastet the 'Sample Profit Target/Trailing Stop Code' for the efs library, however I'm unable to get it to work. I keep on getting a the following syntax error.
if (Strategy.isInTrade() == true) && (nNewTrade == 1)) {
I do believe this was originally written by Brad Matheny.
The full code is outlined below. I have already sent out a request asking if any of you kind people would help me get it to work but no one seems to either know or may be just too busy to help out so I thought I would repost.
Once again I would really appreciate any help I can.
Cheers
Carlton
/*----------------------------------------------------------
Sample Profit Target/Trailing Stop Code
----------------------------------------------------------
Declare new global variables for the code. Notice we are
Only adding one new variables to handle the stop loss
Function – nStopLevel.
nStopLevel = our stop/trailing stop level.
----------------------------------------------------------*/
var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
var nsignal; // returns the direction of the trading signal
var nTradeEntryPrice;
var ProfitTarget1 = 5.0;
var nStopLevel;
function premain() {
setPriceStudy(true);
setStudyTitle("Sample Profit Code");
setCursorLabelName("PT CODE");
}
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
}
/*----------------------------------------------------------------
// 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.isShort() == true)) {
// Check if the profit target has been reached/breached
if (low() <= (nTradeEntryPrice - ProfitTarget1)) {
// Profit Target Breached… Execute Cover order.
Strategy.doCover("Short PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice – ProfitTarget1));
}
}
if (Strategy.isInTrade() == true) && (Strategy.isLong() == true)) {
// Check if the profit target has been reached/breached
if (high() >= (nTradeEntryPrice + ProfitTarget1)) {
// Profit Target Breached… Execute Sell order.
Strategy.doSell("Long PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
}
}
/*----------------------------------------------------------
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.getDefaultLotSize(), nStopLevel);
}
}
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.getDefaultLotSize(),nStopLevel);
}
}
/*----------------------------------------------------------
This portion of the code calculates and sets the new stop levels.
----------------------------------------------------------*/
if (Strategy.isInTrade() == true) && (Strategy.isShort() == true)) {
nStopLevel = high(-1) + 0.25;
}
if (Strategy.isInTrade() == true) && (Strategy.isLong() == true)) {
nStopLevel = low(-1) - 0.25;
}
/*----------------------------------------------------------------
// Identify new trade signals…
----------------------------------------------------------------- */
if (Strategy.isInTrade() == false) {
if (/*Buy Condition*/)) {
nNewTrade = 1; // New Trade Trigger
nsignal = 1; // Buy Signal – Trade Type
}
if (/*Sell Condition*/) {
nNewTrade = 1; // New Trade Trigger
nsignal = -1; // Sell Signal – Trade Type
}
}
/*----------------------------------------------------------------
// 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.doLong("Rev Long", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = low(-1) - 0.25;
}
if ((nsignal < 0) && (Strategy.isLong() == true)) {
Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.NEXTBAR);
Strategy.doShort("Rev Short", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = high(-1) + 0.25;
}
} else {
if ((nsignal > 0)) {
Strategy.doLong("Go Long", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = low(-1) - 0.25;
}
if ((nsignal < 0)) {
Strategy.doShort("Go Short", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = high(-1) + 0.25;
}
} // end if IN TRADE
} // END EXECUTE NEW TRADE
}
Again thanks in advance.
Comment