I trade mostly mechanical and so far I have put on trades manually but in order to learn more about programming I was thinking of automate my systems.
Some of it I figured out but now I've run in to some problems.
When using my "Backtesting" code for automatic trading I found lot of logical errors when the orders are executed.
I'm no experienced programmer so I try here to see if anyone can help me out.
Code: Select all
if marketposition = 1 and shorttrade1 = false then begin
longtrade1 = true;
lstp1 = InitialLongStop1 ;
if UseMoveToBreakEven and h >= entryprice+BreakEvenFactor
then begin
lstp1 = MaxList(lstp1, entryprice);
sell ("ExitLong1BE") from Entry ("Long1") next bar at lstp1 stop;
end;
if UseTrailing and time >= TrailingStartTime and TrailLevel > lstp1
then begin
lstp1 = MaxList(lstp1, TrailLevel );
sell ("ExitLong1 TRAIL") from Entry ("Long1") next bar at TrailLevel stop;
end;
if time >= CloseTime then
sell ("ExitLong1 EOD") from Entry ("Long1") this bar at c;
if time < NoNewPositionsTime and shorttrade1 = false then begin
sellshort ("Short2") 1 contract next bar at SellLevel stop;
sstp2 = InitialShortStop2;
BuyToCover ("InitExit Short2") from Entry ("Short2") next bar at sstp2 stop;
end;
The problems I found so far:
1. If MarketPosition = 1 and UseTrailing = true
(I'm long and have a initial stop&reverse in the market and also using a trailing stop)
If the trailing stop level moves one tick above the initial stop level the trailing is active.
Now I have a stop order at the trailinglevel and a stop&reverse order one tick below.
The stop&reverse order is twice as big as my current position. (flat+short)
The problem i see is that if the market moves really fast I'll be flat at the trailing level and short twice the initial position at the stop&reverse level.
When I trade this manually I put in one order at the trailing level to make me flat and one order at the stop&reverse order to make me short.
I'll attach a pic to make it easier to understand what I mean. (It shows a short position but the issue is the same)
Auto Trading:
Manual Trading:
2. If MarketPosition = 1 and UseMoveToBreakEven = true
Basically same problem.
I'll try to explain the logic I'm looking for:
- If I'm long 1 contract I want to have a stop&reverse at a specified level below the entryprice.
(Flat and Short at the same level.)
- If I'm long 1 contract and using UseTrailing and the trailing stop is above the initial stop&reverse I want to Sell 1 contract at the trailing level and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)
- If I'm long 1 contract and using MoveToBreakEven and the BreakEven level is above the initial stop&reverse I want to Sell 1 contract at the BreakEven level and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)
- If I'm long 1 contract and using MoveToBreakEven and UseTrailing I want to sell 1 contract at the highest of the two and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)
Hope you understand what I'm trying to describe....
If anyone can give me some ideas I'd really appreciate it.
By the way, if someone feels the urge to write that learning EasyLanguage is a huge project and requires time and effort to understand and recommending some "Learn the basics" book.
Please don't, It's not what I'm after with this post..
Thanks, Lars