In this Tutorial we will show you how to open a position long and short in EasyLanguage and PowerLanguage. It’s time to open a trade. We resume the two previous tutorials related to the strategy based on the moving average: LINK 1 – LINK 2.
Basically we will open a LONG position when:
– Moving average is decreasing
– The price rebounds on average and rebounds
Vice versa for short.
EasyLanguage and PowerLanguage:
SellShort This Bar on Close;
Buy This Bar on Close;
Tradestation | Multicharts – Easylanguage | PowerLanguage commands to open trade are:
BUY and SELLSHORT
While the commands to close the open positions are:
SELL and BUYTOCOVER
Going more in detail, it will be possible to give a name to the order to better understand during the revision phase what the system is doing:
Buy (“Entry_Buy_Tutorial_Example”) This Bar on Close;
Also, you can choose the number of contracts to buy:
Buy (“Entry_ Buy”) 5 Contracts This Bar on Close;
Or if we are using stocks:
Buy (“Entry_ Buy”) 50 Shares This Bar on Close;
It is then necessary to decide when to open the position and in this case, there are various possibilities:
Let’s try the strategy right away!
The stock used is the SPY ETF so, having a bullish bias, I will avoid opening short positions with the same strategy.
We will enter with a fixed number of 100 shares, without any volatility filter to make the code as simple as possible.
Defining an exit rule will be necessary. I will insert a classic exit in time after x bars:
If barssinceentry (0)> = time_exit then sell (“TE_L”) next bar at open;
Being a trend-following strategy, it will be convenient to remain in the market for several bars, say ten days and I will insert a catastrophic stoploss of 1000 dollars.
Here is the code for EasyLanguage and PowerLanguage:
//Ema Cross – Tutorial EasyLanguage and PowerLanguage for TradeStation and MultiCharts
Input: MA_length(20), N_Shares(100),time_exit(5),Money_Stop(300);
If MA>MA and MA>MA and Close>MA and Low<MA and Close>MA then
Buy (“Entry_Long_Tutorial_Example”) N_Shares Shares this bar on close;
If barssinceentry(0)>=time_exit then sell (“TE_L”) NEXT bar at open;