Code question on Stops

Questions about MultiCharts and user contributed studies.
jmi88
Posts: 29
Joined: 31 Jan 2007

Code question on Stops

Postby jmi88 » 12 Jul 2007

I'm trying to add a stop criteria at the end of a simple moving average cross system...If I get stopped out I do not want to reenter at all, until the next signal the next day...even if theres more signals that day....But I dont know how to say 'once stopped take no more entries', 'or once stopped out be done for the day'...here is the basic moving average cross system with a plain target stop at the end..


Code: Select all

inputs: Price( Close ), FastLength( 9 ), SlowLength( 18 ) ;
variables: FastAvg( 0 ), SlowAvg( 0 ) ;

FastAvg = AverageFC( Price, FastLength ) ;
SlowAvg = AverageFC( Price, SlowLength ) ;

if CurrentBar > 1 and FastAvg crosses over SlowAvg then
Buy ( "MA2CrossLE" ) next bar at market ;


if CurrentBar > 1 and FastAvg crosses under SlowAvg then
Sell Short ( "MA2CrossSE" ) next bar at market ;


Inputs: PositionBasis(True), Amount(20);

If PositionBasis Then
SetStopPosition
Else
SetStopContract;

SetStopLoss(Amount);



thankyou for any help.

User avatar
gautama2
Posts: 96
Joined: 10 Jul 2007
Has thanked: 1 time
Been thanked: 1 time

Postby gautama2 » 13 Jul 2007

Maybe this works:

Code: Select all

inputs: Price( Close ), FastLength( 9 ), SlowLength( 18 ), tradesperday(2) ;
variables: FastAvg( 0 ), SlowAvg( 0 ), counter(0) ;

FastAvg = AverageFC( Price, FastLength ) ;
SlowAvg = AverageFC( Price, SlowLength ) ;

if counter <= tradesperday and CurrentBar > 1 and FastAvg crosses over SlowAvg then
begin
Buy ( "MA2CrossLE" ) next bar at market ;
counter=counter+1;
end;

if counter <= tradesperday and CurrentBar > 1 and FastAvg crosses under SlowAvg then
begin
Sell Short ( "MA2CrossSE" ) next bar at market ;
counter=counter+1;
end;

if date <> date[1] then counter = 0;

Inputs: PositionBasis(True), Amount(20);

If PositionBasis Then
SetStopPosition
Else
SetStopContract ;

SetStopLoss(Amount);
Regards
Robert

jmi88
Posts: 29
Joined: 31 Jan 2007

Postby jmi88 » 13 Jul 2007

Thankyou for the response.....But sometimes I would be making multiple entries in and out, for example lets say I got 4 or 5 entries during the day and some were winners and some some were small losers but then the next one I was stopped out on, then I would like to quit for the day. So I guess I would never know how many signals i would get prior to getting stopped out....So I need to take all signals generated until one of them is stopped out......

User avatar
gautama2
Posts: 96
Joined: 10 Jul 2007
Has thanked: 1 time
Been thanked: 1 time

Postby gautama2 » 13 Jul 2007

Then you could put a counter the same way into your exitcondition instead of the entryconditions. So only the tradeexits are counted then. This should work. but do also add the lines to reset the counter at next days start.

Or simply just like that at the end of the formula (haven't tried this):

Code: Select all

if marketposition[1]<>0 and marketposition = 0 then counter = counter+1;
if date[1] <> date then counter = 0;
Regards
Robert

jmi88
Posts: 29
Joined: 31 Jan 2007

Postby jmi88 » 13 Jul 2007

Thankyou again for helping me with this code, but two things, one the word 'counter' is not being reconized in the PE, i might need to upgrade to the new beta i guess, im using ver2.0.777.777 .....and two is if I'm not sure how many trades I'm going to make prior to getting stopped out, how does putting a counter in help?

Thankyou again.

User avatar
gautama2
Posts: 96
Joined: 10 Jul 2007
Has thanked: 1 time
Been thanked: 1 time

Postby gautama2 » 13 Jul 2007

Hello,

counter is just a variable you should initialize in the first "vars:" lines.
Sorry i did not mention.

Code: Select all

vars: counter(0), your variables;

{your system code}

if marketposition[1]<>0 and marketposition = 0 then counter = counter+1;
if date[1] <> date then counter = 0;
This is my idea for your problem. Hope it helps. If not, then i don't know either :?

Thankyou again for helping me with this code, but two things, one the word 'counter' is not being reconized in the PE, i might need to upgrade to the new beta i guess, im using ver2.0.777.777 .....and two is if I'm not sure how many trades I'm going to make prior to getting stopped out, how does putting a counter in help?

Thankyou again.


Return to “MultiCharts”