NexusFi: Find Your Edge


Home Menu

 





ema strategy


Discussion in EasyLanguage Programming

Updated
    1. trending_up 2,495 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

ema strategy

  #1 (permalink)
ezrollin
Cleburne
 
Posts: 14 since Dec 2019
Thanks Given: 21
Thanks Received: 0

h ttp://s000.tinyupload.com/index.php?file_id=00048931909552677340


I'm trying to make an automated trading strategy based on simple 3/8 EMA crossovers
Buy when the 3 crosses above the 8
sell when the 8 crosses above the 3 (inverted)

I understand that the variables dont get carried over to the next bar.
I'm not understand how to make it buy at the current limit bid/ask inter candle
and limit sell right when a cross happens



Quoting 

inputs:
dailyloss(10); // daily loss maximum Has to be positive!


variables:
//todaynet(0),yesterdaynet(0);
double EMAFast(0), EMASlow(0), EMASell(0);
EMAFast = XAverage(close,3);
EMASlow = XAverage(close,8);
EMASell = XAverage(close,8);



Condition1 = EMAFast crosses above EMASlow; // buy condition
Condition2 = EMASell crosses above EMAFast; // sell condition





//{ +++++++++++++++++++++ }
//{ +++ Entries +++++++++ }
//{ +++++++++++++++++++++ }
if Condition1 then begin
Buy 1 shares this bar ; // InsideBid
Alert( "" );
end;




//{ +++++++++++++ }
//{ ++ Exits ++++ }
//{ +++++++++++++ }
if Condition2 then begin
Sell all shares this bar ;
Alert( "" );
end;

//if netprofit <= dailyloss then sell all shares next bar at market; // I want to completely exit the script, dont know how
//if positionprofit < -2 then sell all shares next bar at market; // trying to be like a drawdown maximum


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
MC PL editor upgrade
MultiCharts
Cheap historycal L1 data for stocks
Stocks and ETFs
Better Renko Gaps
The Elite Circle
 
  #2 (permalink)
ezrollin
Cleburne
 
Posts: 14 since Dec 2019
Thanks Given: 21
Thanks Received: 0

I've tried several different ways on several different time frames. I cant get it to work right
Maybe someone can help.
Thanks






Quoting 


// watch out for overnight gap downs




///// This code ends trading once the Daily Net Points are achieved or end time constraint is met.
//// Designed for use on futures contracts
/// If used within an Auto Execution Strategy, you may elect to revisit the "marketposition" terms.
//// Co-written by ZAC & RANGER 2010 Rev 0 / tested and found operational


inputs:
DailyNetPoints(9),
StopLossTicks(12),
ProfitTargetTicks(12),
BreakEvenTicks (6),
NumberContracts(4),

start_time(0830),
end_time(1445),

ExitOnClose( true );

variables:
Prior_Date(date),
Prior_Net_Profit(0),
StopPx_Amt(0),
Daily_NP_Amt(0),
ownshares(0),
Profit_Target_Amt(111110),
Break_Even_Amt(0);



StopPx_Amt = ((StopLossTicks/4)*BigPointValue) * NumberContracts;

Daily_NP_Amt = (DailyNetPoints * BigPointValue) * NumberContracts;

Profit_Target_Amt = ((ProfitTargetTicks/4)*BigPointValue) * NumberContracts;

Break_Even_Amt = ((BreakEvenTicks/4)*BigPointValue) * NumberContracts;

once
begin
Prior_Date = Date - 1;
end;

if Time > start_time and Time < end_time then
BEGIN

if Date > Prior_Date then
begin
Prior_Date = Date;
Prior_Net_Profit = netprofit;
end;

if netprofit - Prior_Net_Profit <= Daily_NP_Amt then
begin

///////////START ENTRY/EXIT SIGNALS HERE








inputs:
dailyloss(30); // daily loss maximum Has to be positive!
inputs:
FastLength( 12 ),
SlowLength( 26 ) ,
MACDLength( 9 ) ;


variables:
//todaynet(0),yesterdaynet(0);
double EMAFast(0), EMASlow(0), EMASell(0), stats(0);
EMAFast = XAverage(close,3);
EMASlow = XAverage(close,8);
EMASell = XAverage(close,1);

variables: MovAvg1(0), MovAvg2(0);
MovAvg1 = Average(Close,10);
MovAvg2 = Average(Close,20);

