Hello everyone,
This is the first time I have posted a thread here so forgive me if I am not following etiquette.
Anyway, I copied and pasted the code below, taken from the EFS database. It is designed to set trailing stops, however I keep on getting a message informing me that there is a syntax error with the following code :
if (Strategy.isInTrade() == true) && (nNewTrade == 1)) {
The full code is as follows:
*----------------------------------------------------------
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
}
If one of you programming gurus could help me I would be very appreciative.
Cheers
Carlton
This is the first time I have posted a thread here so forgive me if I am not following etiquette.
Anyway, I copied and pasted the code below, taken from the EFS database. It is designed to set trailing stops, however I keep on getting a message informing me that there is a syntax error with the following code :
if (Strategy.isInTrade() == true) && (nNewTrade == 1)) {
The full code is as follows:
*----------------------------------------------------------
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
}
If one of you programming gurus could help me I would be very appreciative.
Cheers
Carlton
Comment