Announcement

Collapse
No announcement yet.

more code help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • more code help

    Again, I am new to EFS studies, the last posting helped me immensly with understanding EFS and creating my strategy, below I have include more code which includes the code offered by members from the previous posting and al lot of code taken from parts of the tutorial to accomplish my goal. The strategy is rather simple, to buy or sell at the break of a 9 am high or low after 9 and before 11, with a 2.5 trailing stop and a 2 point profit objective... I am doing this to learn EFS and the funcionality of the back testing with stops and price objectives. This program makes it through synatex check, and the chart, however when I run it in the backtester, no trades are executed. I am testing this on emini s&p's march 03... if anyone has any suggestions please post.. Thank you.

    function preMain() {
    setStudyTitle("9:00 High/Low");
    setCursorLabelName("9:00 High", 0);
    setCursorLabelName("9:00 Low", 1);
    setPriceStudy(true);
    //setStudyMax(10);
    //setStudyMin(0);
    }
    // global variable defs

    var vFlag = true;
    var vHigh = null;
    var vLow = null;
    var vDay1 = null;
    var vDay2 = null;
    var nSignal;
    var nNewTrade;
    var lEntry = null;
    var sEntry = null;
    var ProfitTarget1 = 2.0;
    var nTradeEntryPrice;
    var nStopLevel;
    var hStop = null;
    var lStop = null;

    function main() {
    nNewTrade = 0;
    var nState = getBarState();


    if (nState == BARSTATE_NEWBAR) {
    if (vDay1 == null) {
    vDay2 = getDay();
    } else {
    vDay2 = vDay1;
    }
    vDay1 = getDay();
    if (vDay1 != vDay2) {
    vHigh = null;
    vLow = null;
    vFlag = true;
    }
    var vHour = getHour();
    if (vHour >= 9) {
    vFlag = false;
    }
    }

    if (vFlag == true) {
    if (vHigh == null) {
    vHigh = high();
    }
    if (vLow == null) {
    vLow = low();
    }
    vHigh = Math.max(high(), vHigh); //calc pretime high
    vLow = Math.min(low(), vLow); //calc pretime low
    lEntry = (vHigh + .25); //calc long limit entry price high + 1 tick
    sEntry = (vLow -.25); // calc short entry limit price low -1 tick
    }
    var vHour = getHour();
    if ((vHour >= 9) && (vHour < 11)) { //begins the 9 to 11 trade entry testing

    // Test for Profit Target Breach (ProfitTarget1)


    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 Cover order.
    Strategy.doSell("Long PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
    }
    } //End profit test

    //Test for a stop breach
    //on short

    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);
    }
    }
    //on Long

    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);
    }
    }

    //Calculate new Stop Levels
    //short

    if ((Strategy.isInTrade() == true) && (Strategy.isShort() == true)) {
    nStopLevel = (Math.min(low(),lStop) + 2.50);
    }

    //long
    if ((Strategy.isInTrade() == true) && (Strategy.isLong() == true)) {
    nStopLevel = (Math.max(high(),hStop) - 2.50);
    }

    // Identify new trades

    if(Strategy.isInTrade() == false){

    //test for buy signal
    if(vHigh < high()){
    nNewTrade = 1; //New Trade
    nsignal = 1; // buy signal
    drawTextRelative(-2, lEntry, "Buy Placed @ " + lEntry, Color.black, Color.red, Text.FRAME | Text.ONTOP | Text.BOLD | Text.RIGHT | Text.TOP, null, null, "OS");
    }

    //Test for sell signal
    if(vLow > low()){
    nNewTrade = 1;
    nsignal = -1;
    drawTextRelative(-2, sEntry, "Sell Placed @ " + sEntry, Color.black, Color.red, Text.FRAME | Text.ONTOP | Text.BOLD | Text.RIGHT | Text.TOP, null, null, "OS");
    }
    } //End trade signal ID

    //Execute Trade if signal has been generated
    if(nNewTrade == 1){

    //long trade
    if(nsignal > 0){
    Strategy.dolong("Go Long", Strategy.LIMIT, Strategy.THISBAR, Strategy.getDefaultLotSize(), lEntry);
    nTradeEntryPrice = lEntry;
    }

    //Short Trade
    if(nsignal < 0){
    Strategy.doshort("Go Short", Strategy.LIMIT, Strategy.THISBAR, Strategy.getDefaultLotSize(), sEntry);
    nTradeEntryPrice = sEntry;
    }
    } //End trade execution

    } //End trade between 9 and 11

    drawTextRelative(-2, vHigh, "High is " + vHigh + " Low is " + vLow, Color.black, Color.red, Text.FRAME | Text.ONTOP | Text.BOLD | Text.RIGHT | Text.TOP, null, null, "OS");
    return new Array(vHigh, vLow, lEntry, sEntry);
    }
Working...
X