variables:
MyMACD( 0.000 ), MACDAvg( 0.000 );

MyMACD = MACD( Close, FastLength, SlowLength );
MACDAvg = XAverage( MyMACD, MACDLength );


Condition1 = EMAFast crosses above EMASlow; // buy condition
Condition2 = EMASell crosses below EMAFast; // sell condition
Condition3 = (MyMACD > MACDAvg); // bullish
//if ownshares = 1 then Condition4 = MACDAvg < MyMACD; // bearish

Condition5 = MovAvg1 > MovAvg2;
Condition6 = MovAvg2 > MovAvg1;




//{ +++++++++++++++++++++ }
//{ +++ Entries +++++++++ }
//{ +++++++++++++++++++++ }
if ownshares = 0 then begin
if Condition5 {and Condition3} then begin
Buy ("cond1") 1 shares this bar at close ;
ownshares = ownshares + 1;
//Alert( "" );
end;
end;

//{ +++++++++++++ }
//{ ++ Exits ++++ }
//{ +++++++++++++ }
if ownshares = 1 then begin
//If Close < Close[1] and Close[1] < Close[2] then begin

if Condition6 {or Condition4} then begin
Sell ("cond2 sell") all shares this bar;
ownshares = 0;
//Alert( "" );
end;
end;


//if netprofit <= dailyloss then begin // I want to completely exit the script
// sell all shares next bar at market;
// end;
// end;
if ownshares = 1 then begin
//if positionprofit < -100 {or Condition4} then sell all shares next bar at market; // trying to be like a drawdown maximum / panic sell
end;












///////////////END ENTRY EXIT SIGNALS HERE

end else {ends when daily profit objective is reached}
begin
if marketposition = 1 then sell ("ProfitMax") next bar on Open;
if marketposition = -1 then buytocover ("ProfitMaxx") next bar on Open;
ownshares = 0;
end;

END else

if Time > end_time then {ends when time limit is hit}
begin
if marketposition = 1 then sell ("EOD-0") this bar on Close;
if marketposition = -1 then buytocover ("EOD-1") this bar on Close;
ownshares = 0;
end;

if ExitOnClose = true then SetExitOnClose ; {exit on close of market - this can be rem if desired}

if marketposition <> 0 then
begin
SetDollarTrailing( StopPx_Amt ) ;
SetBreakeven( Break_Even_Amt ) ;
if Profit_Target_Amt > 0 then SetProfitTarget(Profit_Target_Amt ) ;
end;





//while stats < 1 begin
print ("Profit: " , GrossProfit , " Profit: " , NetProfit , " Loss: " , GrossLoss , " Losers: " , NumLosTrades , " Win Percent: " , PercentProfit , "% Total Trades: " , TotalTrades, " pos profit ",positionprofit);
//stats = stats + 1;
//end;


{if High > High[1] AND Close < Close[1] Then Plot1(High, “Key Rev Dn”);if Low < Low[1] AND Close > Close[1] Then Plot2(Low, “Key Rev Up”);}


Reply With Quote
  #3 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130


I'm new to NT and C#, but you seem to have a typo in this line:

Condition2 = EMASell crosses above EMAFast; // sell condition

Should EMASell actually be EMASlow?

Reply With Quote
  #4 (permalink)
ezrollin
Cleburne
 
Posts: 14 since Dec 2019
Thanks Given: 21
Thanks Received: 0

No, it was just a 3rd condition I was using to try to get out of a trade but it wasnt working right and I didnt like it.
I guess I dont like how you cant buy interbar
Gonna try more stuff.

Reply With Quote
  #5 (permalink)
 
SMCJB's Avatar
 SMCJB 
Houston TX
Legendary Market Wizard
 
Experience: Advanced
Platform: TT and Stellar
Broker: Advantage Futures
Trading: Primarily Energy but also a little Equities, Fixed Income, Metals and Crypto.
Frequency: Many times daily
Duration: Never
Posts: 5,049 since Dec 2013
Thanks Given: 4,388
Thanks Received: 10,207

Tradestation is designed to evaluate the conditions at the end of the bar and execute orders at the beginning of the next bar. One way to get around this is to use smaller time frame bars. If necessary have a second data sources. ie Data1 60mins bar, Data2 Daily Bars. You can actually execute orders during the bar but you will need to use intrabar order generation. Also you may want to look up setstoploss

Reply With Quote
Thanked by:




Last Updated on March 15, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts