Back

Why Buy stops in FOREX?

Dear Honored Community!
I am a beginner here. I have created a simple FOREX ALGO: I create a list or SMAs for the first period. Than in a second period I compare prices to this first period:
- if current price = an earlier price in an increasing trend I Buy,
- if current price = an earlier price in a decreasing trend I sell.
So far I could define the Buy/Sell conditions correctly. But the Orders are not filled, or only the first Buy is done, the Algo seems to not do further actions for the remaining cases.
I don't have the guiding patterns (like do sell only if I am long, or do Buy only once until a selling case). I made my code as simple as possible to see what is the issue, step-by-step. I have more logging points in comments to see the steps.

I was digging here the forums and tutorials to find out what the Buy method does, how does it generate an order, than an Order Ticket...but no luck so far. Please look at it and let me know any hints, suggestions, thank you.

Another question: what is the simplest way in Python to check the status of my portfolio? (ie. do I have bought Securities, how much, etc.)

Update Backtest








Could you please share your code with the community?
It will make it easier for us to help.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


#!/usr/bin/python
# -*- coding: utf-8 -*-

#
# Trading Orders Algorithm
#
# Ref: https://www.quantconnect.com/docs#Trading-and-Orders
# https://www.quantconnect.com/docs#Charting
#

import decimal
#from datetime import timedelta, datetime
import datetime
import pandas as pd
import numpy as np

class TradingOrdersAlgorithm(QCAlgorithm):

# period1 will have the prices to which we will compare possible trade prices
period1 = []

def Initialize(self):
self.SetCash(5000)
self.SetStartDate(2016, 1, 1)
self.SetEndDate(2016, 1, 30)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.AddForex('EURUSD', Resolution.Hour)
self.SetBenchmark('EURUSD')
self.sma = self.SMA('EURUSD', 12, Resolution.Hour)
self.SetWarmup(datetime.timedelta(1))

def OnData(self, slice):
# staring and closing dates of pediod1
tlimit1 = datetime.datetime(2016, 1, 3, 23)
tlimit2 = datetime.datetime(2016, 1, 5, 23)

if self.Portfolio['EURUSD'].IsLong:
return

price = slice['EURUSD'].Value
quoteBar = slice['EURUSD']

# tresh stands for trashhold (or crossing)
tresh = price

# I write the Ticks into the log to get records to see exactly
self.Log('Tick: {0}'.format(price))

# filling up the 1st period
if tlimit1 <= slice.Time and tlimit2 >= slice.Time:
self.period1.append(price)
#checking the number of SMA values in period1
#self.Log('Period1 value: {0}'.format(len(self.period1)))

#if we reach the end of the period1, possible tarding prices come
if tlimit2 < slice.Time:

# go through on each par of the 1st period and do check crossing with tresh
for i in range(len(self.period1) - 1):
#self.Log('For i is reached!')

# ...if cross happens (current prices is between 2 neighbourhood price)
if self.period1[i] <= tresh and self.period1[i + 1] \
>= tresh or self.period1[i] >= tresh \
and self.period1[i + 1] <= tresh:

# Buy 1000 of EURUSD if the direction of the changes of the means increase
# Sell 1000 of EURUSD if the direction of the changes of the means decrease

self.Log('Tresh!, Period1i: {0}, Period1i+: {1}, Tresh: {2}'.format(self.period1[i], self.period1[i+1], tresh), )
if self.period1[i] < self.period1[i + 1]:
self.Log('Buy should happen!')
self.Buy('EURUSD', 100)
break
if self.period1[i] > self.period1[i + 1]:
#Sell is turned off, as to avoid selling if I do not have the security
#self.Sell('EURUSD', 100)
self.Log('Sell should happen!')
break

def OnOrderEvent(self, orderEvent):
#As I wanted to know the reason why my orders are not filled, I added in log the Order Status:
#self.Log('Order Status: {0}'.format(OrderEvent.Status))
# Order Status Submitted: 1
# Order Status Canceled: 5
# Order Status CancelPending: 8
# Order Status Filled: 3
# Order Status Invalid: 7
# Order Status New: 0
# Order Status None: 6
# Order Status PatiallyFiled: 2
if orderEvent.Status == OrderStatus.Submitted \
or orderEvent.Status == OrderStatus.Canceled:
#self.Log('Order Event Status under if: {0}'.format(orderEvent.Status))
return

if orderEvent.FillQuantity < 0:
#self.Log('Order Fill Quantity: {0}'.format(orderEvent.FillQuantity))
self.Transactions.CancelOpenOrders('EURUSD')
else:
self.Log('Buy/Sell EURUSD at {0}. SMA Value: {1}'.format(orderEvent.FillPrice,
self.sma.Current.Value))
0

Sorry, I thought my code it was added.

0

In this code, there is a check that prevends OnData to continue when we are long EURUSD:

# In OnData, line #27
if self.Portfolio['EURUSD'].IsLong:
return

and there is no other event/method that liquidates the position.

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Aha, I see...thank you, that helps.

0

...actually this tiny info gave me the result I wished! THANKS!

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed