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;