Profit Target Stop Percentage  [SOLVED]

Questions about MultiCharts and user contributed studies.
shane1800
Posts: 84
Joined: 28 Feb 2014
Has thanked: 5 times
Been thanked: 5 times

Profit Target Stop Percentage

Postby shane1800 » 02 Mar 2014

Hi,

Looking for some help, moving from TS and not able to get my stops working, they were not perfectly accurate in TS either but now they don't work at all.

Basically setting percent target and stops. Position goes up X% then exit etc...

Thanks for looking!


Code Example:
(Note the strategy trades 100% of the account balance for each trade)
(And trading TS:$INX CME with a 1/10000 price scale, that's why I did "EntryShares * 10000")

Code: Select all


inputs:
PcntMarginToTrade( 100 ) , // 95 = 95%. Applies both to historical only

LongFastMov( 9) , {Fast moving average of OverUnderPer for long trades}
ShortFastMov( 9) , {Short moving average of OverUnderPer for short trades}

Percent_ProfitStrat1L(0.1) , {.01 means 1% profit target}
Percent_StopStrat1L(0.1) , {.01 means 1% stop target}
Percent_ProfitStrat1S(0.1) , {.01 means 1% profit target}
Percent_StopStrat1S(0.1) ; {.01 means 1% stop target}


variables:
Double MyAccountBalance( 0) , //Not sure if this is best CHECK
Double MyTradeEquity( 0 ) ,
Int NumShares( 0 ) ,
Int EntryShares( 0 ) ,

Double LongFastVar( 0 ) ,
Double ShortFastVar( 0 ) ,

TradeTrigger(0),

ProfitTargetL(0) ,
ProfitTargetS(0) ,
StopTargetL(0) ,
StopTargetS(0) ;



// Get amound $ in account
MyAccountBalance = InitialCapital + netprofit + openpositionprofit;

// Get the calculated trade equity
MyTradeEquity = MyAccountBalance * (PcntMarginToTrade/100) ;

// Calculate the position size or number of shares to trade based upon the
// trade equity and current price.
if MyTradeEquity > 0 then
NumShares = MyTradeEquity / Close ;


{DATA}

LongFastVar = AverageFC( Close, LongFastMov ) ; {Moving average of Close for long trades}
ShortFastVar = AverageFC( Close, ShortFastMov ) ; {Moving average of Close for short trades}


{LONG}

if CurrentBar > 1 and LongFastVar crosses over Close then
Begin
// CB > 1 check used to avoid spurious cross confirmation at CB = 1
Buy NumShares shares at next bar at market ;
EntryShares = NumShares ;
TradeTrigger = 1 ;
End ;

{SHORT}

if CurrentBar > 1 and ShortFastVar crosses under Close then
Begin
// CB > 1 check used to avoid spurious cross confirmation at CB = 1
Sell Short NumShares shares at next bar at market ;
EntryShares = NumShares ;
TradeTrigger = 1 ;
End ;


{EXITS}

If TradeTrigger = 1 and MarketPosition = 1 then
begin
ProfitTargetL = Entryprice(1) * Percent_ProfitStrat1L * EntryShares * 10000; {*10000 is for price conversion. for percentage stops, Target seems to not be perfect percentage}
StopTargetL = Entryprice(1) * Percent_StopStrat1L * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
SetProfitTarget(ProfitTargetL);
SetStopLoss(StopTargetL);
end;

If TradeTrigger = 1 and MarketPosition = -1 then
begin
ProfitTargetS = Entryprice(1) * Percent_ProfitStrat1S * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
StopTargetS = Entryprice(1) * Percent_StopStrat1S * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
SetProfitTarget(ProfitTargetS);
SetStopLoss(StopTargetS);
end;
Attachments
Screen Shot 2014-03-02 at 3.50.19 PM.png
(35.39 KiB) Downloaded 710 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Profit Target Stop Percentage

Postby TJ » 02 Mar 2014

Hi,

Looking for some help, moving from TS and not able to get my stops working, they were not perfectly accurate in TS either but now they don't work at all.

Basically setting percent target and stops. Position goes up X% then exit etc...

Thanks for looking!


Code Example:
(Note the strategy trades 100% of the account balance for each trade)
(And trading TS:$INX CME with a 1/10000 price scale, that's why I did "EntryShares * 10000")

Code: Select all


inputs:
PcntMarginToTrade( 100 ) , // 95 = 95%. Applies both to historical only

LongFastMov( 9) , {Fast moving average of OverUnderPer for long trades}
ShortFastMov( 9) , {Short moving average of OverUnderPer for short trades}

Percent_ProfitStrat1L(0.1) , {.01 means 1% profit target}
Percent_StopStrat1L(0.1) , {.01 means 1% stop target}
Percent_ProfitStrat1S(0.1) , {.01 means 1% profit target}
Percent_StopStrat1S(0.1) ; {.01 means 1% stop target}


variables:
Double MyAccountBalance( 0) , //Not sure if this is best CHECK
Double MyTradeEquity( 0 ) ,
Int NumShares( 0 ) ,
Int EntryShares( 0 ) ,

Double LongFastVar( 0 ) ,
Double ShortFastVar( 0 ) ,

TradeTrigger(0),

ProfitTargetL(0) ,
ProfitTargetS(0) ,
StopTargetL(0) ,
StopTargetS(0) ;



// Get amound $ in account
MyAccountBalance = InitialCapital + netprofit + openpositionprofit;

// Get the calculated trade equity
MyTradeEquity = MyAccountBalance * (PcntMarginToTrade/100) ;

// Calculate the position size or number of shares to trade based upon the
// trade equity and current price.
if MyTradeEquity > 0 then
NumShares = MyTradeEquity / Close ;


{DATA}

LongFastVar = AverageFC( Close, LongFastMov ) ; {Moving average of Close for long trades}
ShortFastVar = AverageFC( Close, ShortFastMov ) ; {Moving average of Close for short trades}


{LONG}

if CurrentBar > 1 and LongFastVar crosses over Close then
Begin
// CB > 1 check used to avoid spurious cross confirmation at CB = 1
Buy NumShares shares at next bar at market ;
EntryShares = NumShares ;
TradeTrigger = 1 ;
End ;

{SHORT}

if CurrentBar > 1 and ShortFastVar crosses under Close then
Begin
// CB > 1 check used to avoid spurious cross confirmation at CB = 1
Sell Short NumShares shares at next bar at market ;
EntryShares = NumShares ;
TradeTrigger = 1 ;
End ;


{EXITS}

If TradeTrigger = 1 and MarketPosition = 1 then
begin
ProfitTargetL = Entryprice(1) * Percent_ProfitStrat1L * EntryShares * 10000; {*10000 is for price conversion. for percentage stops, Target seems to not be perfect percentage}
StopTargetL = Entryprice(1) * Percent_StopStrat1L * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
SetProfitTarget(ProfitTargetL);
SetStopLoss(StopTargetL);
end;

If TradeTrigger = 1 and MarketPosition = -1 then
begin
ProfitTargetS = Entryprice(1) * Percent_ProfitStrat1S * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
StopTargetS = Entryprice(1) * Percent_StopStrat1S * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
SetProfitTarget(ProfitTargetS);
SetStopLoss(StopTargetS);
end;
see post #5
viewtopic.php?f=16&t=10811

shane1800
Posts: 84
Joined: 28 Feb 2014
Has thanked: 5 times
Been thanked: 5 times

Re: Profit Target Stop Percentage

Postby shane1800 » 02 Mar 2014

Thanks for quick reply. So what I'm learning is to not place Global Exits in a condition.

So I moved them out and now the Signal enters a trade then exits the same bar. Thoughts?

Code: Select all

inputs:
PcntMarginToTrade( 100 ) , // 95 = 95%. Applies both to historical only

LongFastMov( 9) , {Fast moving average of OverUnderPer for long trades}
ShortFastMov( 9) , {Short moving average of OverUnderPer for short trades}

Percent_ProfitStrat1L(0.1) , {.01 means 1% profit target}
Percent_StopStrat1L(0.1) , {.01 means 1% stop target}
Percent_ProfitStrat1S(0.1) , {.01 means 1% profit target}
Percent_StopStrat1S(0.1) ; {.01 means 1% stop target}


variables:
Double MyAccountBalance( 0) , //Not sure if this is best CHECK
Double MyTradeEquity( 0 ) ,
Int NumShares( 0 ) ,
Int EntryShares( 0 ) ,

Double LongFastVar( 0 ) ,
Double ShortFastVar( 0 ) ,

TradeTrigger(0),

ProfitTarget1(0) ,
StopTarget1(0) ;



// Get amound $ in account
MyAccountBalance = InitialCapital + netprofit + openpositionprofit;

// Get the calculated trade equity
MyTradeEquity = MyAccountBalance * (PcntMarginToTrade/100) ;

// Calculate the position size or number of shares to trade based upon the
// trade equity and current price.
if MyTradeEquity > 0 then
NumShares = MyTradeEquity / Close ;


{DATA}

LongFastVar = AverageFC( Close, LongFastMov ) ; {Moving average of Close for long trades}
ShortFastVar = AverageFC( Close, ShortFastMov ) ; {Moving average of Close for short trades}


{LONG}

if CurrentBar > 1 and LongFastVar crosses over Close then
Begin
// CB > 1 check used to avoid spurious cross confirmation at CB = 1
Buy NumShares shares at next bar at market ;
EntryShares = NumShares ;
TradeTrigger = 1 ;
End ;

{SHORT}

if CurrentBar > 1 and ShortFastVar crosses under Close then
Begin
// CB > 1 check used to avoid spurious cross confirmation at CB = 1
Sell Short NumShares shares at next bar at market ;
EntryShares = NumShares ;
TradeTrigger = 1 ;
End ;


{EXITS}

If TradeTrigger = 1 and MarketPosition = 1 then
begin
ProfitTarget1 = Entryprice(1) * Percent_ProfitStrat1L * EntryShares * 10000; {*10000 is for price conversion. for percentage stops, Target seems to not be perfect percentage}
StopTarget1 = Entryprice(1) * Percent_StopStrat1L * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
end;

If TradeTrigger = 1 and MarketPosition = -1 then
begin
ProfitTarget1 = Entryprice(1) * Percent_ProfitStrat1S * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
StopTarget1 = Entryprice(1) * Percent_StopStrat1S * EntryShares * 10000; {*10000 is for price conversion. for percentage stops}
end;

SetProfitTarget(ProfitTarget1);
SetStopLoss(StopTarget1);

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Profit Target Stop Percentage

Postby TJ » 02 Mar 2014

Thanks for quick reply. So what I'm learning is to not place Global Exits in a condition.
So I moved them out and now the Signal enters a trade then exits the same bar. Thoughts?
::
You have to trace your variables and find out where is the trigger.

Do you have a logic flow chart?

Have you picked an example and work through the values of each variable?
eg. entry price, target price, stop price, exit price...

Just telling us that "the Signal enters a trade then exits the same bar" is not a lot of information to work on.

shane1800
Posts: 84
Joined: 28 Feb 2014
Has thanked: 5 times
Been thanked: 5 times

Re: Profit Target Stop Percentage

Postby shane1800 » 03 Mar 2014

I'm going to ask for more support as this is over my head, will report back.

shane1800
Posts: 84
Joined: 28 Feb 2014
Has thanked: 5 times
Been thanked: 5 times

Re: Profit Target Stop Percentage  [SOLVED]

Postby shane1800 » 04 Mar 2014

Got some help on Elance, this works great.

Code: Select all


inputs:
PcntMarginToTrade( 100 ) , // 95 = 95%. Applies both to historical only

LongFastMov(9) , {Fast moving average of OverUnderPer for long trades}
ShortFastMov(9) , {Short moving average of OverUnderPer for short trades}

Percent_ProfitStrat1L(0.1) , {.01 means 1% profit target}
Percent_StopStrat1L(0.1) , {.01 means 1% stop target}

Percent_ProfitStrat1S(0.1) , {.01 means 1% profit target}
Percent_StopStrat1S(0.1) ; {.01 means 1% stop target}

variables:
RoundTo(1),
MyAccountBalance(0),
EntrySize(0),
NumShares( 0 ) ,
LongMA(0),
ShortMA(0),
ot(0);

MyAccountBalance = InitialCapital + netprofit + openpositionprofit; // account money
EntrySize = MyAccountBalance * (PcntMarginToTrade / 100); // size to enter
NumShares = Floor(EntrySize / (Close * RoundTo)) * RoundTo; // contracts rounded down to RoundTo

// MA calculations
LongMA = AverageFC( Close, LongFastMov );
ShortMA = AverageFC( Close, ShortFastMov );

// "next bar at market" price
ot = Open of tomorrow;

// Long entry
if CurrentBar > 1 and Close crosses above LongMA then Begin
Buy ("Long") NumShares shares next bar at market;

// exit signals are issued along with the entry
// space after signal name is intended
Sell ("Long SL ") next bar ot * (1 - Percent_StopStrat1L) stop;
Sell ("Long TP ") next bar ot * (1 + Percent_ProfitStrat1L) limit;
End;

// Short entry
if CurrentBar > 1 and Close crosses below ShortMA then Begin
SellShort ("Short") NumShares shares next bar at market;

// exit signals are issued along with the entry
// space after signal name is intended
Buytocover ("Short SL ") next bar ot * (1 + Percent_StopStrat1S) stop;
Buytocover ("Short TP ") next bar ot * (1 - Percent_ProfitStrat1S) limit;
End;

// Exits
If MarketPosition = 1 then begin
Sell ("Long SL") next bar EntryPrice * (1 - Percent_StopStrat1L) stop;
Sell ("Long TP") next bar EntryPrice * (1 + Percent_ProfitStrat1L) limit;
end;
If MarketPosition = -1 then begin
Buytocover ("Short SL") next bar EntryPrice * (1 + Percent_StopStrat1S) stop;
Buytocover ("Short TP") next bar EntryPrice * (1 - Percent_ProfitStrat1S) limit;
end;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Profit Target Stop Percentage

Postby TJ » 04 Mar 2014

Got some help on Elance, this works great.
::
Good to hear it is working.
Thanks for sharing your code. Much appreciated.


Return to “MultiCharts”