NexusFi: Find Your Edge


Home Menu

 





Need help with simple stop/reverse strategy


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one ABCTG with 1 posts (0 thanks)
    2. looks_two flaaghara with 1 posts (0 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 Dvdkite with 1 posts (0 thanks)
    1. trending_up 2,266 views
    2. thumb_up 0 thanks given
    3. group 3 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread

Need help with simple stop/reverse strategy

  #1 (permalink)
flaaghara
NY, NY
 
Posts: 1 since May 2018
Thanks Given: 0
Thanks Received: 0

Hello friends. This is my first time on the forum but I have searched extensively on the internet and the forum for a sample implementation of a stop/reverse strategy in EasyLanguage but have had no luck in finding some currently usable sample code. Here is what I want to do in words:

1. I want to enter a trade either long or short at a fixed time every day, at let's say BeginTime = 0830. The direction of the entry is based on some method EnterLong() which will decide (based on a combination of indicators and daily qualitative analysis) which direction I'm entering.

2. The moment that the trade is entered, I want a fixed stop-loss in the opposite direction of let's say StopLossAmt = 0.20, with a profit target that is set every day but determined at the beginning of the day according to a static variable ProfitTargetForDay = GetProfitTarget() If the profit target is met for the day, exit the trade and stop trading for the day. If the stop loss is reached, immediately exit the trade (incurring the 0.20 loss) and enter a new trade in the same direction as the stop-loss (hence the stop & reverse strategy).

3. Once a trade is reentered in the opposite direction, the same conditions hold except the direction of the profit target and the stop loss are now opposite to what they were in the trade before.

4. Say I want a maximum of NumStops = 5 to occur during the day, after which the strategy is done excuting for the day and the 0.20 * 5 = 1.00 total loss is incurred for the day.

5. If we get to a certain time for the day at let's say EndTime = 1600 and neither the profit target has been met in any direction or the 5 stops have been hit, we immediately liquidate whatever contracts are still live, incurring either a gain or loss depending on where the ticker is relative to our last trade entry.

Also, the strategy should operate using intra-bar settings for both backtesting and live orders.

Here is my attempt:

 
Code
If (time = BeginTime AND NumStops = 0) then begin
	EntryDirection = EnterLong();
	ProfitTargetForDay = GetProfitTarget(); 

	If EntryDirection then begin
		Buy ("Long Entry") NumContracts contracts next bar at Market;
		MorningEntry = EntryPrice;
	end
	Else begin
		SellShort ("Short Entry") NumContracts contracts next bar at Market;
		MorningEntry = EntryPrice;
	end;
end;

If time = EndTime then begin
	NumStops = 0;
	If MarketPosition = 1 then
		Sell ("Long Timed Exit") NumContracts contracts next bar at Market;
	If MarketPosition = -1 then
		BuyToCover ("Short Timed Exit") NumContracts contracts next bar at Market;
end;


If (time >= BeginTime AND time < EndTime AND MarketPosition = 1 AND NumStops <= 5) then begin
	Sell ("Take Profit Long") NumContracts contracts next bar at MorningEntry + ProfitTargetForDay Limit;
	Sell ("Stop Loss Long") NumContracts contracts next bar at MorningEntry - StopLossAmt Stop;
end;

If (time >= BeginTime AND time < EndTime AND MarketPosition = -1 AND NumStops <= 5) then begin
	BuyToCover ("Take Profit Short") NumContracts contracts next bar at MorningEntry - ProfitTargetForDay Limit;
	BuyToCover ("Stop Loss Short") NumContracts contracts next bar at MorningEntry + StopLossAmt Stop;
end;

If (time >= BeginTime AND time < EndTime AND MarketPosition = 0 AND MarketPosition(1) = 1 AND ExitPrice(1) < EntryPrice(1) AND NumStops <= 5) then begin
	SellShort ("Short Reversal") NumContracts contracts next bar at Market;
	NumStops +=1;
end;

If (time >= BeginTime AND time < EndTime AND MarketPosition = 0 AND MarketPosition(1) = -1 AND ExitPrice(1) > EntryPrice(1) AND NumStops <= 5) then begin
	Buy ("Long Reversal") NumContracts contracts next bar at Market;
	NumStops +=1;
end;
It seems to work properly for some days, and then other days it goes haywire and trades way more than the number of stops that I have programmed in. I cannot figure out what is going wrong. There is probably a couple of coding errors and such throughout, so if anyone could help point out the errors it would be much appreciated. Thanks!

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
Quant vue
Trading Reviews and Vendors
How to apply profiles
Traders Hideout
Cheap historycal L1 data for stocks
Stocks and ETFs
What broker to use for trading palladium futures
Commodities
 
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629


flaaghara,

I would suggest taking a look at the NumStops variable values using the print command or the Debugger. Check if it gets incremented when you think it should and also check its values at the moment you issue new orders.
This should tell you what is going on internally and help you to get to the problem.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
  #4 (permalink)
Dvdkite
Trieste Italy
 
Posts: 162 since Feb 2018
Thanks Given: 131
Thanks Received: 25

Hello,

In some of my test I've found useful also using
 
Code
EntriesToday(date)
to check if the incremental of a value was correct (like your numStops) is working correcltly and the only way is using print command to check values...

Anyway maybe you can build your code using EntriesToday(date) and by checking the Last position profit ...

Reply With Quote




Last Updated on May 15, 2018


© 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