Announcement

Collapse
No announcement yet.

Strategy Analyser not exiting positions

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

  • Strategy Analyser not exiting positions

    Hi,

    I've tried to write a code for mixing the ADX and MACD. I want to get long on a cross up of the MACD with ADX > 30 and cover if a bear cross of MACD and ADX < 30.

    However, if there is a MACD bear cross and the ADX is still over 30, I want to close long and open a short.

    If short, then I want to cover when a bull cross of MACD happens with ADX below 30.

    I think the logic of the code is alright but the analyser wont cover shorts and sell longs when it runs.

    Any help would be much appreciated.


    var vMACD12_26 = new MACDStudy(12, 26, 9, "Close", false);
    var vADXDM14_14 = new ADXDMStudy(14, 14);
    var vLastAlert = -1;



    function preMain() {

    setPriceStudy(true);
    setStudyTitle("MACD / ADX");
    setDefaultPriceBarColor(Color.black);



    }

    function main() {


    if(vADXDM14_14.getValue(ADXDMStudy.ADX) > 30 && //IS THE ADX ABOVE 30?
    vMACD12_26.getValue(MACDStudy.HIST) > 0 && //DID MACD GO POSITIVE?
    vMACD12_26.getValue(MACDStudy.HIST, -1) < 0 //IF SATISFIED:
    ) onAction1(); //IF NOT LONG, GO LONG


    if (
    vADXDM14_14.getValue(ADXDMStudy.ADX) < 30 && //IS THE ADX BELOW 30?
    vMACD12_26.getValue(MACDStudy.HIST) < 0 && //DID MACD GO NEGATIVE?
    vMACD12_26.getValue(MACDStudy.HIST, -1) > 0 //IF SATISFIED:
    ) onAction2(); //IF LONG , SELL LONG POSITION

    if (
    vADXDM14_14.getValue(ADXDMStudy.ADX) > 30 && //IS THE ADX ABOVE 30?
    vMACD12_26.getValue(MACDStudy.HIST) < 0 && //DID MACD GO NEGATIVE?
    vMACD12_26.getValue(MACDStudy.HIST, -1) > 0 //IF SATISFIED:
    ) onAction3(); //IF NOT SHORT, GO SHORT

    if(
    vADXDM14_14.getValue(ADXDMStudy.ADX) < 30 && //IS THE ADX BELOW 30?
    vMACD12_26.getValue(MACDStudy.HIST) > 0 && //DID THE MACD GO POSITIVE?
    vMACD12_26.getValue(MACDStudy.HIST, -1) < 0 //IF SATISFIED:
    ) onAction4(); //IF SHORT , COVER SHORT POSITION





    return null;


    }

    function postMain() {

    }


    function onAction1() {
    if (Strategy.isLong() == false) {
    Strategy.doLong("",Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, low() - (low()-low()*0.995), Shape.TRIANGLE, "", Color.RGB(0,0,255), Shape.LEFT);
    setPriceBarColor(Color.green);
    vLastAlert = 1;
    }}



    function onAction2() {
    if (Strategy.isLong == true) {
    Strategy.doSell("", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, high() - (high()-high()*0.995), Shape.TRIANGLE, "", Color.RGB(255,0,0), Shape.LEFT);
    setPriceBarColor(Color.black);
    vLastAlert = 2;
    }}


    function onAction3() {
    if (Strategy.isShort() == false) {
    Strategy.doShort("", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, high() - (high()-high()*0.995), Shape.CIRCLE, "", Color.RGB(0,0,255), Shape.LEFT);
    setPriceBarColor(Color.red);
    vLastAlert = 3;
    }}

    function onAction4() {
    if (Strategy.isShort == true) {
    Strategy.doCover("", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, low() - (low()-low()*0.995), Shape.CIRCLE, "", Color.RGB(255,0,0), Shape.LEFT);
    setPriceBarColor(Color.black);
    vLastAlert = 4;
    }}

  • #2
    Re: Strategy Analyser not exiting positions

    DolmenProp
    You omitted to include the () parenthesis in the conditions that check if Strategy.isLong and Strategy.isShort in the onAction2 and onAction4 functions
    Alex


    Originally posted by DolmenProp
    Hi,

    I've tried to write a code for mixing the ADX and MACD. I want to get long on a cross up of the MACD with ADX > 30 and cover if a bear cross of MACD and ADX < 30.

    However, if there is a MACD bear cross and the ADX is still over 30, I want to close long and open a short.

    If short, then I want to cover when a bull cross of MACD happens with ADX below 30.

    I think the logic of the code is alright but the analyser wont cover shorts and sell longs when it runs.

    Any help would be much appreciated.


    var vMACD12_26 = new MACDStudy(12, 26, 9, "Close", false);
    var vADXDM14_14 = new ADXDMStudy(14, 14);
    var vLastAlert = -1;



    function preMain() {

    setPriceStudy(true);
    setStudyTitle("MACD / ADX");
    setDefaultPriceBarColor(Color.black);



    }

    function main() {


    if(vADXDM14_14.getValue(ADXDMStudy.ADX) > 30 && //IS THE ADX ABOVE 30?
    vMACD12_26.getValue(MACDStudy.HIST) > 0 && //DID MACD GO POSITIVE?
    vMACD12_26.getValue(MACDStudy.HIST, -1) < 0 //IF SATISFIED:
    ) onAction1(); //IF NOT LONG, GO LONG


    if (
    vADXDM14_14.getValue(ADXDMStudy.ADX) < 30 && //IS THE ADX BELOW 30?
    vMACD12_26.getValue(MACDStudy.HIST) < 0 && //DID MACD GO NEGATIVE?
    vMACD12_26.getValue(MACDStudy.HIST, -1) > 0 //IF SATISFIED:
    ) onAction2(); //IF LONG , SELL LONG POSITION

    if (
    vADXDM14_14.getValue(ADXDMStudy.ADX) > 30 && //IS THE ADX ABOVE 30?
    vMACD12_26.getValue(MACDStudy.HIST) < 0 && //DID MACD GO NEGATIVE?
    vMACD12_26.getValue(MACDStudy.HIST, -1) > 0 //IF SATISFIED:
    ) onAction3(); //IF NOT SHORT, GO SHORT

    if(
    vADXDM14_14.getValue(ADXDMStudy.ADX) < 30 && //IS THE ADX BELOW 30?
    vMACD12_26.getValue(MACDStudy.HIST) > 0 && //DID THE MACD GO POSITIVE?
    vMACD12_26.getValue(MACDStudy.HIST, -1) < 0 //IF SATISFIED:
    ) onAction4(); //IF SHORT , COVER SHORT POSITION





    return null;


    }

    function postMain() {

    }


    function onAction1() {
    if (Strategy.isLong() == false) {
    Strategy.doLong("",Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, low() - (low()-low()*0.995), Shape.TRIANGLE, "", Color.RGB(0,0,255), Shape.LEFT);
    setPriceBarColor(Color.green);
    vLastAlert = 1;
    }}



    function onAction2() {
    if (Strategy.isLong == true) {
    Strategy.doSell("", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, high() - (high()-high()*0.995), Shape.TRIANGLE, "", Color.RGB(255,0,0), Shape.LEFT);
    setPriceBarColor(Color.black);
    vLastAlert = 2;
    }}


    function onAction3() {
    if (Strategy.isShort() == false) {
    Strategy.doShort("", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, high() - (high()-high()*0.995), Shape.CIRCLE, "", Color.RGB(0,0,255), Shape.LEFT);
    setPriceBarColor(Color.red);
    vLastAlert = 3;
    }}

    function onAction4() {
    if (Strategy.isShort == true) {
    Strategy.doCover("", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, low() - (low()-low()*0.995), Shape.CIRCLE, "", Color.RGB(255,0,0), Shape.LEFT);
    setPriceBarColor(Color.black);
    vLastAlert = 4;
    }}

    Comment


    • #3
      Yes indeed i did, thank you for the help

      Comment


      • #4
        DolmenProp
        You are welcome
        Alex


        Originally posted by DolmenProp
        Yes indeed i did, thank you for the help

        Comment

        Working...
        X